fastrace-tracing 0.1.0

A compatibility layer that connects tokio-tracing with fastrace tracing library.
Documentation
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#![doc = include_str!("../README.md")]

use std::borrow::Cow;
use std::cell::LazyCell;
use std::fmt;
use std::marker;
use std::thread;

use fastrace::prelude::SpanContext;
use tracing_core::Event;
use tracing_core::Subscriber;
use tracing_core::field;
use tracing_core::span::Attributes;
use tracing_core::span::Id;
use tracing_core::span::Record;
use tracing_core::span::{self};
use tracing_subscriber::Layer;
use tracing_subscriber::layer::Context;
use tracing_subscriber::registry::LookupSpan;

const FIELD_EXCEPTION_MESSAGE: &str = "exception.message";
const FIELD_EXCEPTION_STACKTRACE: &str = "exception.stacktrace";

/// A compatibility layer for using libraries instrumented with
/// `tokio-tracing` in applications using `fastrace`.
///
/// This layer collects spans and events created using the tokio-tracing ecosystem and
/// forwards them to fastrace's collectors.
///
/// # Example
///
/// ```
/// use fastrace::collector::Config;
/// use fastrace::collector::ConsoleReporter;
/// use fastrace::prelude::SpanContext;
/// use fastrace_tracing::FastraceCompatLayer;
/// use tracing_subscriber::layer::SubscriberExt;
///
/// // Set up fastrace reporter.
/// fastrace::set_reporter(ConsoleReporter, Config::default());
///
/// // Create a tracing subscriber with the FastraceCompatLayer.
/// let subscriber = tracing_subscriber::Registry::default().with(FastraceCompatLayer::new());
/// tracing::subscriber::set_global_default(subscriber).unwrap();
///
/// // Create a fastrace root span.
/// let root = fastrace::Span::root("root", SpanContext::random());
///
/// // Set a fastrace span as the local parent - this is critical for connecting the
/// // tokio-tracing spans with the fastrace span.
/// let _guard = root.set_local_parent();
///
/// // Spans from tokio-tracing will be captured by fastrace.
/// let span = tracing::span!(tracing::Level::TRACE, "my_span");
/// let _enter = span.enter();
///
/// // Events from tokio-tracing will also be captured by fastrace.
/// tracing::info!("This event will be captured by fastrace");
/// ```
pub struct FastraceCompatLayer<S> {
    location: bool,
    with_threads: bool,
    with_level: bool,
    _phantom: marker::PhantomData<S>,
}

struct EventNameFinder {
    name: Option<Cow<'static, str>>,
}

impl field::Visit for EventNameFinder {
    fn record_bool(&mut self, field: &field::Field, value: bool) {
        if field.name() == "message" {
            self.name = Some(value.to_string().into())
        }
    }

    fn record_f64(&mut self, field: &field::Field, value: f64) {
        if field.name() == "message" {
            self.name = Some(value.to_string().into())
        }
    }

    fn record_i64(&mut self, field: &field::Field, value: i64) {
        if field.name() == "message" {
            self.name = Some(value.to_string().into())
        }
    }

    fn record_str(&mut self, field: &field::Field, value: &str) {
        if field.name() == "message" {
            self.name = Some(value.to_string().into())
        }
    }

    fn record_debug(&mut self, field: &field::Field, value: &dyn fmt::Debug) {
        if field.name() == "message" {
            self.name = Some(format!("{:?}", value).into())
        }
    }

    fn record_error(
        &mut self,
        field: &tracing_core::Field,
        value: &(dyn std::error::Error + 'static),
    ) {
        if field.name() == "message" {
            self.name = Some(value.to_string().into())
        }
    }
}

struct EventVisitor<'a> {
    fastrace_event: &'a mut fastrace::Event,
}

