|
17 | 17 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
18 | 18 | import io.opentelemetry.javaagent.instrumentation.jaxrs.JaxrsConstants; |
19 | 19 | import java.lang.reflect.Method; |
| 20 | +import javax.annotation.Nullable; |
20 | 21 | import javax.ws.rs.container.ContainerRequestContext; |
21 | 22 | import net.bytebuddy.asm.Advice; |
22 | | -import net.bytebuddy.asm.Advice.Local; |
23 | 23 | import net.bytebuddy.description.type.TypeDescription; |
24 | 24 | import net.bytebuddy.matcher.ElementMatcher; |
25 | 25 | import org.apache.cxf.jaxrs.impl.AbstractRequestContextImpl; |
@@ -57,50 +57,65 @@ public void transform(TypeTransformer transformer) { |
57 | 57 | @SuppressWarnings("unused") |
58 | 58 | public static class ContainerRequestContextAdvice { |
59 | 59 |
|
| 60 | + public static class AdviceScope { |
| 61 | + private Jaxrs2HandlerData handlerData; |
| 62 | + private Context context; |
| 63 | + private Scope scope; |
| 64 | + |
| 65 | + public AdviceScope enter( |
| 66 | + Class<?> resourceClass, Method method, AbstractRequestContextImpl requestContext) { |
| 67 | + handlerData = new Jaxrs2HandlerData(resourceClass, method); |
| 68 | + context = |
| 69 | + Jaxrs2RequestContextHelper.createOrUpdateAbortSpan( |
| 70 | + instrumenter(), (ContainerRequestContext) requestContext, handlerData); |
| 71 | + if (context != null) { |
| 72 | + scope = context.makeCurrent(); |
| 73 | + } |
| 74 | + |
| 75 | + return this; |
| 76 | + } |
| 77 | + |
| 78 | + public void exit(@Nullable Throwable throwable) { |
| 79 | + if (scope == null) { |
| 80 | + return; |
| 81 | + } |
| 82 | + scope.close(); |
| 83 | + instrumenter().end(context, handlerData, null, throwable); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @Nullable |
60 | 88 | @Advice.OnMethodEnter(suppress = Throwable.class) |
61 | | - public static void decorateAbortSpan( |
62 | | - @Advice.This AbstractRequestContextImpl requestContext, |
63 | | - @Local("otelHandlerData") Jaxrs2HandlerData handlerData, |
64 | | - @Local("otelContext") Context context, |
65 | | - @Local("otelScope") Scope scope) { |
| 89 | + public static AdviceScope decorateAbortSpan( |
| 90 | + @Advice.This AbstractRequestContextImpl requestContext) { |
66 | 91 |
|
67 | 92 | if (requestContext.getProperty(JaxrsConstants.ABORT_HANDLED) != null |
68 | 93 | || !(requestContext instanceof ContainerRequestContext)) { |
69 | | - return; |
| 94 | + return null; |
70 | 95 | } |
71 | 96 |
|
72 | 97 | Message message = requestContext.getMessage(); |
73 | 98 | OperationResourceInfoStack resourceInfoStack = |
74 | 99 | (OperationResourceInfoStack) |
75 | 100 | message.get("org.apache.cxf.jaxrs.model.OperationResourceInfoStack"); |
76 | 101 | if (resourceInfoStack == null || resourceInfoStack.isEmpty()) { |
77 | | - return; |
| 102 | + return null; |
78 | 103 | } |
79 | 104 |
|
80 | 105 | MethodInvocationInfo invocationInfo = resourceInfoStack.peek(); |
81 | 106 | Method method = invocationInfo.getMethodInfo().getMethodToInvoke(); |
82 | 107 | Class<?> resourceClass = invocationInfo.getRealClass(); |
83 | 108 |
|
84 | | - handlerData = new Jaxrs2HandlerData(resourceClass, method); |
85 | | - context = |
86 | | - Jaxrs2RequestContextHelper.createOrUpdateAbortSpan( |
87 | | - instrumenter(), (ContainerRequestContext) requestContext, handlerData); |
88 | | - if (context != null) { |
89 | | - scope = context.makeCurrent(); |
90 | | - } |
| 109 | + AdviceScope adviceScope = new AdviceScope(); |
| 110 | + return adviceScope.enter(resourceClass, method, requestContext); |
91 | 111 | } |
92 | 112 |
|
93 | 113 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
94 | 114 | public static void stopSpan( |
95 | | - @Local("otelHandlerData") Jaxrs2HandlerData handlerData, |
96 | | - @Local("otelContext") Context context, |
97 | | - @Local("otelScope") Scope scope, |
98 | | - @Advice.Thrown Throwable throwable) { |
99 | | - if (scope == null) { |
100 | | - return; |
| 115 | + @Advice.Thrown Throwable throwable, @Advice.Enter @Nullable AdviceScope adviceScope) { |
| 116 | + if (adviceScope != null) { |
| 117 | + adviceScope.exit(throwable); |
101 | 118 | } |
102 | | - scope.close(); |
103 | | - instrumenter().end(context, handlerData, null, throwable); |
104 | 119 | } |
105 | 120 | } |
106 | 121 | } |
0 commit comments