[−][src]Module opentelemetry::api::context
OpenTelemetry Context API
A Context is a propagation mechanism which carries execution-scoped
values across API boundaries and between logically associated execution
units. Cross-cutting concerns access their data in-process using the same
shared context object.
Contexts are immutable, and their write operations result in the creation
of a new context containing the original values and the new specified values.
Context state
Concerns can create and retrieve their local state in the current execution
state represented by a context through the get and with_value
methods. It is recommended to use application-specific types when storing new
context values to avoid unintentionally overwriting existing state.
Managing the current context
Contexts can be associated with the caller's current execution unit on a
given thread via the attach method, and previous contexts can be restored
by dropping the returned ContextGuard. Context can be nested, and will
restore their parent outer context when detached on drop. To access the
values of the context, a snapshot can be created via the Context::current
method.
Examples
use opentelemetry::api::Context; // Application-specific `a` and `b` values #[derive(Debug, PartialEq)] struct ValueA(&'static str); #[derive(Debug, PartialEq)] struct ValueB(u64); let _outer_guard = Context::new().with_value(ValueA("a")).attach(); // Only value a has been set let current = Context::current(); assert_eq!(current.get::<ValueA>(), Some(&ValueA("a"))); assert_eq!(current.get::<ValueB>(), None); { let _inner_guard = Context::current_with_value(ValueB(42)).attach(); // Both values are set in inner context let current = Context::current(); assert_eq!(current.get::<ValueA>(), Some(&ValueA("a"))); assert_eq!(current.get::<ValueB>(), Some(&ValueB(42))); } // Resets to only the `a` value when inner guard is dropped let current = Context::current(); assert_eq!(current.get::<ValueA>(), Some(&ValueA("a"))); assert_eq!(current.get::<ValueB>(), None);
Modules
| propagation | OpenTelemetry Propagator interface |
Structs
| Context | An execution-scoped collection of values. |
| ContextGuard | A guard that resets the current context to the prior context when dropped. |