Skip to content

Commit 3ac0d81

Browse files
authored
make methods indy-ready (#15128)
1 parent 39137d1 commit 3ac0d81

File tree

2 files changed

+56
-30
lines changed

2 files changed

+56
-30
lines changed

instrumentation/methods/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/methods/MethodInstrumentation.java

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
package io.opentelemetry.javaagent.instrumentation.methods;
77

8-
import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext;
98
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
109
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasSuperType;
1110
import static io.opentelemetry.javaagent.instrumentation.methods.MethodSingletons.getBootstrapLoader;
@@ -25,7 +24,9 @@
2524
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
2625
import java.util.Collection;
2726
import java.util.Map;
27+
import javax.annotation.Nullable;
2828
import net.bytebuddy.asm.Advice;
29+
import net.bytebuddy.asm.Advice.AssignReturned;
2930
import net.bytebuddy.description.enumeration.EnumerationDescription;
3031
import net.bytebuddy.description.type.TypeDescription;
3132
import net.bytebuddy.implementation.bytecode.assign.Assigner;
@@ -91,42 +92,60 @@ public void transform(TypeTransformer transformer) {
9192
@SuppressWarnings("unused")
9293
public static class MethodAdvice {
9394

95+
public static class AdviceScope {
96+
private final MethodAndType classAndMethod;
97+
private final Context context;
98+
private final Scope scope;
99+
100+
private AdviceScope(MethodAndType classAndMethod, Context context, Scope scope) {
101+
this.classAndMethod = classAndMethod;
102+
this.context = context;
103+
this.scope = scope;
104+
}
105+
106+
@Nullable
107+
public static AdviceScope start(
108+
SpanKind spanKind, Class<?> declaringClass, String methodName) {
109+
Context parentContext = Context.current();
110+
MethodAndType methodAndType =
111+
MethodAndType.create(ClassAndMethod.create(declaringClass, methodName), spanKind);
112+
113+
if (!instrumenter().shouldStart(parentContext, methodAndType)) {
114+
return null;
115+
}
116+
Context context = instrumenter().start(parentContext, methodAndType);
117+
return new AdviceScope(methodAndType, context, context.makeCurrent());
118+
}
119+
120+
public Object end(
121+
Class<?> methodReturnType, Object returnValue, @Nullable Throwable throwable) {
122+
scope.close();
123+
return AsyncOperationEndSupport.create(instrumenter(), Void.class, methodReturnType)
124+
.asyncEnd(context, classAndMethod, returnValue, throwable);
125+
}
126+
}
127+
128+
@Nullable
94129
@Advice.OnMethodEnter(suppress = Throwable.class)
95-
public static void onEnter(
130+
public static AdviceScope onEnter(
96131
@MethodSpanKind SpanKind spanKind,
97132
@Advice.Origin("#t") Class<?> declaringClass,
98-
@Advice.Origin("#m") String methodName,
99-
@Advice.Local("otelMethod") MethodAndType classAndMethod,
100-
@Advice.Local("otelContext") Context context,
101-
@Advice.Local("otelScope") Scope scope) {
102-
Context parentContext = currentContext();
103-
classAndMethod =
104-
MethodAndType.create(ClassAndMethod.create(declaringClass, methodName), spanKind);
105-
106-
if (!instrumenter().shouldStart(parentContext, classAndMethod)) {
107-
return;
108-
}
109-
110-
context = instrumenter().start(parentContext, classAndMethod);
111-
scope = context.makeCurrent();
133+
@Advice.Origin("#m") String methodName) {
134+
return AdviceScope.start(spanKind, declaringClass, methodName);
112135
}
113136

137+
@AssignReturned.ToReturned
114138
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
115-
public static void stopSpan(
139+
public static Object stopSpan(
116140
@MethodReturnType Class<?> methodReturnType,
117-
@Advice.Local("otelMethod") MethodAndType classAndMethod,
118-
@Advice.Local("otelContext") Context context,
119-
@Advice.Local("otelScope") Scope scope,
120-
@Advice.Return(typing = Assigner.Typing.DYNAMIC, readOnly = false) Object returnValue,
121-
@Advice.Thrown Throwable throwable) {
122-
if (scope == null) {
123-
return;
124-
}
125-
scope.close();
141+
@Advice.Return(typing = Assigner.Typing.DYNAMIC) Object returnValue,
142+
@Advice.Thrown @Nullable Throwable throwable,
143+
@Advice.Enter @Nullable AdviceScope adviceScope) {
126144

127-
returnValue =
128-
AsyncOperationEndSupport.create(instrumenter(), Void.class, methodReturnType)
129-
.asyncEnd(context, classAndMethod, returnValue, throwable);
145+
if (adviceScope != null) {
146+
return adviceScope.end(methodReturnType, returnValue, throwable);
147+
}
148+
return returnValue;
130149
}
131150
}
132151
}

instrumentation/methods/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/methods/MethodInstrumentationModule.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,16 @@
1515
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig;
1616
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
1717
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
18+
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
1819
import io.opentelemetry.javaagent.tooling.config.MethodsConfigurationParser;
1920
import java.util.List;
2021
import java.util.Map;
2122
import java.util.Set;
2223
import java.util.stream.Collectors;
2324

2425
@AutoService(InstrumentationModule.class)
25-
public class MethodInstrumentationModule extends InstrumentationModule {
26+
public class MethodInstrumentationModule extends InstrumentationModule
27+
implements ExperimentalInstrumentationModule {
2628

2729
private static final String TRACE_METHODS_CONFIG = "otel.instrumentation.methods.include";
2830

@@ -66,4 +68,9 @@ private static List<TypeInstrumentation> parseConfigProperties() {
6668
public List<TypeInstrumentation> typeInstrumentations() {
6769
return typeInstrumentations;
6870
}
71+
72+
@Override
73+
public boolean isIndyReady() {
74+
return true;
75+
}
6976
}

0 commit comments

Comments
 (0)