diff --git a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/tracer/HttpClientTracer.java b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/tracer/HttpClientTracer.java index 185cd9952ce4..405a46f04d41 100644 --- a/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/tracer/HttpClientTracer.java +++ b/instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/tracer/HttpClientTracer.java @@ -38,7 +38,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public abstract class HttpClientTracer extends BaseTracer { +public abstract class HttpClientTracer extends BaseTracer { private static final Logger log = LoggerFactory.getLogger(HttpClientTracer.class); @@ -56,7 +56,7 @@ public abstract class HttpClientTracer extends BaseTracer { protected abstract String responseHeader(RESPONSE response, String name); - protected abstract HttpTextFormat.Setter getSetter(); + protected abstract HttpTextFormat.Setter getSetter(); protected HttpClientTracer() { super(); @@ -74,15 +74,15 @@ public Span startSpan(REQUEST request, long startTimeNanos) { return startSpan(request, spanNameForRequest(request), startTimeNanos); } - public Scope startScope(Span span, REQUEST request) { + public Scope startScope(Span span, CARRIER carrier) { Context context = withSpan(span, Context.current()); - Setter setter = getSetter(); + Setter setter = getSetter(); if (setter == null) { throw new IllegalStateException( "getSetter() not defined but calling startScope(), either getSetter must be implemented or the scope should be setup manually"); } - OpenTelemetry.getPropagators().getHttpTextFormat().inject(context, request, setter); + OpenTelemetry.getPropagators().getHttpTextFormat().inject(context, carrier, setter); context = context.withValue(CONTEXT_CLIENT_SPAN_KEY, span); return withScopedContext(context); } diff --git a/instrumentation-api/src/test/groovy/io/opentelemetry/instrumentation/api/tracer/HttpClientTracerTest.groovy b/instrumentation-api/src/test/groovy/io/opentelemetry/instrumentation/api/tracer/HttpClientTracerTest.groovy index 7b71d7318cb9..51f3b44e4f78 100644 --- a/instrumentation-api/src/test/groovy/io/opentelemetry/instrumentation/api/tracer/HttpClientTracerTest.groovy +++ b/instrumentation-api/src/test/groovy/io/opentelemetry/instrumentation/api/tracer/HttpClientTracerTest.groovy @@ -170,7 +170,7 @@ class HttpClientTracerTest extends BaseTracerTest { @Override def newTracer() { - return new HttpClientTracer() { + return new HttpClientTracer() { @Override protected String method(Map m) { diff --git a/instrumentation-core/spring/spring-web-3.1/src/main/java/io/opentelemetry/instrumentation/spring/httpclients/HttpHeadersInjectAdapter.java b/instrumentation-core/spring/spring-web-3.1/src/main/java/io/opentelemetry/instrumentation/spring/httpclients/HttpHeadersInjectAdapter.java index ec3718f50c65..fa5069a3fefa 100644 --- a/instrumentation-core/spring/spring-web-3.1/src/main/java/io/opentelemetry/instrumentation/spring/httpclients/HttpHeadersInjectAdapter.java +++ b/instrumentation-core/spring/spring-web-3.1/src/main/java/io/opentelemetry/instrumentation/spring/httpclients/HttpHeadersInjectAdapter.java @@ -17,14 +17,14 @@ package io.opentelemetry.instrumentation.spring.httpclients; import io.opentelemetry.context.propagation.HttpTextFormat; -import org.springframework.http.HttpRequest; +import org.springframework.http.HttpHeaders; -class HttpHeadersInjectAdapter implements HttpTextFormat.Setter { +class HttpHeadersInjectAdapter implements HttpTextFormat.Setter { public static final HttpHeadersInjectAdapter SETTER = new HttpHeadersInjectAdapter(); @Override - public void set(HttpRequest carrier, String key, String value) { - carrier.getHeaders().set(key, value); + public void set(HttpHeaders carrier, String key, String value) { + carrier.set(key, value); } } diff --git a/instrumentation-core/spring/spring-web-3.1/src/main/java/io/opentelemetry/instrumentation/spring/httpclients/RestTemplateInterceptor.java b/instrumentation-core/spring/spring-web-3.1/src/main/java/io/opentelemetry/instrumentation/spring/httpclients/RestTemplateInterceptor.java index efded28a5f74..feb08c45cb7d 100644 --- a/instrumentation-core/spring/spring-web-3.1/src/main/java/io/opentelemetry/instrumentation/spring/httpclients/RestTemplateInterceptor.java +++ b/instrumentation-core/spring/spring-web-3.1/src/main/java/io/opentelemetry/instrumentation/spring/httpclients/RestTemplateInterceptor.java @@ -41,7 +41,7 @@ public ClientHttpResponse intercept( HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { Span span = TRACER.startSpan(request); - try (Scope scope = TRACER.startScope(span, request)) { + try (Scope scope = TRACER.startScope(span, request.getHeaders())) { ClientHttpResponse response = execution.execute(request, body); TRACER.end(span, response); return response; diff --git a/instrumentation-core/spring/spring-web-3.1/src/main/java/io/opentelemetry/instrumentation/spring/httpclients/RestTemplateTracer.java b/instrumentation-core/spring/spring-web-3.1/src/main/java/io/opentelemetry/instrumentation/spring/httpclients/RestTemplateTracer.java index 4b5add469388..d0ffc50f11cc 100644 --- a/instrumentation-core/spring/spring-web-3.1/src/main/java/io/opentelemetry/instrumentation/spring/httpclients/RestTemplateTracer.java +++ b/instrumentation-core/spring/spring-web-3.1/src/main/java/io/opentelemetry/instrumentation/spring/httpclients/RestTemplateTracer.java @@ -16,34 +16,28 @@ package io.opentelemetry.instrumentation.spring.httpclients; -import static io.opentelemetry.OpenTelemetry.getPropagators; import static io.opentelemetry.instrumentation.spring.httpclients.HttpHeadersInjectAdapter.SETTER; -import io.grpc.Context; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.instrumentation.api.tracer.HttpClientTracer; import java.io.IOException; import java.net.URI; -import java.net.URISyntaxException; +import org.springframework.http.HttpHeaders; import org.springframework.http.HttpRequest; import org.springframework.http.HttpStatus; import org.springframework.http.client.ClientHttpResponse; -class RestTemplateTracer extends HttpClientTracer { +class RestTemplateTracer extends HttpClientTracer { public static final RestTemplateTracer TRACER = new RestTemplateTracer(); - public void inject(Context context, HttpRequest request) { - getPropagators().getHttpTextFormat().inject(context, request, getSetter()); - } - @Override protected String method(HttpRequest httpRequest) { return httpRequest.getMethod().name(); } @Override - protected URI url(HttpRequest request) throws URISyntaxException { + protected URI url(HttpRequest request) { return request.getURI(); } @@ -67,7 +61,7 @@ protected String responseHeader(ClientHttpResponse response, String name) { } @Override - protected Setter getSetter() { + protected Setter getSetter() { return SETTER; } diff --git a/instrumentation-core/spring/spring-webflux-5.0/src/main/java/io/opentelemetry/instrumentation/spring/webflux/client/HttpHeadersInjectAdapter.java b/instrumentation-core/spring/spring-webflux-5.0/src/main/java/io/opentelemetry/instrumentation/spring/webflux/client/HttpHeadersInjectAdapter.java index 5f35b8a77ec4..b7750969a655 100644 --- a/instrumentation-core/spring/spring-webflux-5.0/src/main/java/io/opentelemetry/instrumentation/spring/webflux/client/HttpHeadersInjectAdapter.java +++ b/instrumentation-core/spring/spring-webflux-5.0/src/main/java/io/opentelemetry/instrumentation/spring/webflux/client/HttpHeadersInjectAdapter.java @@ -17,14 +17,14 @@ package io.opentelemetry.instrumentation.spring.webflux.client; import io.opentelemetry.context.propagation.HttpTextFormat; -import org.springframework.http.HttpHeaders; +import org.springframework.web.reactive.function.client.ClientRequest; -class HttpHeadersInjectAdapter implements HttpTextFormat.Setter { +class HttpHeadersInjectAdapter implements HttpTextFormat.Setter { public static final HttpHeadersInjectAdapter SETTER = new HttpHeadersInjectAdapter(); @Override - public void set(HttpHeaders carrier, String key, String value) { - carrier.set(key, value); + public void set(ClientRequest.Builder carrier, String key, String value) { + carrier.header(key, value); } } diff --git a/instrumentation-core/spring/spring-webflux-5.0/src/main/java/io/opentelemetry/instrumentation/spring/webflux/client/SpringWebfluxHttpClientTracer.java b/instrumentation-core/spring/spring-webflux-5.0/src/main/java/io/opentelemetry/instrumentation/spring/webflux/client/SpringWebfluxHttpClientTracer.java index 92e0dc12d96e..d3c8adb368ca 100644 --- a/instrumentation-core/spring/spring-webflux-5.0/src/main/java/io/opentelemetry/instrumentation/spring/webflux/client/SpringWebfluxHttpClientTracer.java +++ b/instrumentation-core/spring/spring-webflux-5.0/src/main/java/io/opentelemetry/instrumentation/spring/webflux/client/SpringWebfluxHttpClientTracer.java @@ -16,21 +16,19 @@ package io.opentelemetry.instrumentation.spring.webflux.client; -import static io.opentelemetry.OpenTelemetry.getPropagators; import static io.opentelemetry.instrumentation.spring.webflux.client.HttpHeadersInjectAdapter.SETTER; -import io.grpc.Context; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.instrumentation.api.tracer.HttpClientTracer; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Tracer; import java.net.URI; import java.util.List; -import org.springframework.http.HttpHeaders; import org.springframework.web.reactive.function.client.ClientRequest; import org.springframework.web.reactive.function.client.ClientResponse; -class SpringWebfluxHttpClientTracer extends HttpClientTracer { +public class SpringWebfluxHttpClientTracer + extends HttpClientTracer { public static final SpringWebfluxHttpClientTracer TRACER = new SpringWebfluxHttpClientTracer(); @@ -39,10 +37,6 @@ public void onCancel(Span span) { span.setAttribute("message", "The subscription was cancelled"); } - public void inject(Context context, HttpHeaders httpHeaders) { - getPropagators().getHttpTextFormat().inject(context, httpHeaders, SETTER); - } - @Override protected String method(ClientRequest httpRequest) { return httpRequest.method().name(); @@ -70,8 +64,8 @@ protected String responseHeader(ClientResponse clientResponse, String name) { } @Override - protected Setter getSetter() { - return null; + protected Setter getSetter() { + return SETTER; } @Override diff --git a/instrumentation-core/spring/spring-webflux-5.0/src/main/java/io/opentelemetry/instrumentation/spring/webflux/client/WebClientTracingFilter.java b/instrumentation-core/spring/spring-webflux-5.0/src/main/java/io/opentelemetry/instrumentation/spring/webflux/client/WebClientTracingFilter.java index 60cf6bb9aaee..93b3b2bfec39 100644 --- a/instrumentation-core/spring/spring-webflux-5.0/src/main/java/io/opentelemetry/instrumentation/spring/webflux/client/WebClientTracingFilter.java +++ b/instrumentation-core/spring/spring-webflux-5.0/src/main/java/io/opentelemetry/instrumentation/spring/webflux/client/WebClientTracingFilter.java @@ -18,7 +18,6 @@ import static io.opentelemetry.instrumentation.spring.webflux.client.SpringWebfluxHttpClientTracer.TRACER; -import io.grpc.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Tracer; @@ -50,12 +49,9 @@ public static void addFilter( public Mono filter(ClientRequest request, ExchangeFunction next) { Span span = TRACER.startSpan(request); - try (Scope scope = tracer.withSpan(span)) { - ClientRequest mutatedRequest = - ClientRequest.from(request) - .headers(httpHeaders -> TRACER.inject(Context.current(), httpHeaders)) - .build(); - return next.exchange(mutatedRequest) + ClientRequest.Builder requestBuilder = ClientRequest.from(request); + try (Scope ignored = TRACER.startScope(span, requestBuilder)) { + return next.exchange(requestBuilder.build()) .doOnSuccessOrError( (clientResponse, throwable) -> { if (throwable != null) { @@ -66,6 +62,7 @@ public Mono filter(ClientRequest request, ExchangeFunction next) }) .doOnCancel( () -> { + TRACER.onCancel(span); TRACER.end(span); }); } diff --git a/instrumentation/akka-http-10.0/src/main/java/io/opentelemetry/instrumentation/auto/akkahttp/AkkaHttpClientInstrumentation.java b/instrumentation/akka-http-10.0/src/main/java/io/opentelemetry/instrumentation/auto/akkahttp/AkkaHttpClientInstrumentation.java index 958b7c90b42a..73d0b2c3f928 100644 --- a/instrumentation/akka-http-10.0/src/main/java/io/opentelemetry/instrumentation/auto/akkahttp/AkkaHttpClientInstrumentation.java +++ b/instrumentation/akka-http-10.0/src/main/java/io/opentelemetry/instrumentation/auto/akkahttp/AkkaHttpClientInstrumentation.java @@ -16,9 +16,7 @@ package io.opentelemetry.instrumentation.auto.akkahttp; -import static io.opentelemetry.context.ContextUtils.withScopedContext; import static io.opentelemetry.instrumentation.auto.akkahttp.AkkaHttpClientTracer.TRACER; -import static io.opentelemetry.trace.TracingContextUtils.withSpan; import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.takesArgument; @@ -27,8 +25,6 @@ import akka.http.scaladsl.model.HttpRequest; import akka.http.scaladsl.model.HttpResponse; import com.google.auto.service.AutoService; -import io.grpc.Context; -import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.Scope; import io.opentelemetry.context.propagation.HttpTextFormat; import io.opentelemetry.instrumentation.auto.api.CallDepthThreadLocalMap.Depth; @@ -60,6 +56,7 @@ public String[] helperClassNames() { return new String[] { AkkaHttpClientInstrumentation.class.getName() + "$OnCompleteHandler", AkkaHttpClientInstrumentation.class.getName() + "$AkkaHttpHeaders", + AkkaHttpClientInstrumentation.class.getName() + "$InjectAdapter", packageName + ".AkkaHttpClientTracer", }; } @@ -95,15 +92,10 @@ public static void methodEnter( callDepth = TRACER.getCallDepth(); if (callDepth.getAndIncrement() == 0) { span = TRACER.startSpan(request); - - Context context = withSpan(span, Context.current()); - if (request != null) { - AkkaHttpHeaders headers = new AkkaHttpHeaders(request); - OpenTelemetry.getPropagators().getHttpTextFormat().inject(context, request, headers); - // Request is immutable, so we have to assign new value once we update headers - request = headers.getRequest(); - } - scope = withScopedContext(context); + // Request is immutable, so we have to assign new value once we update headers + AkkaHttpHeaders headers = new AkkaHttpHeaders(request); + scope = TRACER.startScope(span, headers); + request = headers.getRequest(); } } @@ -145,21 +137,33 @@ public Void apply(Try result) { } } - public static class AkkaHttpHeaders implements HttpTextFormat.Setter { + public static class AkkaHttpHeaders { private HttpRequest request; public AkkaHttpHeaders(HttpRequest request) { this.request = request; } - @Override - public void set(HttpRequest carrier, String key, String value) { - // It looks like this cast is only needed in Java, Scala would have figured it out - request = (HttpRequest) request.addHeader(RawHeader.create(key, value)); - } - public HttpRequest getRequest() { return request; } + + public void setRequest(HttpRequest request) { + this.request = request; + } + } + + public static class InjectAdapter implements HttpTextFormat.Setter { + + public static final InjectAdapter SETTER = new InjectAdapter(); + + @Override + public void set(AkkaHttpHeaders carrier, String key, String value) { + HttpRequest request = carrier.getRequest(); + if (request != null) { + // It looks like this cast is only needed in Java, Scala would have figured it out + carrier.setRequest((HttpRequest) request.addHeader(RawHeader.create(key, value))); + } + } } } diff --git a/instrumentation/akka-http-10.0/src/main/java/io/opentelemetry/instrumentation/auto/akkahttp/AkkaHttpClientTracer.java b/instrumentation/akka-http-10.0/src/main/java/io/opentelemetry/instrumentation/auto/akkahttp/AkkaHttpClientTracer.java index 13972f1a4569..ef44b84b3f0d 100644 --- a/instrumentation/akka-http-10.0/src/main/java/io/opentelemetry/instrumentation/auto/akkahttp/AkkaHttpClientTracer.java +++ b/instrumentation/akka-http-10.0/src/main/java/io/opentelemetry/instrumentation/auto/akkahttp/AkkaHttpClientTracer.java @@ -16,18 +16,22 @@ package io.opentelemetry.instrumentation.auto.akkahttp; +import static io.opentelemetry.instrumentation.auto.akkahttp.AkkaHttpClientInstrumentation.InjectAdapter.SETTER; + import akka.http.javadsl.model.HttpHeader; import akka.http.scaladsl.HttpExt; import akka.http.scaladsl.model.HttpRequest; import akka.http.scaladsl.model.HttpResponse; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.instrumentation.api.tracer.HttpClientTracer; +import io.opentelemetry.instrumentation.auto.akkahttp.AkkaHttpClientInstrumentation.AkkaHttpHeaders; import io.opentelemetry.instrumentation.auto.api.CallDepthThreadLocalMap; import io.opentelemetry.instrumentation.auto.api.CallDepthThreadLocalMap.Depth; import java.net.URI; import java.net.URISyntaxException; -public class AkkaHttpClientTracer extends HttpClientTracer { +public class AkkaHttpClientTracer + extends HttpClientTracer { public static final AkkaHttpClientTracer TRACER = new AkkaHttpClientTracer(); public Depth getCallDepth() { @@ -60,8 +64,8 @@ protected String responseHeader(HttpResponse httpResponse, String name) { } @Override - protected Setter getSetter() { - return null; + protected Setter getSetter() { + return SETTER; } @Override diff --git a/instrumentation/apache-httpasyncclient-4.0/src/main/java/io/opentelemetry/instrumentation/auto/apachehttpasyncclient/ApacheHttpAsyncClientInstrumentation.java b/instrumentation/apache-httpasyncclient-4.0/src/main/java/io/opentelemetry/instrumentation/auto/apachehttpasyncclient/ApacheHttpAsyncClientInstrumentation.java index b802772adf66..f4ab21329099 100644 --- a/instrumentation/apache-httpasyncclient-4.0/src/main/java/io/opentelemetry/instrumentation/auto/apachehttpasyncclient/ApacheHttpAsyncClientInstrumentation.java +++ b/instrumentation/apache-httpasyncclient-4.0/src/main/java/io/opentelemetry/instrumentation/auto/apachehttpasyncclient/ApacheHttpAsyncClientInstrumentation.java @@ -18,11 +18,9 @@ import static io.opentelemetry.instrumentation.api.tracer.HttpClientTracer.DEFAULT_SPAN_NAME; import static io.opentelemetry.instrumentation.auto.apachehttpasyncclient.ApacheHttpAsyncClientTracer.TRACER; -import static io.opentelemetry.instrumentation.auto.apachehttpasyncclient.HttpHeadersInjectAdapter.SETTER; import static io.opentelemetry.javaagent.tooling.ClassLoaderMatcher.hasClassesNamed; import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.implementsInterface; import static io.opentelemetry.trace.TracingContextUtils.currentContextWith; -import static io.opentelemetry.trace.TracingContextUtils.withSpan; import static java.util.Collections.singletonMap; import static net.bytebuddy.matcher.ElementMatchers.isMethod; import static net.bytebuddy.matcher.ElementMatchers.named; @@ -30,8 +28,6 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArguments; import com.google.auto.service.AutoService; -import io.grpc.Context; -import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.Scope; import io.opentelemetry.javaagent.tooling.Instrumenter; import io.opentelemetry.trace.Span; @@ -140,8 +136,9 @@ public HttpRequest generateRequest() throws IOException, HttpException { span.updateName(TRACER.spanNameForRequest(request)); TRACER.onRequest(span, request); - Context context = withSpan(span, Context.current()); - OpenTelemetry.getPropagators().getHttpTextFormat().inject(context, request, SETTER); + // TODO (trask) expose inject separate from startScope, e.g. for async cases + Scope scope = TRACER.startScope(span, request); + scope.close(); return request; } diff --git a/instrumentation/apache-httpasyncclient-4.0/src/main/java/io/opentelemetry/instrumentation/auto/apachehttpasyncclient/ApacheHttpAsyncClientTracer.java b/instrumentation/apache-httpasyncclient-4.0/src/main/java/io/opentelemetry/instrumentation/auto/apachehttpasyncclient/ApacheHttpAsyncClientTracer.java index 5507fffc276d..c06c4da03fd2 100644 --- a/instrumentation/apache-httpasyncclient-4.0/src/main/java/io/opentelemetry/instrumentation/auto/apachehttpasyncclient/ApacheHttpAsyncClientTracer.java +++ b/instrumentation/apache-httpasyncclient-4.0/src/main/java/io/opentelemetry/instrumentation/auto/apachehttpasyncclient/ApacheHttpAsyncClientTracer.java @@ -32,7 +32,8 @@ import org.apache.http.StatusLine; import org.apache.http.client.methods.HttpUriRequest; -public class ApacheHttpAsyncClientTracer extends HttpClientTracer { +public class ApacheHttpAsyncClientTracer + extends HttpClientTracer { public static final ApacheHttpAsyncClientTracer TRACER = new ApacheHttpAsyncClientTracer(); diff --git a/instrumentation/apache-httpclient/apache-httpclient-2.0/src/main/java/io/opentelemetry/instrumentation/auto/apachehttpclient/v2_0/CommonsHttpClientTracer.java b/instrumentation/apache-httpclient/apache-httpclient-2.0/src/main/java/io/opentelemetry/instrumentation/auto/apachehttpclient/v2_0/CommonsHttpClientTracer.java index f7a95372f08d..15a926ce7fcf 100644 --- a/instrumentation/apache-httpclient/apache-httpclient-2.0/src/main/java/io/opentelemetry/instrumentation/auto/apachehttpclient/v2_0/CommonsHttpClientTracer.java +++ b/instrumentation/apache-httpclient/apache-httpclient-2.0/src/main/java/io/opentelemetry/instrumentation/auto/apachehttpclient/v2_0/CommonsHttpClientTracer.java @@ -28,7 +28,7 @@ import org.apache.commons.httpclient.StatusLine; import org.apache.commons.httpclient.URIException; -public class CommonsHttpClientTracer extends HttpClientTracer { +public class CommonsHttpClientTracer extends HttpClientTracer { public static final CommonsHttpClientTracer TRACER = new CommonsHttpClientTracer(); @Override diff --git a/instrumentation/apache-httpclient/apache-httpclient-4.0/src/main/java/io/opentelemetry/instrumentation/auto/apachehttpclient/v4_0/ApacheHttpClientTracer.java b/instrumentation/apache-httpclient/apache-httpclient-4.0/src/main/java/io/opentelemetry/instrumentation/auto/apachehttpclient/v4_0/ApacheHttpClientTracer.java index 662384df5599..cc577c12d1e4 100644 --- a/instrumentation/apache-httpclient/apache-httpclient-4.0/src/main/java/io/opentelemetry/instrumentation/auto/apachehttpclient/v4_0/ApacheHttpClientTracer.java +++ b/instrumentation/apache-httpclient/apache-httpclient-4.0/src/main/java/io/opentelemetry/instrumentation/auto/apachehttpclient/v4_0/ApacheHttpClientTracer.java @@ -27,7 +27,8 @@ import org.apache.http.HttpResponse; import org.apache.http.client.methods.HttpUriRequest; -class ApacheHttpClientTracer extends HttpClientTracer { +class ApacheHttpClientTracer + extends HttpClientTracer { public static final ApacheHttpClientTracer TRACER = new ApacheHttpClientTracer(); diff --git a/instrumentation/armeria-1.0/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_0/client/ArmeriaClientTracer.java b/instrumentation/armeria-1.0/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_0/client/ArmeriaClientTracer.java index dfae1050738f..45a68b8ea5be 100644 --- a/instrumentation/armeria-1.0/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_0/client/ArmeriaClientTracer.java +++ b/instrumentation/armeria-1.0/library/src/main/java/io/opentelemetry/instrumentation/armeria/v1_0/client/ArmeriaClientTracer.java @@ -24,7 +24,8 @@ import java.net.URI; import java.net.URISyntaxException; -public class ArmeriaClientTracer extends HttpClientTracer { +public class ArmeriaClientTracer + extends HttpClientTracer { ArmeriaClientTracer() {} diff --git a/instrumentation/aws-sdk/aws-sdk-1.11/src/main/java/io/opentelemetry/instrumentation/auto/awssdk/v1_11/AwsSdkClientTracer.java b/instrumentation/aws-sdk/aws-sdk-1.11/src/main/java/io/opentelemetry/instrumentation/auto/awssdk/v1_11/AwsSdkClientTracer.java index 3d0851600904..ff19662f33a9 100644 --- a/instrumentation/aws-sdk/aws-sdk-1.11/src/main/java/io/opentelemetry/instrumentation/auto/awssdk/v1_11/AwsSdkClientTracer.java +++ b/instrumentation/aws-sdk/aws-sdk-1.11/src/main/java/io/opentelemetry/instrumentation/auto/awssdk/v1_11/AwsSdkClientTracer.java @@ -32,7 +32,7 @@ import java.util.Map; import java.util.concurrent.ConcurrentHashMap; -public class AwsSdkClientTracer extends HttpClientTracer, Response> { +public class AwsSdkClientTracer extends HttpClientTracer, Request, Response> { static final String COMPONENT_NAME = "java-aws-sdk"; diff --git a/instrumentation/aws-sdk/aws-sdk-2.2/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/AwsSdkClientTracer.java b/instrumentation/aws-sdk/aws-sdk-2.2/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/AwsSdkClientTracer.java index 496f0e03e6f7..464b80f59f8c 100644 --- a/instrumentation/aws-sdk/aws-sdk-2.2/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/AwsSdkClientTracer.java +++ b/instrumentation/aws-sdk/aws-sdk-2.2/library/src/main/java/io/opentelemetry/instrumentation/awssdk/v2_2/AwsSdkClientTracer.java @@ -29,7 +29,8 @@ import software.amazon.awssdk.http.SdkHttpRequest; import software.amazon.awssdk.http.SdkHttpResponse; -final class AwsSdkClientTracer extends HttpClientTracer { +final class AwsSdkClientTracer + extends HttpClientTracer { static final AwsSdkClientTracer TRACER = new AwsSdkClientTracer(); // Certain headers in the request like User-Agent are only available after execution. diff --git a/instrumentation/google-http-client-1.19/src/main/java/io/opentelemetry/instrumentation/auto/googlehttpclient/GoogleHttpClientInstrumentation.java b/instrumentation/google-http-client-1.19/src/main/java/io/opentelemetry/instrumentation/auto/googlehttpclient/GoogleHttpClientInstrumentation.java index a7dbe5093925..1e1a97759e92 100644 --- a/instrumentation/google-http-client-1.19/src/main/java/io/opentelemetry/instrumentation/auto/googlehttpclient/GoogleHttpClientInstrumentation.java +++ b/instrumentation/google-http-client-1.19/src/main/java/io/opentelemetry/instrumentation/auto/googlehttpclient/GoogleHttpClientInstrumentation.java @@ -17,8 +17,6 @@ package io.opentelemetry.instrumentation.auto.googlehttpclient; import static io.opentelemetry.instrumentation.auto.googlehttpclient.GoogleHttpClientTracer.TRACER; -import static io.opentelemetry.instrumentation.auto.googlehttpclient.HeadersInjectAdapter.SETTER; -import static io.opentelemetry.trace.TracingContextUtils.withSpan; import static java.util.Collections.singletonMap; import static net.bytebuddy.matcher.ElementMatchers.isMethod; import static net.bytebuddy.matcher.ElementMatchers.isPublic; @@ -30,12 +28,14 @@ import com.google.api.client.http.HttpResponse; import com.google.auto.service.AutoService; import io.grpc.Context; -import io.opentelemetry.OpenTelemetry; +import io.opentelemetry.context.ContextUtils; +import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.auto.api.ContextStore; import io.opentelemetry.instrumentation.auto.api.InstrumentationContext; import io.opentelemetry.javaagent.tooling.Instrumenter; import io.opentelemetry.trace.Span; import io.opentelemetry.trace.Status; +import io.opentelemetry.trace.TracingContextUtils; import java.util.HashMap; import java.util.Map; import net.bytebuddy.asm.Advice; @@ -59,15 +59,13 @@ public ElementMatcher typeMatcher() { @Override public Map contextStore() { - return singletonMap("com.google.api.client.http.HttpRequest", RequestState.class.getName()); + return singletonMap("com.google.api.client.http.HttpRequest", Context.class.getName()); } @Override public String[] helperClassNames() { return new String[] { - packageName + ".GoogleHttpClientTracer", - packageName + ".RequestState", - packageName + ".HeadersInjectAdapter" + packageName + ".GoogleHttpClientTracer", packageName + ".HeadersInjectAdapter" }; } @@ -91,47 +89,45 @@ public Map, String> transfor public static class GoogleHttpClientAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) - public static void methodEnter(@Advice.This HttpRequest request) { - - ContextStore contextStore = - InstrumentationContext.get(HttpRequest.class, RequestState.class); - - RequestState state = contextStore.get(request); - - if (state == null) { - state = new RequestState(TRACER.startSpan(request)); - contextStore.put(request, state); + public static void methodEnter( + @Advice.This HttpRequest request, + @Advice.Local("otelSpan") Span span, + @Advice.Local("otelScope") Scope scope) { + + ContextStore contextStore = + InstrumentationContext.get(HttpRequest.class, Context.class); + Context context = contextStore.get(request); + + if (context == null) { + span = TRACER.startSpan(request); + scope = TRACER.startScope(span, request.getHeaders()); + // TODO (trask) ideally we could pass current context into startScope to avoid extra lookup + contextStore.put(request, Context.current()); + } else { + // span was created by GoogleHttpClientAsyncAdvice instrumentation below + span = TracingContextUtils.getSpan(context); + scope = ContextUtils.withScopedContext(context); } - - Span span = state.getSpan(); - - Context context = withSpan(span, Context.current()); - OpenTelemetry.getPropagators().getHttpTextFormat().inject(context, request, SETTER); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void methodExit( - @Advice.This HttpRequest request, @Advice.Return HttpResponse response, - @Advice.Thrown Throwable throwable) { - - ContextStore contextStore = - InstrumentationContext.get(HttpRequest.class, RequestState.class); - RequestState state = contextStore.get(request); - - if (state != null) { - Span span = state.getSpan(); - - if (throwable == null) { - TRACER.end(span, response); - } else { - TRACER.endExceptionally(span, response, throwable); - } - // If HttpRequest.setThrowExceptionOnExecuteError is set to false, there are no exceptions - // for a failed request. Thus, check the response code - if (response != null && !response.isSuccessStatusCode()) { - span.setStatus(Status.UNKNOWN); - } + @Advice.Thrown Throwable throwable, + @Advice.Local("otelSpan") Span span, + @Advice.Local("otelScope") Scope scope) { + + scope.close(); + + if (throwable == null) { + TRACER.end(span, response); + } else { + TRACER.endExceptionally(span, response, throwable); + } + // If HttpRequest.setThrowExceptionOnExecuteError is set to false, there are no exceptions + // for a failed request. Thus, check the response code + if (response != null && !response.isSuccessStatusCode()) { + span.setStatus(Status.UNKNOWN); } } } @@ -139,31 +135,30 @@ public static void methodExit( public static class GoogleHttpClientAsyncAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) - public static void methodEnter(@Advice.This HttpRequest request) { - Span span = TRACER.startSpan(request); + public static void methodEnter( + @Advice.This HttpRequest request, + @Advice.Local("otelSpan") Span span, + @Advice.Local("otelScope") Scope scope) { - ContextStore contextStore = - InstrumentationContext.get(HttpRequest.class, RequestState.class); + span = TRACER.startSpan(request); + scope = TRACER.startScope(span, request.getHeaders()); - RequestState state = new RequestState(span); - contextStore.put(request, state); + // propagating the context manually here so this instrumentation will work with and without + // the java-concurrent instrumentation + ContextStore contextStore = + InstrumentationContext.get(HttpRequest.class, Context.class); + contextStore.put(request, Context.current()); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void methodExit( - @Advice.This HttpRequest request, @Advice.Thrown Throwable throwable) { + @Advice.Thrown Throwable throwable, + @Advice.Local("otelSpan") Span span, + @Advice.Local("otelScope") Scope scope) { + scope.close(); if (throwable != null) { - - ContextStore contextStore = - InstrumentationContext.get(HttpRequest.class, RequestState.class); - RequestState state = contextStore.get(request); - - if (state != null) { - Span span = state.getSpan(); - - TRACER.endExceptionally(span, throwable); - } + TRACER.endExceptionally(span, throwable); } } } diff --git a/instrumentation/google-http-client-1.19/src/main/java/io/opentelemetry/instrumentation/auto/googlehttpclient/GoogleHttpClientTracer.java b/instrumentation/google-http-client-1.19/src/main/java/io/opentelemetry/instrumentation/auto/googlehttpclient/GoogleHttpClientTracer.java index 9e566b4a4b3d..b39277beb534 100644 --- a/instrumentation/google-http-client-1.19/src/main/java/io/opentelemetry/instrumentation/auto/googlehttpclient/GoogleHttpClientTracer.java +++ b/instrumentation/google-http-client-1.19/src/main/java/io/opentelemetry/instrumentation/auto/googlehttpclient/GoogleHttpClientTracer.java @@ -24,7 +24,8 @@ import java.net.URI; import java.net.URISyntaxException; -public class GoogleHttpClientTracer extends HttpClientTracer { +public class GoogleHttpClientTracer + extends HttpClientTracer { public static final GoogleHttpClientTracer TRACER = new GoogleHttpClientTracer(); @Override @@ -62,7 +63,7 @@ protected String responseHeader(HttpResponse httpResponse, String name) { } @Override - protected Setter getSetter() { + protected Setter getSetter() { return HeadersInjectAdapter.SETTER; } diff --git a/instrumentation/google-http-client-1.19/src/main/java/io/opentelemetry/instrumentation/auto/googlehttpclient/HeadersInjectAdapter.java b/instrumentation/google-http-client-1.19/src/main/java/io/opentelemetry/instrumentation/auto/googlehttpclient/HeadersInjectAdapter.java index dd04ae835381..6adf561f8fce 100644 --- a/instrumentation/google-http-client-1.19/src/main/java/io/opentelemetry/instrumentation/auto/googlehttpclient/HeadersInjectAdapter.java +++ b/instrumentation/google-http-client-1.19/src/main/java/io/opentelemetry/instrumentation/auto/googlehttpclient/HeadersInjectAdapter.java @@ -16,15 +16,15 @@ package io.opentelemetry.instrumentation.auto.googlehttpclient; -import com.google.api.client.http.HttpRequest; +import com.google.api.client.http.HttpHeaders; import io.opentelemetry.context.propagation.HttpTextFormat; -public class HeadersInjectAdapter implements HttpTextFormat.Setter { +public class HeadersInjectAdapter implements HttpTextFormat.Setter { public static final HeadersInjectAdapter SETTER = new HeadersInjectAdapter(); @Override - public void set(HttpRequest carrier, String key, String value) { - carrier.getHeaders().put(key, value); + public void set(HttpHeaders carrier, String key, String value) { + carrier.put(key, value); } } diff --git a/instrumentation/google-http-client-1.19/src/main/java/io/opentelemetry/instrumentation/auto/googlehttpclient/RequestState.java b/instrumentation/google-http-client-1.19/src/main/java/io/opentelemetry/instrumentation/auto/googlehttpclient/RequestState.java deleted file mode 100644 index ad292ee021e6..000000000000 --- a/instrumentation/google-http-client-1.19/src/main/java/io/opentelemetry/instrumentation/auto/googlehttpclient/RequestState.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.opentelemetry.instrumentation.auto.googlehttpclient; - -import io.opentelemetry.trace.Span; -import java.util.Objects; - -public class RequestState { - - private Span span; - - public RequestState(Span span) { - setSpan(span); - } - - public Span getSpan() { - return span; - } - - public void setSpan(Span span) { - Objects.requireNonNull(span); - this.span = span; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - RequestState that = (RequestState) o; - return Objects.equals(span, that.span); - } - - @Override - public int hashCode() { - return Objects.hash(span); - } -} diff --git a/instrumentation/grizzly-client-1.9/src/main/java/io/opentelemetry/instrumentation/auto/grizzly/client/GrizzlyClientTracer.java b/instrumentation/grizzly-client-1.9/src/main/java/io/opentelemetry/instrumentation/auto/grizzly/client/GrizzlyClientTracer.java index 1b4725b4bdcf..1b6e55c647bd 100644 --- a/instrumentation/grizzly-client-1.9/src/main/java/io/opentelemetry/instrumentation/auto/grizzly/client/GrizzlyClientTracer.java +++ b/instrumentation/grizzly-client-1.9/src/main/java/io/opentelemetry/instrumentation/auto/grizzly/client/GrizzlyClientTracer.java @@ -23,7 +23,7 @@ import java.net.URI; import java.net.URISyntaxException; -public class GrizzlyClientTracer extends HttpClientTracer { +public class GrizzlyClientTracer extends HttpClientTracer { public static final GrizzlyClientTracer TRACER = new GrizzlyClientTracer(); diff --git a/instrumentation/http-url-connection/src/main/java/io/opentelemetry/instrumentation/auto/httpurlconnection/HttpUrlConnectionInstrumentation.java b/instrumentation/http-url-connection/src/main/java/io/opentelemetry/instrumentation/auto/httpurlconnection/HttpUrlConnectionInstrumentation.java index 4bc76cbfae3f..b181b732a50a 100644 --- a/instrumentation/http-url-connection/src/main/java/io/opentelemetry/instrumentation/auto/httpurlconnection/HttpUrlConnectionInstrumentation.java +++ b/instrumentation/http-url-connection/src/main/java/io/opentelemetry/instrumentation/auto/httpurlconnection/HttpUrlConnectionInstrumentation.java @@ -16,12 +16,10 @@ package io.opentelemetry.instrumentation.auto.httpurlconnection; -import static io.opentelemetry.instrumentation.auto.httpurlconnection.HeadersInjectAdapter.SETTER; import static io.opentelemetry.instrumentation.auto.httpurlconnection.HttpUrlConnectionTracer.TRACER; import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.extendsClass; import static io.opentelemetry.javaagent.tooling.matcher.NameMatchers.namedOneOf; import static io.opentelemetry.trace.TracingContextUtils.currentContextWith; -import static io.opentelemetry.trace.TracingContextUtils.withSpan; import static java.util.Collections.singletonMap; import static net.bytebuddy.matcher.ElementMatchers.isMethod; import static net.bytebuddy.matcher.ElementMatchers.isPublic; @@ -30,8 +28,6 @@ import static net.bytebuddy.matcher.ElementMatchers.not; import com.google.auto.service.AutoService; -import io.grpc.Context; -import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.auto.api.CallDepthThreadLocalMap; import io.opentelemetry.instrumentation.auto.api.ContextStore; @@ -88,7 +84,9 @@ public static class HttpUrlConnectionAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) public static HttpUrlState methodEnter( - @Advice.This HttpURLConnection thiz, @Advice.FieldValue("connected") boolean connected) { + @Advice.This HttpURLConnection thiz, + @Advice.FieldValue("connected") boolean connected, + @Advice.Local("otelScope") Scope scope) { int callDepth = CallDepthThreadLocalMap.incrementCallDepth(HttpURLConnection.class); if (callDepth > 0) { @@ -103,8 +101,7 @@ public static HttpUrlState methodEnter( if (!state.hasSpan() && !state.isFinished()) { Span span = state.start(thiz); if (!connected) { - Context context = withSpan(span, Context.current()); - OpenTelemetry.getPropagators().getHttpTextFormat().inject(context, thiz, SETTER); + scope = TRACER.startScope(span, thiz); } } return state; @@ -116,8 +113,12 @@ public static void methodExit( @Advice.Enter HttpUrlState state, @Advice.FieldValue("responseCode") int responseCode, @Advice.Thrown Throwable throwable, - @Advice.Origin("#m") String methodName) { + @Advice.Origin("#m") String methodName, + @Advice.Local("otelScope") Scope scope) { + if (scope != null) { + scope.close(); + } if (state == null) { return; } diff --git a/instrumentation/http-url-connection/src/main/java/io/opentelemetry/instrumentation/auto/httpurlconnection/HttpUrlConnectionTracer.java b/instrumentation/http-url-connection/src/main/java/io/opentelemetry/instrumentation/auto/httpurlconnection/HttpUrlConnectionTracer.java index 6da14c605964..a345b05be500 100644 --- a/instrumentation/http-url-connection/src/main/java/io/opentelemetry/instrumentation/auto/httpurlconnection/HttpUrlConnectionTracer.java +++ b/instrumentation/http-url-connection/src/main/java/io/opentelemetry/instrumentation/auto/httpurlconnection/HttpUrlConnectionTracer.java @@ -16,6 +16,8 @@ package io.opentelemetry.instrumentation.auto.httpurlconnection; +import static io.opentelemetry.instrumentation.auto.httpurlconnection.HeadersInjectAdapter.SETTER; + import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.instrumentation.api.tracer.HttpClientTracer; import io.opentelemetry.trace.Span; @@ -24,7 +26,8 @@ import java.net.URI; import java.net.URISyntaxException; -public class HttpUrlConnectionTracer extends HttpClientTracer { +public class HttpUrlConnectionTracer + extends HttpClientTracer { public static final HttpUrlConnectionTracer TRACER = new HttpUrlConnectionTracer(); @@ -55,7 +58,7 @@ protected String responseHeader(Integer integer, String name) { @Override protected Setter getSetter() { - return null; + return SETTER; } @Override diff --git a/instrumentation/http-url-connection/src/main/java/io/opentelemetry/instrumentation/auto/httpurlconnection/UrlInstrumentation.java b/instrumentation/http-url-connection/src/main/java/io/opentelemetry/instrumentation/auto/httpurlconnection/UrlInstrumentation.java index 6c3f9f7f9fc3..d10fd6016593 100644 --- a/instrumentation/http-url-connection/src/main/java/io/opentelemetry/instrumentation/auto/httpurlconnection/UrlInstrumentation.java +++ b/instrumentation/http-url-connection/src/main/java/io/opentelemetry/instrumentation/auto/httpurlconnection/UrlInstrumentation.java @@ -61,7 +61,7 @@ public Map, String> transfor @Override public String[] helperClassNames() { return new String[] { - packageName + ".HttpUrlConnectionTracer", + packageName + ".HttpUrlConnectionTracer", packageName + ".HeadersInjectAdapter" }; } diff --git a/instrumentation/jaxrs-client/jaxrs-client-1.1/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v1_1/InjectAdapter.java b/instrumentation/jaxrs-client/jaxrs-client-1.1/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v1_1/InjectAdapter.java index b7d5b26a4b60..b981a0968bf0 100644 --- a/instrumentation/jaxrs-client/jaxrs-client-1.1/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v1_1/InjectAdapter.java +++ b/instrumentation/jaxrs-client/jaxrs-client-1.1/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v1_1/InjectAdapter.java @@ -24,7 +24,7 @@ public final class InjectAdapter implements HttpTextFormat.Setter public static final InjectAdapter SETTER = new InjectAdapter(); @Override - public void set(ClientRequest clientRequest, String key, String value) { - clientRequest.getHeaders().putSingle(key, value); + public void set(ClientRequest carrier, String key, String value) { + carrier.getHeaders().putSingle(key, value); } } diff --git a/instrumentation/jaxrs-client/jaxrs-client-1.1/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v1_1/JaxRsClientV1Tracer.java b/instrumentation/jaxrs-client/jaxrs-client-1.1/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v1_1/JaxRsClientV1Tracer.java index 6ed9b704142f..ea7831e068b7 100644 --- a/instrumentation/jaxrs-client/jaxrs-client-1.1/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v1_1/JaxRsClientV1Tracer.java +++ b/instrumentation/jaxrs-client/jaxrs-client-1.1/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v1_1/JaxRsClientV1Tracer.java @@ -24,7 +24,8 @@ import io.opentelemetry.instrumentation.api.tracer.HttpClientTracer; import java.net.URI; -public class JaxRsClientV1Tracer extends HttpClientTracer { +public class JaxRsClientV1Tracer + extends HttpClientTracer { public static final JaxRsClientV1Tracer TRACER = new JaxRsClientV1Tracer(); @Override diff --git a/instrumentation/jaxrs-client/jaxrs-client-2.0/jaxrs-client-2.0-jersey-2.0/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v2_0/JerseyClientConnectionErrorInstrumentation.java b/instrumentation/jaxrs-client/jaxrs-client-2.0/jaxrs-client-2.0-jersey-2.0/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v2_0/JerseyClientConnectionErrorInstrumentation.java index f621eb993326..bb3ec0be339e 100644 --- a/instrumentation/jaxrs-client/jaxrs-client-2.0/jaxrs-client-2.0-jersey-2.0/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v2_0/JerseyClientConnectionErrorInstrumentation.java +++ b/instrumentation/jaxrs-client/jaxrs-client-2.0/jaxrs-client-2.0-jersey-2.0/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v2_0/JerseyClientConnectionErrorInstrumentation.java @@ -56,7 +56,9 @@ public ElementMatcher typeMatcher() { @Override public String[] helperClassNames() { return new String[] { - getClass().getName() + "$WrappedFuture", JaxRsClientTracer.class.getName(), + getClass().getName() + "$WrappedFuture", + packageName + ".JaxRsClientTracer", + packageName + ".InjectAdapter", }; } diff --git a/instrumentation/jaxrs-client/jaxrs-client-2.0/jaxrs-client-2.0-resteasy-2.0/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v2_0/ResteasyClientConnectionErrorInstrumentation.java b/instrumentation/jaxrs-client/jaxrs-client-2.0/jaxrs-client-2.0-resteasy-2.0/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v2_0/ResteasyClientConnectionErrorInstrumentation.java index 995f90779396..4cf9141e878f 100644 --- a/instrumentation/jaxrs-client/jaxrs-client-2.0/jaxrs-client-2.0-resteasy-2.0/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v2_0/ResteasyClientConnectionErrorInstrumentation.java +++ b/instrumentation/jaxrs-client/jaxrs-client-2.0/jaxrs-client-2.0-resteasy-2.0/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v2_0/ResteasyClientConnectionErrorInstrumentation.java @@ -56,7 +56,9 @@ public ElementMatcher typeMatcher() { @Override public String[] helperClassNames() { return new String[] { - getClass().getName() + "$WrappedFuture", JaxRsClientTracer.class.getName(), + getClass().getName() + "$WrappedFuture", + packageName + ".JaxRsClientTracer", + packageName + ".InjectAdapter", }; } diff --git a/instrumentation/jaxrs-client/jaxrs-client-2.0/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v2_0/ClientTracingFilter.java b/instrumentation/jaxrs-client/jaxrs-client-2.0/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v2_0/ClientTracingFilter.java index 6a179741f83a..798e675391c0 100644 --- a/instrumentation/jaxrs-client/jaxrs-client-2.0/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v2_0/ClientTracingFilter.java +++ b/instrumentation/jaxrs-client/jaxrs-client-2.0/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v2_0/ClientTracingFilter.java @@ -16,12 +16,9 @@ package io.opentelemetry.instrumentation.auto.jaxrsclient.v2_0; -import static io.opentelemetry.instrumentation.auto.jaxrsclient.v2_0.InjectAdapter.SETTER; import static io.opentelemetry.instrumentation.auto.jaxrsclient.v2_0.JaxRsClientTracer.TRACER; -import static io.opentelemetry.trace.TracingContextUtils.withSpan; -import io.grpc.Context; -import io.opentelemetry.OpenTelemetry; +import io.opentelemetry.context.Scope; import io.opentelemetry.trace.Span; import javax.annotation.Priority; import javax.ws.rs.Priorities; @@ -37,12 +34,9 @@ public class ClientTracingFilter implements ClientRequestFilter, ClientResponseF @Override public void filter(ClientRequestContext requestContext) { Span span = TRACER.startSpan(requestContext); - - Context context = withSpan(span, Context.current()); - OpenTelemetry.getPropagators() - .getHttpTextFormat() - .inject(context, requestContext.getHeaders(), SETTER); - + // TODO (trask) expose inject separate from startScope, e.g. for async cases + Scope scope = TRACER.startScope(span, requestContext); + scope.close(); requestContext.setProperty(SPAN_PROPERTY_NAME, span); } diff --git a/instrumentation/jaxrs-client/jaxrs-client-2.0/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v2_0/InjectAdapter.java b/instrumentation/jaxrs-client/jaxrs-client-2.0/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v2_0/InjectAdapter.java index 13fa79d4cb76..a9418b26f755 100644 --- a/instrumentation/jaxrs-client/jaxrs-client-2.0/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v2_0/InjectAdapter.java +++ b/instrumentation/jaxrs-client/jaxrs-client-2.0/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v2_0/InjectAdapter.java @@ -17,15 +17,15 @@ package io.opentelemetry.instrumentation.auto.jaxrsclient.v2_0; import io.opentelemetry.context.propagation.HttpTextFormat; -import javax.ws.rs.core.MultivaluedMap; +import javax.ws.rs.client.ClientRequestContext; -public final class InjectAdapter implements HttpTextFormat.Setter { +public final class InjectAdapter implements HttpTextFormat.Setter { public static final InjectAdapter SETTER = new InjectAdapter(); @Override - public void set(MultivaluedMap headers, String key, String value) { + public void set(ClientRequestContext carrier, String key, String value) { // Don't allow duplicates. - headers.putSingle(key, value); + carrier.getHeaders().putSingle(key, value); } } diff --git a/instrumentation/jaxrs-client/jaxrs-client-2.0/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v2_0/JaxRsClientTracer.java b/instrumentation/jaxrs-client/jaxrs-client-2.0/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v2_0/JaxRsClientTracer.java index 7e8125983194..f1f269c4a59b 100644 --- a/instrumentation/jaxrs-client/jaxrs-client-2.0/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v2_0/JaxRsClientTracer.java +++ b/instrumentation/jaxrs-client/jaxrs-client-2.0/src/main/java/io/opentelemetry/instrumentation/auto/jaxrsclient/v2_0/JaxRsClientTracer.java @@ -16,6 +16,8 @@ package io.opentelemetry.instrumentation.auto.jaxrsclient.v2_0; +import static io.opentelemetry.instrumentation.auto.jaxrsclient.v2_0.InjectAdapter.SETTER; + import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.instrumentation.api.tracer.HttpClientTracer; import java.net.URI; @@ -23,7 +25,7 @@ import javax.ws.rs.client.ClientResponseContext; public class JaxRsClientTracer - extends HttpClientTracer { + extends HttpClientTracer { public static final JaxRsClientTracer TRACER = new JaxRsClientTracer(); @Override @@ -53,7 +55,7 @@ protected String responseHeader(ClientResponseContext clientResponseContext, Str @Override protected Setter getSetter() { - return null; + return SETTER; } @Override diff --git a/instrumentation/khttp-0.1/src/main/java/io/opentelemetry/instrumentation/auto/khttp/KHttpAdvice.java b/instrumentation/khttp-0.1/src/main/java/io/opentelemetry/instrumentation/auto/khttp/KHttpAdvice.java index 674022e34415..5b914782aa9a 100644 --- a/instrumentation/khttp-0.1/src/main/java/io/opentelemetry/instrumentation/auto/khttp/KHttpAdvice.java +++ b/instrumentation/khttp-0.1/src/main/java/io/opentelemetry/instrumentation/auto/khttp/KHttpAdvice.java @@ -16,14 +16,9 @@ package io.opentelemetry.instrumentation.auto.khttp; -import static io.opentelemetry.context.ContextUtils.withScopedContext; -import static io.opentelemetry.instrumentation.auto.khttp.KHttpHeadersInjectAdapter.SETTER; import static io.opentelemetry.instrumentation.auto.khttp.KHttpHeadersInjectAdapter.asWritable; import static io.opentelemetry.instrumentation.auto.khttp.KHttpTracer.TRACER; -import static io.opentelemetry.trace.TracingContextUtils.withSpan; -import io.grpc.Context; -import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.auto.api.CallDepthThreadLocalMap.Depth; import io.opentelemetry.trace.Span; @@ -46,10 +41,8 @@ public static void methodEnter( if (callDepth.getAndIncrement() == 0) { span = TRACER.startSpan(new RequestWrapper(method, uri, headers)); if (span.getContext().isValid()) { - Context context = withSpan(span, Context.current()); headers = asWritable(headers); - OpenTelemetry.getPropagators().getHttpTextFormat().inject(context, headers, SETTER); - scope = withScopedContext(context); + scope = TRACER.startScope(span, headers); } } } diff --git a/instrumentation/khttp-0.1/src/main/java/io/opentelemetry/instrumentation/auto/khttp/KHttpTracer.java b/instrumentation/khttp-0.1/src/main/java/io/opentelemetry/instrumentation/auto/khttp/KHttpTracer.java index 5b18e83fe2c3..85e22efa643c 100644 --- a/instrumentation/khttp-0.1/src/main/java/io/opentelemetry/instrumentation/auto/khttp/KHttpTracer.java +++ b/instrumentation/khttp-0.1/src/main/java/io/opentelemetry/instrumentation/auto/khttp/KHttpTracer.java @@ -16,16 +16,19 @@ package io.opentelemetry.instrumentation.auto.khttp; +import static io.opentelemetry.instrumentation.auto.khttp.KHttpHeadersInjectAdapter.SETTER; + import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.instrumentation.api.tracer.HttpClientTracer; import io.opentelemetry.instrumentation.auto.api.CallDepthThreadLocalMap; import io.opentelemetry.instrumentation.auto.api.CallDepthThreadLocalMap.Depth; import java.net.URI; import java.net.URISyntaxException; +import java.util.Map; import khttp.KHttp; import khttp.responses.Response; -public class KHttpTracer extends HttpClientTracer { +public class KHttpTracer extends HttpClientTracer, Response> { public static final KHttpTracer TRACER = new KHttpTracer(); public Depth getCallDepth() { @@ -58,8 +61,8 @@ protected String responseHeader(Response response, String name) { } @Override - protected Setter getSetter() { - return null; + protected Setter> getSetter() { + return SETTER; } @Override diff --git a/instrumentation/kubernetes-client-7.0/src/main/java/io/opentelemetry/instrumentation/auto/kubernetesclient/KubernetesClientTracer.java b/instrumentation/kubernetes-client-7.0/src/main/java/io/opentelemetry/instrumentation/auto/kubernetesclient/KubernetesClientTracer.java index 0d8bdbf89a32..6e51222433a8 100644 --- a/instrumentation/kubernetes-client-7.0/src/main/java/io/opentelemetry/instrumentation/auto/kubernetesclient/KubernetesClientTracer.java +++ b/instrumentation/kubernetes-client-7.0/src/main/java/io/opentelemetry/instrumentation/auto/kubernetesclient/KubernetesClientTracer.java @@ -25,7 +25,7 @@ import okhttp3.Request; import okhttp3.Response; -public class KubernetesClientTracer extends HttpClientTracer { +public class KubernetesClientTracer extends HttpClientTracer { public static final KubernetesClientTracer TRACER = new KubernetesClientTracer(); @Override @@ -55,6 +55,7 @@ protected String responseHeader(Response response, String name) { @Override protected Setter getSetter() { + // TODO (trask) no propagation implemented yet? return null; } diff --git a/instrumentation/netty/netty-3.8/src/main/java/io/opentelemetry/instrumentation/auto/netty/v3_8/client/HttpClientRequestTracingHandler.java b/instrumentation/netty/netty-3.8/src/main/java/io/opentelemetry/instrumentation/auto/netty/v3_8/client/HttpClientRequestTracingHandler.java index a2cbb431ec9b..e08eb724d35e 100644 --- a/instrumentation/netty/netty-3.8/src/main/java/io/opentelemetry/instrumentation/auto/netty/v3_8/client/HttpClientRequestTracingHandler.java +++ b/instrumentation/netty/netty-3.8/src/main/java/io/opentelemetry/instrumentation/auto/netty/v3_8/client/HttpClientRequestTracingHandler.java @@ -17,12 +17,8 @@ package io.opentelemetry.instrumentation.auto.netty.v3_8.client; import static io.opentelemetry.instrumentation.auto.netty.v3_8.client.NettyHttpClientTracer.TRACER; -import static io.opentelemetry.instrumentation.auto.netty.v3_8.client.NettyResponseInjectAdapter.SETTER; import static io.opentelemetry.trace.TracingContextUtils.currentContextWith; -import static io.opentelemetry.trace.TracingContextUtils.withSpan; -import io.grpc.Context; -import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.tracer.BaseTracer; import io.opentelemetry.instrumentation.auto.api.ContextStore; @@ -65,12 +61,9 @@ public void writeRequested(ChannelHandlerContext ctx, MessageEvent msg) { Span span = TRACER.startSpan(request); BaseTracer.onPeerConnection(span, (InetSocketAddress) ctx.getChannel().getRemoteAddress()); - Context context = withSpan(span, Context.current()); - OpenTelemetry.getPropagators().getHttpTextFormat().inject(context, request.headers(), SETTER); - channelTraceContext.setClientSpan(span); - try (Scope scope = currentContextWith(span)) { + try (Scope ignored = TRACER.startScope(span, request.headers())) { ctx.sendDownstream(msg); } catch (Throwable throwable) { TRACER.endExceptionally(span, throwable); diff --git a/instrumentation/netty/netty-3.8/src/main/java/io/opentelemetry/instrumentation/auto/netty/v3_8/client/NettyHttpClientTracer.java b/instrumentation/netty/netty-3.8/src/main/java/io/opentelemetry/instrumentation/auto/netty/v3_8/client/NettyHttpClientTracer.java index f11ed1d7a97b..deaea239ca73 100644 --- a/instrumentation/netty/netty-3.8/src/main/java/io/opentelemetry/instrumentation/auto/netty/v3_8/client/NettyHttpClientTracer.java +++ b/instrumentation/netty/netty-3.8/src/main/java/io/opentelemetry/instrumentation/auto/netty/v3_8/client/NettyHttpClientTracer.java @@ -22,19 +22,34 @@ import static org.jboss.netty.handler.codec.http.HttpHeaders.Names.HOST; import io.grpc.Context; -import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.Scope; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.instrumentation.api.tracer.HttpClientTracer; import io.opentelemetry.trace.Span; import java.net.URI; import java.net.URISyntaxException; +import org.jboss.netty.handler.codec.http.HttpHeaders; import org.jboss.netty.handler.codec.http.HttpRequest; import org.jboss.netty.handler.codec.http.HttpResponse; -public class NettyHttpClientTracer extends HttpClientTracer { +public class NettyHttpClientTracer + extends HttpClientTracer { public static final NettyHttpClientTracer TRACER = new NettyHttpClientTracer(); + @Override + public Scope startScope(Span span, HttpHeaders headers) { + if (!headers.contains("amz-sdk-invocation-id")) { + return super.startScope(span, headers); + } else { + // TODO (trask) if we move injection up to aws-sdk layer, and start suppressing nested netty + // spans, do we still need this condition? + // AWS calls are often signed, so we can't add headers without breaking the signature. + Context context = withSpan(span, Context.current()); + context = context.withValue(CONTEXT_CLIENT_SPAN_KEY, span); + return withScopedContext(context); + } + } + @Override protected String method(HttpRequest httpRequest) { return httpRequest.getMethod().getName(); @@ -66,15 +81,8 @@ protected String responseHeader(HttpResponse httpResponse, String name) { } @Override - protected Setter getSetter() { - return null; - } - - @Override - public Scope startScope(Span span, HttpRequest request) { - Context context = withSpan(span, Context.current()); - OpenTelemetry.getPropagators().getHttpTextFormat().inject(context, request.headers(), SETTER); - return withScopedContext(context); + protected Setter getSetter() { + return SETTER; } @Override diff --git a/instrumentation/netty/netty-4.0/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_0/client/HttpClientRequestTracingHandler.java b/instrumentation/netty/netty-4.0/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_0/client/HttpClientRequestTracingHandler.java index 3b9074f1af9e..5f4c62843453 100644 --- a/instrumentation/netty/netty-4.0/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_0/client/HttpClientRequestTracingHandler.java +++ b/instrumentation/netty/netty-4.0/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_0/client/HttpClientRequestTracingHandler.java @@ -17,16 +17,12 @@ package io.opentelemetry.instrumentation.auto.netty.v4_0.client; import static io.opentelemetry.instrumentation.auto.netty.v4_0.client.NettyHttpClientTracer.TRACER; -import static io.opentelemetry.instrumentation.auto.netty.v4_0.client.NettyResponseInjectAdapter.SETTER; import static io.opentelemetry.trace.TracingContextUtils.currentContextWith; -import static io.opentelemetry.trace.TracingContextUtils.withSpan; -import io.grpc.Context; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelOutboundHandlerAdapter; import io.netty.channel.ChannelPromise; import io.netty.handler.codec.http.HttpRequest; -import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.tracer.BaseTracer; import io.opentelemetry.instrumentation.auto.netty.v4_0.AttributeKeys; @@ -59,25 +55,14 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise prm) { } Span span = TRACER.startSpan(request); - try (Scope scope = currentContextWith(span)) { - BaseTracer.onPeerConnection(span, (InetSocketAddress) ctx.channel().remoteAddress()); + BaseTracer.onPeerConnection(span, (InetSocketAddress) ctx.channel().remoteAddress()); + ctx.channel().attr(AttributeKeys.CLIENT_ATTRIBUTE_KEY).set(span); - // AWS calls are often signed, so we can't add headers without breaking the signature. - if (!request.headers().contains("amz-sdk-invocation-id")) { - Context context = withSpan(span, Context.current()); - OpenTelemetry.getPropagators() - .getHttpTextFormat() - .inject(context, request.headers(), SETTER); - } - - ctx.channel().attr(AttributeKeys.CLIENT_ATTRIBUTE_KEY).set(span); - - try { - ctx.write(msg, prm); - } catch (Throwable throwable) { - TRACER.endExceptionally(span, throwable); - throw throwable; - } + try (Scope scope = TRACER.startScope(span, request.headers())) { + ctx.write(msg, prm); + } catch (Throwable throwable) { + TRACER.endExceptionally(span, throwable); + throw throwable; } finally { if (null != parentScope) { parentScope.close(); diff --git a/instrumentation/netty/netty-4.0/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_0/client/NettyHttpClientTracer.java b/instrumentation/netty/netty-4.0/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_0/client/NettyHttpClientTracer.java index 33da68d95e51..99ced1f3c475 100644 --- a/instrumentation/netty/netty-4.0/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_0/client/NettyHttpClientTracer.java +++ b/instrumentation/netty/netty-4.0/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_0/client/NettyHttpClientTracer.java @@ -22,9 +22,9 @@ import static io.opentelemetry.trace.TracingContextUtils.withSpan; import io.grpc.Context; +import io.netty.handler.codec.http.HttpHeaders; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpResponse; -import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.Scope; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.instrumentation.api.tracer.HttpClientTracer; @@ -32,9 +32,24 @@ import java.net.URI; import java.net.URISyntaxException; -public class NettyHttpClientTracer extends HttpClientTracer { +public class NettyHttpClientTracer + extends HttpClientTracer { public static final NettyHttpClientTracer TRACER = new NettyHttpClientTracer(); + @Override + public Scope startScope(Span span, HttpHeaders headers) { + if (!headers.contains("amz-sdk-invocation-id")) { + return super.startScope(span, headers); + } else { + // TODO (trask) if we move injection up to aws-sdk layer, and start suppressing nested netty + // spans, do we still need this condition? + // AWS calls are often signed, so we can't add headers without breaking the signature. + Context context = withSpan(span, Context.current()); + context = context.withValue(CONTEXT_CLIENT_SPAN_KEY, span); + return withScopedContext(context); + } + } + @Override protected String method(HttpRequest httpRequest) { return httpRequest.getMethod().name(); @@ -66,15 +81,8 @@ protected String responseHeader(HttpResponse httpResponse, String name) { } @Override - protected Setter getSetter() { - return null; - } - - @Override - public Scope startScope(Span span, HttpRequest request) { - Context context = withSpan(span, Context.current()); - OpenTelemetry.getPropagators().getHttpTextFormat().inject(context, request.headers(), SETTER); - return withScopedContext(context); + protected Setter getSetter() { + return SETTER; } @Override diff --git a/instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_1/AttributeKeys.java b/instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_1/AttributeKeys.java index c540351c9fb6..26361f48252c 100644 --- a/instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_1/AttributeKeys.java +++ b/instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_1/AttributeKeys.java @@ -37,8 +37,8 @@ public ConcurrentMap> get(ClassLoader ignore) { } }; - public static final AttributeKey PARENT_CONNECT_SPAN_ATTRIBUTE_KEY = - attributeKey("io.opentelemetry.instrumentation.auto.netty.v4_1.parent.connect.span"); + public static final AttributeKey PARENT_CONNECT_CONTEXT_ATTRIBUTE_KEY = + attributeKey("io.opentelemetry.instrumentation.auto.netty.v4_1.parent.connect.context"); /** * This constant is copied over to diff --git a/instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_1/ChannelFutureListenerInstrumentation.java b/instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_1/ChannelFutureListenerInstrumentation.java index a5c54d46b8cc..b7749775bb28 100644 --- a/instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_1/ChannelFutureListenerInstrumentation.java +++ b/instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_1/ChannelFutureListenerInstrumentation.java @@ -16,15 +16,16 @@ package io.opentelemetry.instrumentation.auto.netty.v4_1; +import static io.opentelemetry.context.ContextUtils.withScopedContext; import static io.opentelemetry.javaagent.tooling.ClassLoaderMatcher.hasClassesNamed; import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.implementsInterface; -import static io.opentelemetry.trace.TracingContextUtils.currentContextWith; import static java.util.Collections.singletonMap; import static net.bytebuddy.matcher.ElementMatchers.isMethod; import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.takesArgument; import com.google.auto.service.AutoService; +import io.grpc.Context; import io.netty.channel.ChannelFuture; import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.auto.netty.v4_1.client.NettyHttpClientTracer; @@ -97,12 +98,12 @@ public static Scope activateScope(@Advice.Argument(0) ChannelFuture future) { if (cause == null) { return null; } - Span parentSpan = - future.channel().attr(AttributeKeys.PARENT_CONNECT_SPAN_ATTRIBUTE_KEY).getAndRemove(); - if (parentSpan == null) { + Context parentContext = + future.channel().attr(AttributeKeys.PARENT_CONNECT_CONTEXT_ATTRIBUTE_KEY).getAndRemove(); + if (parentContext == null) { return null; } - Scope parentScope = currentContextWith(parentSpan); + Scope parentScope = withScopedContext(parentContext); Span errorSpan = NettyHttpClientTracer.TRACER.startSpan("CONNECT"); NettyHttpClientTracer.TRACER.endExceptionally(errorSpan, cause); return parentScope; diff --git a/instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_1/NettyChannelPipelineInstrumentation.java b/instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_1/NettyChannelPipelineInstrumentation.java index f11f61d64af2..73980f2a8848 100644 --- a/instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_1/NettyChannelPipelineInstrumentation.java +++ b/instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_1/NettyChannelPipelineInstrumentation.java @@ -16,7 +16,6 @@ package io.opentelemetry.instrumentation.auto.netty.v4_1; -import static io.opentelemetry.instrumentation.auto.netty.v4_1.server.NettyHttpServerTracer.TRACER; import static io.opentelemetry.javaagent.tooling.ClassLoaderMatcher.hasClassesNamed; import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.implementsInterface; import static net.bytebuddy.matcher.ElementMatchers.isMethod; @@ -26,6 +25,7 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArgument; import com.google.auto.service.AutoService; +import io.grpc.Context; import io.netty.channel.ChannelHandler; import io.netty.channel.ChannelPipeline; import io.netty.handler.codec.http.HttpClientCodec; @@ -43,7 +43,6 @@ import io.opentelemetry.instrumentation.auto.netty.v4_1.server.HttpServerResponseTracingHandler; import io.opentelemetry.instrumentation.auto.netty.v4_1.server.HttpServerTracingHandler; import io.opentelemetry.javaagent.tooling.Instrumenter; -import io.opentelemetry.trace.Span; import java.util.HashMap; import java.util.Map; import net.bytebuddy.asm.Advice; @@ -172,12 +171,9 @@ public static void addHandler( public static class ChannelPipelineConnectAdvice { @Advice.OnMethodEnter public static void addParentSpan(@Advice.This ChannelPipeline pipeline) { - Span span = TRACER.getCurrentSpan(); - if (span.getContext().isValid()) { - Attribute attribute = - pipeline.channel().attr(AttributeKeys.PARENT_CONNECT_SPAN_ATTRIBUTE_KEY); - attribute.compareAndSet(null, span); - } + Attribute attribute = + pipeline.channel().attr(AttributeKeys.PARENT_CONNECT_CONTEXT_ATTRIBUTE_KEY); + attribute.compareAndSet(null, Context.current()); } } } diff --git a/instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_1/client/HttpClientRequestTracingHandler.java b/instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_1/client/HttpClientRequestTracingHandler.java index 87c416088413..6fdb85818cf1 100644 --- a/instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_1/client/HttpClientRequestTracingHandler.java +++ b/instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_1/client/HttpClientRequestTracingHandler.java @@ -16,17 +16,14 @@ package io.opentelemetry.instrumentation.auto.netty.v4_1.client; +import static io.opentelemetry.context.ContextUtils.withScopedContext; import static io.opentelemetry.instrumentation.auto.netty.v4_1.client.NettyHttpClientTracer.TRACER; -import static io.opentelemetry.instrumentation.auto.netty.v4_1.client.NettyResponseInjectAdapter.SETTER; -import static io.opentelemetry.trace.TracingContextUtils.currentContextWith; -import static io.opentelemetry.trace.TracingContextUtils.withSpan; import io.grpc.Context; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelOutboundHandlerAdapter; import io.netty.channel.ChannelPromise; import io.netty.handler.codec.http.HttpRequest; -import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.tracer.BaseTracer; import io.opentelemetry.instrumentation.auto.netty.v4_1.AttributeKeys; @@ -43,10 +40,11 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise prm) { } Scope parentScope = null; - Span parentSpan = - ctx.channel().attr(AttributeKeys.PARENT_CONNECT_SPAN_ATTRIBUTE_KEY).getAndRemove(); - if (parentSpan != null) { - parentScope = currentContextWith(parentSpan); + Context parentContext = + ctx.channel().attr(AttributeKeys.PARENT_CONNECT_CONTEXT_ATTRIBUTE_KEY).getAndRemove(); + if (parentContext != null) { + // TODO (trask) if null then do with ROOT scope? + parentScope = withScopedContext(parentContext); } HttpRequest request = (HttpRequest) msg; @@ -59,24 +57,14 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise prm) { } Span span = TRACER.startSpan(request); - try (Scope scope = currentContextWith(span)) { - BaseTracer.onPeerConnection(span, (InetSocketAddress) ctx.channel().remoteAddress()); - // AWS calls are often signed, so we can't add headers without breaking the signature. - if (!request.headers().contains("amz-sdk-invocation-id")) { - Context context = withSpan(span, Context.current()); - OpenTelemetry.getPropagators() - .getHttpTextFormat() - .inject(context, request.headers(), SETTER); - } - - ctx.channel().attr(AttributeKeys.CLIENT_ATTRIBUTE_KEY).set(span); + BaseTracer.onPeerConnection(span, (InetSocketAddress) ctx.channel().remoteAddress()); + ctx.channel().attr(AttributeKeys.CLIENT_ATTRIBUTE_KEY).set(span); - try { - ctx.write(msg, prm); - } catch (Throwable throwable) { - TRACER.endExceptionally(span, throwable); - throw throwable; - } + try (Scope ignored = TRACER.startScope(span, request.headers())) { + ctx.write(msg, prm); + } catch (Throwable throwable) { + TRACER.endExceptionally(span, throwable); + throw throwable; } finally { if (null != parentScope) { parentScope.close(); diff --git a/instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_1/client/NettyHttpClientTracer.java b/instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_1/client/NettyHttpClientTracer.java index 6cff07c625b9..b336b7eacb72 100644 --- a/instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_1/client/NettyHttpClientTracer.java +++ b/instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/instrumentation/auto/netty/v4_1/client/NettyHttpClientTracer.java @@ -17,22 +17,18 @@ package io.opentelemetry.instrumentation.auto.netty.v4_1.client; import static io.netty.handler.codec.http.HttpHeaderNames.HOST; -import static io.opentelemetry.context.ContextUtils.withScopedContext; import static io.opentelemetry.instrumentation.auto.netty.v4_1.client.NettyResponseInjectAdapter.SETTER; -import static io.opentelemetry.trace.TracingContextUtils.withSpan; -import io.grpc.Context; +import io.netty.handler.codec.http.HttpHeaders; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpResponse; -import io.opentelemetry.OpenTelemetry; -import io.opentelemetry.context.Scope; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.instrumentation.api.tracer.HttpClientTracer; -import io.opentelemetry.trace.Span; import java.net.URI; import java.net.URISyntaxException; -public class NettyHttpClientTracer extends HttpClientTracer { +public class NettyHttpClientTracer + extends HttpClientTracer { public static final NettyHttpClientTracer TRACER = new NettyHttpClientTracer(); @Override @@ -66,15 +62,8 @@ protected String responseHeader(HttpResponse httpResponse, String name) { } @Override - protected Setter getSetter() { - return null; - } - - @Override - public Scope startScope(Span span, HttpRequest request) { - Context context = withSpan(span, Context.current()); - OpenTelemetry.getPropagators().getHttpTextFormat().inject(context, request.headers(), SETTER); - return withScopedContext(context); + protected Setter getSetter() { + return SETTER; } @Override diff --git a/instrumentation/okhttp/okhttp-2.2/src/main/java/io/opentelemetry/instrumentation/auto/okhttp/v2_2/OkHttpClientTracer.java b/instrumentation/okhttp/okhttp-2.2/src/main/java/io/opentelemetry/instrumentation/auto/okhttp/v2_2/OkHttpClientTracer.java index d5c38687a5f2..f757ed661840 100644 --- a/instrumentation/okhttp/okhttp-2.2/src/main/java/io/opentelemetry/instrumentation/auto/okhttp/v2_2/OkHttpClientTracer.java +++ b/instrumentation/okhttp/okhttp-2.2/src/main/java/io/opentelemetry/instrumentation/auto/okhttp/v2_2/OkHttpClientTracer.java @@ -16,6 +16,8 @@ package io.opentelemetry.instrumentation.auto.okhttp.v2_2; +import static io.opentelemetry.instrumentation.auto.okhttp.v2_2.RequestBuilderInjectAdapter.SETTER; + import com.squareup.okhttp.Request; import com.squareup.okhttp.Response; import io.opentelemetry.context.propagation.HttpTextFormat.Setter; @@ -23,7 +25,7 @@ import java.net.URI; import java.net.URISyntaxException; -public class OkHttpClientTracer extends HttpClientTracer { +public class OkHttpClientTracer extends HttpClientTracer { public static final OkHttpClientTracer TRACER = new OkHttpClientTracer(); @Override @@ -52,8 +54,8 @@ protected String responseHeader(Response response, String name) { } @Override - protected Setter getSetter() { - return null; + protected Setter getSetter() { + return SETTER; } @Override diff --git a/instrumentation/okhttp/okhttp-2.2/src/main/java/io/opentelemetry/instrumentation/auto/okhttp/v2_2/TracingInterceptor.java b/instrumentation/okhttp/okhttp-2.2/src/main/java/io/opentelemetry/instrumentation/auto/okhttp/v2_2/TracingInterceptor.java index fc3e8d186dd4..5671f43eaec9 100644 --- a/instrumentation/okhttp/okhttp-2.2/src/main/java/io/opentelemetry/instrumentation/auto/okhttp/v2_2/TracingInterceptor.java +++ b/instrumentation/okhttp/okhttp-2.2/src/main/java/io/opentelemetry/instrumentation/auto/okhttp/v2_2/TracingInterceptor.java @@ -16,15 +16,11 @@ package io.opentelemetry.instrumentation.auto.okhttp.v2_2; -import static io.opentelemetry.context.ContextUtils.withScopedContext; import static io.opentelemetry.instrumentation.auto.okhttp.v2_2.OkHttpClientTracer.TRACER; -import static io.opentelemetry.trace.TracingContextUtils.withSpan; import com.squareup.okhttp.Interceptor; import com.squareup.okhttp.Request; import com.squareup.okhttp.Response; -import io.grpc.Context; -import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.Scope; import io.opentelemetry.trace.Span; import java.io.IOException; @@ -33,14 +29,10 @@ public class TracingInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Span span = TRACER.startSpan(chain.request()); - Context context = withSpan(span, Context.current()); Request.Builder requestBuilder = chain.request().newBuilder(); - OpenTelemetry.getPropagators() - .getHttpTextFormat() - .inject(context, requestBuilder, RequestBuilderInjectAdapter.SETTER); Response response; - try (Scope scope = withScopedContext(context)) { + try (Scope scope = TRACER.startScope(span, requestBuilder)) { response = chain.proceed(requestBuilder.build()); } catch (Exception e) { TRACER.endExceptionally(span, e); diff --git a/instrumentation/okhttp/okhttp-3.0/src/main/java/io/opentelemetry/instrumentation/auto/okhttp/v3_0/OkHttpClientTracer.java b/instrumentation/okhttp/okhttp-3.0/src/main/java/io/opentelemetry/instrumentation/auto/okhttp/v3_0/OkHttpClientTracer.java index 36b5dc88cc14..92fde11c8338 100644 --- a/instrumentation/okhttp/okhttp-3.0/src/main/java/io/opentelemetry/instrumentation/auto/okhttp/v3_0/OkHttpClientTracer.java +++ b/instrumentation/okhttp/okhttp-3.0/src/main/java/io/opentelemetry/instrumentation/auto/okhttp/v3_0/OkHttpClientTracer.java @@ -16,13 +16,15 @@ package io.opentelemetry.instrumentation.auto.okhttp.v3_0; +import static io.opentelemetry.instrumentation.auto.okhttp.v3_0.RequestBuilderInjectAdapter.SETTER; + import io.opentelemetry.context.propagation.HttpTextFormat.Setter; import io.opentelemetry.instrumentation.api.tracer.HttpClientTracer; import java.net.URI; import okhttp3.Request; import okhttp3.Response; -public class OkHttpClientTracer extends HttpClientTracer { +public class OkHttpClientTracer extends HttpClientTracer { public static final OkHttpClientTracer TRACER = new OkHttpClientTracer(); @Override @@ -51,8 +53,8 @@ protected String responseHeader(Response response, String name) { } @Override - protected Setter getSetter() { - return null; + protected Setter getSetter() { + return SETTER; } @Override diff --git a/instrumentation/okhttp/okhttp-3.0/src/main/java/io/opentelemetry/instrumentation/auto/okhttp/v3_0/TracingInterceptor.java b/instrumentation/okhttp/okhttp-3.0/src/main/java/io/opentelemetry/instrumentation/auto/okhttp/v3_0/TracingInterceptor.java index 469f5473365d..aef4e5b70d00 100644 --- a/instrumentation/okhttp/okhttp-3.0/src/main/java/io/opentelemetry/instrumentation/auto/okhttp/v3_0/TracingInterceptor.java +++ b/instrumentation/okhttp/okhttp-3.0/src/main/java/io/opentelemetry/instrumentation/auto/okhttp/v3_0/TracingInterceptor.java @@ -16,12 +16,8 @@ package io.opentelemetry.instrumentation.auto.okhttp.v3_0; -import static io.opentelemetry.context.ContextUtils.withScopedContext; import static io.opentelemetry.instrumentation.auto.okhttp.v3_0.OkHttpClientTracer.TRACER; -import static io.opentelemetry.trace.TracingContextUtils.withSpan; -import io.grpc.Context; -import io.opentelemetry.OpenTelemetry; import io.opentelemetry.context.Scope; import io.opentelemetry.trace.Span; import java.io.IOException; @@ -34,15 +30,10 @@ public class TracingInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Span span = TRACER.startSpan(chain.request()); - Context context = withSpan(span, Context.current()); - - Request.Builder requestBuilder = chain.request().newBuilder(); - OpenTelemetry.getPropagators() - .getHttpTextFormat() - .inject(context, requestBuilder, RequestBuilderInjectAdapter.SETTER); Response response; - try (Scope scope = withScopedContext(context)) { + Request.Builder requestBuilder = chain.request().newBuilder(); + try (Scope ignored = TRACER.startScope(span, requestBuilder)) { response = chain.proceed(requestBuilder.build()); } catch (Exception e) { TRACER.endExceptionally(span, e); diff --git a/instrumentation/play-ws/play-ws-1.0/src/main/java/io/opentelemetry/instrumentation/auto/playws/v1_0/PlayWSClientInstrumentation.java b/instrumentation/play-ws/play-ws-1.0/src/main/java/io/opentelemetry/instrumentation/auto/playws/v1_0/PlayWSClientInstrumentation.java index d3c49dc648a7..bca623bfaba6 100644 --- a/instrumentation/play-ws/play-ws-1.0/src/main/java/io/opentelemetry/instrumentation/auto/playws/v1_0/PlayWSClientInstrumentation.java +++ b/instrumentation/play-ws/play-ws-1.0/src/main/java/io/opentelemetry/instrumentation/auto/playws/v1_0/PlayWSClientInstrumentation.java @@ -42,7 +42,7 @@ public static void methodEnter( Context parentContext = Context.current(); span = TRACER.startSpan(request); - scope = TRACER.startScope(span, request); + scope = TRACER.startScope(span, request.getHeaders()); if (asyncHandler instanceof StreamedAsyncHandler) { asyncHandler = diff --git a/instrumentation/play-ws/play-ws-2.0/src/main/java/io/opentelemetry/instrumentation/auto/playws/v2_0/PlayWSClientInstrumentation.java b/instrumentation/play-ws/play-ws-2.0/src/main/java/io/opentelemetry/instrumentation/auto/playws/v2_0/PlayWSClientInstrumentation.java index 773ddffd7cad..b538912f7360 100644 --- a/instrumentation/play-ws/play-ws-2.0/src/main/java/io/opentelemetry/instrumentation/auto/playws/v2_0/PlayWSClientInstrumentation.java +++ b/instrumentation/play-ws/play-ws-2.0/src/main/java/io/opentelemetry/instrumentation/auto/playws/v2_0/PlayWSClientInstrumentation.java @@ -16,13 +16,10 @@ package io.opentelemetry.instrumentation.auto.playws.v2_0; -import static io.opentelemetry.instrumentation.auto.playws.HeadersInjectAdapter.SETTER; import static io.opentelemetry.instrumentation.auto.playws.PlayWSClientTracer.TRACER; -import static io.opentelemetry.trace.TracingContextUtils.withSpan; import com.google.auto.service.AutoService; -import io.grpc.Context; -import io.opentelemetry.OpenTelemetry; +import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.auto.playws.BasePlayWSClientInstrumentation; import io.opentelemetry.javaagent.tooling.Instrumenter; import io.opentelemetry.trace.Span; @@ -42,8 +39,9 @@ public static void methodEnter( @Advice.Local("otelSpan") Span span) { span = TRACER.startSpan(request); - Context context = withSpan(span, Context.current()); - OpenTelemetry.getPropagators().getHttpTextFormat().inject(context, request, SETTER); + // TODO (trask) expose inject separate from startScope, e.g. for async cases + Scope scope = TRACER.startScope(span, request.getHeaders()); + scope.close(); if (asyncHandler instanceof StreamedAsyncHandler) { asyncHandler = new StreamedAsyncHandlerWrapper((StreamedAsyncHandler) asyncHandler, span); diff --git a/instrumentation/play-ws/play-ws-2.1/src/main/java/io/opentelemetry/instrumentation/auto/playws/v2_1/PlayWSClientInstrumentation.java b/instrumentation/play-ws/play-ws-2.1/src/main/java/io/opentelemetry/instrumentation/auto/playws/v2_1/PlayWSClientInstrumentation.java index 91e7c59ed310..a50a5b1b4236 100644 --- a/instrumentation/play-ws/play-ws-2.1/src/main/java/io/opentelemetry/instrumentation/auto/playws/v2_1/PlayWSClientInstrumentation.java +++ b/instrumentation/play-ws/play-ws-2.1/src/main/java/io/opentelemetry/instrumentation/auto/playws/v2_1/PlayWSClientInstrumentation.java @@ -16,13 +16,10 @@ package io.opentelemetry.instrumentation.auto.playws.v2_1; -import static io.opentelemetry.instrumentation.auto.playws.HeadersInjectAdapter.SETTER; import static io.opentelemetry.instrumentation.auto.playws.PlayWSClientTracer.TRACER; -import static io.opentelemetry.trace.TracingContextUtils.withSpan; import com.google.auto.service.AutoService; -import io.grpc.Context; -import io.opentelemetry.OpenTelemetry; +import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.auto.playws.BasePlayWSClientInstrumentation; import io.opentelemetry.javaagent.tooling.Instrumenter; import io.opentelemetry.trace.Span; @@ -42,8 +39,9 @@ public static void methodEnter( @Advice.Local("otelSpan") Span span) { span = TRACER.startSpan(request); - Context context = withSpan(span, Context.current()); - OpenTelemetry.getPropagators().getHttpTextFormat().inject(context, request, SETTER); + // TODO (trask) expose inject separate from startScope, e.g. for async cases + Scope scope = TRACER.startScope(span, request.getHeaders()); + scope.close(); if (asyncHandler instanceof StreamedAsyncHandler) { asyncHandler = new StreamedAsyncHandlerWrapper((StreamedAsyncHandler) asyncHandler, span); diff --git a/instrumentation/play-ws/play-ws-common/src/main/java/io/opentelemetry/instrumentation/auto/playws/HeadersInjectAdapter.java b/instrumentation/play-ws/play-ws-common/src/main/java/io/opentelemetry/instrumentation/auto/playws/HeadersInjectAdapter.java index b72ace1ed206..b8fce4bad9a6 100644 --- a/instrumentation/play-ws/play-ws-common/src/main/java/io/opentelemetry/instrumentation/auto/playws/HeadersInjectAdapter.java +++ b/instrumentation/play-ws/play-ws-common/src/main/java/io/opentelemetry/instrumentation/auto/playws/HeadersInjectAdapter.java @@ -17,14 +17,14 @@ package io.opentelemetry.instrumentation.auto.playws; import io.opentelemetry.context.propagation.HttpTextFormat; -import play.shaded.ahc.org.asynchttpclient.Request; +import play.shaded.ahc.io.netty.handler.codec.http.HttpHeaders; -public class HeadersInjectAdapter implements HttpTextFormat.Setter { +public class HeadersInjectAdapter implements HttpTextFormat.Setter { public static final HeadersInjectAdapter SETTER = new HeadersInjectAdapter(); @Override - public void set(Request carrier, String key, String value) { - carrier.getHeaders().add(key, value); + public void set(HttpHeaders carrier, String key, String value) { + carrier.add(key, value); } } diff --git a/instrumentation/play-ws/play-ws-common/src/main/java/io/opentelemetry/instrumentation/auto/playws/PlayWSClientTracer.java b/instrumentation/play-ws/play-ws-common/src/main/java/io/opentelemetry/instrumentation/auto/playws/PlayWSClientTracer.java index 146bf150af7f..d06d4d5fb054 100644 --- a/instrumentation/play-ws/play-ws-common/src/main/java/io/opentelemetry/instrumentation/auto/playws/PlayWSClientTracer.java +++ b/instrumentation/play-ws/play-ws-common/src/main/java/io/opentelemetry/instrumentation/auto/playws/PlayWSClientTracer.java @@ -22,10 +22,11 @@ import io.opentelemetry.instrumentation.api.tracer.HttpClientTracer; import java.net.URI; import java.net.URISyntaxException; +import play.shaded.ahc.io.netty.handler.codec.http.HttpHeaders; import play.shaded.ahc.org.asynchttpclient.Request; import play.shaded.ahc.org.asynchttpclient.Response; -public class PlayWSClientTracer extends HttpClientTracer { +public class PlayWSClientTracer extends HttpClientTracer { public static final PlayWSClientTracer TRACER = new PlayWSClientTracer(); @Override @@ -54,7 +55,7 @@ protected String responseHeader(Response response, String name) { } @Override - protected Setter getSetter() { + protected Setter getSetter() { return SETTER; } diff --git a/instrumentation/spring-webflux-5.0/src/test/groovy/client/SpringWebfluxHttpClientTest.groovy b/instrumentation/spring-webflux-5.0/src/test/groovy/client/SpringWebfluxHttpClientTest.groovy index d0484eff6d71..e4a126e4bedc 100644 --- a/instrumentation/spring-webflux-5.0/src/test/groovy/client/SpringWebfluxHttpClientTest.groovy +++ b/instrumentation/spring-webflux-5.0/src/test/groovy/client/SpringWebfluxHttpClientTest.groovy @@ -16,12 +16,7 @@ package client -import static io.opentelemetry.trace.Span.Kind.CLIENT - -import io.opentelemetry.auto.test.asserts.TraceAssert import io.opentelemetry.auto.test.base.HttpClientTest -import io.opentelemetry.instrumentation.api.MoreAttributes -import io.opentelemetry.trace.attributes.SemanticAttributes import org.springframework.http.HttpMethod import org.springframework.web.reactive.function.client.ClientResponse import org.springframework.web.reactive.function.client.WebClient @@ -44,44 +39,6 @@ class SpringWebfluxHttpClientTest extends HttpClientTest { response.statusCode().value() } - @Override - // parent spanRef must be cast otherwise it breaks debugging classloading (junit loads it early) - void clientSpan(TraceAssert trace, int index, Object parentSpan, String method = "GET", boolean tagQueryString = false, URI uri = server.address.resolve("/success"), Integer status = 200, Throwable exception = null) { - super.clientSpan(trace, index, parentSpan, method, tagQueryString, uri, status, exception) - if (!exception) { - trace.span(index + 1) { - childOf(trace.span(index)) - operationName "HTTP $method" - spanKind CLIENT - errored exception != null - if (exception) { - errorEvent(exception.class, exception.message) - } - attributes { - "${SemanticAttributes.NET_PEER_NAME.key()}" "localhost" - "${SemanticAttributes.NET_PEER_PORT.key()}" uri.port - "${SemanticAttributes.NET_PEER_IP.key()}" { it == null || it == "127.0.0.1" } // Optional - "${SemanticAttributes.HTTP_URL.key()}" { it == "${uri}" || it == "${removeFragment(uri)}" } - "${SemanticAttributes.HTTP_METHOD.key()}" method - "${SemanticAttributes.HTTP_USER_AGENT.key()}" { it.startsWith("ReactorNetty") } - if (status) { - "${SemanticAttributes.HTTP_STATUS_CODE.key()}" status - } - if (tagQueryString) { - "$MoreAttributes.HTTP_QUERY" uri.query - "$MoreAttributes.HTTP_FRAGMENT" { it == null || it == uri.fragment } // Optional - } - } - } - } - } - - @Override - int extraClientSpans() { - // has netty-client span inside of spring-webflux-client - return 1 - } - boolean testRedirects() { false }