Remove unnecessary scopes
MODIFY
instrumentation/http-url-connection/src/main/java/io/opentelemetry/javaagent/instrumentation/httpurlconnection/HttpUrlConnectionInstrumentation.java
instrumentation/http-url-connection/src/main/java/io/opentelemetry/javaagent/instrumentation/httpurlconnection/HttpUrlConnectionInstrumentation.java
@@ -8,7 +8,6 @@ package io.opentelemetry.javaagent.instrumentation.httpurlconnection;
import static io.opentelemetry.javaagent.instrumentation.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 java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
@@ -152,12 +151,10 @@ public class HttpUrlConnectionInstrumentation extends Instrumenter.Default {
}
public void finishSpan(Throwable throwable) {
- try (Scope scope = currentContextWith(span)) {
TRACER.endExceptionally(span, throwable);
span = null;
finished = true;
}
- }
public void finishSpan(int responseCode) {
/*
@@ -166,7 +163,6 @@ public class HttpUrlConnectionInstrumentation extends Instrumenter.Default {
* (e.g. breaks getOutputStream).
*/
if (responseCode > 0) {
- try (Scope scope = currentContextWith(span)) {
// Need to explicitly cast to boxed type to make sure correct method is called.
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/946
TRACER.end(span, (Integer) responseCode);
@@ -176,4 +172,3 @@ public class HttpUrlConnectionInstrumentation extends Instrumenter.Default {
}
}
}
-}

MODIFY
instrumentation/jms-1.1/src/main/java/io/opentelemetry/javaagent/instrumentation/jms/JMSMessageConsumerInstrumentation.java
instrumentation/jms-1.1/src/main/java/io/opentelemetry/javaagent/instrumentation/jms/JMSMessageConsumerInstrumentation.java
@@ -12,7 +12,6 @@ import static io.opentelemetry.javaagent.instrumentation.jms.MessageExtractAdapt
import static io.opentelemetry.javaagent.tooling.ClassLoaderMatcher.hasClassesNamed;
import static io.opentelemetry.javaagent.tooling.bytebuddy.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.trace.Span.Kind.CLIENT;
-import static io.opentelemetry.trace.TracingContextUtils.currentContextWith;
import static io.opentelemetry.trace.TracingContextUtils.getSpan;
import static java.util.Collections.singletonMap;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
@@ -21,7 +20,6 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
import com.google.auto.service.AutoService;
import io.grpc.Context;
-import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext;
import io.opentelemetry.javaagent.tooling.Instrumenter;
import io.opentelemetry.trace.Span;
@@ -116,7 +114,6 @@ public final class JMSMessageConsumerInstrumentation extends Instrumenter.Defaul
}
Span span = spanBuilder.startSpan();
- try (Scope scope = currentContextWith(span)) {
DECORATE.afterStart(span, spanName, message);
DECORATE.onError(span, throwable);
DECORATE.beforeFinish(span);
@@ -124,4 +121,3 @@ public final class JMSMessageConsumerInstrumentation extends Instrumenter.Defaul
}
}
}
-}

MODIFY
instrumentation/netty/netty-4.0/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/HttpClientResponseTracingHandler.java
instrumentation/netty/netty-4.0/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/client/HttpClientResponseTracingHandler.java
@@ -7,7 +7,6 @@ package io.opentelemetry.javaagent.instrumentation.netty.v4_0.client;
import static io.opentelemetry.context.ContextUtils.withScopedContext;
import static io.opentelemetry.javaagent.instrumentation.netty.v4_0.client.NettyHttpClientTracer.TRACER;
-import static io.opentelemetry.trace.TracingContextUtils.currentContextWith;
import io.grpc.Context;
import io.netty.channel.ChannelHandlerContext;
@@ -29,10 +28,8 @@ public class HttpClientResponseTracingHandler extends ChannelInboundHandlerAdapt
boolean finishSpan = msg instanceof HttpResponse;
if (span != null && finishSpan) {
- try (Scope scope = currentContextWith(span)) {
TRACER.end(span, (HttpResponse) msg);
}
- }
// We want the callback in the scope of the parent, not the client span
if (parentContext != null) {

MODIFY
instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_1/client/HttpClientResponseTracingHandler.java
instrumentation/netty/netty-4.1/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_1/client/HttpClientResponseTracingHandler.java
@@ -7,7 +7,6 @@ package io.opentelemetry.javaagent.instrumentation.netty.v4_1.client;
import static io.opentelemetry.context.ContextUtils.withScopedContext;
import static io.opentelemetry.javaagent.instrumentation.netty.v4_1.client.NettyHttpClientTracer.TRACER;
-import static io.opentelemetry.trace.TracingContextUtils.currentContextWith;
import io.grpc.Context;
import io.netty.channel.ChannelHandlerContext;
@@ -29,10 +28,8 @@ public class HttpClientResponseTracingHandler extends ChannelInboundHandlerAdapt
boolean finishSpan = msg instanceof HttpResponse;
if (span != null && finishSpan) {
- try (Scope scope = currentContextWith(span)) {
TRACER.end(span, (HttpResponse) msg);
}
- }
// We want the callback in the scope of the parent, not the client span
if (parentContext != null) {

MODIFY
instrumentation/rabbitmq-2.7/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/amqp/RabbitChannelInstrumentation.java
instrumentation/rabbitmq-2.7/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/amqp/RabbitChannelInstrumentation.java
@@ -39,7 +39,6 @@ import com.rabbitmq.client.GetResponse;
import com.rabbitmq.client.MessageProperties;
import io.grpc.Context;
import io.opentelemetry.OpenTelemetry;
-import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap;
import io.opentelemetry.javaagent.instrumentation.api.SpanWithScope;
import io.opentelemetry.javaagent.tooling.Instrumenter;
@@ -258,17 +257,14 @@ public class RabbitChannelInstrumentation extends Instrumenter.Default {
span.setAttribute("message.size", response.getBody().length);
}
span.setAttribute(SemanticAttributes.NET_PEER_PORT, (long) connection.getPort());
- try (Scope scope = currentContextWith(span)) {
DECORATE.afterStart(span);
DECORATE.onGet(span, queue);
DECORATE.onPeerConnection(span, connection.getAddress());
DECORATE.onError(span, throwable);
DECORATE.beforeFinish(span);
- } finally {
span.end();
}
}
- }
public static class ChannelConsumeAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)

