Make sure CallDepthThreadLocalMap is always reset
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
@@ -64,7 +64,7 @@ public class CouchbaseBucketInstrumentation extends Instrumenter.Default {
return CallDepthThreadLocalMap.incrementCallDepth(CouchbaseCluster.class);
}
- @Advice.OnMethodExit
+ @Advice.OnMethodExit(onThrowable = Throwable.class)
public static void subscribeResult(
@Advice.Enter final int callDepth,
@Advice.Origin final Method method,

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
@@ -64,7 +64,7 @@ public class CouchbaseClusterInstrumentation extends Instrumenter.Default {
return CallDepthThreadLocalMap.incrementCallDepth(CouchbaseCluster.class);
}
- @Advice.OnMethodExit
+ @Advice.OnMethodExit(onThrowable = Throwable.class)
public static void subscribeResult(
@Advice.Enter final int callDepth,
@Advice.Origin final Method method,

MODIFY
dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/PreparedStatementInstrumentation.java
dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/PreparedStatementInstrumentation.java
@@ -61,13 +61,13 @@ public final class PreparedStatementInstrumentation extends Instrumenter.Default
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AgentScope onEnter(@Advice.This final PreparedStatement statement) {
- final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(PreparedStatement.class);
- if (callDepth > 0) {
+ final Connection connection = connectionFromStatement(statement);
+ if (connection == null) {
return null;
}
- final Connection connection = connectionFromStatement(statement);
- if (connection == null) {
+ final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(PreparedStatement.class);
+ if (callDepth > 0) {
return null;
}

MODIFY
dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/StatementInstrumentation.java
dd-java-agent/instrumentation/jdbc/src/main/java/datadog/trace/instrumentation/jdbc/StatementInstrumentation.java
@@ -62,13 +62,13 @@ public final class StatementInstrumentation extends Instrumenter.Default {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static AgentScope onEnter(
@Advice.Argument(0) final String sql, @Advice.This final Statement statement) {
- final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(Statement.class);
- if (callDepth > 0) {
+ final Connection connection = connectionFromStatement(statement);
+ if (connection == null) {
return null;
}
- final Connection connection = connectionFromStatement(statement);
- if (connection == null) {
+ final int callDepth = CallDepthThreadLocalMap.incrementCallDepth(Statement.class);
+ if (callDepth > 0) {
return null;
}

MODIFY
dd-java-agent/instrumentation/netty-4.0/src/main/java/datadog/trace/instrumentation/netty40/NettyChannelPipelineInstrumentation.java
dd-java-agent/instrumentation/netty-4.0/src/main/java/datadog/trace/instrumentation/netty40/NettyChannelPipelineInstrumentation.java
@@ -101,7 +101,7 @@ public class NettyChannelPipelineInstrumentation extends Instrumenter.Default {
return CallDepthThreadLocalMap.incrementCallDepth(ChannelPipeline.class);
}
- @Advice.OnMethodExit(suppress = Throwable.class)
+ @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void addHandler(
@Advice.Enter final int depth,
@Advice.This final ChannelPipeline pipeline,

MODIFY
dd-java-agent/instrumentation/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/NettyChannelPipelineInstrumentation.java
dd-java-agent/instrumentation/netty-4.1/src/main/java/datadog/trace/instrumentation/netty41/NettyChannelPipelineInstrumentation.java
@@ -110,7 +110,7 @@ public class NettyChannelPipelineInstrumentation extends Instrumenter.Default {
return CallDepthThreadLocalMap.incrementCallDepth(handler.getClass());
}
- @Advice.OnMethodExit(suppress = Throwable.class)
+ @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void addHandler(
@Advice.Enter final int depth,
@Advice.This final ChannelPipeline pipeline,

MODIFY
dd-java-agent/instrumentation/spymemcached-2.12/src/main/java/datadog/trace/instrumentation/spymemcached/MemcachedClientInstrumentation.java
dd-java-agent/instrumentation/spymemcached-2.12/src/main/java/datadog/trace/instrumentation/spymemcached/MemcachedClientInstrumentation.java
@@ -81,14 +81,17 @@ public final class MemcachedClientInstrumentation extends Instrumenter.Default {
return CallDepthThreadLocalMap.incrementCallDepth(MemcachedClient.class) <= 0;
}
- @Advice.OnMethodExit(suppress = Throwable.class)
+ @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
@Advice.Enter final boolean shouldInjectListener,
@Advice.This final MemcachedClient client,
@Advice.Origin("#m") final String methodName,
@Advice.Return final OperationFuture future) {
- if (shouldInjectListener && future != null) {
+ if (!shouldInjectListener) {
+ return;
+ }
CallDepthThreadLocalMap.reset(MemcachedClient.class);
+ if (future != null) {
final OperationCompletionListener listener =
new OperationCompletionListener(client.getConnection(), methodName);
future.addListener(listener);
@@ -103,14 +106,17 @@ public final class MemcachedClientInstrumentation extends Instrumenter.Default {
return CallDepthThreadLocalMap.incrementCallDepth(MemcachedClient.class) <= 0;
}
- @Advice.OnMethodExit(suppress = Throwable.class)
+ @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
@Advice.Enter final boolean shouldInjectListener,
@Advice.This final MemcachedClient client,
@Advice.Origin("#m") final String methodName,
@Advice.Return final GetFuture future) {
- if (shouldInjectListener && future != null) {
+ if (!shouldInjectListener) {
+ return;
+ }
CallDepthThreadLocalMap.reset(MemcachedClient.class);
+ if (future != null) {
final GetCompletionListener listener =
new GetCompletionListener(client.getConnection(), methodName);
future.addListener(listener);
@@ -125,14 +131,17 @@ public final class MemcachedClientInstrumentation extends Instrumenter.Default {
return CallDepthThreadLocalMap.incrementCallDepth(MemcachedClient.class) <= 0;
}
- @Advice.OnMethodExit(suppress = Throwable.class)
+ @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
@Advice.Enter final boolean shouldInjectListener,
@Advice.This final MemcachedClient client,
@Advice.Origin("#m") final String methodName,
@Advice.Return final BulkFuture future) {
- if (shouldInjectListener && future != null) {
+ if (!shouldInjectListener) {
+ return;
+ }
CallDepthThreadLocalMap.reset(MemcachedClient.class);
+ if (future != null) {
final BulkGetCompletionListener listener =
new BulkGetCompletionListener(client.getConnection(), methodName);
future.addListener(listener);

Make sure CallDepthThreadLocalMap is always reset