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:

  1. Why is the method1() instrumentation not working?
  2. Is there an issue with the typeMatcher or transform logic in MyLibraryInstrumentation?
  3. How can I debug the instrumentation process to identify what is going wrong?

本文标签: open telemetryOpenTelemetry Java AutoInstrumentation for Custom Library Not WorkingStack Overflow