|
21 | 21 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
22 | 22 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
23 | 23 | import java.util.concurrent.Executor; |
| 24 | +import javax.annotation.Nullable; |
24 | 25 | import net.bytebuddy.asm.Advice; |
25 | 26 | import net.bytebuddy.description.type.TypeDescription; |
26 | 27 | import net.bytebuddy.matcher.ElementMatcher; |
@@ -49,78 +50,93 @@ public void transform(TypeTransformer transformer) { |
49 | 50 | this.getClass().getName() + "$ExecuteAsyncAdvice"); |
50 | 51 | } |
51 | 52 |
|
| 53 | + public static class AdviceLocals { |
| 54 | + public final Context context; |
| 55 | + public final Scope scope; |
| 56 | + |
| 57 | + public AdviceLocals(Context context, Scope scope) { |
| 58 | + this.context = context; |
| 59 | + this.scope = scope; |
| 60 | + } |
| 61 | + |
| 62 | + public static AdviceLocals start(HttpRequest request) { |
| 63 | + |
| 64 | + Context parentContext = currentContext(); |
| 65 | + if (!instrumenter().shouldStart(parentContext, request)) { |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + Context context = instrumenter().start(parentContext, request); |
| 70 | + return new AdviceLocals(context, context.makeCurrent()); |
| 71 | + } |
| 72 | + |
| 73 | + public void end(HttpRequest request, HttpResponse response, Throwable throwable) { |
| 74 | + scope.close(); |
| 75 | + instrumenter().end(context, request, response, throwable); |
| 76 | + } |
| 77 | + |
| 78 | + public void endWhenThrown(HttpRequest request, Throwable throwable) { |
| 79 | + scope.close(); |
| 80 | + if (throwable != null) { |
| 81 | + instrumenter().end(context, request, null, throwable); |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
52 | 86 | @SuppressWarnings("unused") |
53 | 87 | public static class ExecuteAdvice { |
54 | 88 |
|
| 89 | + @Nullable |
55 | 90 | @Advice.OnMethodEnter(suppress = Throwable.class) |
56 | | - public static void methodEnter( |
57 | | - @Advice.This HttpRequest request, |
58 | | - @Advice.Local("otelContext") Context context, |
59 | | - @Advice.Local("otelScope") Scope scope) { |
60 | | - |
61 | | - context = VirtualField.find(HttpRequest.class, Context.class).get(request); |
62 | | - if (context != null) { |
| 91 | + public static AdviceLocals methodEnter(@Advice.This HttpRequest request) { |
| 92 | + Context virtualFieldContext = |
| 93 | + VirtualField.find(HttpRequest.class, Context.class).get(request); |
| 94 | + if (virtualFieldContext != null) { |
63 | 95 | // span was created by GoogleHttpClientAsyncAdvice instrumentation below |
64 | 96 | // (executeAsync ends up calling execute from a separate thread) |
65 | 97 | // so make it current and end it in method exit |
66 | | - scope = context.makeCurrent(); |
67 | | - return; |
68 | | - } |
69 | | - Context parentContext = currentContext(); |
70 | | - if (!instrumenter().shouldStart(parentContext, request)) { |
71 | | - return; |
| 98 | + return new AdviceLocals(virtualFieldContext, virtualFieldContext.makeCurrent()); |
72 | 99 | } |
73 | | - context = instrumenter().start(parentContext, request); |
74 | | - scope = context.makeCurrent(); |
| 100 | + |
| 101 | + return AdviceLocals.start(request); |
75 | 102 | } |
76 | 103 |
|
77 | 104 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
78 | 105 | public static void methodExit( |
79 | 106 | @Advice.This HttpRequest request, |
80 | 107 | @Advice.Return HttpResponse response, |
81 | 108 | @Advice.Thrown Throwable throwable, |
82 | | - @Advice.Local("otelContext") Context context, |
83 | | - @Advice.Local("otelScope") Scope scope) { |
84 | | - if (scope == null) { |
85 | | - return; |
86 | | - } |
| 109 | + @Advice.Enter @Nullable AdviceLocals locals) { |
87 | 110 |
|
88 | | - scope.close(); |
89 | | - instrumenter().end(context, request, response, throwable); |
| 111 | + if (locals != null) { |
| 112 | + locals.end(request, response, throwable); |
| 113 | + } |
90 | 114 | } |
91 | 115 | } |
92 | 116 |
|
93 | 117 | @SuppressWarnings("unused") |
94 | 118 | public static class ExecuteAsyncAdvice { |
95 | 119 |
|
| 120 | + @Nullable |
96 | 121 | @Advice.OnMethodEnter(suppress = Throwable.class) |
97 | | - public static void methodEnter( |
98 | | - @Advice.This HttpRequest request, |
99 | | - @Advice.Local("otelContext") Context context, |
100 | | - @Advice.Local("otelScope") Scope scope) { |
101 | | - Context parentContext = currentContext(); |
102 | | - if (!instrumenter().shouldStart(parentContext, request)) { |
103 | | - return; |
| 122 | + public static AdviceLocals methodEnter(@Advice.This HttpRequest request) { |
| 123 | + |
| 124 | + AdviceLocals locals = AdviceLocals.start(request); |
| 125 | + if (locals != null) { |
| 126 | + VirtualField.find(HttpRequest.class, Context.class).set(request, locals.context); |
104 | 127 | } |
105 | | - context = instrumenter().start(parentContext, request); |
106 | | - scope = context.makeCurrent(); |
107 | 128 |
|
108 | | - VirtualField.find(HttpRequest.class, Context.class).set(request, context); |
| 129 | + return locals; |
109 | 130 | } |
110 | 131 |
|
111 | 132 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
112 | 133 | public static void methodExit( |
113 | 134 | @Advice.This HttpRequest request, |
114 | 135 | @Advice.Thrown Throwable throwable, |
115 | | - @Advice.Local("otelContext") Context context, |
116 | | - @Advice.Local("otelScope") Scope scope) { |
117 | | - if (scope == null) { |
118 | | - return; |
119 | | - } |
| 136 | + @Advice.Enter @Nullable AdviceLocals locals) { |
120 | 137 |
|
121 | | - scope.close(); |
122 | | - if (throwable != null) { |
123 | | - instrumenter().end(context, request, null, throwable); |
| 138 | + if (locals != null) { |
| 139 | + locals.endWhenThrown(request, throwable); |
124 | 140 | } |
125 | 141 | } |
126 | 142 | } |
|
0 commit comments