MODIFY
instrumentation/ratpack-1.4/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/TracingHandler.java
instrumentation/ratpack-1.4/src/main/java/io/opentelemetry/javaagent/instrumentation/ratpack/TracingHandler.java
@@ -42,14 +42,12 @@ public final class TracingHandler implements Handler {
ctx.getResponse()
.beforeSend(
response -> {
- try (Scope ignored = currentContextWith(ratpackSpan)) {
if (serverSpanContext != null) {
// Rename the netty span name with the ratpack route.
TRACER.onContext(getSpan(serverSpanContext), ctx);
}
TRACER.onContext(ratpackSpan, ctx);
TRACER.end(ratpackSpan);
- }
});
try (Scope ignored = currentContextWith(ratpackSpan)) {

MODIFY
instrumentation/servlet/servlet-common/src/test/groovy/RequestDispatcherTest.groovy
instrumentation/servlet/servlet-common/src/test/groovy/RequestDispatcherTest.groovy
@@ -80,6 +80,50 @@ class RequestDispatcherTest extends AgentTestRunner {
target = "test-$method"
}
+ def "test dispatcher #method with parent from request attribute"() {
+ setup:
+ def mockContext = null
+ runUnderTrace("parent") {
+ mockContext = Context.current()
+ }
+
+ when:
+ runUnderTrace("notParent") {
+ dispatcher."$method"(target)
+ }
+
+ then:
+ 1 * request.getAttribute(CONTEXT_ATTRIBUTE) >> mockContext
+ assertTraces(2) {
+ trace(0, 3) {
+ basicSpan(it, 0, "parent")
+ span(1) {
+ name "TestDispatcher.$operation"
+ childOf span(0)
+ }
+ basicSpan(it, 2, "$operation-child", span(1))
+ }
+ trace(1, 1) {
+ basicSpan(it, 0, "notParent")
+ }
+ }
+
+ then:
+ 1 * request.getAttribute(CONTEXT_ATTRIBUTE) >> mockContext
+ 1 * request.setAttribute(CONTEXT_ATTRIBUTE, { getSpan(it).name == "TestDispatcher.$operation" })
+ 1 * request.setAttribute(CONTEXT_ATTRIBUTE, { getSpan(it).name == "parent" })
+ 0 * _
+
+ where:
+ operation | method
+ "forward" | "forward"
+ "forward" | "forwardNamed"
+ "include" | "include"
+ "include" | "includeNamed"
+
+ target = "test-$method"
+ }
+
def "test dispatcher #method exception"() {
setup:
def ex = new ServletException("some error")

MODIFY
instrumentation/spymemcached-2.12/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/CompletionListener.java
instrumentation/spymemcached-2.12/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/CompletionListener.java
package io.opentelemetry.javaagent.instrumentation.spymemcached;
-import static io.opentelemetry.context.ContextUtils.withScopedContext;
import static io.opentelemetry.javaagent.instrumentation.spymemcached.MemcacheClientTracer.TRACER;
-import io.grpc.Context;
-import io.opentelemetry.context.Scope;
import io.opentelemetry.trace.Span;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ExecutionException;
@@ -22,16 +19,13 @@ public abstract class CompletionListener<T> {
static final String HIT = "hit";
static final String MISS = "miss";
- private final Context context;
private final Span span;
public CompletionListener(MemcachedConnection connection, String methodName) {
- context = Context.current();
span = TRACER.startSpan(connection, methodName);
}
protected void closeAsyncSpan(T future) {
- try (Scope ignored = withScopedContext(context)) {
try {
processResult(span, future);
} catch (CancellationException e) {
@@ -55,7 +49,6 @@ public abstract class CompletionListener<T> {
TRACER.end(span);
}
}
- }
protected void closeSyncSpan(Throwable thrown) {
TRACER.endExceptionally(span, thrown);

Remove unnecessary scopes (#1351)