Skip to content

Commit 72e6a27

Browse files
committed
jedis-4.0
1 parent 8f8f20d commit 72e6a27

File tree

1 file changed

+43
-48
lines changed

1 file changed

+43
-48
lines changed

instrumentation/jedis/jedis-4.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jedis/v4_0/JedisConnectionInstrumentation.java

Lines changed: 43 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
2121
import io.opentelemetry.javaagent.instrumentation.jedis.JedisRequestContext;
2222
import java.net.Socket;
23+
import javax.annotation.Nullable;
2324
import net.bytebuddy.asm.Advice;
2425
import net.bytebuddy.description.type.TypeDescription;
2526
import net.bytebuddy.matcher.ElementMatcher;
@@ -50,78 +51,72 @@ public void transform(TypeTransformer transformer) {
5051
this.getClass().getName() + "$SendCommandAdvice");
5152
}
5253

53-
@SuppressWarnings("unused")
54-
public static class SendCommandAdvice {
54+
public static class AdviceScope {
55+
private final Context context;
56+
private final Scope scope;
57+
private final JedisRequest request;
5558

56-
@Advice.OnMethodEnter(suppress = Throwable.class)
57-
public static void onEnter(
58-
@Advice.Argument(0) ProtocolCommand command,
59-
@Advice.Argument(1) byte[][] args,
60-
@Advice.Local("otelJedisRequest") JedisRequest request,
61-
@Advice.Local("otelContext") Context context,
62-
@Advice.Local("otelScope") Scope scope) {
59+
private AdviceScope(Context context, Scope scope, JedisRequest request) {
60+
this.context = context;
61+
this.scope = scope;
62+
this.request = request;
63+
}
64+
65+
@Nullable
66+
public static AdviceScope start(JedisRequest request) {
6367
Context parentContext = currentContext();
64-
request = JedisRequest.create(command, asList(args));
6568
if (!instrumenter().shouldStart(parentContext, request)) {
66-
return;
69+
return null;
6770
}
71+
Context context = instrumenter().start(parentContext, request);
72+
return new AdviceScope(context, context.makeCurrent(), request);
73+
}
74+
75+
public void end(Socket socket, @Nullable Throwable throwable) {
76+
request.setSocket(socket);
77+
scope.close();
78+
JedisRequestContext.endIfNotAttached(instrumenter(), context, request, throwable);
79+
}
80+
}
81+
82+
@SuppressWarnings("unused")
83+
public static class SendCommandAdvice {
6884

69-
context = instrumenter().start(parentContext, request);
70-
scope = context.makeCurrent();
85+
@Nullable
86+
@Advice.OnMethodEnter(suppress = Throwable.class)
87+
public static AdviceScope onEnter(
88+
@Advice.Argument(0) ProtocolCommand command, @Advice.Argument(1) byte[][] args) {
89+
return AdviceScope.start(JedisRequest.create(command, asList(args)));
7190
}
7291

7392
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
7493
public static void stopSpan(
7594
@Advice.FieldValue("socket") Socket socket,
76-
@Advice.Thrown Throwable throwable,
77-
@Advice.Local("otelJedisRequest") JedisRequest request,
78-
@Advice.Local("otelContext") Context context,
79-
@Advice.Local("otelScope") Scope scope) {
80-
if (scope == null) {
81-
return;
95+
@Advice.Thrown @Nullable Throwable throwable,
96+
@Advice.Enter @Nullable AdviceScope adviceScope) {
97+
if (adviceScope != null) {
98+
adviceScope.end(socket, throwable);
8299
}
83-
84-
request.setSocket(socket);
85-
86-
scope.close();
87-
JedisRequestContext.endIfNotAttached(instrumenter(), context, request, throwable);
88100
}
89101
}
90102

91103
@SuppressWarnings("unused")
92104
public static class SendCommand2Advice {
93105

106+
@Nullable
94107
@Advice.OnMethodEnter(suppress = Throwable.class)
95-
public static void onEnter(
96-
@Advice.Argument(0) CommandArguments command,
97-
@Advice.Local("otelJedisRequest") JedisRequest request,
98-
@Advice.Local("otelContext") Context context,
99-
@Advice.Local("otelScope") Scope scope) {
100-
Context parentContext = currentContext();
101-
request = JedisRequest.create(command);
102-
if (!instrumenter().shouldStart(parentContext, request)) {
103-
return;
104-
}
105-
106-
context = instrumenter().start(parentContext, request);
107-
scope = context.makeCurrent();
108+
public static AdviceScope onEnter(@Advice.Argument(0) CommandArguments command) {
109+
return AdviceScope.start(JedisRequest.create(command));
108110
}
109111

110112
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
111113
public static void stopSpan(
112114
@Advice.FieldValue("socket") Socket socket,
113-
@Advice.Thrown Throwable throwable,
114-
@Advice.Local("otelJedisRequest") JedisRequest request,
115-
@Advice.Local("otelContext") Context context,
116-
@Advice.Local("otelScope") Scope scope) {
117-
if (scope == null) {
118-
return;
115+
@Advice.Thrown @Nullable Throwable throwable,
116+
@Advice.Enter @Nullable AdviceScope adviceScope) {
117+
if (adviceScope != null) {
118+
adviceScope.end(socket, throwable);
119119
}
120-
121-
request.setSocket(socket);
122-
123-
scope.close();
124-
JedisRequestContext.endIfNotAttached(instrumenter(), context, request, throwable);
125120
}
126121
}
127122
}

0 commit comments

Comments
 (0)