opentelemetry/global/
propagation.rs1use crate::propagation::TextMapPropagator;
2use crate::trace::noop::NoopTextMapPropagator;
3use std::sync::{OnceLock, RwLock};
4
5static GLOBAL_TEXT_MAP_PROPAGATOR: OnceLock<RwLock<Box<dyn TextMapPropagator + Send + Sync>>> =
7 OnceLock::new();
8
9static DEFAULT_TEXT_MAP_PROPAGATOR: OnceLock<NoopTextMapPropagator> = OnceLock::new();
11
12#[inline]
14fn global_text_map_propagator() -> &'static RwLock<Box<dyn TextMapPropagator + Send + Sync>> {
15 GLOBAL_TEXT_MAP_PROPAGATOR.get_or_init(|| RwLock::new(Box::new(NoopTextMapPropagator::new())))
16}
17
18#[inline]
20fn default_text_map_propagator() -> &'static NoopTextMapPropagator {
21 DEFAULT_TEXT_MAP_PROPAGATOR.get_or_init(NoopTextMapPropagator::new)
22}
23
24pub fn set_text_map_propagator<P: TextMapPropagator + Send + Sync + 'static>(propagator: P) {
26 let _lock = global_text_map_propagator()
27 .write()
28 .map(|mut global_propagator| *global_propagator = Box::new(propagator));
29}
30
31pub fn get_text_map_propagator<T, F>(mut f: F) -> T
33where
34 F: FnMut(&dyn TextMapPropagator) -> T,
35{
36 global_text_map_propagator()
37 .read()
38 .map(|propagator| f(&**propagator))
39 .unwrap_or_else(|_| {
40 let default_propagator = default_text_map_propagator();
41 f(default_propagator as &dyn TextMapPropagator)
42 })
43}