|
18 | 18 | import io.opentelemetry.context.Scope; |
19 | 19 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
20 | 20 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 21 | +import javax.annotation.Nullable; |
21 | 22 | import net.bytebuddy.asm.Advice; |
22 | 23 | import net.bytebuddy.description.type.TypeDescription; |
23 | 24 | import net.bytebuddy.matcher.ElementMatcher; |
@@ -49,44 +50,65 @@ public void transform(TypeTransformer transformer) { |
49 | 50 | @SuppressWarnings("unused") |
50 | 51 | public static class ApplyAdvice { |
51 | 52 |
|
52 | | - @Advice.OnMethodEnter(suppress = Throwable.class) |
53 | | - public static void onEnter( |
54 | | - @Advice.Argument(0) Request<?> req, |
55 | | - @Advice.Local("otelContext") Context context, |
56 | | - @Advice.Local("otelScope") Scope scope) { |
57 | | - Context parentContext = currentContext(); |
58 | | - if (!instrumenter().shouldStart(parentContext, null)) { |
59 | | - return; |
| 53 | + public static class AdviceScope { |
| 54 | + private final Context context; |
| 55 | + private final Scope scope; |
| 56 | + |
| 57 | + public AdviceScope(Context context, Scope scope) { |
| 58 | + this.context = context; |
| 59 | + this.scope = scope; |
| 60 | + } |
| 61 | + |
| 62 | + @Nullable |
| 63 | + public static AdviceScope start(Context parentContext) { |
| 64 | + if (!instrumenter().shouldStart(parentContext, null)) { |
| 65 | + return null; |
| 66 | + } |
| 67 | + |
| 68 | + Context context = instrumenter().start(parentContext, null); |
| 69 | + return new AdviceScope(context, context.makeCurrent()); |
| 70 | + } |
| 71 | + |
| 72 | + public Future<Result> end( |
| 73 | + @Nullable Throwable throwable, |
| 74 | + Future<Result> responseFuture, |
| 75 | + Action<?> thisAction, |
| 76 | + Request<?> req) { |
| 77 | + scope.close(); |
| 78 | + updateSpan(context, req); |
| 79 | + |
| 80 | + if (throwable == null) { |
| 81 | + // span is finished when future completes |
| 82 | + // not using responseFuture.onComplete() because that doesn't guarantee this handler span |
| 83 | + // will be completed before the server span completes |
| 84 | + responseFuture = |
| 85 | + ResponseFutureWrapper.wrap(responseFuture, context, thisAction.executionContext()); |
| 86 | + } else { |
| 87 | + instrumenter().end(context, null, null, throwable); |
| 88 | + } |
| 89 | + |
| 90 | + return responseFuture; |
60 | 91 | } |
| 92 | + } |
61 | 93 |
|
62 | | - context = instrumenter().start(parentContext, null); |
63 | | - scope = context.makeCurrent(); |
| 94 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 95 | + public static AdviceScope onEnter(@Advice.Argument(0) Request<?> req) { |
| 96 | + return AdviceScope.start(currentContext()); |
64 | 97 | } |
65 | 98 |
|
66 | 99 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
67 | | - public static void stopTraceOnResponse( |
68 | | - @Advice.This Object thisAction, |
| 100 | + @Advice.AssignReturned.ToReturned |
| 101 | + public static Future<Result> stopTraceOnResponse( |
| 102 | + @Advice.This Action<?> thisAction, |
69 | 103 | @Advice.Thrown Throwable throwable, |
70 | 104 | @Advice.Argument(0) Request<?> req, |
71 | | - @Advice.Return(readOnly = false) Future<Result> responseFuture, |
72 | | - @Advice.Local("otelContext") Context context, |
73 | | - @Advice.Local("otelScope") Scope scope) { |
74 | | - if (scope == null) { |
75 | | - return; |
76 | | - } |
77 | | - scope.close(); |
78 | | - |
79 | | - updateSpan(context, req); |
80 | | - if (throwable == null) { |
81 | | - // span is finished when future completes |
82 | | - // not using responseFuture.onComplete() because that doesn't guarantee this handler span |
83 | | - // will be completed before the server span completes |
84 | | - responseFuture = |
85 | | - ResponseFutureWrapper.wrap( |
86 | | - responseFuture, context, ((Action<?>) thisAction).executionContext()); |
87 | | - } else { |
88 | | - instrumenter().end(context, null, null, throwable); |
| 105 | + @Advice.Return Future<Result> responseFuture, |
| 106 | + @Advice.Enter @Nullable AdviceScope actionScope) { |
| 107 | + if (actionScope == null) { |
| 108 | + return responseFuture; |
89 | 109 | } |
| 110 | + |
| 111 | + return actionScope.end(throwable, responseFuture, thisAction, req); |
90 | 112 | } |
91 | 113 | } |
92 | 114 | } |
0 commit comments