|
24 | 24 | */ |
25 | 25 | package co.elastic.apm.agent.es.restclient.v5_6; |
26 | 26 |
|
27 | | -import co.elastic.apm.agent.bci.VisibleForAdvice; |
28 | 27 | import co.elastic.apm.agent.es.restclient.ElasticsearchRestClientInstrumentation; |
29 | 28 | import co.elastic.apm.agent.es.restclient.ElasticsearchRestClientInstrumentationHelper; |
30 | 29 | import co.elastic.apm.agent.impl.ElasticApmTracer; |
31 | 30 | import co.elastic.apm.agent.impl.transaction.Span; |
32 | 31 | import co.elastic.apm.agent.sdk.advice.AssignTo; |
33 | | -import co.elastic.apm.agent.sdk.state.GlobalThreadLocal; |
34 | 32 | import net.bytebuddy.asm.Advice; |
35 | 33 | import net.bytebuddy.description.method.MethodDescription; |
36 | 34 | import net.bytebuddy.description.type.TypeDescription; |
@@ -77,32 +75,37 @@ public ElementMatcher<? super MethodDescription> getMethodMatcher() { |
77 | 75 |
|
78 | 76 |
|
79 | 77 | public static class ElasticsearchRestClientAsyncAdvice { |
80 | | - @VisibleForAdvice |
81 | | - public static final GlobalThreadLocal<Span> spanTls = GlobalThreadLocal.get(ElasticsearchRestClientAsyncAdvice.class, "spanTls"); |
82 | | - @AssignTo.Argument(5) |
| 78 | + |
| 79 | + @Nullable |
| 80 | + @AssignTo.Argument(index = 1, value = 5) |
83 | 81 | @Advice.OnMethodEnter(suppress = Throwable.class) |
84 | | - public static ResponseListener onBeforeExecute(@Advice.Argument(0) String method, |
85 | | - @Advice.Argument(1) String endpoint, |
86 | | - @Advice.Argument(3) @Nullable HttpEntity entity, |
87 | | - @Advice.Argument(5) ResponseListener responseListener) { |
| 82 | + public static Object[] onBeforeExecute(@Advice.Argument(0) String method, |
| 83 | + @Advice.Argument(1) String endpoint, |
| 84 | + @Advice.Argument(3) @Nullable HttpEntity entity, |
| 85 | + @Advice.Argument(5) ResponseListener responseListener) { |
88 | 86 |
|
89 | 87 | ElasticsearchRestClientInstrumentationHelper<HttpEntity, Response, ResponseListener> helper = esClientInstrHelperManager.getForClassLoaderOfClass(Response.class); |
90 | 88 | if (helper != null) { |
91 | 89 | Span span = helper.createClientSpan(method, endpoint, entity); |
92 | | - spanTls.set(span); |
93 | 90 | if (span != null) { |
94 | | - return helper.<ResponseListener>wrapResponseListener(responseListener, span); |
| 91 | + Object[] ret = new Object[2]; |
| 92 | + ret[0] = span; |
| 93 | + ret[1] = helper.<ResponseListener>wrapResponseListener(responseListener, span); |
| 94 | + return ret; |
95 | 95 | } |
96 | 96 | } |
97 | | - return responseListener; |
| 97 | + return null; |
98 | 98 | } |
99 | 99 |
|
100 | 100 | @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) |
101 | | - public static void onAfterExecute(@Advice.Thrown @Nullable Throwable t) { |
102 | | - final Span span = spanTls.getAndRemove(); |
103 | | - if (span != null) { |
104 | | - // Deactivate in this thread. Span will be ended and reported by the listener |
105 | | - span.deactivate(); |
| 101 | + public static void onAfterExecute(@Advice.Thrown @Nullable Throwable t, |
| 102 | + @Advice.Enter @Nullable Object[] entryArgs) { |
| 103 | + if (entryArgs != null) { |
| 104 | + final Span span = (Span) entryArgs[0]; |
| 105 | + if (span != null) { |
| 106 | + // Deactivate in this thread. Span will be ended and reported by the listener |
| 107 | + span.deactivate(); |
| 108 | + } |
106 | 109 | } |
107 | 110 | } |
108 | 111 | } |
|
0 commit comments