Opentelemetry configuration for auto and manual instrumentation

314 views Asked by At

In an application auto instrumented with the javaagent, it looks possible to add manual instrumentation according to the documentation.

  • For auto instrumentation, I know we can use environment variables or system properties. For instance OTEL_TRACES_EXPORTER=logging to enable logging exporter for traces.
  • Now, for manual instrumentation, we should instantiate somehow a OpenTelemetrySdk and register a SdkTracerProvider (configured either manually or via env variables/system properties as well)

What happens if auto instrumentation configures some exporters (logging for instance) and manual instrumentation define another one (OTLP/HTTP for instance)?

Is there a unique instance of OpenTelemetry in this case or two living each in "different contexts"?

Several questions but related to the same topic: how manual and auto instrumentation live together?

(For additional context, I'm looking to have manual metrics, auto instrumented traces and potentially some manual traces as well)

1

There are 1 answers

0
Gaël J On

Even though the question is a bit broad, let me try to give some hints to users that would read it.

  1. Auto instrumentation with the java agent and manual instrumentation may live together with two distinct OpenTelemetry instances. The java agent would instantiate and register the global instance (GlobalOpenTelemetry) and your code may instantiate another OpenTelemetry but not register it as the global one.
    • This will work but should probably be avoided as long as possible because you may encounter situations where some telemetry data would be "attached" to one OpenTelemetry instance or the other and you'd loose "correlation". For instance, with traces, you may get two traces instead of one.
    • In this scenario, manual instrumentation may be configured either entirely manually by instantiating a OpenTelemetrySdk with manual configs, or by using environment variables / system properties as the java agent is doing thanks to opentelemetry-sdk-extension-autoconfigure dependency.
  2. Another approach, better when possible, is to use the java agent, configure it with environment variables / system properties and then reuse the global instance in your code thanks to GlobalOpenTelemetry.get().
    • If you're using a DI framework (Spring, Guice...), your code should rely on having a OpenTelemetry instance and use your framework to provide it by calling GlobalOpenTelemetry.get() at startup.