Skip to content

Commit 2dfa1b0

Browse files
authored
Indy-ready - rediscala (#15077)
1 parent efb8dee commit 2dfa1b0

File tree

2 files changed

+61
-37
lines changed

2 files changed

+61
-37
lines changed

instrumentation/rediscala-1.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rediscala/RediscalaInstrumentationModule.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 RediscalaInstrumentationModule extends InstrumentationModule {
17+
public class RediscalaInstrumentationModule extends InstrumentationModule
18+
implements ExperimentalInstrumentationModule {
1719

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

instrumentation/rediscala-1.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/rediscala/RequestInstrumentation.java

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package io.opentelemetry.javaagent.instrumentation.rediscala;
77

8-
import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext;
98
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
109
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasSuperType;
1110
import static io.opentelemetry.javaagent.instrumentation.rediscala.RediscalaSingletons.instrumenter;
@@ -20,6 +19,7 @@
2019
import io.opentelemetry.context.Scope;
2120
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2221
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
22+
import javax.annotation.Nullable;
2323
import net.bytebuddy.asm.Advice;
2424
import net.bytebuddy.description.type.TypeDescription;
2525
import net.bytebuddy.matcher.ElementMatcher;
@@ -61,50 +61,67 @@ public void transform(TypeTransformer transformer) {
6161
@SuppressWarnings("unused")
6262
public static class SendAdvice {
6363

64-
@Advice.OnMethodEnter(suppress = Throwable.class)
65-
public static void onEnter(
66-
@Advice.Argument(0) RedisCommand<?, ?> cmd,
67-
@Advice.Local("otelContext") Context context,
68-
@Advice.Local("otelScope") Scope scope) {
64+
public static class AdviceScope {
65+
private final Context context;
66+
private final Scope scope;
67+
68+
private AdviceScope(Context context, Scope scope) {
69+
this.context = context;
70+
this.scope = scope;
71+
}
6972

70-
Context parentContext = currentContext();
71-
if (!instrumenter().shouldStart(parentContext, cmd)) {
72-
return;
73+
@Nullable
74+
public static AdviceScope start(RedisCommand<?, ?> cmd) {
75+
Context parentContext = Context.current();
76+
if (!instrumenter().shouldStart(parentContext, cmd)) {
77+
return null;
78+
}
79+
80+
Context context = instrumenter().start(parentContext, cmd);
81+
return new AdviceScope(context, context.makeCurrent());
7382
}
7483

75-
context = instrumenter().start(parentContext, cmd);
76-
scope = context.makeCurrent();
84+
public void end(
85+
Object action,
86+
RedisCommand<?, ?> cmd,
87+
Future<Object> responseFuture,
88+
Throwable throwable) {
89+
scope.close();
90+
91+
ExecutionContext ctx = null;
92+
if (action instanceof ActorRequest) {
93+
ctx = ((ActorRequest) action).executionContext();
94+
} else if (action instanceof Request) {
95+
ctx = ((Request) action).executionContext();
96+
} else if (action instanceof BufferedRequest) {
97+
ctx = ((BufferedRequest) action).executionContext();
98+
} else if (action instanceof RoundRobinPoolRequest) {
99+
ctx = ((RoundRobinPoolRequest) action).executionContext();
100+
}
101+
102+
if (throwable != null) {
103+
instrumenter().end(context, cmd, null, throwable);
104+
} else {
105+
responseFuture.onComplete(new OnCompleteHandler(context, cmd), ctx);
106+
}
107+
}
108+
}
109+
110+
@Nullable
111+
@Advice.OnMethodEnter(suppress = Throwable.class)
112+
public static AdviceScope onEnter(@Advice.Argument(0) RedisCommand<?, ?> cmd) {
113+
return AdviceScope.start(cmd);
77114
}
78115

79116
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
80117
public static void onExit(
81118
@Advice.This Object action,
82119
@Advice.Argument(0) RedisCommand<?, ?> cmd,
83-
@Advice.Local("otelContext") Context context,
84-
@Advice.Local("otelScope") Scope scope,
85-
@Advice.Thrown Throwable throwable,
86-
@Advice.Return(readOnly = false) Future<Object> responseFuture) {
87-
88-
if (scope == null) {
89-
return;
90-
}
91-
scope.close();
92-
93-
ExecutionContext ctx = null;
94-
if (action instanceof ActorRequest) {
95-
ctx = ((ActorRequest) action).executionContext();
96-
} else if (action instanceof Request) {
97-
ctx = ((Request) action).executionContext();
98-
} else if (action instanceof BufferedRequest) {
99-
ctx = ((BufferedRequest) action).executionContext();
100-
} else if (action instanceof RoundRobinPoolRequest) {
101-
ctx = ((RoundRobinPoolRequest) action).executionContext();
102-
}
103-
104-
if (throwable != null) {
105-
instrumenter().end(context, cmd, null, throwable);
106-
} else {
107-
responseFuture.onComplete(new OnCompleteHandler(context, cmd), ctx);
120+
@Advice.Enter @Nullable AdviceScope adviceScope,
121+
@Advice.Thrown @Nullable Throwable throwable,
122+
@Advice.Return Future<Object> responseFuture) {
123+
if (adviceScope != null) {
124+
adviceScope.end(action, cmd, responseFuture, throwable);
108125
}
109126
}
110127
}

0 commit comments

Comments
 (0)