impl field::Visit for EventVisitor<'_> {
    fn record_bool(&mut self, field: &field::Field, value: bool) {
        if field.name() == "message" {
            return;
        }

        take_mut::take(self.fastrace_event, |event| {
            event.with_property(|| (field.name(), value.to_string()))
        });
    }

    fn record_f64(&mut self, field: &field::Field, value: f64) {
        if field.name() == "message" {
            return;
        }

        take_mut::take(self.fastrace_event, |event| {
            event.with_property(|| (field.name(), value.to_string()))
        });
    }

    fn record_i64(&mut self, field: &field::Field, value: i64) {
        if field.name() == "message" {
            return;
        }

        take_mut::take(self.fastrace_event, |event| {
            event.with_property(|| (field.name(), value.to_string()))
        });
    }

    fn record_str(&mut self, field: &field::Field, value: &str) {
        if field.name() == "message" {
            return;
        }

        take_mut::take(self.fastrace_event, |event| {
            event.with_property(|| (field.name(), value.to_string()))
        });
    }

    fn record_debug(&mut self, field: &field::Field, value: &dyn fmt::Debug) {
        if field.name() == "message" {
            return;
        }

        take_mut::take(self.fastrace_event, |event| {
            event.with_property(|| (field.name(), format!("{:?}", value)))
        });
    }

    fn record_error(
        &mut self,
        field: &tracing_core::Field,
        value: &(dyn std::error::Error + 'static),
    ) {
        if field.name() == "message" {
            return;
        }

        let mut chain: Vec<String> = Vec::new();
        let mut next_err = value.source();

        while let Some(err) = next_err {
            chain.push(err.to_string());
            next_err = err.source();
        }

        let error_msg = value.to_string();

        take_mut::take(self.fastrace_event, |event| {
            event.with_property(|| (field.name(), error_msg.to_string()))
        });
        take_mut::take(self.fastrace_event, |event| {
            event.with_property(|| (FIELD_EXCEPTION_MESSAGE, error_msg.to_string()))
        });
        take_mut::take(self.fastrace_event, |event| {
            event.with_property(|| (format!("{}.chain", field.name()), format!("{:?}", chain)))
        });
        take_mut::take(self.fastrace_event, |event| {
            event.with_property(|| (FIELD_EXCEPTION_STACKTRACE, format!("{:?}", chain)))
        });
    }
}

struct SpanAttributeVisitor<'a> {
    fastrace_span: &'a mut fastrace::Span,
}

impl field::Visit for SpanAttributeVisitor<'_> {
    fn record_bool(&mut self, field: &field::Field, value: bool) {
        take_mut::take(self.fastrace_span, |span| {
            span.with_property(|| (field.name(), value.to_string()))
        });
    }

    fn record_f64(&mut self, field: &field::Field, value: f64) {
        take_mut::take(self.fastrace_span, |span| {
            span.with_property(|| (field.name(), value.to_string()))
        });
    }

    fn record_i64(&mut self, field: &field::Field, value: i64) {
        take_mut::take(self.fastrace_span, |span| {
            span.with_property(|| (field.name(), value.to_string()))
        });
    }

    fn record_str(&mut self, field: &field::Field, value: &str) {
        take_mut::take(self.fastrace_span, |span| {
            span.with_property(|| (field.name(), value.to_string()))
        });
    }

    fn record_debug(&mut self, field: &field::Field, value: &dyn fmt::Debug) {
        take_mut::take(self.fastrace_span, |span| {
            span.with_property(|| (field.name(), format!("{:?}", value)))
        });
    }

    fn record_error(
        &mut self,
        field: &tracing_core::Field,
        value: &(dyn std::error::Error + 'static),
    ) {
        let mut chain: Vec<String> = Vec::new();
        let mut next_err = value.source();

        while let Some(err) = next_err {
            chain.push(err.to_string());
            next_err = err.source();
        }

        let error_msg = value.to_string();

        take_mut::take(self.fastrace_span, |span| {
            span.with_property(|| (field.name(), error_msg.to_string()))
        });
        take_mut::take(self.fastrace_span, |span| {
            span.with_property(|| (FIELD_EXCEPTION_MESSAGE, error_msg.to_string()))
        });
        take_mut::take(self.fastrace_span, |span| {
            span.with_property(|| (format!("{}.chain", field.name()), format!("{:?}", chain)))
        });
        take_mut::take(self.fastrace_span, |span| {
            span.with_property(|| (FIELD_EXCEPTION_STACKTRACE, format!("{:?}", chain)))
        });
    }
}

