|
20 | 20 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
21 | 21 | import io.opentelemetry.javaagent.instrumentation.jedis.JedisRequestContext; |
22 | 22 | import java.net.Socket; |
| 23 | +import javax.annotation.Nullable; |
23 | 24 | import net.bytebuddy.asm.Advice; |
24 | 25 | import net.bytebuddy.description.type.TypeDescription; |
25 | 26 | import net.bytebuddy.matcher.ElementMatcher; |
@@ -50,78 +51,72 @@ public void transform(TypeTransformer transformer) { |
50 | 51 | this.getClass().getName() + "$SendCommandAdvice"); |
51 | 52 | } |
52 | 53 |
|
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; |
55 | 58 |
|
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) { |
63 | 67 | Context parentContext = currentContext(); |
64 | | - request = JedisRequest.create(command, asList(args)); |
65 | 68 | if (!instrumenter().shouldStart(parentContext, request)) { |
66 | | - return; |
| 69 | + return null; |
67 | 70 | } |
| 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 { |
68 | 84 |
|
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))); |
71 | 90 | } |
72 | 91 |
|
73 | 92 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
74 | 93 | public static void stopSpan( |
75 | 94 | @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); |
82 | 99 | } |
83 | | - |
84 | | - request.setSocket(socket); |
85 | | - |
86 | | - scope.close(); |
87 | | - JedisRequestContext.endIfNotAttached(instrumenter(), context, request, throwable); |
88 | 100 | } |
89 | 101 | } |
90 | 102 |
|
91 | 103 | @SuppressWarnings("unused") |
92 | 104 | public static class SendCommand2Advice { |
93 | 105 |
|
| 106 | + @Nullable |
94 | 107 | @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)); |
108 | 110 | } |
109 | 111 |
|
110 | 112 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
111 | 113 | public static void stopSpan( |
112 | 114 | @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); |
119 | 119 | } |
120 | | - |
121 | | - request.setSocket(socket); |
122 | | - |
123 | | - scope.close(); |
124 | | - JedisRequestContext.endIfNotAttached(instrumenter(), context, request, throwable); |
125 | 120 | } |
126 | 121 | } |
127 | 122 | } |
0 commit comments