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
69
70
71
72
73
74
75
76
77
78
use crate::api;
lazy_static::lazy_static! {
static ref NOOP_SPAN: api::NoopSpan = api::NoopSpan::new();
}
struct Span(Box<dyn api::Span>);
struct RemoteSpanContext(api::SpanContext);
pub trait TraceContextExt {
fn current_with_span<T: api::Span>(span: T) -> Self;
fn with_span<T: api::Span>(&self, span: T) -> Self;
fn span(&self) -> &dyn api::Span;
fn with_remote_span_context(&self, span_context: api::SpanContext) -> Self;
fn remote_span_context(&self) -> Option<&api::SpanContext>;
}
impl TraceContextExt for api::Context {
fn current_with_span<T: api::Span>(span: T) -> Self {
api::Context::current_with_value(Span(Box::new(span)))
}
fn with_span<T: api::Span>(&self, span: T) -> Self {
self.with_value(Span(Box::new(span)))
}
fn span(&self) -> &dyn api::Span {
if let Some(span) = self.get::<Span>() {
span.0.as_ref()
} else {
&*NOOP_SPAN
}
}
fn with_remote_span_context(&self, span_context: api::SpanContext) -> Self {
self.with_value(RemoteSpanContext(span_context))
}
fn remote_span_context(&self) -> Option<&api::SpanContext> {
self.get::<RemoteSpanContext>()
.map(|span_context| &span_context.0)
}
}