impl<S> FastraceCompatLayer<S>
where S: Subscriber + for<'span> LookupSpan<'span>
{
    /// Creates a new [`FastraceCompatLayer`] with default settings.
    pub fn new() -> Self {
        FastraceCompatLayer {
            location: true,
            with_threads: true,
            with_level: false,
            _phantom: marker::PhantomData,
        }
    }

    /// Configures whether source code location information is included in spans.
    ///
    /// When enabled, span properties will include:
    /// - `code.filepath`: The file where the span was created
    /// - `code.namespace`: The module path where the span was created
    /// - `code.lineno`: The line number where the span was created
    ///
    /// Default is `true`.
    pub fn with_location(self, location: bool) -> Self {
        Self { location, ..self }
    }

    /// Configures whether thread information is included in spans.
    ///
    /// When enabled, span properties will include:
    /// - `thread.id`: The numeric ID of the thread
    /// - `thread.name`: The name of the thread (if available)
    ///
    /// Default is `true`.
    pub fn with_threads(self, threads: bool) -> Self {
        Self {
            with_threads: threads,
            ..self
        }
    }

    /// Configures whether level information is included in span properties.
    ///
    /// When enabled, spans will include the tracing level (trace, debug, info, etc.)
    /// as a property named `level`.
    ///
    /// Default is `false`.
    pub fn with_level(self, level: bool) -> Self {
        Self {
            with_level: level,
            ..self
        }
    }

    fn new_fastrace_span(&self, attrs: &Attributes<'_>, ctx: &Context<'_, S>) -> fastrace::Span {
        if let Some(parent) = attrs.parent() {
            // A span can have an _explicit_ parent that is NOT seen by this `Layer` (for which
            // `Context::span` returns `None`. This happens if the parent span is filtered away
            // from the layer by a per-layer filter. In that case, we fall-through to the `else`
            // case, and consider this span a root span.
            if let Some(span) = ctx.span(parent) {
                let extensions = span.extensions();
                return extensions
                    .get::<fastrace::Span>()
                    .map(|parent| {
                        fastrace::Span::enter_with_parent(attrs.metadata().name(), parent)
                    })
                    .unwrap_or_default();
            }
        }

        // Else if the span is inferred from context, look up any available current span.
        if attrs.is_contextual() {
            ctx.lookup_current()
                .and_then(|span| {
                    let extensions = span.extensions();
                    extensions.get::<fastrace::Span>().map(|parent| {
                        fastrace::Span::enter_with_parent(attrs.metadata().name(), parent)
                    })
                })
                .or_else(|| {
                    SpanContext::current_local_parent()
                        .map(|_| fastrace::Span::enter_with_local_parent(attrs.metadata().name()))
                })
                .unwrap_or_else(|| {
                    fastrace::Span::root(attrs.metadata().name(), SpanContext::random())
                })
        // Explicit root spans should have no parent context.
        } else {
            fastrace::Span::root(attrs.metadata().name(), SpanContext::random())
        }
    }
}

impl<S> Default for FastraceCompatLayer<S>
where S: Subscriber + for<'span> LookupSpan<'span>
{
    fn default() -> Self {
        Self::new()
    }
}

thread_local! {
    static THREAD_ID: LazyCell<u64> = LazyCell::new(|| {
        thread_id_integer(thread::current().id())
    });
}

