@@ -126,17 +126,52 @@ pub use layer::{layer, OpenTelemetryLayer};
126
126
127
127
#[ cfg( feature = "metrics" ) ]
128
128
pub use metrics:: MetricsLayer ;
129
+ use opentelemetry:: trace:: TraceContextExt as _;
129
130
pub use span_ext:: OpenTelemetrySpanExt ;
130
131
131
132
/// Per-span OpenTelemetry data tracked by this crate.
132
133
#[ derive( Debug ) ]
133
- pub ( crate ) struct OtelData {
134
+ pub struct OtelData {
134
135
/// The state of the OtelData, which can either be a builder or a context.
135
136
state : OtelDataState ,
136
137
/// The end time of the span if it has been exited.
137
138
end_time : Option < SystemTime > ,
138
139
}
139
140
141
+ impl OtelData {
142
+ /// Gets the trace ID of the span.
143
+ ///
144
+ /// Returns `None` if the context has not been built yet. This can be forced e.g. by calling
145
+ /// [`context`] on the span (not on `OtelData`) or if [context activation] was not explicitly
146
+ /// opted-out of, simply entering the span for the first time.
147
+ ///
148
+ /// [`context`]: OpenTelemetrySpanExt::context
149
+ /// [context activation]: OpenTelemetryLayer::with_context_activation
150
+ pub fn trace_id ( & self ) -> Option < opentelemetry:: TraceId > {
151
+ if let OtelDataState :: Context { current_cx } = & self . state {
152
+ Some ( current_cx. span ( ) . span_context ( ) . trace_id ( ) )
153
+ } else {
154
+ None
155
+ }
156
+ }
157
+
158
+ /// Gets the span ID of the span.
159
+ ///
160
+ /// Returns `None` if the context has not been built yet. This can be forced e.g. by calling
161
+ /// [`context`] on the span (not on `OtelData`) or if [context activation] was not explicitly
162
+ /// opted-out of, simply entering the span for the first time.
163
+ ///
164
+ /// [`context`]: OpenTelemetrySpanExt::context
165
+ /// [context activation]: OpenTelemetryLayer::with_context_activation
166
+ pub fn span_id ( & self ) -> Option < opentelemetry:: SpanId > {
167
+ if let OtelDataState :: Context { current_cx } = & self . state {
168
+ Some ( current_cx. span ( ) . span_context ( ) . span_id ( ) )
169
+ } else {
170
+ None
171
+ }
172
+ }
173
+ }
174
+
140
175
/// The state of the OpenTelemetry data for a span.
141
176
#[ derive( Debug ) ]
142
177
#[ allow( clippy:: large_enum_variant) ]
0 commit comments