|
5 | 5 |
|
6 | 6 | package io.opentelemetry.javaagent.instrumentation.rediscala; |
7 | 7 |
|
8 | | -import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; |
9 | 8 | import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; |
10 | 9 | import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasSuperType; |
11 | 10 | import static io.opentelemetry.javaagent.instrumentation.rediscala.RediscalaSingletons.instrumenter; |
|
20 | 19 | import io.opentelemetry.context.Scope; |
21 | 20 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
22 | 21 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 22 | +import javax.annotation.Nullable; |
23 | 23 | import net.bytebuddy.asm.Advice; |
24 | 24 | import net.bytebuddy.description.type.TypeDescription; |
25 | 25 | import net.bytebuddy.matcher.ElementMatcher; |
@@ -61,50 +61,67 @@ public void transform(TypeTransformer transformer) { |
61 | 61 | @SuppressWarnings("unused") |
62 | 62 | public static class SendAdvice { |
63 | 63 |
|
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 | + } |
69 | 72 |
|
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()); |
73 | 82 | } |
74 | 83 |
|
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); |
77 | 114 | } |
78 | 115 |
|
79 | 116 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
80 | 117 | public static void onExit( |
81 | 118 | @Advice.This Object action, |
82 | 119 | @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); |
108 | 125 | } |
109 | 126 | } |
110 | 127 | } |
|
0 commit comments