Module opentelemetry_api::global
source · Expand description
Utilities for working with global telemetry primitives
Global Trace API
The global trace API provides applications access to their configured
TracerProvider
instance from anywhere in the codebase. This allows
applications to be less coupled to the specific Open Telemetry SDK while not
manually passing references to each part of the code that needs to create
Span
s. Additionally, 3rd party middleware or library code can be
written against this generic API and not constrain users to a specific
implementation choice.
Usage in Applications
Applications configure their tracer either by installing a trace pipeline,
or calling set_tracer_provider
.
use opentelemetry_api::trace::{Tracer, noop::NoopTracerProvider};
use opentelemetry_api::global;
fn init_tracer() {
// Swap this no-op provider for your tracing service of choice (jaeger, zipkin, etc)
let provider = NoopTracerProvider::new();
// Configure the global `TracerProvider` singleton when your app starts
// (there is a no-op default if this is not set by your application)
let _ = global::set_tracer_provider(provider);
}
fn do_something_tracked() {
// Then you can get a named tracer instance anywhere in your codebase.
let tracer = global::tracer("my-component");
tracer.in_span("doing_work", |cx| {
// Traced app logic here...
});
}
// in main or other app start
init_tracer();
do_something_tracked();
Usage in Libraries
use opentelemetry_api::trace::{Tracer, TracerProvider};
use opentelemetry_api::global;
pub fn my_traced_library_function() {
// End users of your library will configure their global tracer provider
// so you can use the global tracer without any setup
let tracer = global::tracer_provider().versioned_tracer(
"my-library-name",
Some(env!("CARGO_PKG_VERSION")),
Some("https://opentelemetry.io/schemas/1.17.0"),
None,
);
tracer.in_span("doing_library_work", |cx| {
// Traced library logic here...
});
}
Global Metrics API
The global metrics API provides applications access to their configured
MeterProvider
instance from anywhere in the codebase. This allows
applications to be less coupled to the specific Open Telemetry SDK while not
manually passing references to each part of the code that needs to create
metric instruments. Additionally, 3rd party middleware or library code can be
written against this generic API and not constrain users to a specific
implementation choice.
Usage in Applications
Applications configure their meter either by installing a metrics pipeline,
or calling set_meter_provider
.
use opentelemetry_api::metrics::{Meter, noop::NoopMeterProvider};
use opentelemetry_api::{global, KeyValue};
fn init_meter() {
let provider = NoopMeterProvider::new();
// Configure the global `MeterProvider` singleton when your app starts
// (there is a no-op default if this is not set by your application)
global::set_meter_provider(provider)
}
fn do_something_instrumented() {
// Then you can get a named tracer instance anywhere in your codebase.
let meter = global::meter("my-component");
let counter = meter.u64_counter("my_counter").init();
// record metrics
counter.add(1, &[KeyValue::new("mykey", "myvalue")]);
}
// in main or other app start
init_meter();
do_something_instrumented();
Usage in Libraries
use opentelemetry_api::{global, KeyValue};
pub fn my_traced_library_function() {
// End users of your library will configure their global meter provider
// so you can use the global meter without any setup
let tracer = global::meter("my-library-name");
let counter = tracer.u64_counter("my_counter").init();
// record metrics
counter.add(1, &[KeyValue::new("mykey", "myvalue")]);
}
Structs
- Wraps the
BoxedTracer
’sSpan
so it can be used generically by applications without knowing the underlying type. - Wraps the
GlobalTracerProvider
’sTracer
so it can be used generically by applications without knowing the underlying type. - Represents the globally configured
LoggerProvider
instance. - Represents the globally configured
MeterProvider
instance for this application. - Represents the globally configured
TracerProvider
instance for this application. This allows generic tracing through the returnedBoxedTracer
instances.
Enums
- Wrapper for error from both tracing and metrics part of open telemetry.
Traits
- Allows a specific
LoggerProvider
to be used generically, by mirroring the interface, and boxing the returned types. - Allows a specific MeterProvider to be used generically by the GlobalMeterProvider by mirroring the interface and boxing the return types.
- Allows a specific
crate::trace::Span
to be used generically byBoxedSpan
instances by mirroring the interface and boxing the return types. - Allows a specific
Tracer
to be used generically byBoxedTracer
instances by mirroring the interface and boxing the return types. - Allows a specific
TracerProvider
to be used generically by theGlobalTracerProvider
by mirroring the interface and boxing the return types.
Functions
- Executes a closure with a reference to the current global
TextMapPropagator
propagator. - Handle error using the globally configured error handler.
- Creates a named instance of
Logger
via the configuredGlobalLoggerProvider
. - Returns an instance of the currently configured global
LoggerProvider
throughGlobalLoggerProvider
. - Creates a named
Meter
via the configuredGlobalMeterProvider
. - Returns an instance of the currently configured global
MeterProvider
throughGlobalMeterProvider
. - Creates a
Meter
with the name, version and schema url. - Set global error handler.
- Sets the given
LoggerProvider
instance as the current global provider, returning theLoggerProvider
instance that was previously set as global provider. - Sets the given
MeterProvider
instance as the current global meter provider. - Sets the given
TextMapPropagator
propagator as the current global propagator. - Sets the given
TracerProvider
instance as the current global provider. - Shut down the current global
LoggerProvider
. - Shut down the current tracer provider. This will invoke the shutdown method on all span processors. span processors should export remaining spans before return
- Creates a named instance of
Tracer
via the configuredGlobalTracerProvider
. - Returns an instance of the currently configured global
TracerProvider
throughGlobalTracerProvider
.