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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
use crate::{global, trace::SpanContext, Context, ContextGuard, KeyValue};
use std::error::Error;
use std::sync::Mutex;
lazy_static::lazy_static! {
static ref NOOP_SPAN: SynchronizedSpan = SynchronizedSpan {
span_context: SpanContext::empty_context(),
inner: None,
};
}
#[derive(Debug)]
pub struct SpanRef<'a>(&'a SynchronizedSpan);
#[derive(Debug)]
struct SynchronizedSpan {
span_context: SpanContext,
inner: Option<Box<Mutex<dyn crate::trace::Span + Send + Sync>>>,
}
impl SpanRef<'_> {
fn with_inner_mut<F: FnOnce(&mut dyn crate::trace::Span)>(&self, f: F) {
if let Some(ref inner) = self.0.inner {
match inner.lock() {
Ok(mut locked) => f(&mut *locked),
Err(err) => global::handle_error(err),
}
}
}
}
impl SpanRef<'_> {
pub fn add_event(&self, name: String, attributes: Vec<KeyValue>) {
self.with_inner_mut(|inner| inner.add_event(name, attributes))
}
pub fn record_exception(&self, err: &dyn Error) {
self.with_inner_mut(|inner| inner.record_exception(err))
}
pub fn record_exception_with_stacktrace(&self, err: &dyn Error, stacktrace: String) {
self.with_inner_mut(|inner| inner.record_exception_with_stacktrace(err, stacktrace))
}
pub fn add_event_with_timestamp(
&self,
name: String,
timestamp: std::time::SystemTime,
attributes: Vec<crate::KeyValue>,
) {
self.with_inner_mut(move |inner| {
inner.add_event_with_timestamp(name, timestamp, attributes)
})
}
pub fn span_context(&self) -> &SpanContext {
&self.0.span_context
}
pub fn is_recording(&self) -> bool {
self.0
.inner
.as_ref()
.and_then(|inner| inner.lock().ok().map(|active| active.is_recording()))
.unwrap_or(false)
}
pub fn set_attribute(&self, attribute: crate::KeyValue) {
self.with_inner_mut(move |inner| inner.set_attribute(attribute))
}
pub fn set_status(&self, code: super::StatusCode, message: String) {
self.with_inner_mut(move |inner| inner.set_status(code, message))
}
pub fn update_name(&self, new_name: String) {
self.with_inner_mut(move |inner| inner.update_name(new_name))
}
pub fn end(&self) {
self.end_with_timestamp(crate::time::now());
}
pub fn end_with_timestamp(&self, timestamp: std::time::SystemTime) {
self.with_inner_mut(move |inner| inner.end_with_timestamp(timestamp))
}
}
pub trait TraceContextExt {
fn current_with_span<T: crate::trace::Span + Send + Sync + 'static>(span: T) -> Self;
fn with_span<T: crate::trace::Span + Send + Sync + 'static>(&self, span: T) -> Self;
fn span(&self) -> SpanRef<'_>;
fn has_active_span(&self) -> bool;
fn with_remote_span_context(&self, span_context: crate::trace::SpanContext) -> Self;
}
impl TraceContextExt for Context {
fn current_with_span<T: crate::trace::Span + Send + Sync + 'static>(span: T) -> Self {
Context::current_with_value(SynchronizedSpan {
span_context: span.span_context().clone(),
inner: Some(Box::new(Mutex::new(span))),
})
}
fn with_span<T: crate::trace::Span + Send + Sync + 'static>(&self, span: T) -> Self {
self.with_value(SynchronizedSpan {
span_context: span.span_context().clone(),
inner: Some(Box::new(Mutex::new(span))),
})
}
fn span(&self) -> SpanRef<'_> {
if let Some(span) = self.get::<SynchronizedSpan>() {
SpanRef(span)
} else {
SpanRef(&*NOOP_SPAN)
}
}
fn has_active_span(&self) -> bool {
self.get::<SynchronizedSpan>().is_some()
}
fn with_remote_span_context(&self, span_context: crate::trace::SpanContext) -> Self {
self.with_value(SynchronizedSpan {
span_context,
inner: None,
})
}
}
#[must_use = "Dropping the guard detaches the context."]
pub fn mark_span_as_active<T: crate::trace::Span + Send + Sync + 'static>(span: T) -> ContextGuard {
let cx = Context::current_with_span(span);
cx.attach()
}
pub fn get_active_span<F, T>(f: F) -> T
where
F: FnOnce(SpanRef<'_>) -> T,
{
f(Context::current().span())
}