|
17 | 17 | import io.opentelemetry.instrumentation.jetty.httpclient.v12_0.internal.JettyClientTracingListener; |
18 | 18 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
19 | 19 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 20 | +import javax.annotation.Nullable; |
20 | 21 | import net.bytebuddy.asm.Advice; |
21 | 22 | import net.bytebuddy.description.type.TypeDescription; |
22 | 23 | import net.bytebuddy.matcher.ElementMatcher; |
@@ -45,63 +46,70 @@ public void transform(TypeTransformer transformer) { |
45 | 46 | @SuppressWarnings("unused") |
46 | 47 | public static class JettyHttpClient12SendAdvice { |
47 | 48 |
|
| 49 | + public static class AdviceLocals { |
| 50 | + public final Context context; |
| 51 | + public final Scope scope; |
| 52 | + |
| 53 | + public AdviceLocals(Context context, Scope scope) { |
| 54 | + this.context = context; |
| 55 | + this.scope = scope; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + @Nullable |
48 | 60 | @Advice.OnMethodEnter(suppress = Throwable.class) |
49 | | - public static void onEnterSend( |
50 | | - @Advice.This HttpRequest request, |
51 | | - @Advice.Local("otelContext") Context context, |
52 | | - @Advice.Local("otelScope") Scope scope) { |
| 61 | + public static AdviceLocals onEnterSend(@Advice.This HttpRequest request) { |
| 62 | + |
53 | 63 | // start span |
54 | 64 | Context parentContext = Context.current(); |
55 | | - context = JettyClientTracingListener.handleRequest(parentContext, request, instrumenter()); |
| 65 | + Context context = |
| 66 | + JettyClientTracingListener.handleRequest(parentContext, request, instrumenter()); |
56 | 67 | if (context == null) { |
57 | | - return; |
| 68 | + return null; |
58 | 69 | } |
59 | 70 | // set context for responseListeners |
60 | 71 | request.attribute(JETTY_CLIENT_CONTEXT_KEY, parentContext); |
61 | 72 |
|
62 | | - scope = context.makeCurrent(); |
| 73 | + return new AdviceLocals(context, context.makeCurrent()); |
63 | 74 | } |
64 | 75 |
|
65 | 76 | @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) |
66 | 77 | public static void onExitSend( |
67 | 78 | @Advice.This HttpRequest request, |
68 | 79 | @Advice.Thrown Throwable throwable, |
69 | | - @Advice.Local("otelContext") Context context, |
70 | | - @Advice.Local("otelScope") Scope scope) { |
71 | | - if (scope == null) { |
| 80 | + @Advice.Enter @Nullable AdviceLocals locals) { |
| 81 | + |
| 82 | + if (locals == null) { |
72 | 83 | return; |
73 | 84 | } |
74 | 85 |
|
75 | 86 | // not ending span here unless error, span ended in the interceptor |
76 | | - scope.close(); |
| 87 | + locals.scope.close(); |
77 | 88 | if (throwable != null) { |
78 | | - instrumenter().end(context, request, null, throwable); |
| 89 | + instrumenter().end(locals.context, request, null, throwable); |
79 | 90 | } |
80 | 91 | } |
81 | 92 | } |
82 | 93 |
|
83 | 94 | @SuppressWarnings("unused") |
84 | 95 | public static class JettyHttpClient12NotifyAdvice { |
| 96 | + |
| 97 | + @Nullable |
85 | 98 | @Advice.OnMethodEnter(suppress = Throwable.class) |
86 | | - public static void onEnterNotify( |
87 | | - @Advice.This HttpRequest request, |
88 | | - @Advice.Local("otelContext") Context context, |
89 | | - @Advice.Local("otelScope") Scope scope) { |
90 | | - context = (Context) request.getAttributes().get(JETTY_CLIENT_CONTEXT_KEY); |
| 99 | + public static Scope onEnterNotify(@Advice.This HttpRequest request) { |
| 100 | + |
| 101 | + Context context = (Context) request.getAttributes().get(JETTY_CLIENT_CONTEXT_KEY); |
91 | 102 | if (context == null) { |
92 | | - return; |
| 103 | + return null; |
93 | 104 | } |
94 | | - scope = context.makeCurrent(); |
| 105 | + return context.makeCurrent(); |
95 | 106 | } |
96 | 107 |
|
97 | 108 | @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) |
98 | | - public static void onExitNotify( |
99 | | - @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { |
100 | | - if (scope == null) { |
101 | | - return; |
| 109 | + public static void onExitNotify(@Advice.Enter Scope scope) { |
| 110 | + if (scope != null) { |
| 111 | + scope.close(); |
102 | 112 | } |
103 | | - |
104 | | - scope.close(); |
105 | 113 | } |
106 | 114 | } |
107 | 115 | } |
0 commit comments