Skip to content

Commit

Permalink
Change user_agent.original from recommended to opt-in on HTTP clien…
Browse files Browse the repository at this point in the history
…t spans (open-telemetry#9776)
  • Loading branch information
Mateusz Rzeszutek authored and Abhishekkr3003 committed Nov 7, 2023
1 parent a6c74e3 commit 4f0cf16
Show file tree
Hide file tree
Showing 42 changed files with 20 additions and 277 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST
if (SemconvStability.emitOldHttpSemconv()) {
internalSet(attributes, SemanticAttributes.HTTP_METHOD, method);
}
internalSet(attributes, SemanticAttributes.USER_AGENT_ORIGINAL, userAgent(request));

for (String name : capturedRequestHeaders) {
List<String> values = getter.getHttpRequestHeader(request, name);
Expand Down Expand Up @@ -152,11 +151,6 @@ public void onEnd(
}
}

@Nullable
private String userAgent(REQUEST request) {
return firstHeaderValue(getter.getHttpRequestHeader(request, "user-agent"));
}

@Nullable
private Long requestBodySize(REQUEST request) {
return parseNumber(firstHeaderValue(getter.getHttpRequestHeader(request, "content-length")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public void onStart(AttributesBuilder attributes, Context parentContext, REQUEST
internalClientExtractor.onStart(attributes, request);

internalSet(attributes, SemanticAttributes.HTTP_ROUTE, getter.getHttpRoute(request));
internalSet(attributes, SemanticAttributes.USER_AGENT_ORIGINAL, userAgent(request));
}

@Override
Expand All @@ -140,4 +141,9 @@ public void onEnd(
public SpanKey internalGetSpanKey() {
return SpanKey.HTTP_SERVER;
}

@Nullable
private String userAgent(REQUEST request) {
return firstHeaderValue(getter.getHttpRequestHeader(request, "user-agent"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ void normal() {
.containsOnly(
entry(SemanticAttributes.HTTP_METHOD, "POST"),
entry(SemanticAttributes.HTTP_URL, "http://github.com"),
entry(SemanticAttributes.USER_AGENT_ORIGINAL, "okhttp 3.x"),
entry(
AttributeKey.stringArrayKey("http.request.header.custom_request_header"),
asList("123", "456")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,6 @@ void normal() {
entry(SemanticAttributes.HTTP_REQUEST_METHOD, "POST"),
entry(SemanticAttributes.HTTP_URL, "http://github.com"),
entry(SemanticAttributes.URL_FULL, "http://github.com"),
entry(SemanticAttributes.USER_AGENT_ORIGINAL, "okhttp 3.x"),
entry(
AttributeKey.stringArrayKey("http.request.header.custom_request_header"),
asList("123", "456")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ void normal() {
.containsOnly(
entry(SemanticAttributes.HTTP_REQUEST_METHOD, "POST"),
entry(SemanticAttributes.URL_FULL, "http://github.com"),
entry(SemanticAttributes.USER_AGENT_ORIGINAL, "okhttp 3.x"),
entry(
AttributeKey.stringArrayKey("http.request.header.custom-request-header"),
asList("123", "456")),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientInstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
Expand Down Expand Up @@ -93,11 +92,6 @@ public void sendRequestWithCallback(
HttpClientResult httpClientResult) {
getClient(uri).execute(request, responseCallback(httpClientResult));
}

@Override
protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
configureTest(optionsBuilder);
}
}

@Nested
Expand Down Expand Up @@ -132,11 +126,6 @@ public void sendRequestWithCallback(
request,
responseCallback(httpClientResult));
}

@Override
protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
configureTest(optionsBuilder);
}
}

@Nested
Expand Down Expand Up @@ -170,11 +159,6 @@ public void sendRequestWithCallback(
request,
responseCallback(httpClientResult));
}

@Override
protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
configureTest(optionsBuilder);
}
}

HttpUriRequest configureRequest(HttpUriRequest request, Map<String, String> headers) {
Expand Down Expand Up @@ -220,10 +204,6 @@ public void cancelled() {
};
}

void configureTest(HttpClientTestOptions.Builder optionsBuilder) {
optionsBuilder.setUserAgent("httpasyncclient");
}

static String fullPathFromUri(URI uri) {
StringBuilder builder = new StringBuilder();
if (uri.getPath() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions;
import java.net.URI;
import java.util.Map;
import org.apache.http.HttpHost;
Expand All @@ -19,17 +18,10 @@
abstract class AbstractApacheHttpClientTest<T extends HttpRequest>
extends AbstractHttpClientTest<T> {

private static final String USER_AGENT = "apachehttpclient";

@Override
protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
optionsBuilder.setUserAgent(USER_AGENT);
}

@Override
public T buildRequest(String method, URI uri, Map<String, String> headers) {
T request = createRequest(method, uri);
request.addHeader("user-agent", USER_AGENT);
request.addHeader("user-agent", "apachehttpclient");
headers.forEach(request::setHeader);
return request;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension;
import io.opentelemetry.instrumentation.testing.junit.http.AbstractHttpClientTest;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientResult;
import io.opentelemetry.instrumentation.testing.junit.http.HttpClientTestOptions;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.net.URI;
Expand Down Expand Up @@ -90,11 +89,6 @@ public void sendRequestWithCallback(
httpClientResult.complete(t);
}
}

@Override
protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
configureTest(optionsBuilder);
}
}

@Nested
Expand Down Expand Up @@ -136,11 +130,6 @@ public void sendRequestWithCallback(
httpClientResult.complete(t);
}
}

@Override
protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
configureTest(optionsBuilder);
}
}

@Nested
Expand Down Expand Up @@ -177,11 +166,6 @@ public void sendRequestWithCallback(
httpClientResult.complete(t);
}
}

@Override
protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
configureTest(optionsBuilder);
}
}

@Nested
Expand Down Expand Up @@ -223,11 +207,6 @@ public void sendRequestWithCallback(
httpClientResult.complete(t);
}
}

