opentelemetry-java/api/all/src/main/java/io/opentelemetry/api/GlobalOpenTelemetry.java

179 lines
6.7 KiB
Java

/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.api;
import static java.util.Objects.requireNonNull;
import io.opentelemetry.api.metrics.Meter;
import io.opentelemetry.api.metrics.MeterProvider;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.api.trace.TracerProvider;
import io.opentelemetry.context.propagation.ContextPropagators;
import io.opentelemetry.spi.OpenTelemetryFactory;
import io.opentelemetry.spi.metrics.MeterProviderFactory;
import io.opentelemetry.spi.trace.TracerProviderFactory;
import javax.annotation.Nullable;
/**
* A global singleton for the entrypoint to telemetry functionality for tracing, metrics and
* baggage.
*
* <p>The global singleton can be retrieved by {@link #get()}. The default for the returned {@link
* OpenTelemetry}, if none has been set via {@link #set(OpenTelemetry)}, will be created with any
* {@link OpenTelemetryFactory}, {@link TracerProviderFactory} or {@link MeterProviderFactory} found
* on the classpath, or otherwise will be default, with no-op behavior.
*
* <p>If using the OpenTelemetry SDK, you may want to instantiate the {@link OpenTelemetry} to
* provide configuration, for example of {@code Resource} or {@code Sampler}. See {@code
* OpenTelemetrySdk} and {@code OpenTelemetrySdk.builder} for information on how to construct the
* SDK {@link OpenTelemetry}.
*
* @see TracerProvider
* @see MeterProvider
* @see ContextPropagators
*/
public final class GlobalOpenTelemetry {
private static final Object mutex = new Object();
@Nullable private static volatile OpenTelemetry globalOpenTelemetry;
private GlobalOpenTelemetry() {}
/**
* Returns the registered global {@link OpenTelemetry}. If no call to {@link #set(OpenTelemetry)}
* has been made so far, a default {@link OpenTelemetry} composed of functionality any {@link
* OpenTelemetryFactory}, {@link TracerProviderFactory} or{@link MeterProviderFactory}, found on
* the classpath, or otherwise will be default, with no-op behavior.
*
* @throws IllegalStateException if a provider has been specified by system property using the
* interface FQCN but the specified provider cannot be found.
*/
public static OpenTelemetry get() {
if (globalOpenTelemetry == null) {
synchronized (mutex) {
if (globalOpenTelemetry == null) {
OpenTelemetryFactory openTelemetryFactory = Utils.loadSpi(OpenTelemetryFactory.class);
if (openTelemetryFactory != null) {
set(openTelemetryFactory.create());
} else {
set(DefaultOpenTelemetry.builder().build());
}
}
}
}
return globalOpenTelemetry;
}
/**
* Sets the {@link OpenTelemetry} that should be the global instance. Future calls to {@link
* #get()} will return the provided {@link OpenTelemetry} instance. This should be called once as
* early as possible in your application initialization logic, often in a {@code static} block in
* your main class.
*/
public static void set(OpenTelemetry openTelemetry) {
globalOpenTelemetry = openTelemetry;
}
// for testing
static void reset() {
globalOpenTelemetry = null;
}
/** Returns the globally registered {@link TracerProvider}. */
public static TracerProvider getTracerProvider() {
return get().getTracerProvider();
}
/**
* Gets or creates a named tracer instance from the globally registered {@link TracerProvider}.
*
* <p>This is a shortcut method for {@code getTracerProvider().get(instrumentationName)}
*
* @param instrumentationName The name of the instrumentation library, not the name of the
* instrument*ed* library (e.g., "io.opentelemetry.contrib.mongodb"). Must not be null.
* @return a tracer instance.
*/
public static Tracer getTracer(String instrumentationName) {
return get().getTracer(instrumentationName);
}
/**
* Gets or creates a named and versioned tracer instance from the globally registered {@link
* TracerProvider}.
*
* <p>This is a shortcut method for {@code getTracerProvider().get(instrumentationName,
* instrumentationVersion)}
*
* @param instrumentationName The name of the instrumentation library, not the name of the
* instrument*ed* library (e.g., "io.opentelemetry.contrib.mongodb"). Must not be null.
* @param instrumentationVersion The version of the instrumentation library (e.g., "1.0.0").
* @return a tracer instance.
*/
public static Tracer getTracer(String instrumentationName, String instrumentationVersion) {
return get().getTracer(instrumentationName, instrumentationVersion);
}
/**
* Returns the globally registered {@link MeterProvider}.
*
* @deprecated this will be removed soon in preparation for the initial otel release.
*/
@Deprecated
public static MeterProvider getMeterProvider() {
return get().getMeterProvider();
}
/**
* Gets or creates a named meter instance from the globally registered {@link MeterProvider}.
*
* <p>This is a shortcut method for {@code getMeterProvider().get(instrumentationName)}
*
* @param instrumentationName The name of the instrumentation library, not the name of the
* instrument*ed* library.
* @return a tracer instance.
* @deprecated this will be removed soon in preparation for the initial otel release.
*/
@Deprecated
public static Meter getMeter(String instrumentationName) {
return get().getMeter(instrumentationName);
}
/**
* Gets or creates a named and versioned meter instance from the globally registered {@link
* MeterProvider}.
*
* <p>This is a shortcut method for {@code getMeterProvider().get(instrumentationName,
* instrumentationVersion)}
*
* @param instrumentationName The name of the instrumentation library, not the name of the
* instrument*ed* library.
* @param instrumentationVersion The version of the instrumentation library.
* @return a tracer instance.
* @deprecated this will be removed soon in preparation for the initial otel release.
*/
@Deprecated
public static Meter getMeter(String instrumentationName, String instrumentationVersion) {
return get().getMeter(instrumentationName, instrumentationVersion);
}
/**
* Returns the globally registered {@link ContextPropagators} for remote propagation of a context.
*/
public static ContextPropagators getPropagators() {
return get().getPropagators();
}
/**
* Sets the globally registered {@link ContextPropagators} for remote propagation of a context.
*
* @deprecated this will be removed soon, create a new instance if necessary.
*/
@Deprecated
public static void setPropagators(ContextPropagators propagators) {
requireNonNull(propagators, "propagators");
get().setPropagators(propagators);
}
}