|
9 | 9 |
|
10 | 10 | import io.opentelemetry.context.Context; |
11 | 11 | import io.opentelemetry.context.Scope; |
12 | | -import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; |
13 | 12 | import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseCustomizerHolder; |
| 13 | +import javax.annotation.Nullable; |
14 | 14 | import net.bytebuddy.asm.Advice; |
15 | 15 | import org.apache.coyote.Request; |
16 | 16 | import org.apache.coyote.Response; |
17 | 17 |
|
18 | 18 | @SuppressWarnings("unused") |
19 | 19 | public class Tomcat7ServerHandlerAdvice { |
20 | 20 |
|
21 | | - @Advice.OnMethodEnter(suppress = Throwable.class) |
22 | | - public static void onEnter( |
23 | | - @Advice.Argument(0) Request request, |
24 | | - @Advice.Argument(1) Response response, |
25 | | - @Advice.Local("otelContext") Context context, |
26 | | - @Advice.Local("otelScope") Scope scope) { |
| 21 | + public static class AdviceScope { |
| 22 | + private final Context context; |
| 23 | + private final Scope scope; |
27 | 24 |
|
28 | | - Context parentContext = Java8BytecodeBridge.currentContext(); |
29 | | - if (!helper().shouldStart(parentContext, request)) { |
30 | | - return; |
| 25 | + private AdviceScope(Context context, Scope scope) { |
| 26 | + this.context = context; |
| 27 | + this.scope = scope; |
31 | 28 | } |
32 | 29 |
|
33 | | - context = helper().start(parentContext, request); |
| 30 | + @Nullable |
| 31 | + public static AdviceScope start(Request request, Response response) { |
| 32 | + Context parentContext = Context.current(); |
| 33 | + if (!helper().shouldStart(parentContext, request)) { |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + Context context = helper().start(parentContext, request); |
34 | 38 |
|
35 | | - scope = context.makeCurrent(); |
| 39 | + Scope scope = context.makeCurrent(); |
| 40 | + |
| 41 | + HttpServerResponseCustomizerHolder.getCustomizer() |
| 42 | + .customize(context, response, Tomcat7ResponseMutator.INSTANCE); |
| 43 | + |
| 44 | + return new AdviceScope(context, scope); |
| 45 | + } |
36 | 46 |
|
37 | | - HttpServerResponseCustomizerHolder.getCustomizer() |
38 | | - .customize(context, response, Tomcat7ResponseMutator.INSTANCE); |
| 47 | + public void end(Request request, Response response, Throwable throwable) { |
| 48 | + helper().end(request, response, throwable, context, scope); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + @Nullable |
| 53 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 54 | + public static AdviceScope onEnter( |
| 55 | + @Advice.Argument(0) Request request, @Advice.Argument(1) Response response) { |
| 56 | + return AdviceScope.start(request, response); |
39 | 57 | } |
40 | 58 |
|
41 | 59 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
42 | 60 | public static void stopSpan( |
43 | 61 | @Advice.Argument(0) Request request, |
44 | 62 | @Advice.Argument(1) Response response, |
45 | | - @Advice.Thrown Throwable throwable, |
46 | | - @Advice.Local("otelContext") Context context, |
47 | | - @Advice.Local("otelScope") Scope scope) { |
48 | | - |
49 | | - helper().end(request, response, throwable, context, scope); |
| 63 | + @Advice.Thrown @Nullable Throwable throwable, |
| 64 | + @Advice.Enter AdviceScope adviceScope) { |
| 65 | + if (adviceScope != null) { |
| 66 | + adviceScope.end(request, response, throwable); |
| 67 | + } |
50 | 68 | } |
51 | 69 | } |
0 commit comments