opentelemetry/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//! // Swap this no-op provider for your tracing service of choice (jaeger, zipkin, etc)
26//! let provider = NoopTracerProvider::new();
27//!
28//! // Configure the global `TracerProvider` singleton when your app starts
29//! // (there is a no-op default if this is not set by your application)
30//! let _ = global::set_tracer_provider(provider);
31//! }
32//!
33//! fn do_something_tracked() {
34//! // Then you can get a named tracer instance anywhere in your codebase.
35//! let tracer = global::tracer("my-component");
36//!
37//! tracer.in_span("doing_work", |cx| {
38//! // Traced app logic here...
39//! });
40//! }
41//!
42//! // in main or other app start
43//! init_tracer();
44//! do_something_tracked();
45//! # }
46//! ```
47//!
48//! ### Usage in Libraries
49//!
50//! ```
51//! # #[cfg(feature="trace")]
52//! # {
53//! use std::sync::Arc;
54//! use opentelemetry::trace::Tracer;
55//! use opentelemetry::global;
56//! use opentelemetry::InstrumentationScope;
57//!
58//! pub fn my_traced_library_function() {
59//! // End users of your library will configure their global tracer provider
60//! // so you can use the global tracer without any setup
61//!
62//! let scope = InstrumentationScope::builder("my_library-name")
63//! .with_version(env!("CARGO_PKG_VERSION"))
64//! .with_schema_url("https://opentelemetry.io/schemas/1.17.0")
65//! .build();
66//!
67//! let tracer = global::tracer_with_scope(scope);
68//!
69//! tracer.in_span("doing_library_work", |cx| {
70//! // Traced library logic here...
71//! });
72//! }
73//! # }
74//! ```
75//!
76//! [`TracerProvider`]: crate::trace::TracerProvider
77//! [`Span`]: crate::trace::Span
78//!
79//! ## Global Metrics API
80//!
81//! The global metrics API **provides applications access to their configured
82//! [`MeterProvider`] instance from anywhere in the codebase**. This allows
83//! applications to be less coupled to the specific Open Telemetry SDK while not
84//! manually passing references to each part of the code that needs to create
85//! metric instruments. Additionally, **3rd party middleware** or **library code** can be
86//! written against this generic API and not constrain users to a specific
87//! implementation choice.
88//!
89//! ### Usage in Applications and libraries
90//!
91//! Applications and libraries can obtain meter from the global meter provider,
92//! and use the meter to create instruments to emit measurements.
93//!
94//! ```
95//! # #[cfg(feature="metrics")]
96//! # {
97//! use opentelemetry::metrics::{Meter};
98//! use opentelemetry::{global, KeyValue};
99//!
100//! fn do_something_instrumented() {
101//! let meter = global::meter("my-component");
102//! // It is recommended to reuse the same counter instance for the
103//! // lifetime of the application
104//! let counter = meter.u64_counter("my_counter").build();
105//!
106//! // record measurements
107//! counter.add(1, &[KeyValue::new("mykey", "myvalue")]);
108//! }
109//! }
110//! ```
111//!
112//! ### Usage in Applications
113//! Application owners have the responsibility to set the global meter provider.
114//! The global meter provider can be set using the [`set_meter_provider`] function.
115//! As set_meter_provider takes ownership of the provider, it is recommended to
116//! provide a clone of the provider, if the application needs to use the provider
117//! later to perform operations like shutdown.
118//! ```
119//! # #[cfg(feature="metrics")]
120//! # {
121//! use opentelemetry::{global, KeyValue};
122//!
123//! fn main() {
124//! // Set the global meter provider
125//! // global::set_meter_provider(my_meter_provider().clone());
126//! }
127//! # }
128//! ```
129//!
130//! [`MeterProvider`]: crate::metrics::MeterProvider
131//! [`set_meter_provider`]: crate::global::set_meter_provider
132
133mod internal_logging;
134#[cfg(feature = "metrics")]
135mod metrics;
136#[cfg(feature = "trace")]
137mod propagation;
138#[cfg(feature = "trace")]
139mod trace;
140
141#[cfg(feature = "metrics")]
142#[cfg_attr(docsrs, doc(cfg(feature = "metrics")))]
143pub use metrics::*;
144#[cfg(feature = "trace")]
145#[cfg_attr(docsrs, doc(cfg(feature = "trace")))]
146pub use propagation::*;
147#[cfg(feature = "trace")]
148#[cfg_attr(docsrs, doc(cfg(feature = "trace")))]
149pub use trace::*;