Skip to content

Commit a6cc2ba

Browse files
authored
Indy-ready - spymemcached (#15029)
1 parent 03da778 commit a6cc2ba

File tree

2 files changed

+55
-27
lines changed

2 files changed

+55
-27
lines changed

instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/MemcachedClientInstrumentation.java

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313
import static net.bytebuddy.matcher.ElementMatchers.not;
1414
import static net.bytebuddy.matcher.ElementMatchers.returns;
1515

16+
import io.opentelemetry.context.Context;
1617
import io.opentelemetry.javaagent.bootstrap.CallDepth;
1718
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1819
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
20+
import javax.annotation.Nullable;
1921
import net.bytebuddy.asm.Advice;
2022
import net.bytebuddy.description.type.TypeDescription;
2123
import net.bytebuddy.matcher.ElementMatcher;
@@ -55,17 +57,18 @@ public void transform(TypeTransformer transformer) {
5557
public static class AsyncOperationAdvice {
5658

5759
@Advice.OnMethodEnter(suppress = Throwable.class)
58-
public static void trackCallDepth(@Advice.Local("otelCallDepth") CallDepth callDepth) {
59-
callDepth = CallDepth.forClass(MemcachedClient.class);
60+
public static CallDepth trackCallDepth() {
61+
CallDepth callDepth = CallDepth.forClass(MemcachedClient.class);
6062
callDepth.getAndIncrement();
63+
return callDepth;
6164
}
6265

6366
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
6467
public static void methodExit(
6568
@Advice.This MemcachedClient client,
6669
@Advice.Origin("#m") String methodName,
6770
@Advice.Return OperationFuture<?> future,
68-
@Advice.Local("otelCallDepth") CallDepth callDepth) {
71+
@Advice.Enter CallDepth callDepth) {
6972
if (callDepth.decrementAndGet() > 0) {
7073
return;
7174
}
@@ -85,17 +88,18 @@ public static void methodExit(
8588
public static class AsyncGetAdvice {
8689

8790
@Advice.OnMethodEnter(suppress = Throwable.class)
88-
public static void trackCallDepth(@Advice.Local("otelCallDepth") CallDepth callDepth) {
89-
callDepth = CallDepth.forClass(MemcachedClient.class);
91+
public static CallDepth trackCallDepth() {
92+
CallDepth callDepth = CallDepth.forClass(MemcachedClient.class);
9093
callDepth.getAndIncrement();
94+
return callDepth;
9195
}
9296

9397
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
9498
public static void methodExit(
9599
@Advice.This MemcachedClient client,
96100
@Advice.Origin("#m") String methodName,
97101
@Advice.Return GetFuture<?> future,
98-
@Advice.Local("otelCallDepth") CallDepth callDepth) {
102+
@Advice.Enter CallDepth callDepth) {
99103
if (callDepth.decrementAndGet() > 0) {
100104
return;
101105
}
@@ -114,17 +118,18 @@ public static void methodExit(
114118
public static class AsyncBulkAdvice {
115119

116120
@Advice.OnMethodEnter(suppress = Throwable.class)
117-
public static void trackCallDepth(@Advice.Local("otelCallDepth") CallDepth callDepth) {
118-
callDepth = CallDepth.forClass(MemcachedClient.class);
121+
public static CallDepth trackCallDepth() {
122+
CallDepth callDepth = CallDepth.forClass(MemcachedClient.class);
119123
callDepth.getAndIncrement();
124+
return callDepth;
120125
}
121126

122127
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
123128
public static void methodExit(
124129
@Advice.This MemcachedClient client,
125130
@Advice.Origin("#m") String methodName,
126131
@Advice.Return BulkFuture<?> future,
127-
@Advice.Local("otelCallDepth") CallDepth callDepth) {
132+
@Advice.Enter CallDepth callDepth) {
128133
if (callDepth.decrementAndGet() > 0) {
129134
return;
130135
}
@@ -142,29 +147,45 @@ public static void methodExit(
142147
@SuppressWarnings("unused")
143148
public static class SyncOperationAdvice {
144149

145-
@Advice.OnMethodEnter(suppress = Throwable.class)
146-
public static SyncCompletionListener methodEnter(
147-
@Advice.This MemcachedClient client,
148-
@Advice.Origin("#m") String methodName,
149-
@Advice.Local("otelCallDepth") CallDepth callDepth) {
150-
callDepth = CallDepth.forClass(MemcachedClient.class);
151-
if (callDepth.getAndIncrement() > 0) {
152-
return null;
150+
public static class AdviceScope {
151+
private final CallDepth callDepth;
152+
@Nullable private final SyncCompletionListener listener;
153+
154+
private AdviceScope(CallDepth callDepth, @Nullable SyncCompletionListener listener) {
155+
this.callDepth = callDepth;
156+
this.listener = listener;
153157
}
154158

155-
return SyncCompletionListener.create(currentContext(), client.getConnection(), methodName);
159+
public static AdviceScope start(MemcachedClient client, String methodName) {
160+
CallDepth callDepth = CallDepth.forClass(MemcachedClient.class);
161+
if (callDepth.getAndIncrement() > 0) {
162+
return new AdviceScope(callDepth, null);
163+
}
164+
165+
return new AdviceScope(
166+
callDepth,
167+
SyncCompletionListener.create(Context.current(), client.getConnection(), methodName));
168+
}
169+
170+
public void end(@Nullable Throwable throwable) {
171+
if (callDepth.decrementAndGet() > 0 || listener == null) {
172+
return;
173+
}
174+
175+
listener.done(throwable);
176+
}
177+
}
178+
179+
@Advice.OnMethodEnter(suppress = Throwable.class)
180+
public static AdviceScope methodEnter(
181+
@Advice.This MemcachedClient client, @Advice.Origin("#m") String methodName) {
182+
return AdviceScope.start(client, methodName);
156183
}
157184

158185
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
159186
public static void methodExit(
160-
@Advice.Enter SyncCompletionListener listener,
161-
@Advice.Thrown Throwable thrown,
162-
@Advice.Local("otelCallDepth") CallDepth callDepth) {
163-
if (callDepth.decrementAndGet() > 0 || listener == null) {
164-
return;
165-
}
166-
167-
listener.done(thrown);
187+
@Advice.Thrown @Nullable Throwable thrown, @Advice.Enter AdviceScope adviceScope) {
188+
adviceScope.end(thrown);
168189
}
169190
}
170191
}

instrumentation/spymemcached-2.12/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/spymemcached/SpymemcachedInstrumentationModule.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,12 @@
1010
import com.google.auto.service.AutoService;
1111
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
1212
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
13+
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
1314
import java.util.List;
1415

1516
@AutoService(InstrumentationModule.class)
16-
public class SpymemcachedInstrumentationModule extends InstrumentationModule {
17+
public class SpymemcachedInstrumentationModule extends InstrumentationModule
18+
implements ExperimentalInstrumentationModule {
1719

1820
public SpymemcachedInstrumentationModule() {
1921
super("spymemcached", "spymemcached-2.12");
@@ -23,4 +25,9 @@ public SpymemcachedInstrumentationModule() {
2325
public List<TypeInstrumentation> typeInstrumentations() {
2426
return singletonList(new MemcachedClientInstrumentation());
2527
}
28+
29+
@Override
30+
public boolean isIndyReady() {
31+
return true;
32+
}
2633
}

0 commit comments

Comments
 (0)