Stop using `scopeManager().activate(span, true)`
MODIFY
dd-java-agent/instrumentation/couchbase-2.0/src/main/java/datadog/trace/instrumentation/couchbase/client/CouchbaseBucketInstrumentation.java
dd-java-agent/instrumentation/couchbase-2.0/src/main/java/datadog/trace/instrumentation/couchbase/client/CouchbaseBucketInstrumentation.java
@@ -139,8 +139,9 @@ public class CouchbaseBucketInstrumentation extends Instrumenter.Default {
final Span span = spanRef.getAndSet(null);
if (span != null) {
- try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
DECORATE.beforeFinish(span);
+ span.finish();
}
}
}
@@ -157,9 +158,10 @@ public class CouchbaseBucketInstrumentation extends Instrumenter.Default {
public void call(final Throwable throwable) {
final Span span = spanRef.getAndSet(null);
if (span != null) {
- try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
DECORATE.onError(span, throwable);
DECORATE.beforeFinish(span);
+ span.finish();
}
}
}

MODIFY
dd-java-agent/instrumentation/couchbase-2.0/src/main/java/datadog/trace/instrumentation/couchbase/client/CouchbaseClusterInstrumentation.java
dd-java-agent/instrumentation/couchbase-2.0/src/main/java/datadog/trace/instrumentation/couchbase/client/CouchbaseClusterInstrumentation.java
@@ -133,8 +133,9 @@ public class CouchbaseClusterInstrumentation extends Instrumenter.Default {
final Span span = spanRef.getAndSet(null);
if (span != null) {
- try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
DECORATE.beforeFinish(span);
+ span.finish();
}
}
}
@@ -151,9 +152,10 @@ public class CouchbaseClusterInstrumentation extends Instrumenter.Default {
public void call(final Throwable throwable) {
final Span span = spanRef.getAndSet(null);
if (span != null) {
- try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
DECORATE.onError(span, throwable);
DECORATE.beforeFinish(span);
+ span.finish();
}
}
}

