|
20 | 20 | import io.opentelemetry.context.Scope; |
21 | 21 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
22 | 22 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 23 | +import javax.annotation.Nullable; |
23 | 24 | import net.bytebuddy.asm.Advice; |
| 25 | +import net.bytebuddy.asm.Advice.AssignReturned; |
| 26 | +import net.bytebuddy.asm.Advice.AssignReturned.ToArguments.ToArgument; |
24 | 27 | import net.bytebuddy.description.type.TypeDescription; |
25 | 28 | import net.bytebuddy.matcher.ElementMatcher; |
26 | 29 | import okhttp3.Call; |
@@ -55,19 +58,21 @@ public void transform(TypeTransformer transformer) { |
55 | 58 | @SuppressWarnings("unused") |
56 | 59 | public static class BuildRequestAdvice { |
57 | 60 |
|
| 61 | + @AssignReturned.ToReturned |
58 | 62 | @Advice.OnMethodExit(suppress = Throwable.class) |
59 | | - public static void onExit(@Advice.Return(readOnly = false) Request request) { |
| 63 | + public static Request onExit(@Advice.Return Request originalReturnValue) { |
60 | 64 | Context parentContext = currentContext(); |
61 | | - if (!instrumenter().shouldStart(parentContext, request)) { |
62 | | - return; |
| 65 | + if (!instrumenter().shouldStart(parentContext, originalReturnValue)) { |
| 66 | + return originalReturnValue; |
63 | 67 | } |
64 | 68 |
|
65 | | - Context context = instrumenter().start(parentContext, request); |
| 69 | + Context context = instrumenter().start(parentContext, originalReturnValue); |
66 | 70 | Scope scope = context.makeCurrent(); |
67 | | - Request.Builder requestWithPropagation = request.newBuilder(); |
| 71 | + Request.Builder requestWithPropagation = originalReturnValue.newBuilder(); |
68 | 72 | inject(context, requestWithPropagation); |
69 | | - request = requestWithPropagation.build(); |
| 73 | + Request request = requestWithPropagation.build(); |
70 | 74 | CurrentState.set(parentContext, context, scope, request); |
| 75 | + return request; |
71 | 76 | } |
72 | 77 | } |
73 | 78 |
|
@@ -96,39 +101,34 @@ public static void onExit( |
96 | 101 | @SuppressWarnings("unused") |
97 | 102 | public static class ExecuteAsyncAdvice { |
98 | 103 |
|
| 104 | + @AssignReturned.ToArguments(@ToArgument(value = 2, index = 1)) |
99 | 105 | @Advice.OnMethodEnter(suppress = Throwable.class) |
100 | | - public static void onEnter( |
101 | | - @Advice.Argument(0) Call httpCall, |
102 | | - @Advice.Argument(value = 2, readOnly = false) ApiCallback<?> callback, |
103 | | - @Advice.Local("otelContext") Context context, |
104 | | - @Advice.Local("otelScope") Scope scope, |
105 | | - @Advice.Local("otelRequest") Request request) { |
| 106 | + public static Object[] onEnter( |
| 107 | + @Advice.Argument(0) Call httpCall, @Advice.Argument(2) ApiCallback<?> originalCallback) { |
106 | 108 | CurrentState current = CurrentState.remove(); |
107 | 109 | if (current == null) { |
108 | | - return; |
| 110 | + return new Object[] {null, originalCallback}; |
109 | 111 | } |
110 | | - |
111 | | - context = current.getContext(); |
112 | | - scope = current.getScope(); |
113 | | - request = current.getRequest(); |
114 | | - callback = new TracingApiCallback<>(callback, current.getParentContext(), context, request); |
| 112 | + ApiCallback<?> callback = |
| 113 | + new TracingApiCallback<>( |
| 114 | + originalCallback, |
| 115 | + current.getParentContext(), |
| 116 | + current.getContext(), |
| 117 | + current.getRequest()); |
| 118 | + return new Object[] {current, callback}; |
115 | 119 | } |
116 | 120 |
|
117 | 121 | @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) |
118 | 122 | public static void onExit( |
119 | | - @Advice.Thrown Throwable throwable, |
120 | | - @Advice.Local("otelContext") Context context, |
121 | | - @Advice.Local("otelScope") Scope scope, |
122 | | - @Advice.Local("otelRequest") Request request) { |
123 | | - if (scope == null) { |
124 | | - return; |
125 | | - } |
126 | | - scope.close(); |
127 | | - |
128 | | - if (throwable != null) { |
129 | | - instrumenter().end(context, request, null, throwable); |
| 123 | + @Advice.Thrown @Nullable Throwable throwable, @Advice.Enter Object[] enterResult) { |
| 124 | + CurrentState current = (CurrentState) enterResult[0]; |
| 125 | + if (current != null) { |
| 126 | + current.getScope().close(); |
| 127 | + if (throwable != null) { |
| 128 | + instrumenter().end(current.getContext(), current.getRequest(), null, throwable); |
| 129 | + } |
| 130 | + // else span will be ended in the TracingApiCallback |
130 | 131 | } |
131 | | - // else span will be ended in the TracingApiCallback |
132 | 132 | } |
133 | 133 | } |
134 | 134 | } |
0 commit comments