|
30 | 30 | import io.opentelemetry.javaagent.instrumentation.jaxrs.JaxrsServerSpanNaming; |
31 | 31 | import java.lang.reflect.Method; |
32 | 32 | import java.util.concurrent.CompletionStage; |
| 33 | +import javax.annotation.Nullable; |
33 | 34 | import javax.ws.rs.Path; |
34 | 35 | import javax.ws.rs.container.AsyncResponse; |
35 | 36 | import net.bytebuddy.asm.Advice; |
| 37 | +import net.bytebuddy.asm.Advice.AssignReturned; |
36 | 38 | import net.bytebuddy.description.type.TypeDescription; |
37 | 39 | import net.bytebuddy.implementation.bytecode.assign.Assigner.Typing; |
38 | 40 | import net.bytebuddy.matcher.ElementMatcher; |
@@ -69,96 +71,113 @@ public void transform(TypeTransformer transformer) { |
69 | 71 | @SuppressWarnings("unused") |
70 | 72 | public static class JaxRsAnnotationsAdvice { |
71 | 73 |
|
72 | | - @Advice.OnMethodEnter(suppress = Throwable.class) |
73 | | - public static void nameSpan( |
74 | | - @Advice.This Object target, |
75 | | - @Advice.Origin Method method, |
76 | | - @Advice.AllArguments Object[] args, |
77 | | - @Advice.Local("otelCallDepth") CallDepth callDepth, |
78 | | - @Advice.Local("otelHandlerData") Jaxrs2HandlerData handlerData, |
79 | | - @Advice.Local("otelContext") Context context, |
80 | | - @Advice.Local("otelScope") Scope scope, |
81 | | - @Advice.Local("otelAsyncResponse") AsyncResponse asyncResponse) { |
82 | | - callDepth = CallDepth.forClass(Path.class); |
83 | | - if (callDepth.getAndIncrement() > 0) { |
84 | | - return; |
| 74 | + public static class AdviceScope { |
| 75 | + private final CallDepth callDepth; |
| 76 | + private Jaxrs2HandlerData handlerData; |
| 77 | + private AsyncResponse asyncResponse; |
| 78 | + private Context context; |
| 79 | + private Scope scope; |
| 80 | + |
| 81 | + public AdviceScope(CallDepth callDepth) { |
| 82 | + this.callDepth = callDepth; |
85 | 83 | } |
86 | 84 |
|
87 | | - VirtualField<AsyncResponse, AsyncResponseData> virtualField = null; |
88 | | - for (Object arg : args) { |
89 | | - if (arg instanceof AsyncResponse) { |
90 | | - asyncResponse = (AsyncResponse) arg; |
91 | | - virtualField = VirtualField.find(AsyncResponse.class, AsyncResponseData.class); |
92 | | - if (virtualField.get(asyncResponse) != null) { |
93 | | - /* |
94 | | - * We are probably in a recursive call and don't want to start a new span because it |
95 | | - * would replace the existing span in the asyncResponse and cause it to never finish. We |
96 | | - * could work around this by using a list instead, but we likely don't want the extra |
97 | | - * span anyway. |
98 | | - */ |
99 | | - return; |
| 85 | + public AdviceScope enter(Object[] args, Object target, Method method) { |
| 86 | + if (callDepth.getAndIncrement() > 0) { |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + VirtualField<AsyncResponse, AsyncResponseData> virtualField = null; |
| 91 | + for (Object arg : args) { |
| 92 | + if (arg instanceof AsyncResponse) { |
| 93 | + asyncResponse = (AsyncResponse) arg; |
| 94 | + virtualField = VirtualField.find(AsyncResponse.class, AsyncResponseData.class); |
| 95 | + if (virtualField.get(asyncResponse) != null) { |
| 96 | + /* |
| 97 | + * We are probably in a recursive call and don't want to start a new span because it |
| 98 | + * would replace the existing span in the asyncResponse and cause it to never finish. We |
| 99 | + * could work around this by using a list instead, but we likely don't want the extra |
| 100 | + * span anyway. |
| 101 | + */ |
| 102 | + return this; |
| 103 | + } |
| 104 | + break; |
100 | 105 | } |
101 | | - break; |
102 | 106 | } |
103 | | - } |
104 | 107 |
|
105 | | - Context parentContext = Java8BytecodeBridge.currentContext(); |
106 | | - handlerData = new Jaxrs2HandlerData(target.getClass(), method); |
| 108 | + Context parentContext = Java8BytecodeBridge.currentContext(); |
| 109 | + handlerData = new Jaxrs2HandlerData(target.getClass(), method); |
107 | 110 |
|
108 | | - HttpServerRoute.update( |
109 | | - parentContext, |
110 | | - HttpServerRouteSource.CONTROLLER, |
111 | | - JaxrsServerSpanNaming.SERVER_SPAN_NAME, |
112 | | - handlerData); |
| 111 | + HttpServerRoute.update( |
| 112 | + parentContext, |
| 113 | + HttpServerRouteSource.CONTROLLER, |
| 114 | + JaxrsServerSpanNaming.SERVER_SPAN_NAME, |
| 115 | + handlerData); |
113 | 116 |
|
114 | | - if (!instrumenter().shouldStart(parentContext, handlerData)) { |
115 | | - return; |
116 | | - } |
| 117 | + if (!instrumenter().shouldStart(parentContext, handlerData)) { |
| 118 | + return this; |
| 119 | + } |
117 | 120 |
|
118 | | - context = instrumenter().start(parentContext, handlerData); |
119 | | - scope = context.makeCurrent(); |
| 121 | + context = instrumenter().start(parentContext, handlerData); |
| 122 | + scope = context.makeCurrent(); |
120 | 123 |
|
121 | | - if (virtualField != null && asyncResponse != null) { |
122 | | - virtualField.set(asyncResponse, AsyncResponseData.create(context, handlerData)); |
| 124 | + if (virtualField != null && asyncResponse != null) { |
| 125 | + virtualField.set(asyncResponse, AsyncResponseData.create(context, handlerData)); |
| 126 | + } |
| 127 | + return this; |
123 | 128 | } |
124 | | - } |
125 | 129 |
|
126 | | - @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
127 | | - public static void stopSpan( |
128 | | - @Advice.Return(readOnly = false, typing = Typing.DYNAMIC) Object returnValue, |
129 | | - @Advice.Thrown Throwable throwable, |
130 | | - @Advice.Local("otelCallDepth") CallDepth callDepth, |
131 | | - @Advice.Local("otelHandlerData") Jaxrs2HandlerData handlerData, |
132 | | - @Advice.Local("otelContext") Context context, |
133 | | - @Advice.Local("otelScope") Scope scope, |
134 | | - @Advice.Local("otelAsyncResponse") AsyncResponse asyncResponse) { |
135 | | - if (callDepth.decrementAndGet() > 0) { |
136 | | - return; |
137 | | - } |
| 130 | + @Nullable |
| 131 | + public Object exit(@Nullable Throwable throwable, @Nullable Object returnValue) { |
138 | 132 |
|
139 | | - if (scope == null) { |
140 | | - return; |
141 | | - } |
| 133 | + if (callDepth.decrementAndGet() > 0) { |
| 134 | + return returnValue; |
| 135 | + } |
142 | 136 |
|
143 | | - scope.close(); |
| 137 | + if (scope == null) { |
| 138 | + return returnValue; |
| 139 | + } |
144 | 140 |
|
145 | | - if (throwable != null) { |
146 | | - instrumenter().end(context, handlerData, null, throwable); |
147 | | - return; |
148 | | - } |
| 141 | + scope.close(); |
149 | 142 |
|
150 | | - CompletionStage<?> asyncReturnValue = |
151 | | - returnValue instanceof CompletionStage ? (CompletionStage<?>) returnValue : null; |
152 | | - if (asyncReturnValue != null) { |
153 | | - // span finished by CompletionStageFinishCallback |
154 | | - asyncReturnValue = |
155 | | - asyncReturnValue.handle( |
156 | | - new CompletionStageFinishCallback<>(instrumenter(), context, handlerData)); |
157 | | - } |
158 | | - if (asyncResponse == null && asyncReturnValue == null) { |
159 | | - instrumenter().end(context, handlerData, null, null); |
| 143 | + if (throwable != null) { |
| 144 | + instrumenter().end(context, handlerData, null, throwable); |
| 145 | + return returnValue; |
| 146 | + } |
| 147 | + |
| 148 | + CompletionStage<?> asyncReturnValue = |
| 149 | + returnValue instanceof CompletionStage ? (CompletionStage<?>) returnValue : null; |
| 150 | + if (asyncReturnValue != null) { |
| 151 | + // span finished by CompletionStageFinishCallback |
| 152 | + asyncReturnValue = |
| 153 | + asyncReturnValue.handle( |
| 154 | + new CompletionStageFinishCallback<>(instrumenter(), context, handlerData)); |
| 155 | + } |
| 156 | + if (asyncResponse == null && asyncReturnValue == null) { |
| 157 | + instrumenter().end(context, handlerData, null, null); |
| 158 | + } |
| 159 | + // else span finished by AsyncResponse*Advice |
| 160 | + return returnValue; |
160 | 161 | } |
161 | | - // else span finished by AsyncResponse*Advice |
| 162 | + } |
| 163 | + |
| 164 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 165 | + public static AdviceScope nameSpan( |
| 166 | + @Advice.This Object target, |
| 167 | + @Advice.Origin Method method, |
| 168 | + @Advice.AllArguments Object[] args) { |
| 169 | + AdviceScope adviceScope = new AdviceScope(CallDepth.forClass(Path.class)); |
| 170 | + return adviceScope.enter(args, target, method); |
| 171 | + } |
| 172 | + |
| 173 | + @AssignReturned.ToReturned |
| 174 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 175 | + public static Object stopSpan( |
| 176 | + @Advice.Return(typing = Typing.DYNAMIC) Object returnValue, |
| 177 | + @Advice.Thrown Throwable throwable, |
| 178 | + @Advice.Enter AdviceScope adviceScope) { |
| 179 | + |
| 180 | + return adviceScope.exit(throwable, returnValue); |
162 | 181 | } |
163 | 182 | } |
164 | 183 | } |
0 commit comments