Class GlobalOpenTelemetry
OpenTelemetry instance.
WARNING: To avoid inherent complications around initialization ordering, it's best-practice to
pass around instances of OpenTelemetry rather than using GlobalOpenTelemetry.
However, the OpenTelemetry javagent makes the OpenTelemetry instance it installs
available via GlobalOpenTelemetry. As a result, GlobalOpenTelemetry plays an
important role in native instrumentation and application custom instrumentation.
Native instrumentation should use getOrNoop() as the default OpenTelemetry
instance, and expose APIs for setting a custom instance. This results in the following behavior:
- If the OpenTelemetry javaagent is installed, the native instrumentation will default to the
OpenTelemetryit installs. - If the OpenTelemetry javaagent is not installed, the native instrumentation will default to a noop instance.
- If the user explicitly sets a custom instance, it will be used, regardless of whether or not the OpenTelemetry javaagent is installed.
Applications with custom instrumentation should call isSet() once during
initialization to access the javaagent instance or initialize (e.g. isSet() ?
GlobalOpenTelemetry.get() : initializeSdk()), and pass the resulting instance around manually
(or with dependency injection) to install custom instrumentation. This results in the following
behavior:
- If the OpenTelemetry javaagent is installed, custom instrumentation will use the
OpenTelemetryit installs. - If the OpenTelemetry javaagent is not installed, custom instrumentation will use an
OpenTelemetryinstance initialized by the application.
-
Method Summary
Modifier and TypeMethodDescriptionstatic OpenTelemetryget()Returns the registered globalOpenTelemetryif set, else callsset(OpenTelemetry)with a no-opOpenTelemetryinstance and returns that.static MeterGets or creates a named meter instance from the globally registeredMeterProvider.static MeterProviderReturns the globally registeredMeterProvider.static OpenTelemetryReturns the registered globalOpenTelemetryif set, or elseOpenTelemetry.noop().static io.opentelemetry.context.propagation.ContextPropagatorsReturns the globally registeredContextPropagatorsfor remote propagation of a context.static TracerGets or creates a named tracer instance from the globally registeredTracerProvider.static TracerGets or creates a named and versioned tracer instance from the globally registeredTracerProvider.static TracerProviderReturns the globally registeredTracerProvider.static booleanisSet()static MeterBuildermeterBuilder(String instrumentationScopeName) Creates a MeterBuilder for a namedMeterinstance.static voidUnsets the globalOpenTelemetry.static voidset(OpenTelemetry openTelemetry) Sets theOpenTelemetrythat should be the global instance.static voidset(Supplier<OpenTelemetry> supplier) Sets theOpenTelemetrythat should be the global instance.static TracerBuildertracerBuilder(String instrumentationScopeName) Creates a TracerBuilder for a namedTracerinstance.
-
Method Details
-
getOrNoop
Returns the registered globalOpenTelemetryif set, or elseOpenTelemetry.noop().NOTE: if the global instance is set, the response is obfuscated to prevent callers from casting to SDK implementation instances and inappropriately accessing non-instrumentation APIs.
NOTE: This does not result in the
set(OpenTelemetry)side effects ofget().Native instrumentation should use this method to initialize their default
OpenTelemetryinstance. See class javadoc for more details.- Since:
- 1.57.0
-
isSet
public static boolean isSet()ReturnstrueifGlobalOpenTelemetryis set, otherwisefalse.Application custom instrumentation should use this method during initialization. See class javadoc for more details.
- Since:
- 1.57.0
-
get
Returns the registered globalOpenTelemetryif set, else callsset(OpenTelemetry)with a no-opOpenTelemetryinstance and returns that.NOTE: all returned instanced are obfuscated to prevent callers from casting to SDK implementation instances and inappropriately accessing non-instrumentation APIs.
Native instrumentations should use
getOrNoop()instead. See class javadoc for more details.Application custom instrumentation should use
isSet()and only call this if the response istrue. See class javadoc for more details.If the global instance has not been set, and
io.opentelemetry:opentelemetry-sdk-extension-autoconfigureis present, and "otel.java.global-autoconfigure.enabled" istrue, the global instance will be set to an autoconfigured instance instead ofOpenTelemetry.noop().- Throws:
IllegalStateException- if autoconfigure initialization is triggered and fails.
-
set
Sets theOpenTelemetrythat should be the global instance. Future calls toget()will return the providedOpenTelemetryinstance. This should be called once as early as possible in your application initialization logic, often in astaticblock in your main class. It should only be called once - an attempt to call it a second time will result in an error. If trying to set the globalOpenTelemetrymultiple times in tests, useresetForTest()between them.If you are using the OpenTelemetry SDK, you should generally use
OpenTelemetrySdk.builder().buildAndRegisterGlobal()instead of calling this method directly. -
set
Sets theOpenTelemetrythat should be the global instance.This method calls the given
supplierand callsset(OpenTelemetry), all while holding theGlobalOpenTelemetrymutex.- Since:
- 1.52.0
-
getTracerProvider
Returns the globally registeredTracerProvider. -
getTracer
Gets or creates a named tracer instance from the globally registeredTracerProvider.This is a shortcut method for
getTracerProvider().get(instrumentationScopeName)- Parameters:
instrumentationScopeName- A name uniquely identifying the instrumentation scope, such as the instrumentation library, package, or fully qualified class name. Must not be null.- Returns:
- a tracer instance.
-
getTracer
Gets or creates a named and versioned tracer instance from the globally registeredTracerProvider.This is a shortcut method for
getTracerProvider().get(instrumentationScopeName, instrumentationScopeVersion)- Parameters:
instrumentationScopeName- A name uniquely identifying the instrumentation scope, such as the instrumentation library, package, or fully qualified class name. Must not be null.instrumentationScopeVersion- The version of the instrumentation scope (e.g., "1.0.0").- Returns:
- a tracer instance.
-
tracerBuilder
Creates a TracerBuilder for a namedTracerinstance.This is a shortcut method for
get().tracerBuilder(instrumentationScopeName)- Parameters:
instrumentationScopeName- A name uniquely identifying the instrumentation scope, such as the instrumentation library, package, or fully qualified class name. Must not be null.- Returns:
- a TracerBuilder instance.
- Since:
- 1.4.0
-
getMeterProvider
Returns the globally registeredMeterProvider.- Since:
- 1.10.0
-
getMeter
Gets or creates a named meter instance from the globally registeredMeterProvider.This is a shortcut method for
getMeterProvider().get(instrumentationScopeName)- Parameters:
instrumentationScopeName- A name uniquely identifying the instrumentation scope, such as the instrumentation library, package, or fully qualified class name. Must not be null.- Returns:
- a Meter instance.
- Since:
- 1.10.0
-
meterBuilder
Creates a MeterBuilder for a namedMeterinstance.This is a shortcut method for
get().meterBuilder(instrumentationScopeName)- Parameters:
instrumentationScopeName- A name uniquely identifying the instrumentation scope, such as the instrumentation library, package, or fully qualified class name. Must not be null.- Returns:
- a MeterBuilder instance.
- Since:
- 1.10.0
-
resetForTest
public static void resetForTest()Unsets the globalOpenTelemetry. This is only meant to be used from tests which need to reconfigureOpenTelemetry. -
getPropagators
public static io.opentelemetry.context.propagation.ContextPropagators getPropagators()Returns the globally registeredContextPropagatorsfor remote propagation of a context.
-