|
14 | 14 | import com.ibm.wsspi.http.channel.values.StatusCodes; |
15 | 15 | import io.opentelemetry.context.Context; |
16 | 16 | import io.opentelemetry.context.Scope; |
17 | | -import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; |
18 | 17 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
19 | 18 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 19 | +import javax.annotation.Nullable; |
20 | 20 | import net.bytebuddy.asm.Advice; |
21 | 21 | import net.bytebuddy.description.type.TypeDescription; |
22 | 22 | import net.bytebuddy.matcher.ElementMatcher; |
@@ -50,45 +50,63 @@ public void transform(TypeTransformer transformer) { |
50 | 50 | @SuppressWarnings("unused") |
51 | 51 | public static class SendResponseAdvice { |
52 | 52 |
|
53 | | - @Advice.OnMethodEnter(suppress = Throwable.class) |
54 | | - public static void onEnter( |
55 | | - @Advice.FieldValue("isc") HttpInboundServiceContextImpl isc, |
56 | | - @Advice.Local("otelRequest") LibertyRequest request, |
57 | | - @Advice.Local("otelContext") Context context, |
58 | | - @Advice.Local("otelScope") Scope scope) { |
59 | | - Context parentContext = Java8BytecodeBridge.currentContext(); |
60 | | - request = |
61 | | - new LibertyRequest( |
62 | | - isc.getRequest(), |
63 | | - isc.getLocalAddr(), |
64 | | - isc.getLocalPort(), |
65 | | - isc.getRemoteAddr(), |
66 | | - isc.getRemotePort()); |
67 | | - if (!instrumenter().shouldStart(parentContext, request)) { |
68 | | - return; |
| 53 | + public static class AdviceScope { |
| 54 | + private final LibertyRequest request; |
| 55 | + private final Context context; |
| 56 | + private final Scope scope; |
| 57 | + |
| 58 | + private AdviceScope(LibertyRequest request, Context context, Scope scope) { |
| 59 | + this.request = request; |
| 60 | + this.context = context; |
| 61 | + this.scope = scope; |
| 62 | + } |
| 63 | + |
| 64 | + @Nullable |
| 65 | + public static AdviceScope start(HttpInboundServiceContextImpl isc) { |
| 66 | + Context parentContext = Context.current(); |
| 67 | + LibertyRequest request = |
| 68 | + new LibertyRequest( |
| 69 | + isc.getRequest(), |
| 70 | + isc.getLocalAddr(), |
| 71 | + isc.getLocalPort(), |
| 72 | + isc.getRemoteAddr(), |
| 73 | + isc.getRemotePort()); |
| 74 | + if (!instrumenter().shouldStart(parentContext, request)) { |
| 75 | + return null; |
| 76 | + } |
| 77 | + Context context = instrumenter().start(parentContext, request); |
| 78 | + return new AdviceScope(request, context, context.makeCurrent()); |
| 79 | + } |
| 80 | + |
| 81 | + public void end( |
| 82 | + HttpDispatcherLink httpDispatcherLink, |
| 83 | + @Nullable Throwable throwable, |
| 84 | + StatusCodes statusCode, |
| 85 | + @Nullable Exception failure) { |
| 86 | + scope.close(); |
| 87 | + |
| 88 | + LibertyResponse response = new LibertyResponse(httpDispatcherLink, statusCode); |
| 89 | + Throwable t = failure != null ? failure : throwable; |
| 90 | + instrumenter().end(context, request, response, t); |
69 | 91 | } |
70 | | - context = instrumenter().start(parentContext, request); |
71 | | - scope = context.makeCurrent(); |
| 92 | + } |
| 93 | + |
| 94 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 95 | + public static AdviceScope onEnter(@Advice.FieldValue("isc") HttpInboundServiceContextImpl isc) { |
| 96 | + return AdviceScope.start(isc); |
72 | 97 | } |
73 | 98 |
|
74 | 99 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
75 | 100 | public static void stopSpan( |
76 | 101 | @Advice.This HttpDispatcherLink httpDispatcherLink, |
77 | | - @Advice.Thrown Throwable throwable, |
| 102 | + @Advice.Thrown @Nullable Throwable throwable, |
78 | 103 | @Advice.Argument(value = 0) StatusCodes statusCode, |
79 | 104 | @Advice.Argument(value = 2) Exception failure, |
80 | | - @Advice.Local("otelRequest") LibertyRequest request, |
81 | | - @Advice.Local("otelContext") Context context, |
82 | | - @Advice.Local("otelScope") Scope scope) { |
83 | | - if (scope == null) { |
84 | | - return; |
85 | | - } |
86 | | - scope.close(); |
87 | | - |
88 | | - LibertyResponse response = new LibertyResponse(httpDispatcherLink, statusCode); |
| 105 | + @Advice.Enter @Nullable AdviceScope adviceScope) { |
89 | 106 |
|
90 | | - Throwable t = failure != null ? failure : throwable; |
91 | | - instrumenter().end(context, request, response, t); |
| 107 | + if (adviceScope != null) { |
| 108 | + adviceScope.end(httpDispatcherLink, throwable, statusCode, failure); |
| 109 | + } |
92 | 110 | } |
93 | 111 | } |
94 | 112 | } |
0 commit comments