fn thread_id_integer(id: thread::ThreadId) -> u64 {
    let thread_id = format!("{:?}", id);
    thread_id
        .trim_start_matches("ThreadId(")
        .trim_end_matches(')')
        .parse::<u64>()
        .expect("thread ID should parse as an integer")
}

impl<S> Layer<S> for FastraceCompatLayer<S>
where S: Subscriber + for<'span> LookupSpan<'span>
{
    fn on_new_span(&self, attrs: &Attributes<'_>, id: &span::Id, ctx: Context<'_, S>) {
        let span = ctx.span(id).expect("Span not found, this is a bug");

        let mut fastrace_span = self.new_fastrace_span(attrs, &ctx);

        if self.location {
            let meta = attrs.metadata();

            if let Some(filename) = meta.file() {
                fastrace_span =
                    fastrace_span.with_property(|| ("code.filepath", filename.to_string()));
            }

            if let Some(module) = meta.module_path() {
                fastrace_span =
                    fastrace_span.with_property(|| ("code.namespace", module.to_string()));
            }

            if let Some(line) = meta.line() {
                fastrace_span = fastrace_span.with_property(|| ("code.lineno", line.to_string()));
            }
        }

        if self.with_threads {
            THREAD_ID.with(|id| {
                take_mut::take(&mut fastrace_span, |span| {
                    span.with_property(|| ("thread.id", id.to_string()))
                });
            });
            if let Some(name) = std::thread::current().name() {
                fastrace_span = fastrace_span.with_property(|| ("thread.name", name.to_string()));
            }
        }

        if self.with_level {
            fastrace_span =
                fastrace_span.with_property(|| ("level", attrs.metadata().level().as_str()));
        }

        attrs.record(&mut SpanAttributeVisitor {
            fastrace_span: &mut fastrace_span,
        });

        let mut extensions = span.extensions_mut();
        extensions.insert(fastrace_span);
    }

    fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>) {
        let span = ctx.span(id).expect("Span not found, this is a bug");
        let mut extension = span.extensions_mut();
        let Some(fastrace_span) = extension.get_mut::<fastrace::Span>() else {
            return;
        };
        values.record(&mut SpanAttributeVisitor { fastrace_span });
    }

    fn on_event(&self, event: &Event<'_>, ctx: Context<'_, S>) {
        // Ignore events that are not in the context of a span
        if let Some(span) = event.parent().and_then(|id| ctx.span(id)).or_else(|| {
            event
                .is_contextual()
                .then(|| ctx.lookup_current())
                .flatten()
        }) {
            let mut extensions = span.extensions_mut();
            let fastrace_span = extensions.get_mut::<fastrace::Span>();

            if let Some(fastrace_span) = fastrace_span {
                let mut name_finder = EventNameFinder { name: None };
                event.record(&mut name_finder);
                let event_name = name_finder
                    .name
                    .unwrap_or_else(|| Cow::Borrowed(event.metadata().name()));

                let mut fastrace_event = fastrace::Event::new(event_name).with_properties(|| {
                    [
                        ("level", event.metadata().level().as_str().to_string()),
                        ("target", event.metadata().target().to_string()),
                    ]
                });

                if self.location {
                    if let Some(file) = event.metadata().file() {
                        fastrace_event =
                            fastrace_event.with_property(|| ("code.filepath", file.to_string()));
                    }
                    if let Some(module) = event.metadata().module_path() {
                        fastrace_event =
                            fastrace_event.with_property(|| ("code.namespace", module.to_string()));
                    }
                    if let Some(line) = event.metadata().line() {
                        fastrace_event =
                            fastrace_event.with_property(|| ("code.lineno", line.to_string()));
                    }
                }

                event.record(&mut EventVisitor {
                    fastrace_event: &mut fastrace_event,
                });

                fastrace_span.add_event(fastrace_event);
            }
        };
    }
}