diff --git a/docs/contributing/using-instrumenter-api.md b/docs/contributing/using-instrumenter-api.md index fbe99935e2f7..60d1aad5446d 100644 --- a/docs/contributing/using-instrumenter-api.md +++ b/docs/contributing/using-instrumenter-api.md @@ -84,14 +84,16 @@ Response decoratedMethod(Request request) { } Context context = instrumenter.start(parentContext, request); + Response response; try (Scope scope = context.makeCurrent()) { - Response response = actualMethod(request); - instrumenter.end(context, request, response, null); - return response; - } catch (Throwable error) { - instrumenter.end(context, request, null, error); - throw error; + response = actualMethod(request); + } catch (Throwable t) { + instrumenter.end(context, request, null, t); + throw t; } + // calling end after the scope is closed is a best practice + instrumenter.end(context, request, response, null); + return response; } ``` diff --git a/instrumentation/apache-httpclient/apache-httpclient-4.3/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v4_3/TracingProtocolExec.java b/instrumentation/apache-httpclient/apache-httpclient-4.3/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v4_3/TracingProtocolExec.java index 1f29cf4fa758..7ef518d3b316 100644 --- a/instrumentation/apache-httpclient/apache-httpclient-4.3/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v4_3/TracingProtocolExec.java +++ b/instrumentation/apache-httpclient/apache-httpclient-4.3/library/src/main/java/io/opentelemetry/instrumentation/apachehttpclient/v4_3/TracingProtocolExec.java @@ -89,16 +89,14 @@ private CloseableHttpResponse execute( HttpExecutionAware httpExecutionAware, Context context) throws IOException, HttpException { - CloseableHttpResponse response = null; - Throwable error = null; + CloseableHttpResponse response; try (Scope ignored = context.makeCurrent()) { response = exec.execute(route, request, httpContext, httpExecutionAware); - return response; - } catch (Throwable e) { - error = e; - throw e; - } finally { - instrumenter.end(context, instrumenterRequest, response, error); + } catch (Throwable t) { + instrumenter.end(context, instrumenterRequest, null, t); + throw t; } + instrumenter.end(context, instrumenterRequest, response, null); + return response; } } diff --git a/instrumentation/elasticsearch/elasticsearch-rest-7.0/library/src/main/java/io/opentelemetry/instrumentation/elasticsearch/rest/v7_0/RestClientWrapper.java b/instrumentation/elasticsearch/elasticsearch-rest-7.0/library/src/main/java/io/opentelemetry/instrumentation/elasticsearch/rest/v7_0/RestClientWrapper.java index 9c5d39be5ea5..a9fc34b5637f 100644 --- a/instrumentation/elasticsearch/elasticsearch-rest-7.0/library/src/main/java/io/opentelemetry/instrumentation/elasticsearch/rest/v7_0/RestClientWrapper.java +++ b/instrumentation/elasticsearch/elasticsearch-rest-7.0/library/src/main/java/io/opentelemetry/instrumentation/elasticsearch/rest/v7_0/RestClientWrapper.java @@ -67,19 +67,18 @@ private static Class createProxyClass() { return method.invoke(target, args); } - Response response = null; - Throwable throwable = null; Context context = instrumenter.start(parentContext, otelRequest); - try (Scope scope = context.makeCurrent()) { + + Response response; + try (Scope ignored = context.makeCurrent()) { response = (Response) method.invoke(target, args); - } catch (Throwable exception) { - throwable = exception; - throw throwable; - } finally { - instrumenter.end(context, otelRequest, response, throwable); + } catch (Throwable t) { + instrumenter.end(context, otelRequest, null, t); + throw t; } - + instrumenter.end(context, otelRequest, response, null); return response; + } else if ("performRequestAsync".equals(method.getName()) && args.length == 2 && args[0] instanceof Request @@ -94,22 +93,17 @@ private static Class createProxyClass() { return method.invoke(target, args); } - Throwable throwable = null; Context context = instrumenter.start(parentContext, otelRequest); args[1] = new RestResponseListener( responseListener, parentContext, instrumenter, context, otelRequest); - try (Scope scope = context.makeCurrent()) { + try (Scope ignored = context.makeCurrent()) { return method.invoke(target, args); - } catch (Throwable exception) { - throwable = exception; - throw throwable; - } finally { - if (throwable != null) { - instrumenter.end(context, otelRequest, null, throwable); - } - // span ended in RestResponseListener + } catch (Throwable t) { + instrumenter.end(context, otelRequest, null, t); + throw t; } + // span ended in RestResponseListener } // delegate to wrapped RestClient diff --git a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/TracingClientInterceptor.java b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/TracingClientInterceptor.java index 8790dad4c493..f308e5c253e6 100644 --- a/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/TracingClientInterceptor.java +++ b/instrumentation/grpc-1.6/library/src/main/java/io/opentelemetry/instrumentation/grpc/v1_6/TracingClientInterceptor.java @@ -58,13 +58,11 @@ public ClientCall interceptCall( Context context = instrumenter.start(parentContext, request); ClientCall result; try (Scope ignored = context.makeCurrent()) { - try { - // call other interceptors - result = next.newCall(method, callOptions); - } catch (Throwable e) { - instrumenter.end(context, request, Status.UNKNOWN, e); - throw e; - } + // call other interceptors + result = next.newCall(method, callOptions); + } catch (Throwable t) { + instrumenter.end(context, request, Status.UNKNOWN, t); + throw t; } return new TracingClientCall<>(result, parentContext, context, request); diff --git a/instrumentation/java-http-client/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/httpclient/HttpHeadersInstrumentation.java b/instrumentation/java-http-client/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/httpclient/HttpHeadersInstrumentation.java index faa4ef42c555..3bc3eb292c2a 100644 --- a/instrumentation/java-http-client/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/httpclient/HttpHeadersInstrumentation.java +++ b/instrumentation/java-http-client/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/httpclient/HttpHeadersInstrumentation.java @@ -11,6 +11,7 @@ import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith; import static net.bytebuddy.matcher.ElementMatchers.named; +import io.opentelemetry.context.Context; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import java.net.http.HttpHeaders; @@ -39,7 +40,7 @@ public static class HeadersAdvice { @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void methodExit(@Advice.Return(readOnly = false) HttpHeaders headers) { - headers = setter().inject(headers); + headers = setter().inject(headers, Context.current()); } } } diff --git a/instrumentation/java-http-client/library/src/main/java/io/opentelemetry/instrumentation/httpclient/internal/HttpHeadersSetter.java b/instrumentation/java-http-client/library/src/main/java/io/opentelemetry/instrumentation/httpclient/internal/HttpHeadersSetter.java index 4164ca3f6802..257d1b0fb1be 100644 --- a/instrumentation/java-http-client/library/src/main/java/io/opentelemetry/instrumentation/httpclient/internal/HttpHeadersSetter.java +++ b/instrumentation/java-http-client/library/src/main/java/io/opentelemetry/instrumentation/httpclient/internal/HttpHeadersSetter.java @@ -25,13 +25,13 @@ public HttpHeadersSetter(ContextPropagators contextPropagators) { this.contextPropagators = contextPropagators; } - public HttpHeaders inject(HttpHeaders original) { + public HttpHeaders inject(HttpHeaders original, Context context) { Map> headerMap = new HashMap<>(original.map()); contextPropagators .getTextMapPropagator() .inject( - Context.current(), + context, headerMap, (carrier, key, value) -> { if (carrier != null) { diff --git a/instrumentation/java-http-client/library/src/main/java/io/opentelemetry/instrumentation/httpclient/internal/OpenTelemetryHttpClient.java b/instrumentation/java-http-client/library/src/main/java/io/opentelemetry/instrumentation/httpclient/internal/OpenTelemetryHttpClient.java index 8c03f080ee5b..775dbd2b5619 100644 --- a/instrumentation/java-http-client/library/src/main/java/io/opentelemetry/instrumentation/httpclient/internal/OpenTelemetryHttpClient.java +++ b/instrumentation/java-http-client/library/src/main/java/io/opentelemetry/instrumentation/httpclient/internal/OpenTelemetryHttpClient.java @@ -95,21 +95,17 @@ public HttpResponse send( return client.send(request, responseBodyHandler); } - HttpResponse response = null; - Throwable error = null; Context context = instrumenter.start(parentContext, request); + HttpRequestWrapper requestWrapper = + new HttpRequestWrapper(request, headersSetter.inject(request.headers(), context)); + HttpResponse response; try (Scope ignore = context.makeCurrent()) { - HttpRequestWrapper requestWrapper = - new HttpRequestWrapper(request, headersSetter.inject(request.headers())); - response = client.send(requestWrapper, responseBodyHandler); - } catch (Throwable throwable) { - error = throwable; - throw throwable; - } finally { - instrumenter.end(context, request, response, error); + } catch (Throwable t) { + instrumenter.end(context, request, null, t); + throw t; } - + instrumenter.end(context, request, response, null); return response; } @@ -136,17 +132,18 @@ private CompletableFuture> traceAsync( } Context context = instrumenter.start(parentContext, request); - try (Scope ignore = context.makeCurrent()) { - HttpRequestWrapper requestWrapper = - new HttpRequestWrapper(request, headersSetter.inject(request.headers())); - - CompletableFuture> future = action.apply(requestWrapper); - future = future.whenComplete(new ResponseConsumer(instrumenter, context, request)); - future = CompletableFutureWrapper.wrap(future, parentContext); - return future; - } catch (Throwable throwable) { - instrumenter.end(context, request, null, throwable); - throw throwable; + HttpRequestWrapper requestWrapper = + new HttpRequestWrapper(request, headersSetter.inject(request.headers(), context)); + + CompletableFuture> future; + try (Scope ignored = context.makeCurrent()) { + future = action.apply(requestWrapper); + } catch (Throwable t) { + instrumenter.end(context, request, null, t); + throw t; } + future = future.whenComplete(new ResponseConsumer(instrumenter, context, request)); + future = CompletableFutureWrapper.wrap(future, parentContext); + return future; } } diff --git a/instrumentation/kafka/kafka-clients/kafka-clients-2.6/library/src/main/java/io/opentelemetry/instrumentation/kafkaclients/v2_6/KafkaTelemetry.java b/instrumentation/kafka/kafka-clients/kafka-clients-2.6/library/src/main/java/io/opentelemetry/instrumentation/kafkaclients/v2_6/KafkaTelemetry.java index 7a7de1813739..8f16c325667b 100644 --- a/instrumentation/kafka/kafka-clients/kafka-clients-2.6/library/src/main/java/io/opentelemetry/instrumentation/kafkaclients/v2_6/KafkaTelemetry.java +++ b/instrumentation/kafka/kafka-clients/kafka-clients-2.6/library/src/main/java/io/opentelemetry/instrumentation/kafkaclients/v2_6/KafkaTelemetry.java @@ -252,10 +252,10 @@ Future buildAndInjectSpan( } Context context = producerInstrumenter.start(parentContext, request); + propagator().inject(context, record.headers(), SETTER); + try (Scope ignored = context.makeCurrent()) { - propagator().inject(context, record.headers(), SETTER); - callback = new ProducerCallback(callback, parentContext, context, request); - return sendFn.apply(record, callback); + return sendFn.apply(record, new ProducerCallback(callback, parentContext, context, request)); } } diff --git a/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/HttpClientRequestTracingHandler.java b/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/HttpClientRequestTracingHandler.java index 83fea5b5288c..4e9a68eed4db 100644 --- a/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/HttpClientRequestTracingHandler.java +++ b/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/client/HttpClientRequestTracingHandler.java @@ -55,10 +55,10 @@ public void writeRequested(ChannelHandlerContext ctx, MessageEvent event) throws try (Scope ignored = context.makeCurrent()) { super.writeRequested(ctx, event); - // span is ended normally in HttpClientResponseTracingHandler } catch (Throwable throwable) { instrumenter().end(context, request, null, throwable); throw throwable; } + // span is ended normally in HttpClientResponseTracingHandler } } diff --git a/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/server/HttpServerRequestTracingHandler.java b/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/server/HttpServerRequestTracingHandler.java index 326bca72f129..4f0a2b762ab6 100644 --- a/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/server/HttpServerRequestTracingHandler.java +++ b/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/server/HttpServerRequestTracingHandler.java @@ -51,10 +51,10 @@ public void messageReceived(ChannelHandlerContext ctx, MessageEvent event) throw try (Scope ignored = context.makeCurrent()) { super.messageReceived(ctx, event); - // the span is ended normally in HttpServerResponseTracingHandler } catch (Throwable throwable) { instrumenter().end(context, request, null, throwable); throw throwable; } + // span is ended normally in HttpServerResponseTracingHandler } } diff --git a/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/server/HttpServerResponseTracingHandler.java b/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/server/HttpServerResponseTracingHandler.java index 7de9910e9d84..cf6acd70695a 100644 --- a/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/server/HttpServerResponseTracingHandler.java +++ b/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/server/HttpServerResponseTracingHandler.java @@ -36,18 +36,15 @@ public void writeRequested(ChannelHandlerContext ctx, MessageEvent msg) throws E Context context = requestAndContext.context(); HttpRequestAndChannel request = requestAndContext.request(); HttpResponse response = (HttpResponse) msg.getMessage(); + customizeResponse(context, response); - Throwable error = null; try (Scope ignored = context.makeCurrent()) { - customizeResponse(context, response); super.writeRequested(ctx, msg); } catch (Throwable t) { - error = t; + instrumenter().end(context, request, response, NettyErrorHolder.getOrDefault(context, t)); throw t; - } finally { - error = NettyErrorHolder.getOrDefault(context, error); - instrumenter().end(context, request, response, error); } + instrumenter().end(context, request, response, NettyErrorHolder.getOrDefault(context, null)); } private static void customizeResponse(Context context, HttpResponse response) { diff --git a/instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/HttpClientRequestTracingHandler.java b/instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/HttpClientRequestTracingHandler.java index b284173477ab..c5acee692504 100644 --- a/instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/HttpClientRequestTracingHandler.java +++ b/instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/HttpClientRequestTracingHandler.java @@ -49,11 +49,11 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise prm) thr try (Scope ignored = context.makeCurrent()) { super.write(ctx, msg, prm); - // span is ended normally in HttpClientResponseTracingHandler } catch (Throwable throwable) { instrumenter().end(contextAttr.getAndRemove(), requestAttr.getAndRemove(), null, throwable); parentContextAttr.remove(); throw throwable; } + // span is ended normally in HttpClientResponseTracingHandler } } diff --git a/instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/server/HttpServerRequestTracingHandler.java b/instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/server/HttpServerRequestTracingHandler.java index 9e1a68e1bc99..13a265b2fe8d 100644 --- a/instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/server/HttpServerRequestTracingHandler.java +++ b/instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/server/HttpServerRequestTracingHandler.java @@ -58,11 +58,11 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception try (Scope ignored = context.makeCurrent()) { super.channelRead(ctx, msg); - // the span is ended normally in HttpServerResponseTracingHandler } catch (Throwable throwable) { // make sure to remove the server context on end() call instrumenter().end(contextAttr.getAndRemove(), requestAttr.getAndRemove(), null, throwable); throw throwable; } + // the span is ended normally in HttpServerResponseTracingHandler } } diff --git a/instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/server/HttpServerResponseTracingHandler.java b/instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/server/HttpServerResponseTracingHandler.java index 9388c98a2500..b5fd57d3c487 100644 --- a/instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/server/HttpServerResponseTracingHandler.java +++ b/instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/server/HttpServerResponseTracingHandler.java @@ -31,14 +31,15 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise prm) { return; } + customizeResponse(context, (HttpResponse) msg); + try (Scope ignored = context.makeCurrent()) { - customizeResponse(context, (HttpResponse) msg); ctx.write(msg, prm); - end(ctx.channel(), (HttpResponse) msg, null); - } catch (Throwable throwable) { - end(ctx.channel(), (HttpResponse) msg, throwable); - throw throwable; + } catch (Throwable t) { + end(ctx.channel(), (HttpResponse) msg, t); + throw t; } + end(ctx.channel(), (HttpResponse) msg, null); } // make sure to remove the server context on end() call diff --git a/instrumentation/netty/netty-4.1/library/src/main/java/io/opentelemetry/instrumentation/netty/v4_1/internal/client/HttpClientRequestTracingHandler.java b/instrumentation/netty/netty-4.1/library/src/main/java/io/opentelemetry/instrumentation/netty/v4_1/internal/client/HttpClientRequestTracingHandler.java index fbd20c7064c6..785027a1a545 100644 --- a/instrumentation/netty/netty-4.1/library/src/main/java/io/opentelemetry/instrumentation/netty/v4_1/internal/client/HttpClientRequestTracingHandler.java +++ b/instrumentation/netty/netty-4.1/library/src/main/java/io/opentelemetry/instrumentation/netty/v4_1/internal/client/HttpClientRequestTracingHandler.java @@ -63,12 +63,12 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise prm) thr try (Scope ignored = context.makeCurrent()) { super.write(ctx, msg, prm); - // span is ended normally in HttpClientResponseTracingHandler } catch (Throwable throwable) { instrumenter.end(contextAttr.getAndSet(null), requestAttr.getAndSet(null), null, throwable); parentContextAttr.set(null); throw throwable; } + // span is ended normally in HttpClientResponseTracingHandler } private static boolean isAwsRequest(HttpRequestAndChannel request) { diff --git a/instrumentation/netty/netty-4.1/library/src/main/java/io/opentelemetry/instrumentation/netty/v4_1/internal/server/HttpServerRequestTracingHandler.java b/instrumentation/netty/netty-4.1/library/src/main/java/io/opentelemetry/instrumentation/netty/v4_1/internal/server/HttpServerRequestTracingHandler.java index 8f5b9a56fe84..1e4a30a4a8dc 100644 --- a/instrumentation/netty/netty-4.1/library/src/main/java/io/opentelemetry/instrumentation/netty/v4_1/internal/server/HttpServerRequestTracingHandler.java +++ b/instrumentation/netty/netty-4.1/library/src/main/java/io/opentelemetry/instrumentation/netty/v4_1/internal/server/HttpServerRequestTracingHandler.java @@ -59,15 +59,15 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception try (Scope ignored = context.makeCurrent()) { super.channelRead(ctx, msg); - // the span is ended normally in HttpServerResponseTracingHandler - } catch (Throwable throwable) { + } catch (Throwable t) { // make sure to remove the server context on end() call ServerContext serverContext = serverContexts.pollLast(); if (serverContext != null) { - instrumenter.end(serverContext.context(), serverContext.request(), null, throwable); + instrumenter.end(serverContext.context(), serverContext.request(), null, t); } - throw throwable; + throw t; } + // span is ended normally in HttpServerResponseTracingHandler } @Override diff --git a/instrumentation/okhttp/okhttp-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/okhttp/v2_2/TracingInterceptor.java b/instrumentation/okhttp/okhttp-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/okhttp/v2_2/TracingInterceptor.java index f88f43ac76ce..c9ce403c56b6 100644 --- a/instrumentation/okhttp/okhttp-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/okhttp/v2_2/TracingInterceptor.java +++ b/instrumentation/okhttp/okhttp-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/okhttp/v2_2/TracingInterceptor.java @@ -39,9 +39,9 @@ public Response intercept(Chain chain) throws IOException { Response response; try (Scope ignored = context.makeCurrent()) { response = chain.proceed(request); - } catch (Exception e) { - instrumenter.end(context, request, null, e); - throw e; + } catch (Throwable t) { + instrumenter.end(context, request, null, t); + throw t; } instrumenter.end(context, request, response, null); return response; diff --git a/instrumentation/okhttp/okhttp-3.0/library/src/main/java/io/opentelemetry/instrumentation/okhttp/v3_0/internal/TracingInterceptor.java b/instrumentation/okhttp/okhttp-3.0/library/src/main/java/io/opentelemetry/instrumentation/okhttp/v3_0/internal/TracingInterceptor.java index 14f37fb687f8..4f4c570f260d 100644 --- a/instrumentation/okhttp/okhttp-3.0/library/src/main/java/io/opentelemetry/instrumentation/okhttp/v3_0/internal/TracingInterceptor.java +++ b/instrumentation/okhttp/okhttp-3.0/library/src/main/java/io/opentelemetry/instrumentation/okhttp/v3_0/internal/TracingInterceptor.java @@ -41,17 +41,15 @@ public Response intercept(Chain chain) throws IOException { Context context = instrumenter.start(parentContext, chain); request = injectContextToRequest(request, context); - Response response = null; - Throwable error = null; + Response response; try (Scope ignored = context.makeCurrent()) { response = chain.proceed(request); - return response; - } catch (Exception e) { - error = e; - throw e; - } finally { - instrumenter.end(context, chain, response, error); + } catch (Throwable t) { + instrumenter.end(context, chain, null, t); + throw t; } + instrumenter.end(context, chain, response, null); + return response; } // Context injection is being handled manually for a reason: we want to use the OkHttp Request diff --git a/instrumentation/rabbitmq-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/TracedDelegatingConsumer.java b/instrumentation/rabbitmq-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/TracedDelegatingConsumer.java index 2737d0a947a5..50ed0327ca96 100644 --- a/instrumentation/rabbitmq-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/TracedDelegatingConsumer.java +++ b/instrumentation/rabbitmq-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/TracedDelegatingConsumer.java @@ -74,10 +74,10 @@ public void handleDelivery( try (Scope ignored = context.makeCurrent()) { // Call delegate. delegate.handleDelivery(consumerTag, envelope, properties, body); - deliverInstrumenter().end(context, request, null, null); - } catch (Throwable throwable) { - deliverInstrumenter().end(context, request, null, throwable); - throw throwable; + } catch (Throwable t) { + deliverInstrumenter().end(context, request, null, t); + throw t; } + deliverInstrumenter().end(context, request, null, null); } } diff --git a/instrumentation/reactor/reactor-kafka-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/reactor/kafka/v1_0/InstrumentedKafkaFlux.java b/instrumentation/reactor/reactor-kafka-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/reactor/kafka/v1_0/InstrumentedKafkaFlux.java index 4f8c64db6908..4092646667dc 100644 --- a/instrumentation/reactor/reactor-kafka-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/reactor/kafka/v1_0/InstrumentedKafkaFlux.java +++ b/instrumentation/reactor/reactor-kafka-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/reactor/kafka/v1_0/InstrumentedKafkaFlux.java @@ -75,15 +75,13 @@ public void onNext(ConsumerRecord record) { } Context context = processInstrumenter().start(parentContext, request); - Throwable error = null; try (Scope ignored = context.makeCurrent()) { actual.onNext(record); } catch (Throwable t) { - error = t; + processInstrumenter().end(context, request, null, t); throw t; - } finally { - processInstrumenter().end(context, request, null, error); } + processInstrumenter().end(context, request, null, null); } @Override diff --git a/instrumentation/rocketmq/rocketmq-client/rocketmq-client-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmqclient/v5_0/MessageListenerWrapper.java b/instrumentation/rocketmq/rocketmq-client/rocketmq-client-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmqclient/v5_0/MessageListenerWrapper.java index 0d013bf2a4ef..748074e6df5f 100644 --- a/instrumentation/rocketmq/rocketmq-client/rocketmq-client-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmqclient/v5_0/MessageListenerWrapper.java +++ b/instrumentation/rocketmq/rocketmq-client/rocketmq-client-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rocketmqclient/v5_0/MessageListenerWrapper.java @@ -31,16 +31,15 @@ public ConsumeResult consume(MessageView messageView) { return delegator.consume(messageView); } Context context = processInstrumenter.start(parentContext, messageView); - ConsumeResult consumeResult = null; - Throwable error = null; + + ConsumeResult consumeResult; try (Scope ignored = context.makeCurrent()) { consumeResult = delegator.consume(messageView); - return consumeResult; } catch (Throwable t) { - error = t; + processInstrumenter.end(context, messageView, null, t); throw t; - } finally { - processInstrumenter.end(context, messageView, consumeResult, error); } + processInstrumenter.end(context, messageView, consumeResult, null); + return consumeResult; } } diff --git a/instrumentation/rxjava/rxjava-1.0/library/src/main/java/io/opentelemetry/instrumentation/rxjava/v1_0/TracedSubscriber.java b/instrumentation/rxjava/rxjava-1.0/library/src/main/java/io/opentelemetry/instrumentation/rxjava/v1_0/TracedSubscriber.java index 24783d23cdc0..4aee7a0d0031 100644 --- a/instrumentation/rxjava/rxjava-1.0/library/src/main/java/io/opentelemetry/instrumentation/rxjava/v1_0/TracedSubscriber.java +++ b/instrumentation/rxjava/rxjava-1.0/library/src/main/java/io/opentelemetry/instrumentation/rxjava/v1_0/TracedSubscriber.java @@ -59,15 +59,13 @@ public void onNext(T value) { public void onCompleted() { Context context = contextRef.getAndSet(null); if (context != null) { - Throwable error = null; try (Scope ignored = context.makeCurrent()) { delegate.onCompleted(); } catch (Throwable t) { - error = t; + instrumenter.end(context, request, null, t); throw t; - } finally { - instrumenter.end(context, request, null, error); } + instrumenter.end(context, request, null, null); } else { delegate.onCompleted(); } diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/WithSpanAspect.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/WithSpanAspect.java index f804a820924f..f25506a0b780 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/WithSpanAspect.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/annotations/WithSpanAspect.java @@ -68,12 +68,14 @@ public Object traceMethod(ProceedingJoinPoint pjp) throws Throwable { AsyncOperationEndSupport asyncOperationEndSupport = AsyncOperationEndSupport.create( instrumenter, Object.class, request.method().getReturnType()); + + Object response; try (Scope ignored = context.makeCurrent()) { - Object response = pjp.proceed(); - return asyncOperationEndSupport.asyncEnd(context, request, response, null); + response = pjp.proceed(); } catch (Throwable t) { asyncOperationEndSupport.asyncEnd(context, request, null, t); throw t; } + return asyncOperationEndSupport.asyncEnd(context, request, response, null); } } diff --git a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/scheduling/SpringSchedulingInstrumentationAspect.java b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/scheduling/SpringSchedulingInstrumentationAspect.java index 1f6a55910ceb..5dfec2280e8e 100644 --- a/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/scheduling/SpringSchedulingInstrumentationAspect.java +++ b/instrumentation/spring/spring-boot-autoconfigure/src/main/java/io/opentelemetry/instrumentation/spring/autoconfigure/internal/instrumentation/scheduling/SpringSchedulingInstrumentationAspect.java @@ -78,13 +78,15 @@ public Object execution(ProceedingJoinPoint joinPoint) throws Throwable { return joinPoint.proceed(); } Context context = instrumenter.start(parent, request); + + Object object; try (Scope ignored = context.makeCurrent()) { - Object object = joinPoint.proceed(); - instrumenter.end(context, request, object, null); - return object; + object = joinPoint.proceed(); } catch (Throwable t) { instrumenter.end(context, request, null, t); throw t; } + instrumenter.end(context, request, object, null); + return object; } } diff --git a/instrumentation/spring/spring-data/spring-data-1.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/data/v1_8/SpringDataInstrumentationModule.java b/instrumentation/spring/spring-data/spring-data-1.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/data/v1_8/SpringDataInstrumentationModule.java index fe04690c24ed..b751e6977f99 100644 --- a/instrumentation/spring/spring-data/spring-data-1.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/data/v1_8/SpringDataInstrumentationModule.java +++ b/instrumentation/spring/spring-data/spring-data-1.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/data/v1_8/SpringDataInstrumentationModule.java @@ -117,21 +117,23 @@ public Object invoke(MethodInvocation methodInvocation) throws Throwable { } Context context = instrumenter().start(parentContext, classAndMethod); + + Object result; try (Scope ignored = context.makeCurrent()) { - Object result = methodInvocation.proceed(); - Class type = method.getReturnType(); - // the return type for - // org.springframework.data.repository.kotlin.CoroutineCrudRepository#findById - // is Object but the method may actually return a Mono - if (Object.class == type && MONO_CLASS != null && MONO_CLASS.isInstance(result)) { - type = MONO_CLASS; - } - return AsyncOperationEndSupport.create(instrumenter(), Void.class, type) - .asyncEnd(context, classAndMethod, result, null); + result = methodInvocation.proceed(); } catch (Throwable t) { instrumenter().end(context, classAndMethod, null, t); throw t; } + Class type = method.getReturnType(); + // the return type for + // org.springframework.data.repository.kotlin.CoroutineCrudRepository#findById + // is Object but the method may actually return a Mono + if (Object.class == type && MONO_CLASS != null && MONO_CLASS.isInstance(result)) { + type = MONO_CLASS; + } + return AsyncOperationEndSupport.create(instrumenter(), Void.class, type) + .asyncEnd(context, classAndMethod, result, null); } } } diff --git a/instrumentation/spring/spring-scheduling-3.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/scheduling/v3_1/SpringSchedulingRunnableWrapper.java b/instrumentation/spring/spring-scheduling-3.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/scheduling/v3_1/SpringSchedulingRunnableWrapper.java index be14382a32a4..12c223a5578c 100644 --- a/instrumentation/spring/spring-scheduling-3.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/scheduling/v3_1/SpringSchedulingRunnableWrapper.java +++ b/instrumentation/spring/spring-scheduling-3.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/scheduling/v3_1/SpringSchedulingRunnableWrapper.java @@ -35,11 +35,11 @@ public void run() { TaskContextHolder.set(context); try (Scope ignored = context.makeCurrent()) { runnable.run(); - instrumenter().end(context, runnable, null, null); - } catch (Throwable throwable) { - instrumenter().end(context, runnable, null, throwable); - throw throwable; + } catch (Throwable t) { + instrumenter().end(context, runnable, null, t); + throw t; } + instrumenter().end(context, runnable, null, null); } public static Runnable wrapIfNeeded(Runnable task) { diff --git a/instrumentation/spring/spring-web/spring-web-3.1/library/src/main/java/io/opentelemetry/instrumentation/spring/web/v3_1/RestTemplateInterceptor.java b/instrumentation/spring/spring-web/spring-web-3.1/library/src/main/java/io/opentelemetry/instrumentation/spring/web/v3_1/RestTemplateInterceptor.java index fca275eed96e..3f4e7b2d55c5 100644 --- a/instrumentation/spring/spring-web/spring-web-3.1/library/src/main/java/io/opentelemetry/instrumentation/spring/web/v3_1/RestTemplateInterceptor.java +++ b/instrumentation/spring/spring-web/spring-web-3.1/library/src/main/java/io/opentelemetry/instrumentation/spring/web/v3_1/RestTemplateInterceptor.java @@ -31,13 +31,15 @@ public ClientHttpResponse intercept( } Context context = instrumenter.start(parentContext, request); + + ClientHttpResponse response; try (Scope ignored = context.makeCurrent()) { - ClientHttpResponse response = execution.execute(request, body); - instrumenter.end(context, request, response, null); - return response; + response = execution.execute(request, body); } catch (Throwable t) { instrumenter.end(context, request, null, t); throw t; } + instrumenter.end(context, request, response, null); + return response; } } diff --git a/instrumentation/vertx/vertx-kafka-client-3.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/kafka/v3_6/InstrumentedBatchRecordsHandler.java b/instrumentation/vertx/vertx-kafka-client-3.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/kafka/v3_6/InstrumentedBatchRecordsHandler.java index 829945a766d4..cb2cd7842e89 100644 --- a/instrumentation/vertx/vertx-kafka-client-3.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/kafka/v3_6/InstrumentedBatchRecordsHandler.java +++ b/instrumentation/vertx/vertx-kafka-client-3.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/kafka/v3_6/InstrumentedBatchRecordsHandler.java @@ -42,15 +42,13 @@ public void handle(ConsumerRecords records) { boolean previousWrappingEnabled = KafkaClientsConsumerProcessTracing.setEnabled(false); try { Context context = batchProcessInstrumenter().start(parentContext, request); - Throwable error = null; try (Scope ignored = context.makeCurrent()) { callDelegateHandler(records); } catch (Throwable t) { - error = t; + batchProcessInstrumenter().end(context, request, null, t); throw t; - } finally { - batchProcessInstrumenter().end(context, request, null, error); } + batchProcessInstrumenter().end(context, request, null, null); } finally { KafkaClientsConsumerProcessTracing.setEnabled(previousWrappingEnabled); } diff --git a/instrumentation/vertx/vertx-kafka-client-3.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/kafka/v3_6/InstrumentedSingleRecordHandler.java b/instrumentation/vertx/vertx-kafka-client-3.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/kafka/v3_6/InstrumentedSingleRecordHandler.java index c0b23b21873e..018c24202c95 100644 --- a/instrumentation/vertx/vertx-kafka-client-3.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/kafka/v3_6/InstrumentedSingleRecordHandler.java +++ b/instrumentation/vertx/vertx-kafka-client-3.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vertx/kafka/v3_6/InstrumentedSingleRecordHandler.java @@ -38,15 +38,13 @@ public void handle(ConsumerRecord record) { } Context context = processInstrumenter().start(parentContext, request); - Throwable error = null; try (Scope ignored = context.makeCurrent()) { callDelegateHandler(record); } catch (Throwable t) { - error = t; + processInstrumenter().end(context, request, null, t); throw t; - } finally { - processInstrumenter().end(context, request, null, error); } + processInstrumenter().end(context, request, null, null); } private void callDelegateHandler(ConsumerRecord record) { diff --git a/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/TestInstrumenters.java b/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/TestInstrumenters.java index d23fc3efbe5f..c070fca33af4 100644 --- a/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/TestInstrumenters.java +++ b/testing-common/src/main/java/io/opentelemetry/instrumentation/testing/TestInstrumenters.java @@ -98,15 +98,16 @@ private static T runWithInstrumenter( String spanName, Instrumenter instrumenter, ThrowingSupplier callback) throws E { Context context = instrumenter.start(Context.current(), spanName); - Throwable err = null; + + T result; try (Scope ignored = context.makeCurrent()) { - return callback.get(); + result = callback.get(); } catch (Throwable t) { - err = t; + instrumenter.end(context, spanName, null, t); throw t; - } finally { - instrumenter.end(context, spanName, null, err); } + instrumenter.end(context, spanName, null, null); + return result; } private static final class SpanKeyAttributesExtractor