Nikunj Patel
Nikunj Patel

Reputation: 80

OpenTelemetry Java Auto-Instrumentation for Custom Library Not Working

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:

Debugging Steps I Tried:

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?

Upvotes: 0

Views: 37

Answers (0)

Related Questions