MODIFY
dd-java-agent/instrumentation/datastax-cassandra-3/src/main/java/datadog/trace/instrumentation/datastax/cassandra/TracingSession.java
dd-java-agent/instrumentation/datastax-cassandra-3/src/main/java/datadog/trace/instrumentation/datastax/cassandra/TracingSession.java
@@ -60,7 +60,7 @@ public class TracingSession implements Session {
@Override
public ResultSet execute(final String query) {
- try (final Scope scope = startSpanWithScope(query, true)) {
+ try (final Scope scope = startSpanWithScope(query)) {
try {
final ResultSet resultSet = session.execute(query);
beforeSpanFinish(scope.span(), resultSet);
@@ -68,13 +68,15 @@ public class TracingSession implements Session {
} catch (final RuntimeException e) {
beforeSpanFinish(scope.span(), e);
throw e;
+ } finally {
+ scope.span().finish();
}
}
}
@Override
public ResultSet execute(final String query, final Object... values) {
- try (final Scope scope = startSpanWithScope(query, true)) {
+ try (final Scope scope = startSpanWithScope(query)) {
try {
final ResultSet resultSet = session.execute(query, values);
beforeSpanFinish(scope.span(), resultSet);
@@ -82,13 +84,15 @@ public class TracingSession implements Session {
} catch (final RuntimeException e) {
beforeSpanFinish(scope.span(), e);
throw e;
+ } finally {
+ scope.span().finish();
}
}
}
@Override
public ResultSet execute(final String query, final Map<String, Object> values) {
- try (final Scope scope = startSpanWithScope(query, true)) {
+ try (final Scope scope = startSpanWithScope(query)) {
try {
final ResultSet resultSet = session.execute(query, values);
beforeSpanFinish(scope.span(), resultSet);
@@ -96,6 +100,8 @@ public class TracingSession implements Session {
} catch (final RuntimeException e) {
beforeSpanFinish(scope.span(), e);
throw e;
+ } finally {
+ scope.span().finish();
}
}
}
@@ -103,7 +109,7 @@ public class TracingSession implements Session {
@Override
public ResultSet execute(final Statement statement) {
final String query = getQuery(statement);
- try (final Scope scope = startSpanWithScope(query, true)) {
+ try (final Scope scope = startSpanWithScope(query)) {
try {
final ResultSet resultSet = session.execute(statement);
beforeSpanFinish(scope.span(), resultSet);
@@ -111,13 +117,15 @@ public class TracingSession implements Session {
} catch (final RuntimeException e) {
beforeSpanFinish(scope.span(), e);
throw e;
+ } finally {
+ scope.span().finish();
}
}
}
@Override
public ResultSetFuture executeAsync(final String query) {
- try (final Scope scope = startSpanWithScope(query, false)) {
+ try (final Scope scope = startSpanWithScope(query)) {
final ResultSetFuture future = session.executeAsync(query);
future.addListener(createListener(scope.span(), future), executorService);
@@ -127,7 +135,7 @@ public class TracingSession implements Session {
@Override
public ResultSetFuture executeAsync(final String query, final Object... values) {
- try (final Scope scope = startSpanWithScope(query, false)) {
+ try (final Scope scope = startSpanWithScope(query)) {
final ResultSetFuture future = session.executeAsync(query, values);
future.addListener(createListener(scope.span(), future), executorService);
@@ -137,7 +145,7 @@ public class TracingSession implements Session {
@Override
public ResultSetFuture executeAsync(final String query, final Map<String, Object> values) {
- try (final Scope scope = startSpanWithScope(query, false)) {
+ try (final Scope scope = startSpanWithScope(query)) {
final ResultSetFuture future = session.executeAsync(query, values);
future.addListener(createListener(scope.span(), future), executorService);
@@ -148,7 +156,7 @@ public class TracingSession implements Session {
@Override
public ResultSetFuture executeAsync(final Statement statement) {
final String query = getQuery(statement);
- try (final Scope scope = startSpanWithScope(query, false)) {
+ try (final Scope scope = startSpanWithScope(query)) {
final ResultSetFuture future = session.executeAsync(statement);
future.addListener(createListener(scope.span(), future), executorService);
@@ -216,18 +224,20 @@ public class TracingSession implements Session {
return new Runnable() {
@Override
public void run() {
- try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
beforeSpanFinish(span, future.get());
} catch (final InterruptedException | ExecutionException e) {
beforeSpanFinish(span, e);
+ } finally {
+ span.finish();
}
}
};
}
- private Scope startSpanWithScope(final String query, final boolean finishOnClose) {
+ private Scope startSpanWithScope(final String query) {
final Span span = tracer.buildSpan("cassandra.execute").start();
- final Scope scope = tracer.scopeManager().activate(span, finishOnClose);
+ final Scope scope = tracer.scopeManager().activate(span, false);
DECORATE.afterStart(span);
DECORATE.onConnection(span, session);
DECORATE.onStatement(span, query);

MODIFY
dd-java-agent/instrumentation/grpc-1.5/src/main/java/datadog/trace/instrumentation/grpc/client/TracingClientInterceptor.java
dd-java-agent/instrumentation/grpc-1.5/src/main/java/datadog/trace/instrumentation/grpc/client/TracingClientInterceptor.java
@@ -131,14 +131,14 @@ public class TracingClientInterceptor implements ClientInterceptor {
public void onClose(final Status status, final Metadata trailers) {
DECORATE.onClose(span, status);
// Finishes span.
- try (final Scope ignored = tracer.scopeManager().activate(span, true)) {
+ try (final Scope ignored = tracer.scopeManager().activate(span, false)) {
delegate().onClose(status, trailers);
- DECORATE.beforeFinish(span);
} catch (final Throwable e) {
DECORATE.onError(span, e);
+ throw e;
+ } finally {
DECORATE.beforeFinish(span);
span.finish();
- throw e;
}
}

MODIFY
dd-java-agent/instrumentation/grpc-1.5/src/main/java/datadog/trace/instrumentation/grpc/server/TracingServerInterceptor.java
dd-java-agent/instrumentation/grpc-1.5/src/main/java/datadog/trace/instrumentation/grpc/server/TracingServerInterceptor.java
@@ -140,7 +140,7 @@ public class TracingServerInterceptor implements ServerInterceptor {
@Override
public void onCancel() {
// Finishes span.
- try (final Scope scope = tracer.scopeManager().activate(span, true)) {
+ try (final Scope scope = tracer.scopeManager().activate(span, false)) {
if (scope instanceof TraceScope) {
((TraceScope) scope).setAsyncPropagation(true);
}
@@ -149,19 +149,19 @@ public class TracingServerInterceptor implements ServerInterceptor {
if (scope instanceof TraceScope) {
((TraceScope) scope).setAsyncPropagation(false);
}
- DECORATE.beforeFinish(span);
} catch (final Throwable e) {
DECORATE.onError(span, e);
+ throw e;
+ } finally {
DECORATE.beforeFinish(span);
span.finish();
- throw e;
}
}
@Override
public void onComplete() {
// Finishes span.
- try (final Scope scope = tracer.scopeManager().activate(span, true)) {
+ try (final Scope scope = tracer.scopeManager().activate(span, false)) {
if (scope instanceof TraceScope) {
((TraceScope) scope).setAsyncPropagation(true);
}
@@ -169,12 +169,12 @@ public class TracingServerInterceptor implements ServerInterceptor {
if (scope instanceof TraceScope) {
((TraceScope) scope).setAsyncPropagation(false);
}
- DECORATE.beforeFinish(span);
} catch (final Throwable e) {
DECORATE.onError(span, e);
+ throw e;
+ } finally {
DECORATE.beforeFinish(span);
span.finish();
- throw e;
}
}

MODIFY
dd-java-agent/instrumentation/http-url-connection/src/main/java/datadog/trace/instrumentation/http_url_connection/HttpUrlConnectionInstrumentation.java
dd-java-agent/instrumentation/http-url-connection/src/main/java/datadog/trace/instrumentation/http_url_connection/HttpUrlConnectionInstrumentation.java
@@ -199,9 +199,10 @@ public class HttpUrlConnectionInstrumentation extends Instrumenter.Default {
}
public void finishSpan(final Throwable throwable) {
- try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
DECORATE.onError(span, throwable);
DECORATE.beforeFinish(span);
+ span.finish();
span = null;
finished = true;
}
@@ -214,9 +215,10 @@ public class HttpUrlConnectionInstrumentation extends Instrumenter.Default {
* (e.g. breaks getOutputStream).
*/
if (responseCode > 0) {
- try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
DECORATE.onResponse(span, responseCode);
DECORATE.beforeFinish(span);
+ span.finish();
span = null;
finished = true;
}

MODIFY
dd-java-agent/instrumentation/http-url-connection/src/main/java/datadog/trace/instrumentation/http_url_connection/UrlInstrumentation.java
dd-java-agent/instrumentation/http-url-connection/src/main/java/datadog/trace/instrumentation/http_url_connection/UrlInstrumentation.java
@@ -66,7 +66,7 @@ public class UrlInstrumentation extends Instrumenter.Default {
.withTag(DDTags.SPAN_TYPE, DDSpanTypes.HTTP_CLIENT)
.withTag(Tags.COMPONENT.getKey(), COMPONENT)
.start();
- try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
Tags.HTTP_URL.set(span, url.toString());
Tags.PEER_PORT.set(span, url.getPort() == -1 ? 80 : url.getPort());
Tags.PEER_HOSTNAME.set(span, url.getHost());
@@ -76,6 +76,7 @@ public class UrlInstrumentation extends Instrumenter.Default {
Tags.ERROR.set(span, true);
span.log(Collections.singletonMap(ERROR_OBJECT, throwable));
+ span.finish();
}
}
}

MODIFY
dd-java-agent/instrumentation/jms/src/main/java/datadog/trace/instrumentation/jms/JMSMessageConsumerInstrumentation.java
dd-java-agent/instrumentation/jms/src/main/java/datadog/trace/instrumentation/jms/JMSMessageConsumerInstrumentation.java
@@ -93,7 +93,7 @@ public final class JMSMessageConsumerInstrumentation extends Instrumenter.Defaul
}
final Span span = spanBuilder.start();
- try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
CONSUMER_DECORATE.afterStart(span);
if (message == null) {
CONSUMER_DECORATE.onReceive(span, method);
@@ -102,6 +102,7 @@ public final class JMSMessageConsumerInstrumentation extends Instrumenter.Defaul
}
CONSUMER_DECORATE.onError(span, throwable);
CONSUMER_DECORATE.beforeFinish(span);
+ span.finish();
}
}
}

MODIFY
dd-java-agent/instrumentation/netty-4.0/src/main/java/datadog/trace/instrumentation/netty40/ChannelFutureListenerInstrumentation.java
dd-java-agent/instrumentation/netty-4.0/src/main/java/datadog/trace/instrumentation/netty40/ChannelFutureListenerInstrumentation.java
@@ -90,9 +90,10 @@ public class ChannelFutureListenerInstrumentation extends Instrumenter.Default {
.buildSpan("netty.connect")
.withTag(Tags.COMPONENT.getKey(), "netty")
.start();
- try (final Scope scope = GlobalTracer.get().scopeManager().activate(errorSpan, true)) {
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(errorSpan, false)) {
NettyHttpServerDecorator.DECORATE.onError(errorSpan, cause);
NettyHttpServerDecorator.DECORATE.beforeFinish(errorSpan);
+ errorSpan.finish();
}
return parentScope;

MODIFY
dd-java-agent/instrumentation/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/ChannelFutureListenerInstrumentation.java
dd-java-agent/instrumentation/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/ChannelFutureListenerInstrumentation.java
@@ -90,9 +90,10 @@ public class ChannelFutureListenerInstrumentation extends Instrumenter.Default {
.buildSpan("netty.connect")
.withTag(Tags.COMPONENT.getKey(), "netty")
.start();
- try (final Scope scope = GlobalTracer.get().scopeManager().activate(errorSpan, true)) {
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(errorSpan, false)) {
NettyHttpServerDecorator.DECORATE.onError(errorSpan, cause);
NettyHttpServerDecorator.DECORATE.beforeFinish(errorSpan);
+ errorSpan.finish();
}
return parentScope;

MODIFY
dd-java-agent/instrumentation/okhttp-3/src/main/java/datadog/trace/instrumentation/okhttp3/TracingCallFactory.java
dd-java-agent/instrumentation/okhttp-3/src/main/java/datadog/trace/instrumentation/okhttp3/TracingCallFactory.java
@@ -42,13 +42,14 @@ public class TracingCallFactory implements Call.Factory {
@Override
public Response intercept(final Chain chain) throws IOException {
try (final Scope interceptorScope =
- GlobalTracer.get().scopeManager().activate(span, true)) {
+ GlobalTracer.get().scopeManager().activate(span, false)) {
return chain.proceed(chain.request());
} catch (final Exception ex) {
DECORATE.onError(scope, ex);
throw ex;
} finally {
DECORATE.beforeFinish(span);
+ span.finish();
}
}
});

MODIFY
dd-java-agent/instrumentation/rabbitmq-amqp-2.7/src/main/java/datadog/trace/instrumentation/rabbitmq/amqp/RabbitChannelInstrumentation.java
dd-java-agent/instrumentation/rabbitmq-amqp-2.7/src/main/java/datadog/trace/instrumentation/rabbitmq/amqp/RabbitChannelInstrumentation.java
@@ -193,7 +193,7 @@ public class RabbitChannelInstrumentation extends Instrumenter.Default {
@Advice.Local("callDepth") int callDepth) {
callDepth = CallDepthThreadLocalMap.incrementCallDepth(Channel.class);
// Don't want RabbitCommandInstrumentation to mess up our actual parent span.
- placeholderScope = GlobalTracer.get().scopeManager().activate(NoopSpan.INSTANCE, true);
+ placeholderScope = GlobalTracer.get().scopeManager().activate(NoopSpan.INSTANCE, false);
return System.currentTimeMillis();
}
@@ -247,13 +247,14 @@ public class RabbitChannelInstrumentation extends Instrumenter.Default {
.withTag("message.size", length)
.withTag(Tags.PEER_PORT.getKey(), connection.getPort())
.start();
- try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
CONSUMER_DECORATE.afterStart(span);
CONSUMER_DECORATE.onGet(span, queue);
CONSUMER_DECORATE.onPeerConnection(span, connection.getAddress());
CONSUMER_DECORATE.onError(span, throwable);
CONSUMER_DECORATE.beforeFinish(span);
} finally {
+ span.finish();
CallDepthThreadLocalMap.reset(Channel.class);
}
}

MODIFY
dd-java-agent/instrumentation/spymemcached-2.12/src/main/java/datadog/trace/instrumentation/spymemcached/CompletionListener.java
dd-java-agent/instrumentation/spymemcached-2.12/src/main/java/datadog/trace/instrumentation/spymemcached/CompletionListener.java
@@ -38,7 +38,7 @@ public abstract class CompletionListener<T> {
}
protected void closeAsyncSpan(final T future) {
- try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
try {
processResult(span, future);
} catch (final CancellationException e) {
@@ -60,14 +60,16 @@ public abstract class CompletionListener<T> {
DECORATE.onError(span, e);
} finally {
DECORATE.beforeFinish(span);
+ span.finish();
}
}
}
protected void closeSyncSpan(final Throwable thrown) {
- try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, true)) {
+ try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
DECORATE.onError(span, thrown);
DECORATE.beforeFinish(span);
+ span.finish();
}
}

Stop using `scopeManager().activate(span, true)`