-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
span.rs
351 lines (285 loc) · 9.46 KB
/
span.rs
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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
use std::collections::HashMap;
use std::{borrow::Cow, sync::Arc};
use chrono::{DateTime, Utc};
use crate::{ctx::SpanContext, TraceCollector};
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum SpanStatus {
Unknown,
Ok,
Err,
}
/// A `Span` is a representation of a an interval of time spent performing some operation
///
/// A `Span` has a name, metadata, a start and end time and a unique ID. Additionally they
/// have relationships with other Spans that together comprise a Trace
///
///
#[derive(Debug, Clone)]
pub struct Span {
pub name: Cow<'static, str>,
pub ctx: SpanContext,
pub start: Option<DateTime<Utc>>,
pub end: Option<DateTime<Utc>>,
pub status: SpanStatus,
pub metadata: HashMap<Cow<'static, str>, MetaValue>,
pub events: Vec<SpanEvent>,
}
impl Span {
/// Create new span with given context and name.
pub(crate) fn new(name: impl Into<Cow<'static, str>>, ctx: SpanContext) -> Self {
Self {
name: name.into(),
ctx,
start: None,
end: None,
status: SpanStatus::Unknown,
// assume no metadata by default
metadata: HashMap::with_capacity(0),
// assume no events by default
events: Vec::with_capacity(0),
}
}
/// Create new root span.
pub fn root(name: impl Into<Cow<'static, str>>, collector: Arc<dyn TraceCollector>) -> Self {
let ctx = SpanContext::new(collector);
Self::new(name, ctx)
}
/// Record an event on this `Span`
pub fn event(&mut self, event: SpanEvent) {
self.events.push(event);
}
/// Record success on this `Span` setting the status if it isn't already set
pub fn ok(&mut self, msg: impl Into<Cow<'static, str>>) {
self.event(SpanEvent::new(msg));
self.status(SpanStatus::Ok);
}
/// Record an error on this `Span` setting the status if it isn't already set
pub fn error(&mut self, msg: impl Into<Cow<'static, str>>) {
self.event(SpanEvent::new(msg));
self.status(SpanStatus::Err);
}
/// Set status of `Span`
pub fn status(&mut self, status: SpanStatus) {
if self.status == SpanStatus::Unknown {
self.status = status;
}
}
/// Exports this `Span` to its registered collector if any
pub fn export(mut self) {
if let Some(collector) = self.ctx.collector.take() {
collector.export(self)
}
}
/// Create a new child span with the specified name
///
/// Note that the created Span will not be emitted
/// automatically. The caller must explicitly call [`Self::export`].
///
/// See [`SpanRecorder`] for a helper that automatically emits span data.
pub fn child(&self, name: impl Into<Cow<'static, str>>) -> Self {
self.ctx.child(name)
}
/// Link this span to another context.
pub fn link(&mut self, other: &SpanContext) {
self.ctx.links.push((other.trace_id, other.span_id));
}
}
#[derive(Debug, Clone)]
pub struct SpanEvent {
pub time: DateTime<Utc>,
pub msg: Cow<'static, str>,
pub metadata: HashMap<Cow<'static, str>, MetaValue>,
}
impl SpanEvent {
/// Create new event.
pub fn new(msg: impl Into<Cow<'static, str>>) -> Self {
Self {
time: Utc::now(),
msg: msg.into(),
// assume no metadata by default
metadata: HashMap::with_capacity(0),
}
}
/// Set meta data.
pub fn set_metadata(&mut self, key: impl Into<Cow<'static, str>>, value: impl Into<MetaValue>) {
self.metadata.insert(key.into(), value.into());
}
}
/// Values that can be stored in a Span's metadata and events
#[derive(Debug, Clone, PartialEq)]
pub enum MetaValue {
String(Cow<'static, str>),
Float(f64),
Int(i64),
Bool(bool),
}
impl MetaValue {
pub fn string(&self) -> Option<&str> {
match &self {
Self::String(s) => Some(s.as_ref()),
_ => None,
}
}
}
impl From<&'static str> for MetaValue {
fn from(v: &'static str) -> Self {
Self::String(Cow::Borrowed(v))
}
}
impl From<String> for MetaValue {
fn from(v: String) -> Self {
Self::String(Cow::Owned(v))
}
}
impl From<f64> for MetaValue {
fn from(v: f64) -> Self {
Self::Float(v)
}
}
impl From<i64> for MetaValue {
fn from(v: i64) -> Self {
Self::Int(v)
}
}
/// Utility for instrumenting code that produces [`Span`].
///
/// If a [`SpanRecorder`] is created from a [`Span`] it will update the start timestamp
/// of the span on creation, and on Drop will set the finish time and call [`Span::export`]
///
/// If not created with a `Span`, e.g. this request is not being sampled, all operations
/// called on this `SpanRecorder` will be a no-op
#[derive(Debug, Default)]
pub struct SpanRecorder {
span: Option<Span>,
}
impl SpanRecorder {
pub fn new(mut span: Option<Span>) -> Self {
if let Some(span) = span.as_mut() {
span.start = Some(Utc::now());
}
Self { span }
}
/// Set meta data on the [`Span`], if any.
pub fn set_metadata(&mut self, key: impl Into<Cow<'static, str>>, value: impl Into<MetaValue>) {
if let Some(span) = self.span.as_mut() {
span.metadata.insert(key.into(), value.into());
}
}
/// Record an event on the contained `Span` if any
pub fn event(&mut self, event: SpanEvent) {
if let Some(span) = self.span.as_mut() {
span.event(event);
}
}
/// Record success on the contained `Span` if any
pub fn ok(&mut self, meta: impl Into<Cow<'static, str>>) {
if let Some(span) = self.span.as_mut() {
span.ok(meta)
}
}
/// Record an error on the contained `Span` if any
pub fn error(&mut self, meta: impl Into<Cow<'static, str>>) {
if let Some(span) = self.span.as_mut() {
span.error(meta)
}
}
/// Set status of contained `Span` if any
pub fn status(&mut self, status: SpanStatus) {
if let Some(span) = self.span.as_mut() {
span.status(status);
}
}
/// Take the contents of this recorder returning a new recorder
///
/// From this point on `self` will behave as if it were created with no span
pub fn take(&mut self) -> Self {
std::mem::take(self)
}
/// If this `SpanRecorder` has a `Span`, creates a new child of that `Span` and
/// returns a `SpanRecorder` for it. Otherwise returns an empty `SpanRecorder`
pub fn child(&self, name: impl Into<Cow<'static, str>>) -> Self {
Self::new(self.child_span(name))
}
/// Return a reference to the span contained in this SpanRecorder,
/// or None if there is no active span
pub fn span(&self) -> Option<&Span> {
self.span.as_ref()
}
/// Return a child span of the specified name, if this SpanRecorder
/// has an active span, `None` otherwise.
pub fn child_span(&self, name: impl Into<Cow<'static, str>>) -> Option<Span> {
self.span.as_ref().map(|span| span.child(name))
}
/// Link this span to another context.
pub fn link(&mut self, other: &SpanContext) {
if let Some(span) = self.span.as_mut() {
span.link(other);
}
}
}
/// Helper trait to make spans easier to work with
pub trait SpanExt {
/// Return a child_span, if that makes sense
fn child_span(&self, name: &'static str) -> Option<Span>;
}
impl SpanExt for Option<SpanContext> {
fn child_span(&self, name: &'static str) -> Option<Span> {
self.as_ref().child_span(name)
}
}
impl SpanExt for Option<&SpanContext> {
fn child_span(&self, name: &'static str) -> Option<Span> {
self.map(|span| span.child(name))
}
}
impl Drop for SpanRecorder {
fn drop(&mut self) {
if let Some(mut span) = self.span.take() {
let now = Utc::now();
// SystemTime is not monotonic so must also check min
span.start = Some(match span.start {
Some(a) => a.min(now),
None => now,
});
span.end = Some(match span.end {
Some(a) => a.max(now),
None => now,
});
span.export()
}
}
}
#[cfg(test)]
mod tests {
use std::sync::Arc;
use crate::{RingBufferTraceCollector, TraceCollector};
use super::*;
fn make_span(collector: Arc<dyn TraceCollector>) -> Span {
SpanContext::new(collector).child("foo")
}
#[test]
fn test_span() {
let collector = Arc::new(RingBufferTraceCollector::new(5));
let span = make_span(Arc::<RingBufferTraceCollector>::clone(&collector));
assert_eq!(collector.spans().len(), 0);
span.export();
// Should publish span
let spans = collector.spans();
assert_eq!(spans.len(), 1);
}
#[test]
fn test_entered_span() {
let collector = Arc::new(RingBufferTraceCollector::new(5));
let span = make_span(Arc::<RingBufferTraceCollector>::clone(&collector));
let recorder = SpanRecorder::new(Some(span));
std::thread::sleep(std::time::Duration::from_millis(100));
std::mem::drop(recorder);
// Span should have been published on drop with set spans
let spans = collector.spans();
assert_eq!(spans.len(), 1);
let span = &spans[0];
assert!(span.start.is_some());
assert!(span.end.is_some());
assert!(span.start.unwrap() < span.end.unwrap());
}
}