From b9fcb6b4982c27428395ff53b155cc19a28687c6 Mon Sep 17 00:00:00 2001 From: Mateusz Rzeszutek Date: Tue, 6 Jul 2021 09:24:19 +0200 Subject: [PATCH] Reduce CallDepth classes' API surface (#3497) --- .../v2_0/CouchbaseBucketInstrumentation.java | 10 ++--- .../v2_0/CouchbaseClusterInstrumentation.java | 6 +-- .../GrpcServerBuilderInstrumentation.java | 6 +-- .../v3_3/SessionInstrumentation.java | 4 +- .../v4_0/SessionInstrumentation.java | 4 +- .../hibernate/SessionMethodUtils.java | 6 +-- .../HttpUrlConnectionInstrumentation.java | 3 +- .../ClassLoaderInstrumentation.java | 6 +-- .../v1_0/JaxRsAnnotationsInstrumentation.java | 6 +-- .../v2_0/JaxRsAnnotationsInstrumentation.java | 6 +-- .../WebServiceProviderInstrumentation.java | 6 +-- .../v1_1/JwsAnnotationsInstrumentation.java | 6 +-- .../PreparedStatementInstrumentation.java | 6 +-- .../jdbc/StatementInstrumentation.java | 6 +-- .../JmsMessageProducerInstrumentation.java | 10 ++--- .../NettyChannelPipelineInstrumentation.java | 8 ++-- .../NettyChannelPipelineInstrumentation.java | 6 +-- .../NettyChannelPipelineInstrumentation.java | 6 +-- .../okhttp/v3_0/OkHttp3Instrumentation.java | 6 +-- .../RabbitChannelInstrumentation.java | 10 ++--- .../v0_9/HttpClientInstrumentation.java | 6 +-- .../v1_0/HttpClientInstrumentation.java | 6 +-- .../server/RemoteServerInstrumentation.java | 6 +-- .../servlet/v2_2/Servlet2Advice.java | 6 +-- .../servlet/v3_0/AsyncDispatchAdvice.java | 6 +-- .../servlet/v3_0/Servlet3Advice.java | 4 +- .../v3_0/Servlet3AsyncStartAdvice.java | 6 +-- .../v5_0/async/AsyncDispatchAdvice.java | 6 +-- .../servlet/v5_0/async/AsyncStartAdvice.java | 6 +-- .../v5_0/response/ResponseSendAdvice.java | 3 +- .../service/JakartaServletServiceAdvice.java | 4 +- .../service/ServletAndFilterAdviceHelper.java | 4 +- .../javax/response/ResponseSendAdvice.java | 3 +- .../ws/AnnotatedMethodInstrumentation.java | 6 +-- .../MemcachedClientInstrumentation.java | 9 ++-- .../instrumentation/api/CallDepth.java | 35 ++++++++++++++++ .../api/CallDepthThreadLocalMap.java | 31 +------------- .../instrumentation/api/CallDepthTest.java | 42 +++++++++++++++++++ .../api/CallDepthThreadLocalMapTest.java | 42 ------------------- 39 files changed, 181 insertions(+), 177 deletions(-) create mode 100644 javaagent-api/src/test/java/io/opentelemetry/javaagent/instrumentation/api/CallDepthTest.java delete mode 100644 javaagent-api/src/test/java/io/opentelemetry/javaagent/instrumentation/api/CallDepthThreadLocalMapTest.java diff --git a/instrumentation/couchbase/couchbase-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/couchbase/v2_0/CouchbaseBucketInstrumentation.java b/instrumentation/couchbase/couchbase-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/couchbase/v2_0/CouchbaseBucketInstrumentation.java index c884f77477..0880e590d6 100644 --- a/instrumentation/couchbase/couchbase-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/couchbase/v2_0/CouchbaseBucketInstrumentation.java +++ b/instrumentation/couchbase/couchbase-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/couchbase/v2_0/CouchbaseBucketInstrumentation.java @@ -15,7 +15,7 @@ import static net.bytebuddy.matcher.ElementMatchers.returns; import com.couchbase.client.java.CouchbaseCluster; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import java.lang.reflect.Method; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; @@ -46,7 +46,7 @@ public class CouchbaseBucketInstrumentation implements TypeInstrumentation { @Advice.OnMethodEnter public static int trackCallDepth() { - return CallDepthThreadLocalMap.incrementCallDepth(CouchbaseCluster.class); + return CallDepth.forClass(CouchbaseCluster.class).getAndIncrement(); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) @@ -58,7 +58,7 @@ public class CouchbaseBucketInstrumentation implements TypeInstrumentation { if (callDepth > 0) { return; } - CallDepthThreadLocalMap.reset(CouchbaseCluster.class); + CallDepth.forClass(CouchbaseCluster.class).reset(); result = Observable.create(CouchbaseOnSubscribe.create(result, bucket, method)); } } @@ -68,7 +68,7 @@ public class CouchbaseBucketInstrumentation implements TypeInstrumentation { @Advice.OnMethodEnter public static int trackCallDepth() { - return CallDepthThreadLocalMap.incrementCallDepth(CouchbaseCluster.class); + return CallDepth.forClass(CouchbaseCluster.class).getAndIncrement(); } @Advice.OnMethodExit(onThrowable = Throwable.class) @@ -81,7 +81,7 @@ public class CouchbaseBucketInstrumentation implements TypeInstrumentation { if (callDepth > 0) { return; } - CallDepthThreadLocalMap.reset(CouchbaseCluster.class); + CallDepth.forClass(CouchbaseCluster.class).reset(); if (query != null) { // A query can be of many different types. We could track the creation of them and try to diff --git a/instrumentation/couchbase/couchbase-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/couchbase/v2_0/CouchbaseClusterInstrumentation.java b/instrumentation/couchbase/couchbase-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/couchbase/v2_0/CouchbaseClusterInstrumentation.java index 6f3b8baff0..14d5810301 100644 --- a/instrumentation/couchbase/couchbase-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/couchbase/v2_0/CouchbaseClusterInstrumentation.java +++ b/instrumentation/couchbase/couchbase-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/couchbase/v2_0/CouchbaseClusterInstrumentation.java @@ -15,7 +15,7 @@ import static net.bytebuddy.matcher.ElementMatchers.returns; import com.couchbase.client.java.CouchbaseCluster; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import java.lang.reflect.Method; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; @@ -43,7 +43,7 @@ public class CouchbaseClusterInstrumentation implements TypeInstrumentation { @Advice.OnMethodEnter public static int trackCallDepth() { - return CallDepthThreadLocalMap.incrementCallDepth(CouchbaseCluster.class); + return CallDepth.forClass(CouchbaseCluster.class).getAndIncrement(); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) @@ -54,7 +54,7 @@ public class CouchbaseClusterInstrumentation implements TypeInstrumentation { if (callDepth > 0) { return; } - CallDepthThreadLocalMap.reset(CouchbaseCluster.class); + CallDepth.forClass(CouchbaseCluster.class).reset(); result = Observable.create(CouchbaseOnSubscribe.create(result, null, method)); } diff --git a/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcServerBuilderInstrumentation.java b/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcServerBuilderInstrumentation.java index c353eaca07..56cbc6bb05 100644 --- a/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcServerBuilderInstrumentation.java +++ b/instrumentation/grpc-1.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/grpc/v1_6/GrpcServerBuilderInstrumentation.java @@ -15,7 +15,7 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArguments; import io.grpc.ServerBuilder; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.matcher.ElementMatcher; @@ -44,7 +44,7 @@ public class GrpcServerBuilderInstrumentation implements TypeInstrumentation { @Advice.OnMethodEnter(suppress = Throwable.class) public static void onEnter(@Advice.This ServerBuilder serverBuilder) { - int callDepth = CallDepthThreadLocalMap.incrementCallDepth(ServerBuilder.class); + int callDepth = CallDepth.forClass(ServerBuilder.class).getAndIncrement(); if (callDepth == 0) { serverBuilder.intercept(GrpcSingletons.SERVER_INTERCEPTOR); } @@ -52,7 +52,7 @@ public class GrpcServerBuilderInstrumentation implements TypeInstrumentation { @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void onExit(@Advice.This ServerBuilder serverBuilder) { - CallDepthThreadLocalMap.decrementCallDepth(ServerBuilder.class); + CallDepth.forClass(ServerBuilder.class).decrementAndGet(); } } } diff --git a/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/SessionInstrumentation.java b/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/SessionInstrumentation.java index 951c705ef4..8f8a6c85d9 100644 --- a/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/SessionInstrumentation.java +++ b/instrumentation/hibernate/hibernate-3.3/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v3_3/SessionInstrumentation.java @@ -20,7 +20,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import io.opentelemetry.javaagent.instrumentation.api.ContextStore; import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext; import io.opentelemetry.javaagent.instrumentation.hibernate.SessionMethodUtils; @@ -150,7 +150,7 @@ public class SessionInstrumentation implements TypeInstrumentation { return; // No state found. We aren't in a Session. } - if (CallDepthThreadLocalMap.incrementCallDepth(SessionMethodUtils.class) > 0) { + if (CallDepth.forClass(SessionMethodUtils.class).getAndIncrement() > 0) { return; // This method call is being traced already. } diff --git a/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/SessionInstrumentation.java b/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/SessionInstrumentation.java index 7991ac18c9..d898585013 100644 --- a/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/SessionInstrumentation.java +++ b/instrumentation/hibernate/hibernate-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/v4_0/SessionInstrumentation.java @@ -20,7 +20,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import io.opentelemetry.javaagent.instrumentation.api.ContextStore; import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext; import io.opentelemetry.javaagent.instrumentation.hibernate.SessionMethodUtils; @@ -132,7 +132,7 @@ public class SessionInstrumentation implements TypeInstrumentation { return; // No state found. We aren't in a Session. } - if (CallDepthThreadLocalMap.incrementCallDepth(SessionMethodUtils.class) > 0) { + if (CallDepth.forClass(SessionMethodUtils.class).getAndIncrement() > 0) { return; // This method call is being traced already. } diff --git a/instrumentation/hibernate/hibernate-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/SessionMethodUtils.java b/instrumentation/hibernate/hibernate-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/SessionMethodUtils.java index 978f0d4916..0287e39fa1 100644 --- a/instrumentation/hibernate/hibernate-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/SessionMethodUtils.java +++ b/instrumentation/hibernate/hibernate-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/hibernate/SessionMethodUtils.java @@ -11,7 +11,7 @@ import io.opentelemetry.api.trace.Span; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.db.SqlStatementInfo; import io.opentelemetry.instrumentation.api.db.SqlStatementSanitizer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import io.opentelemetry.javaagent.instrumentation.api.ContextStore; import java.util.Arrays; import java.util.HashSet; @@ -43,7 +43,7 @@ public final class SessionMethodUtils { return null; // No state found. We aren't in a Session. } - int depth = CallDepthThreadLocalMap.incrementCallDepth(SessionMethodUtils.class); + int depth = CallDepth.forClass(SessionMethodUtils.class).getAndIncrement(); if (depth > 0) { return null; // This method call is being traced already. } @@ -73,7 +73,7 @@ public final class SessionMethodUtils { public static void end( @Nullable Context context, Throwable throwable, String operationName, Object entity) { - CallDepthThreadLocalMap.reset(SessionMethodUtils.class); + CallDepth.forClass(SessionMethodUtils.class).reset(); if (context == null) { return; diff --git a/instrumentation/http-url-connection/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/httpurlconnection/HttpUrlConnectionInstrumentation.java b/instrumentation/http-url-connection/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/httpurlconnection/HttpUrlConnectionInstrumentation.java index 1045a861b7..c4bb4c17ea 100644 --- a/instrumentation/http-url-connection/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/httpurlconnection/HttpUrlConnectionInstrumentation.java +++ b/instrumentation/http-url-connection/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/httpurlconnection/HttpUrlConnectionInstrumentation.java @@ -23,7 +23,6 @@ import io.opentelemetry.instrumentation.api.tracer.HttpStatusConverter; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import io.opentelemetry.javaagent.instrumentation.api.CallDepth; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; import io.opentelemetry.javaagent.instrumentation.api.ContextStore; import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext; import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; @@ -70,7 +69,7 @@ public class HttpUrlConnectionInstrumentation implements TypeInstrumentation { @Advice.Local("otelScope") Scope scope, @Advice.Local("otelCallDepth") CallDepth callDepth) { - callDepth = CallDepthThreadLocalMap.getCallDepth(HttpURLConnection.class); + callDepth = CallDepth.forClass(HttpURLConnection.class); if (callDepth.getAndIncrement() > 0) { // only want the rest of the instrumentation rules (which are complex enough) to apply to // top-level HttpURLConnection calls diff --git a/instrumentation/internal/internal-class-loader/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/internal/classloader/ClassLoaderInstrumentation.java b/instrumentation/internal/internal-class-loader/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/internal/classloader/ClassLoaderInstrumentation.java index 5851d6a79f..74140ec690 100644 --- a/instrumentation/internal/internal-class-loader/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/internal/classloader/ClassLoaderInstrumentation.java +++ b/instrumentation/internal/internal-class-loader/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/internal/classloader/ClassLoaderInstrumentation.java @@ -18,7 +18,7 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArguments; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import io.opentelemetry.javaagent.instrumentation.api.internal.BootstrapPackagePrefixesHolder; import io.opentelemetry.javaagent.tooling.Constants; import java.lang.invoke.MethodHandle; @@ -107,7 +107,7 @@ public class ClassLoaderInstrumentation implements TypeInstrumentation { // because on some JVMs (e.g. IBM's, though IBM bootstrap loader is explicitly excluded above) // Class.forName() ends up calling loadClass() on the bootstrap loader which would then come // back to this instrumentation over and over, causing a StackOverflowError - int callDepth = CallDepthThreadLocalMap.incrementCallDepth(ClassLoader.class); + int callDepth = CallDepth.forClass(ClassLoader.class).getAndIncrement(); if (callDepth > 0) { return null; } @@ -128,7 +128,7 @@ public class ClassLoaderInstrumentation implements TypeInstrumentation { // ends up calling a ClassFileTransformer which ends up calling loadClass() further down the // stack on one of our bootstrap packages (since the call depth check would then suppress // the nested loadClass instrumentation) - CallDepthThreadLocalMap.reset(ClassLoader.class); + CallDepth.forClass(ClassLoader.class).reset(); } return null; } diff --git a/instrumentation/jaxrs/jaxrs-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v1_0/JaxRsAnnotationsInstrumentation.java b/instrumentation/jaxrs/jaxrs-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v1_0/JaxRsAnnotationsInstrumentation.java index 0b0e451a8c..4b8b00a7da 100644 --- a/instrumentation/jaxrs/jaxrs-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v1_0/JaxRsAnnotationsInstrumentation.java +++ b/instrumentation/jaxrs/jaxrs-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v1_0/JaxRsAnnotationsInstrumentation.java @@ -19,7 +19,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import java.lang.reflect.Method; import javax.ws.rs.Path; import net.bytebuddy.asm.Advice; @@ -67,7 +67,7 @@ public class JaxRsAnnotationsInstrumentation implements TypeInstrumentation { @Advice.Origin Method method, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - if (CallDepthThreadLocalMap.incrementCallDepth(Path.class) > 0) { + if (CallDepth.forClass(Path.class).getAndIncrement() > 0) { return; } context = tracer().startSpan(target.getClass(), method); @@ -82,7 +82,7 @@ public class JaxRsAnnotationsInstrumentation implements TypeInstrumentation { if (scope == null) { return; } - CallDepthThreadLocalMap.reset(Path.class); + CallDepth.forClass(Path.class).reset(); scope.close(); if (throwable == null) { diff --git a/instrumentation/jaxrs/jaxrs-2.0/jaxrs-2.0-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v2_0/JaxRsAnnotationsInstrumentation.java b/instrumentation/jaxrs/jaxrs-2.0/jaxrs-2.0-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v2_0/JaxRsAnnotationsInstrumentation.java index 0eea983d44..c8deaf2ec7 100644 --- a/instrumentation/jaxrs/jaxrs-2.0/jaxrs-2.0-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v2_0/JaxRsAnnotationsInstrumentation.java +++ b/instrumentation/jaxrs/jaxrs-2.0/jaxrs-2.0-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v2_0/JaxRsAnnotationsInstrumentation.java @@ -19,7 +19,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import io.opentelemetry.javaagent.instrumentation.api.ContextStore; import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext; import java.lang.reflect.Method; @@ -92,7 +92,7 @@ public class JaxRsAnnotationsInstrumentation implements TypeInstrumentation { } } - if (CallDepthThreadLocalMap.incrementCallDepth(Path.class) > 0) { + if (CallDepth.forClass(Path.class).getAndIncrement() > 0) { return; } @@ -115,7 +115,7 @@ public class JaxRsAnnotationsInstrumentation implements TypeInstrumentation { if (context == null || scope == null) { return; } - CallDepthThreadLocalMap.reset(Path.class); + CallDepth.forClass(Path.class).reset(); if (throwable != null) { tracer().endExceptionally(context, throwable); diff --git a/instrumentation/jaxws/jaxws-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxws/v2_0/WebServiceProviderInstrumentation.java b/instrumentation/jaxws/jaxws-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxws/v2_0/WebServiceProviderInstrumentation.java index 1a9c838b21..cec5402cf7 100644 --- a/instrumentation/jaxws/jaxws-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxws/v2_0/WebServiceProviderInstrumentation.java +++ b/instrumentation/jaxws/jaxws-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxws/v2_0/WebServiceProviderInstrumentation.java @@ -18,7 +18,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import java.lang.reflect.Method; import javax.xml.ws.Provider; import net.bytebuddy.asm.Advice; @@ -53,7 +53,7 @@ public class WebServiceProviderInstrumentation implements TypeInstrumentation { @Advice.Origin Method method, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - if (CallDepthThreadLocalMap.incrementCallDepth(Provider.class) > 0) { + if (CallDepth.forClass(Provider.class).getAndIncrement() > 0) { return; } context = tracer().startSpan(target.getClass(), method); @@ -68,7 +68,7 @@ public class WebServiceProviderInstrumentation implements TypeInstrumentation { if (scope == null) { return; } - CallDepthThreadLocalMap.reset(Provider.class); + CallDepth.forClass(Provider.class).reset(); scope.close(); if (throwable == null) { diff --git a/instrumentation/jaxws/jws-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxws/jws/v1_1/JwsAnnotationsInstrumentation.java b/instrumentation/jaxws/jws-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxws/jws/v1_1/JwsAnnotationsInstrumentation.java index b3b73f99af..186040d615 100644 --- a/instrumentation/jaxws/jws-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxws/jws/v1_1/JwsAnnotationsInstrumentation.java +++ b/instrumentation/jaxws/jws-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxws/jws/v1_1/JwsAnnotationsInstrumentation.java @@ -20,7 +20,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import java.lang.reflect.Method; import javax.jws.WebService; import net.bytebuddy.asm.Advice; @@ -64,7 +64,7 @@ public class JwsAnnotationsInstrumentation implements TypeInstrumentation { @Advice.Origin Method method, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - if (CallDepthThreadLocalMap.incrementCallDepth(WebService.class) > 0) { + if (CallDepth.forClass(WebService.class).getAndIncrement() > 0) { return; } context = tracer().startSpan(target.getClass(), method); @@ -79,7 +79,7 @@ public class JwsAnnotationsInstrumentation implements TypeInstrumentation { if (scope == null) { return; } - CallDepthThreadLocalMap.reset(WebService.class); + CallDepth.forClass(WebService.class).reset(); scope.close(); if (throwable == null) { diff --git a/instrumentation/jdbc/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jdbc/PreparedStatementInstrumentation.java b/instrumentation/jdbc/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jdbc/PreparedStatementInstrumentation.java index a198ecc5cb..60c18422fc 100644 --- a/instrumentation/jdbc/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jdbc/PreparedStatementInstrumentation.java +++ b/instrumentation/jdbc/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jdbc/PreparedStatementInstrumentation.java @@ -19,7 +19,7 @@ import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.jdbc.internal.DbRequest; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import java.sql.PreparedStatement; import java.sql.Statement; import net.bytebuddy.asm.Advice; @@ -61,7 +61,7 @@ public class PreparedStatementInstrumentation implements TypeInstrumentation { // using CallDepth prevents this, because this check happens before Connection#getMetadata() // is called - the first recursive Statement call is just skipped and we do not create a span // for it - if (CallDepthThreadLocalMap.getCallDepth(Statement.class).getAndIncrement() > 0) { + if (CallDepth.forClass(Statement.class).getAndIncrement() > 0) { return; } @@ -85,7 +85,7 @@ public class PreparedStatementInstrumentation implements TypeInstrumentation { if (scope == null) { return; } - CallDepthThreadLocalMap.reset(Statement.class); + CallDepth.forClass(Statement.class).reset(); scope.close(); instrumenter().end(context, request, null, throwable); diff --git a/instrumentation/jdbc/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jdbc/StatementInstrumentation.java b/instrumentation/jdbc/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jdbc/StatementInstrumentation.java index 3cbbcf857d..c7e65cf5a0 100644 --- a/instrumentation/jdbc/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jdbc/StatementInstrumentation.java +++ b/instrumentation/jdbc/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jdbc/StatementInstrumentation.java @@ -19,7 +19,7 @@ import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.jdbc.internal.DbRequest; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import java.sql.Statement; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; @@ -61,7 +61,7 @@ public class StatementInstrumentation implements TypeInstrumentation { // using CallDepth prevents this, because this check happens before Connection#getMetadata() // is called - the first recursive Statement call is just skipped and we do not create a span // for it - if (CallDepthThreadLocalMap.getCallDepth(Statement.class).getAndIncrement() > 0) { + if (CallDepth.forClass(Statement.class).getAndIncrement() > 0) { return; } @@ -85,7 +85,7 @@ public class StatementInstrumentation implements TypeInstrumentation { if (scope == null) { return; } - CallDepthThreadLocalMap.reset(Statement.class); + CallDepth.forClass(Statement.class).reset(); scope.close(); instrumenter().end(context, request, null, throwable); diff --git a/instrumentation/jms-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jms/JmsMessageProducerInstrumentation.java b/instrumentation/jms-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jms/JmsMessageProducerInstrumentation.java index 93d002b036..a5258305df 100644 --- a/instrumentation/jms-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jms/JmsMessageProducerInstrumentation.java +++ b/instrumentation/jms-1.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jms/JmsMessageProducerInstrumentation.java @@ -17,7 +17,7 @@ import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.instrumenter.messaging.MessageOperation; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; import javax.jms.Destination; import javax.jms.JMSException; @@ -62,7 +62,7 @@ public class JmsMessageProducerInstrumentation implements TypeInstrumentation { @Advice.Local("otelRequest") MessageWithDestination request, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - int callDepth = CallDepthThreadLocalMap.incrementCallDepth(MessageProducer.class); + int callDepth = CallDepth.forClass(MessageProducer.class).getAndIncrement(); if (callDepth > 0) { return; } @@ -93,7 +93,7 @@ public class JmsMessageProducerInstrumentation implements TypeInstrumentation { if (scope == null) { return; } - CallDepthThreadLocalMap.reset(MessageProducer.class); + CallDepth.forClass(MessageProducer.class).reset(); scope.close(); producerInstrumenter().end(context, request, null, throwable); @@ -110,7 +110,7 @@ public class JmsMessageProducerInstrumentation implements TypeInstrumentation { @Advice.Local("otelRequest") MessageWithDestination request, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - int callDepth = CallDepthThreadLocalMap.incrementCallDepth(MessageProducer.class); + int callDepth = CallDepth.forClass(MessageProducer.class).getAndIncrement(); if (callDepth > 0) { return; } @@ -134,7 +134,7 @@ public class JmsMessageProducerInstrumentation implements TypeInstrumentation { if (scope == null) { return; } - CallDepthThreadLocalMap.reset(MessageProducer.class); + CallDepth.forClass(MessageProducer.class).reset(); scope.close(); producerInstrumenter().end(context, request, null, throwable); diff --git a/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/NettyChannelPipelineInstrumentation.java b/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/NettyChannelPipelineInstrumentation.java index 982c23b6d4..ddcab93bf0 100644 --- a/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/NettyChannelPipelineInstrumentation.java +++ b/instrumentation/netty/netty-3.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v3_8/NettyChannelPipelineInstrumentation.java @@ -14,7 +14,7 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArgument; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import io.opentelemetry.javaagent.instrumentation.api.ContextStore; import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext; import io.opentelemetry.javaagent.instrumentation.netty.v3_8.client.HttpClientRequestTracingHandler; @@ -100,7 +100,7 @@ public class NettyChannelPipelineInstrumentation implements TypeInstrumentation new HttpClientResponseTracingHandler(contextStore)); } } finally { - CallDepthThreadLocalMap.reset(ChannelPipeline.class); + CallDepth.forClass(ChannelPipeline.class).reset(); } } } @@ -117,7 +117,7 @@ public class NettyChannelPipelineInstrumentation implements TypeInstrumentation if (pipeline.get(handler.getClass().getName()) != null) { pipeline.remove(handler.getClass().getName()); } - return CallDepthThreadLocalMap.incrementCallDepth(ChannelPipeline.class); + return CallDepth.forClass(ChannelPipeline.class).getAndIncrement(); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) @@ -148,7 +148,7 @@ public class NettyChannelPipelineInstrumentation implements TypeInstrumentation if (pipeline.get(handler.getClass().getName()) != null) { pipeline.remove(handler.getClass().getName()); } - return CallDepthThreadLocalMap.incrementCallDepth(ChannelPipeline.class); + return CallDepth.forClass(ChannelPipeline.class).getAndIncrement(); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) diff --git a/instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/NettyChannelPipelineInstrumentation.java b/instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/NettyChannelPipelineInstrumentation.java index 37a63302b8..a7b38fff73 100644 --- a/instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/NettyChannelPipelineInstrumentation.java +++ b/instrumentation/netty/netty-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_0/NettyChannelPipelineInstrumentation.java @@ -22,7 +22,7 @@ import io.netty.handler.codec.http.HttpServerCodec; import io.netty.util.Attribute; import io.opentelemetry.context.Context; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext; import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; import io.opentelemetry.javaagent.instrumentation.netty.common.AbstractNettyChannelPipelineInstrumentation; @@ -61,7 +61,7 @@ public class NettyChannelPipelineInstrumentation @Advice.OnMethodEnter public static int trackCallDepth() { - return CallDepthThreadLocalMap.incrementCallDepth(ChannelPipeline.class); + return CallDepth.forClass(ChannelPipeline.class).getAndIncrement(); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) @@ -72,7 +72,7 @@ public class NettyChannelPipelineInstrumentation if (callDepth > 0) { return; } - CallDepthThreadLocalMap.reset(ChannelPipeline.class); + CallDepth.forClass(ChannelPipeline.class).reset(); ChannelHandler ourHandler = null; // Server pipeline handlers diff --git a/instrumentation/netty/netty-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_1/NettyChannelPipelineInstrumentation.java b/instrumentation/netty/netty-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_1/NettyChannelPipelineInstrumentation.java index 30be5ed079..dc49b5c428 100644 --- a/instrumentation/netty/netty-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_1/NettyChannelPipelineInstrumentation.java +++ b/instrumentation/netty/netty-4.1/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/netty/v4_1/NettyChannelPipelineInstrumentation.java @@ -24,7 +24,7 @@ import io.netty.util.Attribute; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.netty.v4_1.AttributeKeys; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext; import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; import io.opentelemetry.javaagent.instrumentation.netty.common.AbstractNettyChannelPipelineInstrumentation; @@ -73,7 +73,7 @@ public class NettyChannelPipelineInstrumentation // Using the specific handler key instead of the generic ChannelPipeline.class will help us // both to handle such cases and avoid adding our additional handlers in case of internal // calls of `addLast` to other method overloads with a compatible signature. - return CallDepthThreadLocalMap.incrementCallDepth(handler.getClass()); + return CallDepth.forClass(handler.getClass()).getAndIncrement(); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) @@ -85,7 +85,7 @@ public class NettyChannelPipelineInstrumentation if (callDepth > 0) { return; } - CallDepthThreadLocalMap.reset(handler.getClass()); + CallDepth.forClass(handler.getClass()).reset(); String name = handlerName; if (name == null) { diff --git a/instrumentation/okhttp/okhttp-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/okhttp/v3_0/OkHttp3Instrumentation.java b/instrumentation/okhttp/okhttp-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/okhttp/v3_0/OkHttp3Instrumentation.java index d83bb0bf77..11333f790c 100644 --- a/instrumentation/okhttp/okhttp-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/okhttp/v3_0/OkHttp3Instrumentation.java +++ b/instrumentation/okhttp/okhttp-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/okhttp/v3_0/OkHttp3Instrumentation.java @@ -10,7 +10,7 @@ import static net.bytebuddy.matcher.ElementMatchers.named; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.matcher.ElementMatcher; @@ -33,7 +33,7 @@ public class OkHttp3Instrumentation implements TypeInstrumentation { @Advice.OnMethodEnter(suppress = Throwable.class) public static void trackCallDepth(@Advice.Local("callDepth") int callDepth) { - callDepth = CallDepthThreadLocalMap.incrementCallDepth(OkHttpClient.Builder.class); + callDepth = CallDepth.forClass(OkHttpClient.Builder.class).getAndIncrement(); } @Advice.OnMethodExit(suppress = Throwable.class) @@ -44,7 +44,7 @@ public class OkHttp3Instrumentation implements TypeInstrumentation { if (callDepth > 0) { return; } - CallDepthThreadLocalMap.reset(OkHttpClient.Builder.class); + CallDepth.forClass(OkHttpClient.Builder.class).reset(); if (builder.interceptors().contains(OkHttp3Interceptors.TRACING_INTERCEPTOR)) { return; } diff --git a/instrumentation/rabbitmq-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/RabbitChannelInstrumentation.java b/instrumentation/rabbitmq-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/RabbitChannelInstrumentation.java index d7fb3a315e..1b72e3f9f0 100644 --- a/instrumentation/rabbitmq-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/RabbitChannelInstrumentation.java +++ b/instrumentation/rabbitmq-2.7/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rabbitmq/RabbitChannelInstrumentation.java @@ -31,7 +31,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; import io.opentelemetry.semconv.trace.attributes.SemanticAttributes; import java.io.IOException; @@ -94,7 +94,7 @@ public class RabbitChannelInstrumentation implements TypeInstrumentation { @Advice.Origin("Channel.#m") String method, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - int callDepth = CallDepthThreadLocalMap.incrementCallDepth(Channel.class); + int callDepth = CallDepth.forClass(Channel.class).getAndIncrement(); if (callDepth > 0) { return; } @@ -113,7 +113,7 @@ public class RabbitChannelInstrumentation implements TypeInstrumentation { return; } scope.close(); - CallDepthThreadLocalMap.reset(Channel.class); + CallDepth.forClass(Channel.class).reset(); CURRENT_RABBIT_CONTEXT.remove(); if (throwable != null) { @@ -181,7 +181,7 @@ public class RabbitChannelInstrumentation implements TypeInstrumentation { @Advice.OnMethodEnter public static long takeTimestamp(@Advice.Local("callDepth") int callDepth) { - callDepth = CallDepthThreadLocalMap.incrementCallDepth(Channel.class); + callDepth = CallDepth.forClass(Channel.class).getAndIncrement(); return System.currentTimeMillis(); } @@ -196,7 +196,7 @@ public class RabbitChannelInstrumentation implements TypeInstrumentation { if (callDepth > 0) { return; } - CallDepthThreadLocalMap.reset(Channel.class); + CallDepth.forClass(Channel.class).reset(); // can't create span and put into scope in method enter above, because can't add parent after // span creation diff --git a/instrumentation/reactor-netty/reactor-netty-0.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v0_9/HttpClientInstrumentation.java b/instrumentation/reactor-netty/reactor-netty-0.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v0_9/HttpClientInstrumentation.java index 6d38ead122..fa043679b0 100644 --- a/instrumentation/reactor-netty/reactor-netty-0.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v0_9/HttpClientInstrumentation.java +++ b/instrumentation/reactor-netty/reactor-netty-0.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v0_9/HttpClientInstrumentation.java @@ -14,7 +14,7 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArguments; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import java.util.function.BiConsumer; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; @@ -75,14 +75,14 @@ public class HttpClientInstrumentation implements TypeInstrumentation { @Advice.OnMethodEnter(suppress = Throwable.class) public static void onEnter() { - CallDepthThreadLocalMap.incrementCallDepth(HttpClient.class); + CallDepth.forClass(HttpClient.class).getAndIncrement(); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void stopSpan( @Advice.Thrown Throwable throwable, @Advice.Return(readOnly = false) HttpClient client) { - if (CallDepthThreadLocalMap.decrementCallDepth(HttpClient.class) == 0 && throwable == null) { + if (CallDepth.forClass(HttpClient.class).decrementAndGet() == 0 && throwable == null) { client = client.doOnRequest(new OnRequest()).mapConnect(new MapConnect()); } } diff --git a/instrumentation/reactor-netty/reactor-netty-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v1_0/HttpClientInstrumentation.java b/instrumentation/reactor-netty/reactor-netty-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v1_0/HttpClientInstrumentation.java index 4aeec7c917..67c3f82481 100644 --- a/instrumentation/reactor-netty/reactor-netty-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v1_0/HttpClientInstrumentation.java +++ b/instrumentation/reactor-netty/reactor-netty-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/reactornetty/v1_0/HttpClientInstrumentation.java @@ -14,7 +14,7 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArguments; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import java.util.function.BiConsumer; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; @@ -75,14 +75,14 @@ public class HttpClientInstrumentation implements TypeInstrumentation { @Advice.OnMethodEnter(suppress = Throwable.class) public static void onEnter() { - CallDepthThreadLocalMap.incrementCallDepth(HttpClient.class); + CallDepth.forClass(HttpClient.class).getAndIncrement(); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void stopSpan( @Advice.Thrown Throwable throwable, @Advice.Return(readOnly = false) HttpClient client) { - if (CallDepthThreadLocalMap.decrementCallDepth(HttpClient.class) == 0 && throwable == null) { + if (CallDepth.forClass(HttpClient.class).decrementAndGet() == 0 && throwable == null) { client = client.doOnRequest(new OnRequest()).mapConnect(new MapConnect()); } } diff --git a/instrumentation/rmi/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rmi/server/RemoteServerInstrumentation.java b/instrumentation/rmi/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rmi/server/RemoteServerInstrumentation.java index 27be497d24..7cc862eba0 100644 --- a/instrumentation/rmi/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rmi/server/RemoteServerInstrumentation.java +++ b/instrumentation/rmi/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rmi/server/RemoteServerInstrumentation.java @@ -18,7 +18,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import java.lang.reflect.Method; import java.rmi.server.RemoteServer; import net.bytebuddy.asm.Advice; @@ -46,7 +46,7 @@ public class RemoteServerInstrumentation implements TypeInstrumentation { @Advice.Origin Method method, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - int callDepth = CallDepthThreadLocalMap.incrementCallDepth(RemoteServer.class); + int callDepth = CallDepth.forClass(RemoteServer.class).getAndIncrement(); if (callDepth > 0) { return; } @@ -68,7 +68,7 @@ public class RemoteServerInstrumentation implements TypeInstrumentation { } scope.close(); - CallDepthThreadLocalMap.reset(RemoteServer.class); + CallDepth.forClass(RemoteServer.class).reset(); if (throwable != null) { RmiServerTracer.tracer().endExceptionally(context, throwable); } else { diff --git a/instrumentation/servlet/servlet-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v2_2/Servlet2Advice.java b/instrumentation/servlet/servlet-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v2_2/Servlet2Advice.java index 3a4eb39f75..3ed499949e 100644 --- a/instrumentation/servlet/servlet-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v2_2/Servlet2Advice.java +++ b/instrumentation/servlet/servlet-2.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v2_2/Servlet2Advice.java @@ -11,7 +11,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.servlet.AppServerBridge; import io.opentelemetry.instrumentation.servlet.v2_2.ResponseWithStatus; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext; import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; import javax.servlet.ServletRequest; @@ -30,7 +30,7 @@ public class Servlet2Advice { @Advice.Argument(value = 1, typing = Assigner.Typing.DYNAMIC) ServletResponse response, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - CallDepthThreadLocalMap.incrementCallDepth(AppServerBridge.getCallDepthKey()); + CallDepth.forClass(AppServerBridge.getCallDepthKey()).getAndIncrement(); if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) { return; @@ -62,7 +62,7 @@ public class Servlet2Advice { @Advice.Thrown Throwable throwable, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - int callDepth = CallDepthThreadLocalMap.decrementCallDepth(AppServerBridge.getCallDepthKey()); + int callDepth = CallDepth.forClass(AppServerBridge.getCallDepthKey()).decrementAndGet(); if (scope != null) { scope.close(); diff --git a/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/AsyncDispatchAdvice.java b/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/AsyncDispatchAdvice.java index cb9eb49f39..2db4d0a49e 100644 --- a/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/AsyncDispatchAdvice.java +++ b/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/AsyncDispatchAdvice.java @@ -9,7 +9,7 @@ import static io.opentelemetry.instrumentation.api.tracer.HttpServerTracer.CONTE import io.opentelemetry.api.trace.Span; import io.opentelemetry.context.Context; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; import javax.servlet.AsyncContext; import javax.servlet.ServletRequest; @@ -21,7 +21,7 @@ public class AsyncDispatchAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) public static boolean enter( @Advice.This AsyncContext context, @Advice.AllArguments Object[] args) { - int depth = CallDepthThreadLocalMap.incrementCallDepth(AsyncContext.class); + int depth = CallDepth.forClass(AsyncContext.class).getAndIncrement(); if (depth > 0) { return false; } @@ -47,7 +47,7 @@ public class AsyncDispatchAdvice { @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void exit(@Advice.Enter boolean topLevel) { if (topLevel) { - CallDepthThreadLocalMap.reset(AsyncContext.class); + CallDepth.forClass(AsyncContext.class).reset(); } } } diff --git a/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3Advice.java b/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3Advice.java index f277ce5cff..7b76cec594 100644 --- a/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3Advice.java +++ b/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3Advice.java @@ -12,7 +12,7 @@ import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.servlet.AppServerBridge; import io.opentelemetry.instrumentation.api.servlet.MappingResolver; import io.opentelemetry.instrumentation.api.tracer.ServerSpan; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext; import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; import io.opentelemetry.javaagent.instrumentation.servlet.common.service.ServletAndFilterAdviceHelper; @@ -35,7 +35,7 @@ public class Servlet3Advice { @Advice.Argument(value = 1, readOnly = false) ServletResponse response, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - CallDepthThreadLocalMap.incrementCallDepth(AppServerBridge.getCallDepthKey()); + CallDepth.forClass(AppServerBridge.getCallDepthKey()).getAndIncrement(); if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) { return; } diff --git a/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3AsyncStartAdvice.java b/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3AsyncStartAdvice.java index a10fb1c10b..0402270957 100644 --- a/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3AsyncStartAdvice.java +++ b/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3AsyncStartAdvice.java @@ -7,7 +7,7 @@ package io.opentelemetry.javaagent.instrumentation.servlet.v3_0; import static io.opentelemetry.instrumentation.servlet.v3_0.Servlet3HttpServerTracer.tracer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import javax.servlet.AsyncContext; import javax.servlet.ServletRequest; import javax.servlet.http.HttpServletRequest; @@ -19,12 +19,12 @@ public class Servlet3AsyncStartAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) public static void startAsyncEnter() { // This allows to detect the outermost invocation of startAsync in method exit - CallDepthThreadLocalMap.incrementCallDepth(AsyncContext.class); + CallDepth.forClass(AsyncContext.class).getAndIncrement(); } @Advice.OnMethodExit(suppress = Throwable.class) public static void startAsyncExit(@Advice.This ServletRequest servletRequest) { - int callDepth = CallDepthThreadLocalMap.decrementCallDepth(AsyncContext.class); + int callDepth = CallDepth.forClass(AsyncContext.class).decrementAndGet(); if (callDepth != 0) { // This is not the outermost invocation, ignore. diff --git a/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/async/AsyncDispatchAdvice.java b/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/async/AsyncDispatchAdvice.java index 1d9b2eb70f..00154b59ad 100644 --- a/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/async/AsyncDispatchAdvice.java +++ b/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/async/AsyncDispatchAdvice.java @@ -9,7 +9,7 @@ import static io.opentelemetry.instrumentation.api.tracer.HttpServerTracer.CONTE import io.opentelemetry.api.trace.Span; import io.opentelemetry.context.Context; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; import jakarta.servlet.AsyncContext; import jakarta.servlet.ServletRequest; @@ -21,7 +21,7 @@ public class AsyncDispatchAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) public static boolean enter( @Advice.This AsyncContext context, @Advice.AllArguments Object[] args) { - int depth = CallDepthThreadLocalMap.incrementCallDepth(AsyncContext.class); + int depth = CallDepth.forClass(AsyncContext.class).getAndIncrement(); if (depth > 0) { return false; } @@ -47,7 +47,7 @@ public class AsyncDispatchAdvice { @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void exit(@Advice.Enter boolean topLevel) { if (topLevel) { - CallDepthThreadLocalMap.reset(AsyncContext.class); + CallDepth.forClass(AsyncContext.class).reset(); } } } diff --git a/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/async/AsyncStartAdvice.java b/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/async/AsyncStartAdvice.java index 6ca9d3cc36..e6c6f948d0 100644 --- a/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/async/AsyncStartAdvice.java +++ b/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/async/AsyncStartAdvice.java @@ -7,7 +7,7 @@ package io.opentelemetry.javaagent.instrumentation.servlet.v5_0.async; import static io.opentelemetry.instrumentation.servlet.jakarta.v5_0.JakartaServletHttpServerTracer.tracer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import jakarta.servlet.AsyncContext; import jakarta.servlet.http.HttpServletRequest; import net.bytebuddy.asm.Advice; @@ -19,14 +19,14 @@ public class AsyncStartAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) public static void startAsyncEnter() { // This allows to detect the outermost invocation of startAsync in method exit - CallDepthThreadLocalMap.incrementCallDepth(AsyncContext.class); + CallDepth.forClass(AsyncContext.class).getAndIncrement(); } @Advice.OnMethodExit(suppress = Throwable.class) public static void startAsyncExit( @Advice.This(typing = Assigner.Typing.DYNAMIC) HttpServletRequest request) { - int callDepth = CallDepthThreadLocalMap.decrementCallDepth(AsyncContext.class); + int callDepth = CallDepth.forClass(AsyncContext.class).decrementAndGet(); if (callDepth != 0) { // This is not the outermost invocation, ignore. diff --git a/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/response/ResponseSendAdvice.java b/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/response/ResponseSendAdvice.java index 099eec3c7c..039e8e4f69 100644 --- a/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/response/ResponseSendAdvice.java +++ b/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/response/ResponseSendAdvice.java @@ -10,7 +10,6 @@ import static io.opentelemetry.javaagent.instrumentation.servlet.v5_0.response.R import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.javaagent.instrumentation.api.CallDepth; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; import io.opentelemetry.javaagent.instrumentation.servlet.common.response.HttpServletResponseAdviceHelper; import jakarta.servlet.http.HttpServletResponse; @@ -26,7 +25,7 @@ public class ResponseSendAdvice { @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope, @Advice.Local("otelCallDepth") CallDepth callDepth) { - callDepth = CallDepthThreadLocalMap.getCallDepth(HttpServletResponse.class); + callDepth = CallDepth.forClass(HttpServletResponse.class); // Don't want to generate a new top-level span if (callDepth.getAndIncrement() == 0 && Java8BytecodeBridge.currentSpan().getSpanContext().isValid()) { diff --git a/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/service/JakartaServletServiceAdvice.java b/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/service/JakartaServletServiceAdvice.java index 2579612c0d..33d10e9023 100644 --- a/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/service/JakartaServletServiceAdvice.java +++ b/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/service/JakartaServletServiceAdvice.java @@ -12,7 +12,7 @@ import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.servlet.AppServerBridge; import io.opentelemetry.instrumentation.api.servlet.MappingResolver; import io.opentelemetry.instrumentation.api.tracer.ServerSpan; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext; import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; import io.opentelemetry.javaagent.instrumentation.servlet.common.service.ServletAndFilterAdviceHelper; @@ -36,7 +36,7 @@ public class JakartaServletServiceAdvice { @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - CallDepthThreadLocalMap.incrementCallDepth(AppServerBridge.getCallDepthKey()); + CallDepth.forClass(AppServerBridge.getCallDepthKey()).getAndIncrement(); if (!(request instanceof HttpServletRequest) || !(response instanceof HttpServletResponse)) { return; } diff --git a/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/common/service/ServletAndFilterAdviceHelper.java b/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/common/service/ServletAndFilterAdviceHelper.java index a5c2645dbe..8a572a8d31 100644 --- a/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/common/service/ServletAndFilterAdviceHelper.java +++ b/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/common/service/ServletAndFilterAdviceHelper.java @@ -9,7 +9,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.instrumentation.api.servlet.AppServerBridge; import io.opentelemetry.instrumentation.servlet.ServletHttpServerTracer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; public class ServletAndFilterAdviceHelper { @@ -20,7 +20,7 @@ public class ServletAndFilterAdviceHelper { Throwable throwable, Context context, Scope scope) { - int callDepth = CallDepthThreadLocalMap.decrementCallDepth(AppServerBridge.getCallDepthKey()); + int callDepth = CallDepth.forClass(AppServerBridge.getCallDepthKey()).decrementAndGet(); if (scope != null) { scope.close(); diff --git a/instrumentation/servlet/servlet-javax-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/javax/response/ResponseSendAdvice.java b/instrumentation/servlet/servlet-javax-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/javax/response/ResponseSendAdvice.java index 19abd9af9e..ec1f57cbef 100644 --- a/instrumentation/servlet/servlet-javax-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/javax/response/ResponseSendAdvice.java +++ b/instrumentation/servlet/servlet-javax-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/javax/response/ResponseSendAdvice.java @@ -10,7 +10,6 @@ import static io.opentelemetry.javaagent.instrumentation.servlet.javax.response. import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.javaagent.instrumentation.api.CallDepth; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge; import io.opentelemetry.javaagent.instrumentation.servlet.common.response.HttpServletResponseAdviceHelper; import java.lang.reflect.Method; @@ -26,7 +25,7 @@ public class ResponseSendAdvice { @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope, @Advice.Local("otelCallDepth") CallDepth callDepth) { - callDepth = CallDepthThreadLocalMap.getCallDepth(HttpServletResponse.class); + callDepth = CallDepth.forClass(HttpServletResponse.class); // Don't want to generate a new top-level span if (callDepth.getAndIncrement() == 0 && Java8BytecodeBridge.currentSpan().getSpanContext().isValid()) { diff --git a/instrumentation/spring/spring-ws-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/ws/AnnotatedMethodInstrumentation.java b/instrumentation/spring/spring-ws-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/ws/AnnotatedMethodInstrumentation.java index b1896b468e..c560363f9d 100644 --- a/instrumentation/spring/spring-ws-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/ws/AnnotatedMethodInstrumentation.java +++ b/instrumentation/spring/spring-ws-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spring/ws/AnnotatedMethodInstrumentation.java @@ -16,7 +16,7 @@ import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; +import io.opentelemetry.javaagent.instrumentation.api.CallDepth; import java.lang.reflect.Method; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; @@ -56,7 +56,7 @@ public class AnnotatedMethodInstrumentation implements TypeInstrumentation { @Advice.Origin Method method, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - if (CallDepthThreadLocalMap.incrementCallDepth(PayloadRoot.class) > 0) { + if (CallDepth.forClass(PayloadRoot.class).getAndIncrement() > 0) { return; } context = tracer().startSpan(method); @@ -71,7 +71,7 @@ public class AnnotatedMethodInstrumentation implements TypeInstrumentation { if (scope == null) { return; } - CallDepthThreadLocalMap.reset(PayloadRoot.class); + CallDepth.forClass(PayloadRoot.class).reset(); scope.close(); if (throwable == null) { diff --git a/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/MemcachedClientInstrumentation.java b/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/MemcachedClientInstrumentation.java index 7829f8d871..9f727748e8 100644 --- a/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/MemcachedClientInstrumentation.java +++ b/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/MemcachedClientInstrumentation.java @@ -16,7 +16,6 @@ import static net.bytebuddy.matcher.ElementMatchers.returns; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; import io.opentelemetry.javaagent.instrumentation.api.CallDepth; -import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.matcher.ElementMatcher; @@ -57,7 +56,7 @@ public class MemcachedClientInstrumentation implements TypeInstrumentation { @Advice.OnMethodEnter(suppress = Throwable.class) public static void trackCallDepth(@Advice.Local("otelCallDepth") CallDepth callDepth) { - callDepth = CallDepthThreadLocalMap.getCallDepth(MemcachedClient.class); + callDepth = CallDepth.forClass(MemcachedClient.class); callDepth.getAndIncrement(); } @@ -84,7 +83,7 @@ public class MemcachedClientInstrumentation implements TypeInstrumentation { @Advice.OnMethodEnter(suppress = Throwable.class) public static void trackCallDepth(@Advice.Local("otelCallDepth") CallDepth callDepth) { - callDepth = CallDepthThreadLocalMap.getCallDepth(MemcachedClient.class); + callDepth = CallDepth.forClass(MemcachedClient.class); callDepth.getAndIncrement(); } @@ -111,7 +110,7 @@ public class MemcachedClientInstrumentation implements TypeInstrumentation { @Advice.OnMethodEnter(suppress = Throwable.class) public static void trackCallDepth(@Advice.Local("otelCallDepth") CallDepth callDepth) { - callDepth = CallDepthThreadLocalMap.getCallDepth(MemcachedClient.class); + callDepth = CallDepth.forClass(MemcachedClient.class); callDepth.getAndIncrement(); } @@ -141,7 +140,7 @@ public class MemcachedClientInstrumentation implements TypeInstrumentation { @Advice.This MemcachedClient client, @Advice.Origin("#m") String methodName, @Advice.Local("otelCallDepth") CallDepth callDepth) { - callDepth = CallDepthThreadLocalMap.getCallDepth(MemcachedClient.class); + callDepth = CallDepth.forClass(MemcachedClient.class); if (callDepth.getAndIncrement() > 0) { return null; } diff --git a/javaagent-api/src/main/java/io/opentelemetry/javaagent/instrumentation/api/CallDepth.java b/javaagent-api/src/main/java/io/opentelemetry/javaagent/instrumentation/api/CallDepth.java index 999a87f76b..10002d32a7 100644 --- a/javaagent-api/src/main/java/io/opentelemetry/javaagent/instrumentation/api/CallDepth.java +++ b/javaagent-api/src/main/java/io/opentelemetry/javaagent/instrumentation/api/CallDepth.java @@ -5,17 +5,51 @@ package io.opentelemetry.javaagent.instrumentation.api; +/** + * A utility to track nested calls in an instrumentation. + * + *

For example, this can be used to track nested calls to {@code super()} in constructors by + * calling {@link #getAndIncrement()} at the beginning of each constructor. + * + *

This works the following way: when you enter some method that you want to track, you call + * {@link #getAndIncrement()} method. If returned number is larger than 0, then you have already + * been in this method and are in recursive call now. When you then leave the method, you call + * {@link #decrementAndGet()} method. If returned number is larger than 0, then you have already + * been in this method and are in recursive call now. + * + *

In short, the semantic of both methods is the same: they will return value 0 if and only if + * current method invocation is the first one for the current call stack. + */ public final class CallDepth { + private int depth; CallDepth() { this.depth = 0; } + /** + * Return the current call depth for a given class (not method; we want to be able to track calls + * between different methods in a class). + * + *

The returned instance is unique per given class and per thread. + */ + public static CallDepth forClass(Class cls) { + return CallDepthThreadLocalMap.getCallDepth(cls); + } + + /** + * Increment the current call depth and return the previous value. This method will always return + * 0 if it's the first (outermost) call. + */ public int getAndIncrement() { return this.depth++; } + /** + * Decrement the current call depth and return the current value. This method will always return 0 + * if it's the last (outermost) call. + */ public int decrementAndGet() { return --this.depth; } @@ -28,6 +62,7 @@ public final class CallDepth { return depth; } + /** Reset the call depth to its initial value. */ public void reset() { depth = 0; } diff --git a/javaagent-api/src/main/java/io/opentelemetry/javaagent/instrumentation/api/CallDepthThreadLocalMap.java b/javaagent-api/src/main/java/io/opentelemetry/javaagent/instrumentation/api/CallDepthThreadLocalMap.java index 43d83adccc..e958ba71c6 100644 --- a/javaagent-api/src/main/java/io/opentelemetry/javaagent/instrumentation/api/CallDepthThreadLocalMap.java +++ b/javaagent-api/src/main/java/io/opentelemetry/javaagent/instrumentation/api/CallDepthThreadLocalMap.java @@ -5,22 +5,7 @@ package io.opentelemetry.javaagent.instrumentation.api; -/** - * Utility to track nested instrumentation. - * - *

For example, this can be used to track nested calls to super() in constructors by calling - * #incrementCallDepth at the beginning of each constructor. - * - *

This works the following way. When you enter some method that you want to track, you call - * {@link #incrementCallDepth} method. If returned number is larger than 0, then you have already - * been in this method and are in recursive call now. When you then leave the method, you call - * {@link #decrementCallDepth} method. If returned number is larger than 0, then you have already - * been in this method and are in recursive call now. - * - *

In short, the semantic of both methods is the same: they will return value 0 if and only if - * current method invocation is the first one for the current call stack. - */ -public final class CallDepthThreadLocalMap { +final class CallDepthThreadLocalMap { private static final ClassValue TLS = new ClassValue() { @@ -30,22 +15,10 @@ public final class CallDepthThreadLocalMap { } }; - public static CallDepth getCallDepth(Class k) { + static CallDepth getCallDepth(Class k) { return TLS.get(k).get(); } - public static int incrementCallDepth(Class k) { - return TLS.get(k).get().getAndIncrement(); - } - - public static int decrementCallDepth(Class k) { - return TLS.get(k).get().decrementAndGet(); - } - - public static void reset(Class k) { - TLS.get(k).get().reset(); - } - private static final class ThreadLocalDepth extends ThreadLocal { @Override protected CallDepth initialValue() { diff --git a/javaagent-api/src/test/java/io/opentelemetry/javaagent/instrumentation/api/CallDepthTest.java b/javaagent-api/src/test/java/io/opentelemetry/javaagent/instrumentation/api/CallDepthTest.java new file mode 100644 index 0000000000..b6090b1a1b --- /dev/null +++ b/javaagent-api/src/test/java/io/opentelemetry/javaagent/instrumentation/api/CallDepthTest.java @@ -0,0 +1,42 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.api; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; + +class CallDepthTest { + + @Test + void incrementDecrement() { + assertThat(CallDepth.forClass(String.class).getAndIncrement()).isZero(); + assertThat(CallDepth.forClass(Integer.class).getAndIncrement()).isZero(); + + assertThat(CallDepth.forClass(String.class).getAndIncrement()).isOne(); + assertThat(CallDepth.forClass(Integer.class).getAndIncrement()).isOne(); + + CallDepth.forClass(String.class).reset(); + assertThat(CallDepth.forClass(Integer.class).getAndIncrement()).isEqualTo(2); + + CallDepth.forClass(Integer.class).reset(); + + assertThat(CallDepth.forClass(String.class).getAndIncrement()).isZero(); + assertThat(CallDepth.forClass(Integer.class).getAndIncrement()).isZero(); + + assertThat(CallDepth.forClass(String.class).getAndIncrement()).isOne(); + assertThat(CallDepth.forClass(Integer.class).getAndIncrement()).isOne(); + + assertThat(CallDepth.forClass(String.class).decrementAndGet()).isOne(); + assertThat(CallDepth.forClass(Integer.class).decrementAndGet()).isOne(); + + assertThat(CallDepth.forClass(String.class).decrementAndGet()).isZero(); + assertThat(CallDepth.forClass(Integer.class).decrementAndGet()).isZero(); + + assertThat(CallDepth.forClass(Double.class).getAndIncrement()).isZero(); + assertThat(CallDepth.forClass(Double.class).decrementAndGet()).isZero(); + } +} diff --git a/javaagent-api/src/test/java/io/opentelemetry/javaagent/instrumentation/api/CallDepthThreadLocalMapTest.java b/javaagent-api/src/test/java/io/opentelemetry/javaagent/instrumentation/api/CallDepthThreadLocalMapTest.java deleted file mode 100644 index 9b255d714a..0000000000 --- a/javaagent-api/src/test/java/io/opentelemetry/javaagent/instrumentation/api/CallDepthThreadLocalMapTest.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.javaagent.instrumentation.api; - -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.jupiter.api.Test; - -class CallDepthThreadLocalMapTest { - - @Test - void incrementDecrement() { - assertThat(CallDepthThreadLocalMap.incrementCallDepth(String.class)).isZero(); - assertThat(CallDepthThreadLocalMap.incrementCallDepth(Integer.class)).isZero(); - - assertThat(CallDepthThreadLocalMap.incrementCallDepth(String.class)).isOne(); - assertThat(CallDepthThreadLocalMap.incrementCallDepth(Integer.class)).isOne(); - - CallDepthThreadLocalMap.reset(String.class); - assertThat(CallDepthThreadLocalMap.incrementCallDepth(Integer.class)).isEqualTo(2); - - CallDepthThreadLocalMap.reset(Integer.class); - - assertThat(CallDepthThreadLocalMap.incrementCallDepth(String.class)).isZero(); - assertThat(CallDepthThreadLocalMap.incrementCallDepth(Integer.class)).isZero(); - - assertThat(CallDepthThreadLocalMap.incrementCallDepth(String.class)).isOne(); - assertThat(CallDepthThreadLocalMap.incrementCallDepth(Integer.class)).isOne(); - - assertThat(CallDepthThreadLocalMap.decrementCallDepth(String.class)).isOne(); - assertThat(CallDepthThreadLocalMap.decrementCallDepth(Integer.class)).isOne(); - - assertThat(CallDepthThreadLocalMap.decrementCallDepth(String.class)).isZero(); - assertThat(CallDepthThreadLocalMap.decrementCallDepth(Integer.class)).isZero(); - - assertThat(CallDepthThreadLocalMap.incrementCallDepth(Double.class)).isZero(); - assertThat(CallDepthThreadLocalMap.decrementCallDepth(Double.class)).isZero(); - } -}