Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
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;
Expand Down Expand Up @@ -55,17 +56,18 @@ 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)
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;
}
Expand All @@ -85,17 +87,18 @@ 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)
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;
}
Expand All @@ -114,17 +117,18 @@ 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)
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;
}
Expand All @@ -142,29 +146,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(currentContext(), client.getConnection(), methodName));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use Context.current()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it. It's updated now.

}

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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -23,4 +25,9 @@ public SpymemcachedInstrumentationModule() {
public List<TypeInstrumentation> typeInstrumentations() {
return singletonList(new MemcachedClientInstrumentation());
}

@Override
public boolean isIndyReady() {
return true;
}
}
Loading