1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use crateWithContext;
use Dispatch;
use ExtensionsMut;
/// Utility functions to allow tracing [`ExtensionsMut`]s to return
/// [OpenTelemetry] [`Context`]s.
///
/// [`ExtensionsMut`]: tracing_subscriber::registry::ExtensionsMut
/// [OpenTelemetry]: https://opentelemetry.io
/// [`Context`]: opentelemetry::Context
///
/// Extracts the OpenTelemetry [`Context`] associated with this span extensions.
///
/// This method retrieves the OpenTelemetry context data that has been stored
/// for the span by the OpenTelemetry layer. The context includes the span's
/// OpenTelemetry span context, which contains trace ID, span ID, and other
/// trace-related metadata.
///
/// [`Context`]: opentelemetry::Context
///
/// # Examples
///
/// ```rust
/// use tracing_opentelemetry::get_otel_context;
/// use tracing::dispatcher::WeakDispatch;
/// use tracing_subscriber::registry::LookupSpan;
/// use opentelemetry::trace::TraceContextExt;
///
/// fn do_things_with_otel_context<'a, D>(
/// span_ref: &tracing_subscriber::registry::SpanRef<'a, D>,
/// weak_dispatch: &WeakDispatch
/// ) where
/// D: LookupSpan<'a>,
/// {
/// if let Some(dispatch) = weak_dispatch.upgrade() {
/// if let Some(otel_context) = get_otel_context(&mut span_ref.extensions_mut(), &dispatch) {
/// // Process the extracted context
/// let span = otel_context.span();
/// let span_context = span.span_context();
/// if span_context.is_valid() {
/// // Handle the valid context...
/// }
/// }
/// }
/// }
/// ```
///
/// # Use Cases
///
/// - When working with multiple subscriber configurations
/// - When implementing advanced tracing middleware that manages multiple dispatches