opentelemetry_spanprocessor_any/global/mod.rs
1//! Utilities for working with global telemetry primitives
2//!
3//! ## Global Trace API
4//!
5//! The global trace API **provides applications access to their configured
6//! [`TracerProvider`] instance from anywhere in the codebase**. This allows
7//! applications to be less coupled to the specific Open Telemetry SDK while not
8//! manually passing references to each part of the code that needs to create
9//! [`Span`]s. Additionally, **3rd party middleware** or **library code** can be
10//! written against this generic API and not constrain users to a specific
11//! implementation choice.
12//!
13//! ### Usage in Applications
14//!
15//! Applications configure their tracer either by [installing a trace pipeline],
16//! or calling [`set_tracer_provider`].
17//!
18//! ```
19//! # #[cfg(feature="trace")]
20//! # {
21//! use opentelemetry::trace::{Tracer, noop::NoopTracerProvider};
22//! use opentelemetry::global;
23//!
24//! fn init_tracer() {
25//! let provider = NoopTracerProvider::new();
26//!
27//! // Configure the global `TracerProvider` singleton when your app starts
28//! // (there is a no-op default if this is not set by your application)
29//! let _ = global::set_tracer_provider(provider);
30//! }
31//!
32//! fn do_something_tracked() {
33//! // Then you can get a named tracer instance anywhere in your codebase.
34//! let tracer = global::tracer("my-component");
35//!
36//! tracer.in_span("doing_work", |cx| {
37//! // Traced app logic here...
38//! });
39//! }
40//!
41//! // in main or other app start
42//! let _ = init_tracer();
43//! do_something_tracked();
44//! # }
45//! ```
46//!
47//! ### Usage in Libraries
48//!
49//! ```
50//! # #[cfg(feature="trace")]
51//! # {
52//! use opentelemetry::trace::{Tracer, TracerProvider};
53//! use opentelemetry::global;
54//!
55//! pub fn my_traced_library_function() {
56//! // End users of your library will configure their global tracer provider
57//! // so you can use the global tracer without any setup
58//! let tracer = global::tracer_provider().versioned_tracer(
59//! "my-library-name",
60//! Some(env!("CARGO_PKG_VERSION")),
61//! None,
62//! );
63//!
64//! tracer.in_span("doing_library_work", |cx| {
65//! // Traced library logic here...
66//! });
67//! }
68//! # }
69//! ```
70//!
71//! [installing a trace pipeline]: crate::sdk::export::trace::stdout::PipelineBuilder::install_simple
72//! [`TracerProvider`]: crate::trace::TracerProvider
73//! [`Span`]: crate::trace::Span
74//!
75//! ## Global Metrics API
76//!
77//! The global metrics API **provides applications access to their configured
78//! [`MeterProvider`] instance from anywhere in the codebase**. This allows
79//! applications to be less coupled to the specific Open Telemetry SDK while not
80//! manually passing references to each part of the code that needs to create
81//! metric instruments. Additionally, **3rd party middleware** or **library code** can be
82//! written against this generic API and not constrain users to a specific
83//! implementation choice.
84//!
85//! ### Usage in Applications
86//!
87//! Applications configure their meter either by [installing a metrics pipeline],
88//! or calling [`set_meter_provider`].
89//!
90//! ```
91//! # #[cfg(feature="metrics")]
92//! # {
93//! use opentelemetry::metrics::{Meter, noop::NoopMeterProvider};
94//! use opentelemetry::{global, KeyValue};
95//!
96//! fn init_meter() {
97//! let provider = NoopMeterProvider::new();
98//!
99//! // Configure the global `MeterProvider` singleton when your app starts
100//! // (there is a no-op default if this is not set by your application)
101//! global::set_meter_provider(provider)
102//! }
103//!
104//! fn do_something_instrumented() {
105//! // Then you can get a named tracer instance anywhere in your codebase.
106//! let meter = global::meter("my-component");
107//! let counter = meter.u64_counter("my_counter").init();
108//!
109//! // record metrics
110//! counter.add(1, &[KeyValue::new("mykey", "myvalue")]);
111//! }
112//!
113//! // in main or other app start
114//! init_meter();
115//! do_something_instrumented();
116//! # }
117//! ```
118//!
119//! ### Usage in Libraries
120//!
121//! ```
122//! # #[cfg(feature="metrics")]
123//! # {
124//! use opentelemetry::{global, KeyValue};
125//!
126//! pub fn my_traced_library_function() {
127//! // End users of your library will configure their global meter provider
128//! // so you can use the global meter without any setup
129//! let tracer = global::meter("my-library-name");
130//! let counter = tracer.u64_counter("my_counter").init();
131//!
132//! // record metrics
133//! counter.add(1, &[KeyValue::new("mykey", "myvalue")]);
134//! }
135//! # }
136//! ```
137//!
138//! [installing a metrics pipeline]: crate::sdk::export::metrics::stdout::StdoutExporterBuilder::init
139//! [`MeterProvider`]: crate::metrics::MeterProvider
140//! [`set_meter_provider`]: crate::global::set_meter_provider
141
142mod error_handler;
143#[cfg(feature = "metrics")]
144mod metrics;
145#[cfg(feature = "trace")]
146mod propagation;
147#[cfg(feature = "trace")]
148mod trace;
149
150pub use error_handler::{handle_error, set_error_handler, Error};
151#[cfg(feature = "metrics")]
152#[cfg_attr(docsrs, doc(cfg(feature = "metrics")))]
153pub use metrics::{
154 meter, meter_provider, meter_with_version, set_meter_provider, GlobalMeterProvider,
155};
156#[cfg(feature = "trace")]
157#[cfg_attr(docsrs, doc(cfg(feature = "trace")))]
158pub use propagation::{get_text_map_propagator, set_text_map_propagator};
159#[cfg(feature = "trace")]
160#[cfg_attr(docsrs, doc(cfg(feature = "trace")))]
161pub use trace::{
162 force_flush_tracer_provider, set_tracer_provider, shutdown_tracer_provider, tracer,
163 tracer_provider, BoxedSpan, BoxedTracer, GlobalTracerProvider, ObjectSafeTracer,
164 ObjectSafeTracerProvider,
165};