Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch from http.flavor to net.protocol.* in HTTP server instrumentat… #8244

Merged
merged 2 commits into from
Apr 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public static <REQUEST, RESPONSE> HttpServerAttributesExtractorBuilder<REQUEST,
public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST request) {
super.onStart(attributes, parentContext, request);

internalSet(attributes, SemanticAttributes.HTTP_FLAVOR, getter.getFlavor(request));
String forwardedProto = forwardedProto(request);
String value = forwardedProto != null ? forwardedProto : getter.getScheme(request);
internalSet(attributes, SemanticAttributes.HTTP_SCHEME, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.opentelemetry.instrumentation.api.instrumenter.http;

import io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesGetter;
import javax.annotation.Nullable;

/**
Expand All @@ -19,8 +20,17 @@ public interface HttpServerAttributesGetter<REQUEST, RESPONSE>

// Attributes that always exist in a request

/**
* Extracts the {@code http.flavor} span attribute.
*
* @deprecated Use {@link NetServerAttributesGetter#getProtocolName(Object)} and {@link
* NetServerAttributesGetter#getProtocolVersion(Object)} instead.
*/
@Deprecated
@Nullable
String getFlavor(REQUEST request);
default String getFlavor(REQUEST request) {
return null;
}

@Nullable
String getTarget(REQUEST request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ private static Set<AttributeKey> buildDurationAlwaysInclude() {
Set<AttributeKey> view = new HashSet<>();
view.add(SemanticAttributes.HTTP_METHOD);
view.add(SemanticAttributes.HTTP_STATUS_CODE); // Optional
view.add(SemanticAttributes.HTTP_FLAVOR); // Optional
view.add(NetAttributes.NET_PROTOCOL_NAME); // Optional
view.add(NetAttributes.NET_PROTOCOL_VERSION); // Optional
return view;
Expand Down Expand Up @@ -67,7 +66,6 @@ private static Set<AttributeKey> buildActiveRequestsView() {
Set<AttributeKey> view = new HashSet<>();
view.add(SemanticAttributes.HTTP_METHOD);
view.add(SemanticAttributes.HTTP_SCHEME);
view.add(SemanticAttributes.HTTP_FLAVOR);
view.add(SemanticAttributes.NET_HOST_NAME);
view.add(SemanticAttributes.NET_HOST_PORT);
return view;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.opentelemetry.api.common.AttributesBuilder;
import io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesGetter;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.util.Locale;
import java.util.function.BiPredicate;

/**
Expand All @@ -34,7 +35,11 @@ public InternalNetServerAttributesExtractor(
public void onStart(AttributesBuilder attributes, REQUEST request) {

internalSet(attributes, SemanticAttributes.NET_TRANSPORT, getter.getTransport(request));
internalSet(attributes, NetAttributes.NET_PROTOCOL_NAME, getter.getProtocolName(request));
String protocolName = getter.getProtocolName(request);
if (protocolName != null) {
internalSet(
attributes, NetAttributes.NET_PROTOCOL_NAME, protocolName.toLowerCase(Locale.ROOT));
}
internalSet(attributes, NetAttributes.NET_PROTOCOL_VERSION, getter.getProtocolVersion(request));

boolean setSockFamily = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.OperationListener;
import io.opentelemetry.instrumentation.api.instrumenter.net.internal.NetAttributes;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
Expand Down Expand Up @@ -44,10 +45,10 @@ void collectsMetrics() {

Attributes responseAttributes =
Attributes.builder()
.put("http.flavor", "2.0")
.put("http.status_code", 200)
.put("http.response_content_length", 200)
.put("net.sock.family", "inet")
.put(NetAttributes.NET_PROTOCOL_NAME, "http")
.put(NetAttributes.NET_PROTOCOL_VERSION, "2.0")
.put("net.sock.peer.addr", "1.2.3.4")
.put("net.sock.peer.name", "somehost20")
.put("net.sock.peer.port", 8080)
Expand Down Expand Up @@ -88,7 +89,8 @@ void collectsMetrics() {
.hasAttributesSatisfying(
equalTo(SemanticAttributes.HTTP_METHOD, "GET"),
equalTo(SemanticAttributes.HTTP_STATUS_CODE, 200),
equalTo(SemanticAttributes.HTTP_FLAVOR, "2.0"),
equalTo(NetAttributes.NET_PROTOCOL_NAME, "http"),
equalTo(NetAttributes.NET_PROTOCOL_VERSION, "2.0"),
equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"),
equalTo(SemanticAttributes.NET_PEER_PORT, 1234),
equalTo(
Expand All @@ -111,7 +113,8 @@ void collectsMetrics() {
.hasAttributesSatisfying(
equalTo(SemanticAttributes.HTTP_METHOD, "GET"),
equalTo(SemanticAttributes.HTTP_STATUS_CODE, 200),
equalTo(SemanticAttributes.HTTP_FLAVOR, "2.0"),
equalTo(NetAttributes.NET_PROTOCOL_NAME, "http"),
equalTo(NetAttributes.NET_PROTOCOL_VERSION, "2.0"),
equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"),
equalTo(SemanticAttributes.NET_PEER_PORT, 1234),
equalTo(
Expand All @@ -134,7 +137,8 @@ void collectsMetrics() {
.hasAttributesSatisfying(
equalTo(SemanticAttributes.HTTP_METHOD, "GET"),
equalTo(SemanticAttributes.HTTP_STATUS_CODE, 200),
equalTo(SemanticAttributes.HTTP_FLAVOR, "2.0"),
equalTo(NetAttributes.NET_PROTOCOL_NAME, "http"),
equalTo(NetAttributes.NET_PROTOCOL_VERSION, "2.0"),
equalTo(SemanticAttributes.NET_PEER_NAME, "localhost"),
equalTo(SemanticAttributes.NET_PEER_PORT, 1234),
equalTo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.net.NetServerAttributesGetter;
import io.opentelemetry.instrumentation.api.instrumenter.net.internal.NetAttributes;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -70,11 +71,6 @@ public Integer getStatusCode(
return value == null ? null : Integer.parseInt(value);
}

@Override
public String getFlavor(Map<String, Object> request) {
return (String) request.get("flavor");
}

@Override
public List<String> getResponseHeader(
Map<String, Object> request, Map<String, Object> response, String name) {
Expand All @@ -91,6 +87,18 @@ public String getTransport(Map<String, Object> request) {
return (String) request.get("transport");
}

@Nullable
@Override
public String getProtocolName(Map<String, Object> request) {
return (String) request.get("protocolName");
}

@Nullable
@Override
public String getProtocolVersion(Map<String, Object> request) {
return (String) request.get("protocolVersion");
}

@Nullable
@Override
public String getHostName(Map<String, Object> request) {
Expand All @@ -112,12 +120,13 @@ void normal() {
request.put("target", "/repositories/1");
request.put("scheme", "http");
request.put("header.content-length", "10");
request.put("flavor", "http/2");
request.put("route", "/repositories/{id}");
request.put("header.user-agent", "okhttp 3.x");
request.put("header.host", "github.com");
request.put("header.forwarded", "for=1.1.1.1;proto=https");
request.put("header.custom-request-header", "123,456");
request.put("protocolName", "http");
request.put("protocolVersion", "2.0");

Map<String, Object> response = new HashMap<>();
response.put("statusCode", "202");
Expand All @@ -139,7 +148,8 @@ void normal() {
assertThat(attributes.build())
.containsOnly(
entry(SemanticAttributes.NET_HOST_NAME, "github.com"),
entry(SemanticAttributes.HTTP_FLAVOR, "http/2"),
entry(NetAttributes.NET_PROTOCOL_NAME, "http"),
entry(NetAttributes.NET_PROTOCOL_VERSION, "2.0"),
entry(SemanticAttributes.HTTP_METHOD, "POST"),
entry(SemanticAttributes.HTTP_SCHEME, "https"),
entry(SemanticAttributes.HTTP_TARGET, "/repositories/1"),
Expand All @@ -154,6 +164,8 @@ void normal() {
assertThat(attributes.build())
.containsOnly(
entry(SemanticAttributes.NET_HOST_NAME, "github.com"),
entry(NetAttributes.NET_PROTOCOL_NAME, "http"),
entry(NetAttributes.NET_PROTOCOL_VERSION, "2.0"),
entry(SemanticAttributes.HTTP_METHOD, "POST"),
entry(SemanticAttributes.HTTP_SCHEME, "https"),
entry(SemanticAttributes.HTTP_TARGET, "/repositories/1"),
Expand All @@ -164,7 +176,6 @@ void normal() {
entry(
AttributeKey.stringArrayKey("http.request.header.custom_request_header"),
asList("123", "456")),
entry(SemanticAttributes.HTTP_FLAVOR, "http/2"),
entry(SemanticAttributes.HTTP_STATUS_CODE, 202L),
entry(SemanticAttributes.HTTP_RESPONSE_CONTENT_LENGTH, 20L),
entry(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.assertThat;
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.HttpFlavorValues.HTTP_2_0;
import static io.opentelemetry.semconv.trace.attributes.SemanticAttributes.NetTransportValues.IP_TCP;

import io.opentelemetry.api.common.Attributes;
Expand All @@ -17,6 +16,7 @@
import io.opentelemetry.api.trace.TraceState;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.instrumenter.OperationListener;
import io.opentelemetry.instrumentation.api.instrumenter.net.internal.NetAttributes;
import io.opentelemetry.sdk.metrics.SdkMeterProvider;
import io.opentelemetry.sdk.testing.exporter.InMemoryMetricReader;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
Expand All @@ -36,10 +36,11 @@ void collectsMetrics() {
Attributes requestAttributes =
Attributes.builder()
.put("http.method", "GET")
.put("http.flavor", HTTP_2_0)
.put("http.target", "/")
.put("http.scheme", "https")
.put("net.transport", IP_TCP)
.put(NetAttributes.NET_PROTOCOL_NAME, "http")
.put(NetAttributes.NET_PROTOCOL_VERSION, "2.0")
.put("net.host.name", "localhost")
.put("net.host.port", 1234)
.put("net.sock.family", "inet")
Expand Down Expand Up @@ -89,7 +90,6 @@ void collectsMetrics() {
.hasAttributesSatisfying(
equalTo(SemanticAttributes.HTTP_METHOD, "GET"),
equalTo(SemanticAttributes.HTTP_SCHEME, "https"),
equalTo(SemanticAttributes.HTTP_FLAVOR, HTTP_2_0),
equalTo(SemanticAttributes.NET_HOST_NAME, "localhost"),
equalTo(SemanticAttributes.NET_HOST_PORT, 1234L))
.hasExemplarsSatisfying(
Expand All @@ -115,7 +115,6 @@ void collectsMetrics() {
.hasAttributesSatisfying(
equalTo(SemanticAttributes.HTTP_METHOD, "GET"),
equalTo(SemanticAttributes.HTTP_SCHEME, "https"),
equalTo(SemanticAttributes.HTTP_FLAVOR, HTTP_2_0),
equalTo(SemanticAttributes.NET_HOST_NAME, "localhost"),
equalTo(SemanticAttributes.NET_HOST_PORT, 1234L))
.hasExemplarsSatisfying(
Expand All @@ -140,7 +139,6 @@ void collectsMetrics() {
.hasAttributesSatisfying(
equalTo(SemanticAttributes.HTTP_METHOD, "GET"),
equalTo(SemanticAttributes.HTTP_SCHEME, "https"),
equalTo(SemanticAttributes.HTTP_FLAVOR, HTTP_2_0),
equalTo(SemanticAttributes.NET_HOST_NAME, "localhost"),
equalTo(SemanticAttributes.NET_HOST_PORT, 1234L))
.hasExemplarsSatisfying(
Expand All @@ -161,7 +159,8 @@ void collectsMetrics() {
.hasAttributesSatisfying(
equalTo(SemanticAttributes.HTTP_METHOD, "GET"),
equalTo(SemanticAttributes.HTTP_STATUS_CODE, 200),
equalTo(SemanticAttributes.HTTP_FLAVOR, "2.0"),
equalTo(NetAttributes.NET_PROTOCOL_NAME, "http"),
equalTo(NetAttributes.NET_PROTOCOL_VERSION, "2.0"),
equalTo(SemanticAttributes.HTTP_SCHEME, "https"),
equalTo(SemanticAttributes.NET_HOST_NAME, "localhost"),
equalTo(SemanticAttributes.NET_HOST_PORT, 1234))
Expand All @@ -183,7 +182,8 @@ void collectsMetrics() {
.hasAttributesSatisfying(
equalTo(SemanticAttributes.HTTP_METHOD, "GET"),
equalTo(SemanticAttributes.HTTP_STATUS_CODE, 200),
equalTo(SemanticAttributes.HTTP_FLAVOR, "2.0"),
equalTo(NetAttributes.NET_PROTOCOL_NAME, "http"),
equalTo(NetAttributes.NET_PROTOCOL_VERSION, "2.0"),
equalTo(SemanticAttributes.HTTP_SCHEME, "https"),
equalTo(SemanticAttributes.NET_HOST_NAME, "localhost"),
equalTo(SemanticAttributes.NET_HOST_PORT, 1234))
Expand All @@ -205,7 +205,8 @@ void collectsMetrics() {
.hasAttributesSatisfying(
equalTo(SemanticAttributes.HTTP_METHOD, "GET"),
equalTo(SemanticAttributes.HTTP_STATUS_CODE, 200),
equalTo(SemanticAttributes.HTTP_FLAVOR, "2.0"),
equalTo(NetAttributes.NET_PROTOCOL_NAME, "http"),
equalTo(NetAttributes.NET_PROTOCOL_VERSION, "2.0"),
equalTo(SemanticAttributes.HTTP_SCHEME, "https"),
equalTo(SemanticAttributes.NET_HOST_NAME, "localhost"),
equalTo(SemanticAttributes.NET_HOST_PORT, 1234))
Expand Down
Loading