Class GlobalOpenTelemetry
- java.lang.Object
-
- io.opentelemetry.api.GlobalOpenTelemetry
-
public final class GlobalOpenTelemetry extends java.lang.Object
A global singleton for the entrypoint to telemetry functionality for tracing, metrics and baggage.If using the OpenTelemetry SDK, you may want to instantiate the
OpenTelemetry
to provide configuration, for example ofResource
orSampler
. SeeOpenTelemetrySdk
andOpenTelemetrySdk.builder
for information on how to construct the SDK'sOpenTelemetry
implementation.WARNING: Due to the inherent complications around initialization order involving this class and its single global instance, we strongly recommend *not* using GlobalOpenTelemetry unless you have a use-case that absolutely requires it. Please favor using instances of OpenTelemetry wherever possible.
If you are using the OpenTelemetry javaagent, it is generally best to only call GlobalOpenTelemetry.get() once, and then pass the resulting reference where you need to use it.
- See Also:
TracerProvider
,ContextPropagators
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
GlobalOpenTelemetry.ObfuscatedOpenTelemetry
Static global instances are obfuscated when they are returned from the API to prevent users from casting them to their SDK-specific implementation.
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
GLOBAL_AUTOCONFIGURE_ENABLED_PROPERTY
private static GlobalOpenTelemetry.ObfuscatedOpenTelemetry
globalOpenTelemetry
private static java.util.logging.Logger
logger
private static java.lang.Object
mutex
private static java.lang.Throwable
setGlobalCaller
-
Constructor Summary
Constructors Modifier Constructor Description private
GlobalOpenTelemetry()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static OpenTelemetry
get()
Returns the registered globalOpenTelemetry
.static Meter
getMeter(java.lang.String instrumentationScopeName)
Gets or creates a named meter instance from the globally registeredMeterProvider
.static MeterProvider
getMeterProvider()
Returns the globally registeredMeterProvider
.static ContextPropagators
getPropagators()
Returns the globally registeredContextPropagators
for remote propagation of a context.static Tracer
getTracer(java.lang.String instrumentationScopeName)
Gets or creates a named tracer instance from the globally registeredTracerProvider
.static Tracer
getTracer(java.lang.String instrumentationScopeName, java.lang.String instrumentationScopeVersion)
Gets or creates a named and versioned tracer instance from the globally registeredTracerProvider
.static TracerProvider
getTracerProvider()
Returns the globally registeredTracerProvider
.private static OpenTelemetry
maybeAutoConfigureAndSetGlobal()
static MeterBuilder
meterBuilder(java.lang.String instrumentationScopeName)
Creates a MeterBuilder for a namedMeter
instance.static void
resetForTest()
Unsets the globalOpenTelemetry
.static void
set(OpenTelemetry openTelemetry)
Sets theOpenTelemetry
that should be the global instance.static TracerBuilder
tracerBuilder(java.lang.String instrumentationScopeName)
Creates a TracerBuilder for a namedTracer
instance.
-
-
-
Field Detail
-
GLOBAL_AUTOCONFIGURE_ENABLED_PROPERTY
private static final java.lang.String GLOBAL_AUTOCONFIGURE_ENABLED_PROPERTY
- See Also:
- Constant Field Values
-
logger
private static final java.util.logging.Logger logger
-
mutex
private static final java.lang.Object mutex
-
globalOpenTelemetry
@Nullable private static volatile GlobalOpenTelemetry.ObfuscatedOpenTelemetry globalOpenTelemetry
-
setGlobalCaller
@Nullable private static java.lang.Throwable setGlobalCaller
-
-
Method Detail
-
get
public static OpenTelemetry get()
Returns the registered globalOpenTelemetry
.- Throws:
java.lang.IllegalStateException
- if a provider has been specified by system property using the interface FQCN but the specified provider cannot be found.
-
set
public static void set(OpenTelemetry openTelemetry)
Sets theOpenTelemetry
that should be the global instance. Future calls toget()
will return the providedOpenTelemetry
instance. This should be called once as early as possible in your application initialization logic, often in astatic
block 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 globalOpenTelemetry
multiple 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.
-
getTracerProvider
public static TracerProvider getTracerProvider()
Returns the globally registeredTracerProvider
.
-
getTracer
public static Tracer getTracer(java.lang.String instrumentationScopeName)
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
public static Tracer getTracer(java.lang.String instrumentationScopeName, java.lang.String instrumentationScopeVersion)
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
public static TracerBuilder tracerBuilder(java.lang.String instrumentationScopeName)
Creates a TracerBuilder for a namedTracer
instance.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
public static MeterProvider getMeterProvider()
Returns the globally registeredMeterProvider
.- Since:
- 1.10.0
-
getMeter
public static Meter getMeter(java.lang.String instrumentationScopeName)
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
public static MeterBuilder meterBuilder(java.lang.String instrumentationScopeName)
Creates a MeterBuilder for a namedMeter
instance.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 ContextPropagators getPropagators()
Returns the globally registeredContextPropagators
for remote propagation of a context.
-
maybeAutoConfigureAndSetGlobal
@Nullable private static OpenTelemetry maybeAutoConfigureAndSetGlobal()
-
-