admin管理员组文章数量:1122832
I am trying to create auto-instrumentation for a custom library (MyLibrary) using OpenTelemetry Java Instrumentation. My goal is to generate traces for the method1() method in MyLibrary, which takes no parameters and writes to a file.
I have created a Java agent and the following classes for instrumentation:
MyLibraryInstrumentationModule
@AutoService(InstrumentationModule.class)
public class MyLibraryInstrumentationModule extends InstrumentationModule {
public MyLibraryInstrumentationModule() {
super("my-library");
}
@Override
public List<TypeInstrumentation> typeInstrumentations() {
return Collections.singletonList(new MyLibraryInstrumentation());
}
}
MyLibrarySingletons
public class MyLibrarySingletons {
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.mylibrary";
private static final Instrumenter<String, Object> INSTRUMENTER;
static {
SpanNameExtractor<String> spanNameExtractor = String::toString;
InstrumenterBuilder<String, Object> builder =
Instrumenter.<String, Object>builder(
GlobalOpenTelemetry.get(), INSTRUMENTATION_NAME, spanNameExtractor);
// .addAttributesExtractor();
INSTRUMENTER = builder.buildInstrumenter();
}
public static Instrumenter<String, Object> instrumenter() {
return INSTRUMENTER;
}
private MyLibrarySingletons() {}
}
MyLibraryInstrumentation
public class MyLibraryInstrumentation implements TypeInstrumentation {
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
return named("method1");
}
@Override
public void transform(TypeTransformer transformer) {
transformer.applyAdviceToMethod(
isPublic().and(named("method1")).and(takesNoArguments()),
this.getClass().getName() + "$MethodAdvice");
}
@SuppressWarnings("unused")
public static class MethodAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void methodEnter(
@Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) {
Context parentContext = currentContext();
instrumenter().start(parentContext, "this is request from start");
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void methodExit(
@Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) {
if (scope == null) {
return;
}
scope.close();
instrumenter()
.end(
context,
"This is request end of the method1",
"This is response for end of method1",
null);
}
}
}
Issue:
- I created the Java agent JAR with the above code .
- I then launched the application with -javaagent:path/to/opentelemetry-javaagent.jar. The application runs fine, but no traces are generated for method1() in my MyLibrary.
Debugging Steps I Tried:
- Verified that method1() in MyLibrary is being called.
- Confirmed that the Java agent is loading properly by adding other instrumentation, which worked as expected.
- Checked that the InstrumentationModule is correctly registered in META-INF/services/io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule.
Questions:
- Why is the method1() instrumentation not working?
- Is there an issue with the typeMatcher or transform logic in MyLibraryInstrumentation?
- How can I debug the instrumentation process to identify what is going wrong?
本文标签: open telemetryOpenTelemetry Java AutoInstrumentation for Custom Library Not WorkingStack Overflow
版权声明:本文标题:open telemetry - OpenTelemetry Java Auto-Instrumentation for Custom Library Not Working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736282114a1926538.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论