|
20 | 20 | import io.opentelemetry.context.Scope; |
21 | 21 | import io.opentelemetry.instrumentation.api.incubator.config.internal.InstrumentationConfig; |
22 | 22 | import io.opentelemetry.instrumentation.api.incubator.semconv.util.ClassAndMethod; |
23 | | -import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; |
24 | 23 | import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig; |
25 | 24 | import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
26 | 25 | import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
|
32 | 31 | import java.util.Map; |
33 | 32 | import java.util.Set; |
34 | 33 | import java.util.logging.Logger; |
| 34 | +import javax.annotation.Nullable; |
35 | 35 | import net.bytebuddy.asm.Advice; |
36 | 36 | import net.bytebuddy.description.ByteCodeElement; |
37 | 37 | import net.bytebuddy.description.NamedElement; |
@@ -168,36 +168,48 @@ private static ElementMatcher.Junction<MethodDescription> configureExcludedMetho |
168 | 168 | @SuppressWarnings("unused") |
169 | 169 | public static class ExternalAnnotationAdvice { |
170 | 170 |
|
171 | | - @Advice.OnMethodEnter(suppress = Throwable.class) |
172 | | - public static void onEnter( |
173 | | - @Advice.Origin("#t") Class<?> declaringClass, |
174 | | - @Advice.Origin("#m") String methodName, |
175 | | - @Advice.Local("otelRequest") ClassAndMethod request, |
176 | | - @Advice.Local("otelContext") Context context, |
177 | | - @Advice.Local("otelScope") Scope scope) { |
178 | | - |
179 | | - Context parentContext = Java8BytecodeBridge.currentContext(); |
180 | | - request = ClassAndMethod.create(declaringClass, methodName); |
181 | | - if (!instrumenter().shouldStart(parentContext, request)) { |
182 | | - return; |
| 171 | + public static class AdviceScope { |
| 172 | + private final ClassAndMethod classAndMethod; |
| 173 | + private final Context context; |
| 174 | + private final Scope scope; |
| 175 | + |
| 176 | + private AdviceScope(ClassAndMethod classAndMethod, Context context, Scope scope) { |
| 177 | + this.classAndMethod = classAndMethod; |
| 178 | + this.context = context; |
| 179 | + this.scope = scope; |
| 180 | + } |
| 181 | + |
| 182 | + @Nullable |
| 183 | + public static AdviceScope start(Class<?> declaringClass, String methodName) { |
| 184 | + Context parentContext = Context.current(); |
| 185 | + ClassAndMethod classAndMethod = ClassAndMethod.create(declaringClass, methodName); |
| 186 | + if (!instrumenter().shouldStart(parentContext, classAndMethod)) { |
| 187 | + return null; |
| 188 | + } |
| 189 | + |
| 190 | + Context context = instrumenter().start(parentContext, classAndMethod); |
| 191 | + return new AdviceScope(classAndMethod, context, context.makeCurrent()); |
183 | 192 | } |
184 | 193 |
|
185 | | - context = instrumenter().start(parentContext, request); |
186 | | - scope = context.makeCurrent(); |
| 194 | + public void end(@Nullable Throwable throwable) { |
| 195 | + scope.close(); |
| 196 | + instrumenter().end(context, classAndMethod, null, throwable); |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 200 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 201 | + public static AdviceScope onEnter( |
| 202 | + @Advice.Origin("#t") Class<?> declaringClass, @Advice.Origin("#m") String methodName) { |
| 203 | + return AdviceScope.start(declaringClass, methodName); |
187 | 204 | } |
188 | 205 |
|
189 | 206 | @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
190 | 207 | public static void stopSpan( |
191 | | - @Advice.Local("otelRequest") ClassAndMethod request, |
192 | | - @Advice.Local("otelContext") Context context, |
193 | | - @Advice.Local("otelScope") Scope scope, |
194 | | - @Advice.Thrown Throwable throwable) { |
195 | | - |
196 | | - if (scope == null) { |
197 | | - return; |
| 208 | + @Advice.Thrown @Nullable Throwable throwable, |
| 209 | + @Advice.Enter @Nullable AdviceScope adviceScope) { |
| 210 | + if (adviceScope != null) { |
| 211 | + adviceScope.end(throwable); |
198 | 212 | } |
199 | | - scope.close(); |
200 | | - instrumenter().end(context, request, null, throwable); |
201 | 213 | } |
202 | 214 | } |
203 | 215 | } |
0 commit comments