Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.util.Collection;
import java.util.Map;
import javax.annotation.Nullable;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.asm.Advice.AssignReturned;
import net.bytebuddy.description.enumeration.EnumerationDescription;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.implementation.bytecode.assign.Assigner;
Expand Down Expand Up @@ -91,42 +93,60 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class MethodAdvice {

public static class AdviceScope {
private final MethodAndType classAndMethod;
private final Context context;
private final Scope scope;

private AdviceScope(MethodAndType classAndMethod, Context context, Scope scope) {
this.classAndMethod = classAndMethod;
this.context = context;
this.scope = scope;
}

@Nullable
public static AdviceScope start(
SpanKind spanKind, Class<?> declaringClass, String methodName) {
Context parentContext = currentContext();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could use Context.current()

MethodAndType methodAndType =
MethodAndType.create(ClassAndMethod.create(declaringClass, methodName), spanKind);

if (!instrumenter().shouldStart(parentContext, methodAndType)) {
return null;
}
Context context = instrumenter().start(parentContext, methodAndType);
return new AdviceScope(methodAndType, context, context.makeCurrent());
}

public Object end(
Class<?> methodReturnType, Object returnValue, @Nullable Throwable throwable) {
scope.close();
return AsyncOperationEndSupport.create(instrumenter(), Void.class, methodReturnType)
.asyncEnd(context, classAndMethod, returnValue, throwable);
}
}

@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
public static AdviceScope onEnter(
@MethodSpanKind SpanKind spanKind,
@Advice.Origin("#t") Class<?> declaringClass,
@Advice.Origin("#m") String methodName,
@Advice.Local("otelMethod") MethodAndType classAndMethod,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
Context parentContext = currentContext();
classAndMethod =
MethodAndType.create(ClassAndMethod.create(declaringClass, methodName), spanKind);

if (!instrumenter().shouldStart(parentContext, classAndMethod)) {
return;
}

context = instrumenter().start(parentContext, classAndMethod);
scope = context.makeCurrent();
@Advice.Origin("#m") String methodName) {
return AdviceScope.start(spanKind, declaringClass, methodName);
}

@AssignReturned.ToReturned
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
public static Object stopSpan(
@MethodReturnType Class<?> methodReturnType,
@Advice.Local("otelMethod") MethodAndType classAndMethod,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope,
@Advice.Return(typing = Assigner.Typing.DYNAMIC, readOnly = false) Object returnValue,
@Advice.Thrown Throwable throwable) {
if (scope == null) {
return;
}
scope.close();
@Advice.Return(typing = Assigner.Typing.DYNAMIC) Object returnValue,
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter @Nullable AdviceScope adviceScope) {

returnValue =
AsyncOperationEndSupport.create(instrumenter(), Void.class, methodReturnType)
.asyncEnd(context, classAndMethod, returnValue, throwable);
if (adviceScope != null) {
return adviceScope.end(methodReturnType, returnValue, throwable);
}
return returnValue;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,16 @@
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
import io.opentelemetry.javaagent.tooling.config.MethodsConfigurationParser;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

@AutoService(InstrumentationModule.class)
public class MethodInstrumentationModule extends InstrumentationModule {
public class MethodInstrumentationModule extends InstrumentationModule
implements ExperimentalInstrumentationModule {

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

Expand Down Expand Up @@ -66,4 +68,9 @@ private static List<TypeInstrumentation> parseConfigProperties() {
public List<TypeInstrumentation> typeInstrumentations() {
return typeInstrumentations;
}

@Override
public boolean isIndyReady() {
return true;
}
}
Loading