Skip to content

Commit 35ea647

Browse files
authored
make jedis indy-ready (#14792)
1 parent dccbb93 commit 35ea647

File tree

6 files changed

+151
-117
lines changed

6 files changed

+151
-117
lines changed

instrumentation/jedis/jedis-1.4/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jedis/v1_4/JedisConnectionInstrumentation.java

Lines changed: 46 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2121
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
2222
import io.opentelemetry.javaagent.instrumentation.jedis.JedisRequestContext;
23+
import javax.annotation.Nullable;
2324
import net.bytebuddy.asm.Advice;
2425
import net.bytebuddy.description.type.TypeDescription;
2526
import net.bytebuddy.implementation.bytecode.assign.Assigner;
@@ -60,74 +61,75 @@ public void transform(TypeTransformer transformer) {
6061
this.getClass().getName() + "$SendCommandWithArgsAdvice");
6162
}
6263

63-
@SuppressWarnings("unused")
64-
public static class SendCommandNoArgsAdvice {
64+
public static class AdviceScope {
65+
private final Context context;
66+
private final Scope scope;
67+
private final JedisRequest request;
6568

66-
@Advice.OnMethodEnter(suppress = Throwable.class)
67-
public static void onEnter(
68-
@Advice.This Connection connection,
69-
@Advice.Argument(value = 0, typing = Assigner.Typing.DYNAMIC) Protocol.Command command,
70-
@Advice.Local("otelJedisRequest") JedisRequest request,
71-
@Advice.Local("otelContext") Context context,
72-
@Advice.Local("otelScope") Scope scope) {
69+
private AdviceScope(Context context, Scope scope, JedisRequest request) {
70+
this.context = context;
71+
this.scope = scope;
72+
this.request = request;
73+
}
74+
75+
@Nullable
76+
public static AdviceScope start(JedisRequest request) {
7377
Context parentContext = currentContext();
74-
request = JedisRequest.create(connection, command);
7578
if (!instrumenter().shouldStart(parentContext, request)) {
76-
return;
79+
return null;
7780
}
81+
Context context = instrumenter().start(parentContext, request);
82+
return new AdviceScope(context, context.makeCurrent(), request);
83+
}
84+
85+
public void end(@Nullable Throwable throwable) {
86+
scope.close();
87+
JedisRequestContext.endIfNotAttached(instrumenter(), context, request, throwable);
88+
}
89+
}
90+
91+
@SuppressWarnings("unused")
92+
public static class SendCommandNoArgsAdvice {
7893

79-
context = instrumenter().start(parentContext, request);
80-
scope = context.makeCurrent();
94+
@Nullable
95+
@Advice.OnMethodEnter(suppress = Throwable.class)
96+
public static AdviceScope onEnter(
97+
@Advice.This Connection connection,
98+
@Advice.Argument(value = 0, typing = Assigner.Typing.DYNAMIC) Protocol.Command command) {
99+
JedisRequest request = JedisRequest.create(connection, command);
100+
return AdviceScope.start(request);
81101
}
82102

83103
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
84104
public static void stopSpan(
85-
@Advice.Thrown Throwable throwable,
86-
@Advice.Local("otelJedisRequest") JedisRequest request,
87-
@Advice.Local("otelContext") Context context,
88-
@Advice.Local("otelScope") Scope scope) {
89-
if (scope == null) {
90-
return;
105+
@Advice.Thrown @Nullable Throwable throwable,
106+
@Advice.Enter @Nullable AdviceScope adviceScope) {
107+
if (adviceScope != null) {
108+
adviceScope.end(throwable);
91109
}
92-
93-
scope.close();
94-
JedisRequestContext.endIfNotAttached(instrumenter(), context, request, throwable);
95110
}
96111
}
97112

98113
@SuppressWarnings("unused")
99114
public static class SendCommandWithArgsAdvice {
100115

116+
@Nullable
101117
@Advice.OnMethodEnter(suppress = Throwable.class)
102-
public static void onEnter(
118+
public static AdviceScope onEnter(
103119
@Advice.This Connection connection,
104120
@Advice.Argument(value = 0, typing = Assigner.Typing.DYNAMIC) Protocol.Command command,
105-
@Advice.Argument(1) byte[][] args,
106-
@Advice.Local("otelJedisRequest") JedisRequest request,
107-
@Advice.Local("otelContext") Context context,
108-
@Advice.Local("otelScope") Scope scope) {
109-
Context parentContext = currentContext();
110-
request = JedisRequest.create(connection, command, asList(args));
111-
if (!instrumenter().shouldStart(parentContext, request)) {
112-
return;
113-
}
114-
115-
context = instrumenter().start(parentContext, request);
116-
scope = context.makeCurrent();
121+
@Advice.Argument(1) byte[][] args) {
122+
JedisRequest request = JedisRequest.create(connection, command, asList(args));
123+
return AdviceScope.start(request);
117124
}
118125

119126
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
120127
public static void stopSpan(
121-
@Advice.Thrown Throwable throwable,
122-
@Advice.Local("otelJedisRequest") JedisRequest request,
123-
@Advice.Local("otelContext") Context context,
124-
@Advice.Local("otelScope") Scope scope) {
125-
if (scope == null) {
126-
return;
128+
@Advice.Thrown @Nullable Throwable throwable,
129+
@Advice.Enter @Nullable AdviceScope adviceScope) {
130+
if (adviceScope != null) {
131+
adviceScope.end(throwable);
127132
}
128-
129-
scope.close();
130-
JedisRequestContext.endIfNotAttached(instrumenter(), context, request, throwable);
131133
}
132134
}
133135
}

instrumentation/jedis/jedis-1.4/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jedis/v1_4/JedisInstrumentationModule.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
import com.google.auto.service.AutoService;
1313
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
1414
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
15+
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
1516
import java.util.List;
1617
import net.bytebuddy.matcher.ElementMatcher;
1718

1819
@AutoService(InstrumentationModule.class)
19-
public class JedisInstrumentationModule extends InstrumentationModule {
20+
public class JedisInstrumentationModule extends InstrumentationModule
21+
implements ExperimentalInstrumentationModule {
2022

2123
public JedisInstrumentationModule() {
2224
super("jedis", "jedis-1.4");
@@ -32,4 +34,9 @@ public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
3234
public List<TypeInstrumentation> typeInstrumentations() {
3335
return asList(new JedisConnectionInstrumentation(), new JedisInstrumentation());
3436
}
37+
38+
@Override
39+
public boolean isIndyReady() {
40+
return true;
41+
}
3542
}

instrumentation/jedis/jedis-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jedis/v3_0/JedisConnectionInstrumentation.java

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2020
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
2121
import io.opentelemetry.javaagent.instrumentation.jedis.JedisRequestContext;
22+
import javax.annotation.Nullable;
2223
import net.bytebuddy.asm.Advice;
2324
import net.bytebuddy.description.type.TypeDescription;
2425
import net.bytebuddy.matcher.ElementMatcher;
@@ -45,36 +46,51 @@ public void transform(TypeTransformer transformer) {
4546
@SuppressWarnings("unused")
4647
public static class SendCommandAdvice {
4748

49+
public static class AdviceScope {
50+
private final Context context;
51+
private final Scope scope;
52+
private final JedisRequest request;
53+
54+
private AdviceScope(Context context, Scope scope, JedisRequest request) {
55+
this.context = context;
56+
this.scope = scope;
57+
this.request = request;
58+
}
59+
60+
@Nullable
61+
public static AdviceScope start(
62+
Connection connection, ProtocolCommand command, byte[][] args) {
63+
Context parentContext = currentContext();
64+
JedisRequest request = JedisRequest.create(connection, command, asList(args));
65+
if (!instrumenter().shouldStart(parentContext, request)) {
66+
return null;
67+
}
68+
Context context = instrumenter().start(parentContext, request);
69+
return new AdviceScope(context, context.makeCurrent(), request);
70+
}
71+
72+
public void end(@Nullable Throwable throwable) {
73+
scope.close();
74+
JedisRequestContext.endIfNotAttached(instrumenter(), context, request, throwable);
75+
}
76+
}
77+
78+
@Nullable
4879
@Advice.OnMethodEnter(suppress = Throwable.class)
49-
public static void onEnter(
80+
public static AdviceScope onEnter(
5081
@Advice.This Connection connection,
5182
@Advice.Argument(0) ProtocolCommand command,
52-
@Advice.Argument(1) byte[][] args,
53-
@Advice.Local("otelJedisRequest") JedisRequest request,
54-
@Advice.Local("otelContext") Context context,
55-
@Advice.Local("otelScope") Scope scope) {
56-
Context parentContext = currentContext();
57-
request = JedisRequest.create(connection, command, asList(args));
58-
if (!instrumenter().shouldStart(parentContext, request)) {
59-
return;
60-
}
61-
62-
context = instrumenter().start(parentContext, request);
63-
scope = context.makeCurrent();
83+
@Advice.Argument(1) byte[][] args) {
84+
return AdviceScope.start(connection, command, args);
6485
}
6586

6687
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
6788
public static void stopSpan(
68-
@Advice.Thrown Throwable throwable,
69-
@Advice.Local("otelJedisRequest") JedisRequest request,
70-
@Advice.Local("otelContext") Context context,
71-
@Advice.Local("otelScope") Scope scope) {
72-
if (scope == null) {
73-
return;
89+
@Advice.Thrown @Nullable Throwable throwable,
90+
@Advice.Enter @Nullable AdviceScope adviceScope) {
91+
if (adviceScope != null) {
92+
adviceScope.end(throwable);
7493
}
75-
76-
scope.close();
77-
JedisRequestContext.endIfNotAttached(instrumenter(), context, request, throwable);
7894
}
7995
}
8096
}

instrumentation/jedis/jedis-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jedis/v3_0/JedisInstrumentationModule.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@
1212
import com.google.auto.service.AutoService;
1313
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
1414
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
15+
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
1516
import java.util.List;
1617
import net.bytebuddy.matcher.ElementMatcher;
1718

1819
@AutoService(InstrumentationModule.class)
19-
public class JedisInstrumentationModule extends InstrumentationModule {
20+
public class JedisInstrumentationModule extends InstrumentationModule
21+
implements ExperimentalInstrumentationModule {
2022

2123
public JedisInstrumentationModule() {
2224
super("jedis", "jedis-3.0");
@@ -34,4 +36,9 @@ public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
3436
public List<TypeInstrumentation> typeInstrumentations() {
3537
return asList(new JedisConnectionInstrumentation(), new JedisInstrumentation());
3638
}
39+
40+
@Override
41+
public boolean isIndyReady() {
42+
return true;
43+
}
3744
}

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)