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 0b7f75f865b4..645579db34f2 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 @@ -13,9 +13,11 @@ import static net.bytebuddy.matcher.ElementMatchers.not; import static net.bytebuddy.matcher.ElementMatchers.returns; +import io.opentelemetry.context.Context; import io.opentelemetry.javaagent.bootstrap.CallDepth; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; +import javax.annotation.Nullable; import net.bytebuddy.asm.Advice; import net.bytebuddy.description.type.TypeDescription; import net.bytebuddy.matcher.ElementMatcher; @@ -55,9 +57,10 @@ public void transform(TypeTransformer transformer) { public static class AsyncOperationAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) - public static void trackCallDepth(@Advice.Local("otelCallDepth") CallDepth callDepth) { - callDepth = CallDepth.forClass(MemcachedClient.class); + public static CallDepth trackCallDepth() { + CallDepth callDepth = CallDepth.forClass(MemcachedClient.class); callDepth.getAndIncrement(); + return callDepth; } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) @@ -65,7 +68,7 @@ public static void methodExit( @Advice.This MemcachedClient client, @Advice.Origin("#m") String methodName, @Advice.Return OperationFuture future, - @Advice.Local("otelCallDepth") CallDepth callDepth) { + @Advice.Enter CallDepth callDepth) { if (callDepth.decrementAndGet() > 0) { return; } @@ -85,9 +88,10 @@ public static void methodExit( public static class AsyncGetAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) - public static void trackCallDepth(@Advice.Local("otelCallDepth") CallDepth callDepth) { - callDepth = CallDepth.forClass(MemcachedClient.class); + public static CallDepth trackCallDepth() { + CallDepth callDepth = CallDepth.forClass(MemcachedClient.class); callDepth.getAndIncrement(); + return callDepth; } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) @@ -95,7 +99,7 @@ public static void methodExit( @Advice.This MemcachedClient client, @Advice.Origin("#m") String methodName, @Advice.Return GetFuture future, - @Advice.Local("otelCallDepth") CallDepth callDepth) { + @Advice.Enter CallDepth callDepth) { if (callDepth.decrementAndGet() > 0) { return; } @@ -114,9 +118,10 @@ public static void methodExit( public static class AsyncBulkAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) - public static void trackCallDepth(@Advice.Local("otelCallDepth") CallDepth callDepth) { - callDepth = CallDepth.forClass(MemcachedClient.class); + public static CallDepth trackCallDepth() { + CallDepth callDepth = CallDepth.forClass(MemcachedClient.class); callDepth.getAndIncrement(); + return callDepth; } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) @@ -124,7 +129,7 @@ public static void methodExit( @Advice.This MemcachedClient client, @Advice.Origin("#m") String methodName, @Advice.Return BulkFuture future, - @Advice.Local("otelCallDepth") CallDepth callDepth) { + @Advice.Enter CallDepth callDepth) { if (callDepth.decrementAndGet() > 0) { return; } @@ -142,29 +147,45 @@ public static void methodExit( @SuppressWarnings("unused") public static class SyncOperationAdvice { - @Advice.OnMethodEnter(suppress = Throwable.class) - public static SyncCompletionListener methodEnter( - @Advice.This MemcachedClient client, - @Advice.Origin("#m") String methodName, - @Advice.Local("otelCallDepth") CallDepth callDepth) { - callDepth = CallDepth.forClass(MemcachedClient.class); - if (callDepth.getAndIncrement() > 0) { - return null; + public static class AdviceScope { + private final CallDepth callDepth; + @Nullable private final SyncCompletionListener listener; + + private AdviceScope(CallDepth callDepth, @Nullable SyncCompletionListener listener) { + this.callDepth = callDepth; + this.listener = listener; } - return SyncCompletionListener.create(currentContext(), client.getConnection(), methodName); + public static AdviceScope start(MemcachedClient client, String methodName) { + CallDepth callDepth = CallDepth.forClass(MemcachedClient.class); + if (callDepth.getAndIncrement() > 0) { + return new AdviceScope(callDepth, null); + } + + return new AdviceScope( + callDepth, + SyncCompletionListener.create(Context.current(), client.getConnection(), methodName)); + } + + public void end(@Nullable Throwable throwable) { + if (callDepth.decrementAndGet() > 0 || listener == null) { + return; + } + + listener.done(throwable); + } + } + + @Advice.OnMethodEnter(suppress = Throwable.class) + public static AdviceScope methodEnter( + @Advice.This MemcachedClient client, @Advice.Origin("#m") String methodName) { + return AdviceScope.start(client, methodName); } @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) public static void methodExit( - @Advice.Enter SyncCompletionListener listener, - @Advice.Thrown Throwable thrown, - @Advice.Local("otelCallDepth") CallDepth callDepth) { - if (callDepth.decrementAndGet() > 0 || listener == null) { - return; - } - - listener.done(thrown); + @Advice.Thrown @Nullable Throwable thrown, @Advice.Enter AdviceScope adviceScope) { + adviceScope.end(thrown); } } } diff --git a/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/SpymemcachedInstrumentationModule.java b/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/SpymemcachedInstrumentationModule.java index d7a6b20a6842..6f150ef5ef6c 100644 --- a/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/SpymemcachedInstrumentationModule.java +++ b/instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/SpymemcachedInstrumentationModule.java @@ -10,10 +10,12 @@ import com.google.auto.service.AutoService; import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; +import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule; import java.util.List; @AutoService(InstrumentationModule.class) -public class SpymemcachedInstrumentationModule extends InstrumentationModule { +public class SpymemcachedInstrumentationModule extends InstrumentationModule + implements ExperimentalInstrumentationModule { public SpymemcachedInstrumentationModule() { super("spymemcached", "spymemcached-2.12"); @@ -23,4 +25,9 @@ public SpymemcachedInstrumentationModule() { public List typeInstrumentations() { return singletonList(new MemcachedClientInstrumentation()); } + + @Override + public boolean isIndyReady() { + return true; + } }