|
16 | 16 | import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseCustomizerHolder; |
17 | 17 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
18 | 18 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 19 | +import javax.annotation.Nullable; |
19 | 20 | import net.bytebuddy.asm.Advice; |
20 | 21 | import net.bytebuddy.description.type.TypeDescription; |
21 | 22 | import net.bytebuddy.matcher.ElementMatcher; |
@@ -43,39 +44,53 @@ public void transform(TypeTransformer transformer) { |
43 | 44 | @SuppressWarnings("unused") |
44 | 45 | public static class HandlerAdvice { |
45 | 46 |
|
46 | | - @Advice.OnMethodEnter(suppress = Throwable.class) |
47 | | - public static void onEnter( |
48 | | - @Advice.This Object source, |
49 | | - @Advice.Argument(0) Request request, |
50 | | - @Advice.Argument(1) Response response, |
51 | | - @Advice.Local("otelContext") Context context, |
52 | | - @Advice.Local("otelScope") Scope scope) { |
| 47 | + public static class AdviceScope { |
| 48 | + private final Context context; |
| 49 | + private final Scope scope; |
| 50 | + |
| 51 | + private AdviceScope(Context context, Scope scope) { |
| 52 | + this.context = context; |
| 53 | + this.scope = scope; |
| 54 | + } |
53 | 55 |
|
54 | | - Context parentContext = Java8BytecodeBridge.currentContext(); |
55 | | - if (!helper().shouldStart(parentContext, request)) { |
56 | | - return; |
| 56 | + @Nullable |
| 57 | + public static AdviceScope start(Object source, Request request, Response response) { |
| 58 | + Context parentContext = Java8BytecodeBridge.currentContext(); |
| 59 | + if (!helper().shouldStart(parentContext, request)) { |
| 60 | + return null; |
| 61 | + } |
| 62 | + Context context = helper().start(parentContext, request, response); |
| 63 | + Scope scope = context.makeCurrent(); |
| 64 | + HttpServerResponseCustomizerHolder.getCustomizer() |
| 65 | + .customize(context, response, Jetty12ResponseMutator.INSTANCE); |
| 66 | + return new AdviceScope(context, scope); |
57 | 67 | } |
58 | 68 |
|
59 | | - context = helper().start(parentContext, request, response); |
60 | | - scope = context.makeCurrent(); |
| 69 | + public void end(Request request, Response response, @Nullable Throwable throwable) { |
| 70 | + if (scope != null) { |
| 71 | + scope.close(); |
| 72 | + helper().end(context, request, response, throwable); |
| 73 | + } |
| 74 | + } |
| 75 | + } |
61 | 76 |
|
62 | | - HttpServerResponseCustomizerHolder.getCustomizer() |
63 | | - .customize(context, response, Jetty12ResponseMutator.INSTANCE); |
| 77 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 78 | + @Nullable |
| 79 | + public static AdviceScope onEnter( |
| 80 | + @Advice.This Object source, |
| 81 | + @Advice.Argument(0) Request request, |
| 82 | + @Advice.Argument(1) Response response) { |
| 83 | + return AdviceScope.start(source, request, response); |
64 | 84 | } |
65 | 85 |
|
66 | 86 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
67 | 87 | public static void stopSpan( |
68 | 88 | @Advice.Argument(0) Request request, |
69 | 89 | @Advice.Argument(1) Response response, |
70 | | - @Advice.Thrown Throwable throwable, |
71 | | - @Advice.Local("otelContext") Context context, |
72 | | - @Advice.Local("otelScope") Scope scope) { |
73 | | - if (scope == null) { |
74 | | - return; |
75 | | - } |
76 | | - scope.close(); |
77 | | - if (throwable != null) { |
78 | | - helper().end(context, request, response, throwable); |
| 90 | + @Advice.Thrown @Nullable Throwable throwable, |
| 91 | + @Advice.Enter @Nullable AdviceScope adviceScope) { |
| 92 | + if (adviceScope != null) { |
| 93 | + adviceScope.end(request, response, throwable); |
79 | 94 | } |
80 | 95 | } |
81 | 96 | } |
|
0 commit comments