@Override
protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
configureTest(optionsBuilder);
}
}

@Nested
Expand Down Expand Up @@ -259,11 +238,6 @@ public void sendRequestWithCallback(
httpClientResult.complete(t);
}
}

@Override
protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
configureTest(optionsBuilder);
}
}

@Nested
Expand Down Expand Up @@ -295,11 +269,6 @@ public void sendRequestWithCallback(
httpClientResult.complete(t);
}
}

@Override
protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
configureTest(optionsBuilder);
}
}

static <T extends HttpRequest> T configureRequest(T request, Map<String, String> headers) {
Expand Down Expand Up @@ -331,10 +300,6 @@ static ResponseHandler<HttpResponse> responseCallback(HttpClientResult httpClien
};
}

static void configureTest(HttpClientTestOptions.Builder optionsBuilder) {
optionsBuilder.setUserAgent("apachehttpclient");
}

static String fullPathFromUri(URI uri) {
StringBuilder builder = new StringBuilder();
if (uri.getPath() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@

abstract class AbstractApacheHttpClientTest<T extends HttpRequest>
extends AbstractHttpClientTest<T> {
private static final String USER_AGENT = "apachehttpclient";

@Override
protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
optionsBuilder.setUserAgent(USER_AGENT);
optionsBuilder.setHttpAttributes(this::getHttpAttributes);
}

Expand All @@ -55,7 +53,7 @@ protected Set<AttributeKey<?>> getHttpAttributes(URI uri) {
@Override
public T buildRequest(String method, URI uri, Map<String, String> headers) {
T request = createRequest(method, uri);
request.addHeader("user-agent", USER_AGENT);
request.addHeader("user-agent", "apachehttpclient");
headers.forEach((key, value) -> request.setHeader(new BasicHeader(key, value)));
return request;
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public final class AsyncHttpClientSingletons {
.addAttributesExtractor(
HttpClientPeerServiceAttributesExtractor.create(
httpAttributesGetter, CommonConfig.get().getPeerServiceResolver()))
.addAttributesExtractor(new AsyncHttpClientAdditionalAttributesExtractor())
.addOperationMetrics(HttpClientMetrics.get());
if (CommonConfig.get().shouldEmitExperimentalHttpClientMetrics()) {
builder.addOperationMetrics(HttpClientExperimentalMetrics.get());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,5 @@ public void onThrowable(Throwable throwable) {
@Override
protected void configure(HttpClientTestOptions.Builder optionsBuilder) {
optionsBuilder.disableTestRedirects();
optionsBuilder.setUserAgent("AHC");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,6 @@ class S3TracingTest extends AgentInstrumentationSpecification {
"http.method" "POST"
"http.status_code" 200
"http.url" String
"$SemanticAttributes.USER_AGENT_ORIGINAL" String
"net.peer.name" String
"$SemanticAttributes.NET_PROTOCOL_NAME" "http"
"$SemanticAttributes.NET_PROTOCOL_VERSION" "1.1"
Expand Down Expand Up @@ -572,7 +571,6 @@ class S3TracingTest extends AgentInstrumentationSpecification {
"http.method" "POST"
"http.status_code" 200
"http.url" String
"$SemanticAttributes.USER_AGENT_ORIGINAL" String
"net.peer.name" String
"$SemanticAttributes.NET_PROTOCOL_NAME" "http"
"$SemanticAttributes.NET_PROTOCOL_VERSION" "1.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,6 @@ class SnsTracingTest extends AgentInstrumentationSpecification {
"http.method" "POST"
"http.status_code" 200
"http.url" String
"$SemanticAttributes.USER_AGENT_ORIGINAL" String
"net.peer.name" String
"$SemanticAttributes.NET_PROTOCOL_NAME" "http"
"$SemanticAttributes.NET_PROTOCOL_VERSION" "1.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ abstract class AbstractSqsTracingTest extends InstrumentationSpecification {
"http.method" "POST"
"http.status_code" 200
"http.url" "http://localhost:$sqsPort"
"$SemanticAttributes.USER_AGENT_ORIGINAL" String
"net.peer.name" "localhost"
"net.peer.port" sqsPort
"$SemanticAttributes.MESSAGING_SYSTEM" "AmazonSQS"
Expand Down Expand Up @@ -213,7 +212,6 @@ abstract class AbstractSqsTracingTest extends InstrumentationSpecification {
"http.method" "POST"
"http.status_code" 200
"http.url" "http://localhost:$sqsPort"
"$SemanticAttributes.USER_AGENT_ORIGINAL" String
"net.peer.name" "localhost"
"net.peer.port" sqsPort
"$SemanticAttributes.MESSAGING_SYSTEM" "AmazonSQS"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.time.Instant;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.annotation.Nullable;
Expand Down Expand Up @@ -322,7 +321,6 @@ public void afterExecution(
executionAttributes.putAttribute(SDK_HTTP_REQUEST_ATTRIBUTE, context.httpRequest());

Span span = Span.fromContext(otelContext);
onUserAgentHeaderAvailable(span, executionAttributes);
onSdkResponse(span, context.response(), executionAttributes);

SdkHttpResponse httpResponse = context.httpResponse();
Expand All @@ -336,15 +334,6 @@ public void afterExecution(
clearAttributes(executionAttributes);
}

// Certain headers in the request like User-Agent are only available after execution.
private static void onUserAgentHeaderAvailable(Span span, ExecutionAttributes request) {
List<String> userAgent =
AwsSdkInstrumenterFactory.httpAttributesGetter.getHttpRequestHeader(request, "User-Agent");
if (!userAgent.isEmpty()) {
span.setAttribute(SemanticAttributes.USER_AGENT_ORIGINAL, userAgent.get(0));
}
}

private void onSdkResponse(
Span span, SdkResponse response, ExecutionAttributes executionAttributes) {
if (captureExperimentalSpanAttributes) {
Expand Down
Loading

0 comments on commit 4f0cf16

Please sign in to comment.