Reduce CallDepth classes' API surface (#3497)

This commit is contained in:
Mateusz Rzeszutek 2021-07-06 09:24:19 +02:00 committed by GitHub
parent 0299428017
commit b9fcb6b498
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
39 changed files with 181 additions and 177 deletions

View File

@ -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

View File

@ -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));
}

View File

@ -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();
}
}
}

View File

@ -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.
}

View File

@ -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.
}

View File

@ -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;

View File

@ -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

View File

@ -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;
}

View File

@ -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) {

View File

@ -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);

View File

@ -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) {

View File

@ -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) {

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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)

View File

@ -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

View File

@ -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) {

View File

@ -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;
}

View File

@ -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

View File

@ -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());
}
}

View File

@ -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());
}
}

View File

@ -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 {

View File

@ -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();

View File

@ -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();
}
}
}

View File

@ -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;
}

View File

@ -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.

View File

@ -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();
}
}
}

View File

@ -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.

View File

@ -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()) {

View File

@ -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;
}

View File

@ -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();

View File

@ -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()) {

View File

@ -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) {

View File

@ -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;
}

View File

@ -5,17 +5,51 @@
package io.opentelemetry.javaagent.instrumentation.api;
/**
* A utility to track nested calls in an instrumentation.
*
* <p>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.
*
* <p>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.
*
* <p>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).
*
* <p>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;
}

View File

@ -5,22 +5,7 @@
package io.opentelemetry.javaagent.instrumentation.api;
/**
* Utility to track nested instrumentation.
*
* <p>For example, this can be used to track nested calls to super() in constructors by calling
* #incrementCallDepth at the beginning of each constructor.
*
* <p>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.
*
* <p>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<ThreadLocalDepth> TLS =
new ClassValue<ThreadLocalDepth>() {
@ -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<CallDepth> {
@Override
protected CallDepth initialValue() {

View File

@ -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();
}
}

View File

@ -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();
}
}