tracing-texray 0.2.0

Tracing layer to view a plaintext timeline of spans and events
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#![doc = include_str!("../README.md")]
#![warn(
    missing_docs,
    rustdoc::missing_crate_level_docs,
    missing_debug_implementations,
    rust_2018_idioms,
    unreachable_pub
)]

mod tracked_spans;
mod tracker;

use parking_lot::Mutex;
use std::borrow::Cow;

use parking_lot::lock_api::{MutexGuard, RawMutex};
use std::collections::HashSet;
use std::fmt::{Debug, Formatter};
use std::io::{stderr, Write};
use std::ops::{Deref, DerefMut};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{Duration, SystemTime};

use tracing::span::{Attributes, Record};

use tracing::subscriber::set_global_default;
use tracing::{Event as TracingEvent, Id, Span, Subscriber};

use tracing_subscriber::layer::Context;
use tracing_subscriber::registry::LookupSpan;
use tracing_subscriber::{Layer, Registry};
use tracker::{EventInfo, FieldSettings, InterestTracker, RootTracker, SpanInfo, TrackedMetadata};

lazy_static::lazy_static! {
    static ref GLOBAL_TEXRAY_LAYER: TeXRayLayer = TeXRayLayer::uninitialized();
}

macro_rules! check_initialized {
    ($self: expr) => {
        if !$self.initialized() {
            return;
        }
    };
}

/// Examine a given span with custom settings
///
/// _Note_: A [`TeXRayLayer`] must be installed as a subscriber.
///
/// # Examples
/// ```no_run
/// use tracing_texray::{Settings, TeXRayLayer};
/// use tracing_subscriber::Registry;
/// use tracing::info_span;
/// use tracing_subscriber::layer::SubscriberExt;
/// let layer = TeXRayLayer::new().enable_events();
/// let subscriber = Registry::default().with(layer);
/// tracing::subscriber::set_global_default(subscriber).unwrap();
/// tracing_texray::examine_with(info_span!("hello"), Settings::auto().enable_events()).in_scope(|| {
///   println!("I'm in this span!");
/// });
/// ```
pub fn examine_with(span: Span, local_settings: Settings) -> Span {
    GLOBAL_TEXRAY_LAYER.dump_on_exit(&span, Some(local_settings.locked()));
    span
}

/// Examine a given span with settings from the base `TeXRayLayer`
///
/// _Note_: A [`TeXRayLayer`] must be installed as a subscriber.
///
/// # Examples
/// ```no_run
/// use tracing_texray::{Settings, TeXRayLayer};
/// use tracing_subscriber::Registry;
/// use tracing::info_span;
/// use tracing_subscriber::layer::SubscriberExt;
/// let layer = TeXRayLayer::new().enable_events();
/// let subscriber = Registry::default().with(layer);
/// tracing::subscriber::set_global_default(subscriber).unwrap();
/// tracing_texray::examine(info_span!("hello")).in_scope(|| {
///   println!("I'm in this span!");
/// });
/// ```
pub fn examine(span: Span) -> Span {
    GLOBAL_TEXRAY_LAYER.dump_on_exit(&span, None);
    span
}

#[derive(Clone, Debug, PartialEq)]
struct Types {
    events: bool,
    spans: bool,
}

/// Settings for [`TeXRayLayer`] output
#[derive(Clone, Debug)]
pub struct Settings {
    width: usize,
    min_duration: Option<Duration>,
    types: Types,
    field_filter: FieldFilter,
    default_output: DynWriter,
}

impl Settings {
    fn locked(self) -> SpanSettings {
        SpanSettings {
            render: RenderSettings {
                width: self.width,
                min_duration: self.min_duration,
                types: self.types,
            },
            fields: FieldSettings::new(self.field_filter),
            out: self.default_output,
        }
    }
}

/// Wrap a dyn writer to get a Debug implementation
#[derive(Clone)]
struct DynWriter {
    inner: Arc<Mutex<dyn Write + Send>>,
}

impl Debug for DynWriter {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "dyn Writer")
    }
}

/// Filter to control which fields are printed along with spans
#[derive(Clone, Debug)]
enum FieldFilter {
    AllowList(HashSet<Cow<'static, str>>),
    DenyList(HashSet<Cow<'static, str>>),
}

impl Default for FieldFilter {
    fn default() -> Self {
        Self::DenyList(HashSet::new())
    }
}

impl FieldFilter {
    fn should_print(&self, field: &str) -> bool {
        if field == "message" {
            return true;
        }
        match &self {
            FieldFilter::DenyList(deny) => !deny.contains(field),
            FieldFilter::AllowList(allow) => allow.contains(field),
        }
    }
}

impl Default for Settings {
    fn default() -> Self {
        Self {
            width: 120,
            min_duration: None,
            types: Types {
                events: false,
                spans: true,
            },
            field_filter: Default::default(),
            default_output: DynWriter {
                inner: Arc::new(Mutex::new(stderr())),
            },
        }
    }
}

impl Settings {
    /// Load settings via term-size & defaults
    ///
    /// By default:
    /// - All fields are printed
    /// - Only spans are printed, events are not
    /// - Spans of any duration are printed
    pub fn auto() -> Self {
        let mut base = Settings::default();
        if let Some((w, _h)) = term_size::dimensions() {
            base.width = w;
        };
        base
    }

    /// Set the max-width when printing output
    #[must_use]
    pub fn width(mut self, width: usize) -> Self {
        self.width = width;
        self
    }

    /// Overwrite the writer [`TexRayLayer`] will output to
    pub fn writer<W: Write + Send + 'static>(&mut self, w: W) -> &mut Self {
        self.default_output = DynWriter {
            inner: Arc::new(Mutex::new(w)),
        };
        self
    }

    /// Print events in addition to spans
    pub fn enable_events(mut self) -> Self {
        self.set_enable_events(true);
        self
    }

    fn set_enable_events(&mut self, enabled: bool) -> &mut Self {
        self.types.events = enabled;
        self
    }

    /// When printing spans & events, only render the following fields
    pub fn only_show_fields(&mut self, fields: &[&'static str]) -> &mut Self {
        self.field_filter =
            FieldFilter::AllowList(fields.iter().map(|item| Cow::Borrowed(*item)).collect());
        self
    }

    /// When printing spans & events, print all fields, except for these fields
    #[must_use]
    pub fn suppress_fields(mut self, fields: &[&'static str]) -> Self {
        self.field_filter =
            FieldFilter::DenyList(fields.iter().map(|item| Cow::Borrowed(*item)).collect());
        self
    }

    /// Only show spans longer than a minimum duration
    pub fn min_duration(&mut self, duration: Duration) -> &mut Self {
        self.min_duration = Some(duration);
        self
    }
}

/// Tracing Layer to display a summary of spans.
///
/// _Note:_ This layer does nothing on its own. It must be used in combination with [`examine`] to
/// print the summary of a specific span.
#[derive(Debug, Clone)]
pub struct TeXRayLayer {
    settings: Arc<Mutex<SettingsContainer>>,
    initialized: Arc<AtomicBool>,
    tracker: Arc<RootTracker>,
}

impl Default for TeXRayLayer {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(Debug)]
enum SettingsContainer {
    Unlocked(Settings),
    Locked(SpanSettings),
}

#[derive(Debug, Clone)]
struct SpanSettings {
    render: RenderSettings,
    fields: FieldSettings,
    out: DynWriter,
}

#[derive(Debug, Clone)]
struct RenderSettings {
    width: usize,
    min_duration: Option<Duration>,
    types: Types,
}

impl Default for RenderSettings {
    fn default() -> Self {
        Self {
            width: 120,
            min_duration: None,
            types: Types {
                events: false,
                spans: true,
            },
        }
    }
}

impl SettingsContainer {
    fn lock_settings(&mut self) -> &SpanSettings {
        match self {
            SettingsContainer::Locked(settings) => settings,
            SettingsContainer::Unlocked(settings) => {
                let cloned = settings.clone();
                *self = SettingsContainer::Locked(cloned.locked());
                self.lock_settings()
            }
        }
    }
    fn settings_mut(&mut self) -> Option<&mut Settings> {
        match self {
            SettingsContainer::Unlocked(settings) => Some(settings),
            SettingsContainer::Locked(_) => None,
        }
    }
}

impl Default for SettingsContainer {
    fn default() -> Self {
        SettingsContainer::Unlocked(Settings::default())
    }
}

/// Initialize a default subscriber and install it as the global default
pub fn init() {
    let layer = TeXRayLayer::new();
    use tracing_subscriber::layer::SubscriberExt;
    let registry = Registry::default().with(layer);
    set_global_default(registry).expect("failed to install subscriber");
}

impl TeXRayLayer {
    fn uninitialized() -> Self {
        Self {
            settings: Default::default(),
            initialized: Arc::new(AtomicBool::new(false)),
            tracker: Arc::new(RootTracker::new()),
        }
    }

    /// Create a new [`TeXRayLayer`] with settings from [`Settings::auto`]
    pub fn new() -> Self {
        let dumper = GLOBAL_TEXRAY_LAYER.clone();
        dumper.initialized.store(true, Ordering::Relaxed);
        *dumper.settings.lock() = SettingsContainer::Unlocked(Settings::auto());
        dumper
    }

    pub(crate) fn settings_mut(&mut self) -> impl DerefMut<Target = Settings> + '_ {
        struct DerefSettings<'a, R: RawMutex> {
            target: MutexGuard<'a, R, SettingsContainer>,
        }
        impl<'a, R: RawMutex> Deref for DerefSettings<'a, R> {
            type Target = Settings;

            fn deref(&self) -> &Self::Target {
                match self.target.deref() {
                    SettingsContainer::Unlocked(s) => s,
                    SettingsContainer::Locked(_s) => panic!(),
                }
            }
        }
        impl<'a, R: RawMutex> DerefMut for DerefSettings<'a, R> {
            fn deref_mut(&mut self) -> &mut Self::Target {
                self.target
                    .deref_mut()
                    .settings_mut()
                    .expect("cannot modify settings when already in progress")
            }
        }
        DerefSettings {
            target: self.settings.lock(),
        }
    }

    /// Install [`TexRayLayer`] as the global default
    pub fn init(self) {
        let registry = tracing_subscriber::registry().with(self);
        use tracing_subscriber::layer::SubscriberExt;
        set_global_default(registry).expect("failed to install subscriber")
    }

    /// Show events in output in addition to spans
    pub fn enable_events(mut self) -> Self {
        self.settings_mut().set_enable_events(true);
        self
    }

    /// Override the rendered width
    ///
    /// By default, the width is loaded by inspecting the TTY. If a TTY is not available,
    /// it defaults to 120
    pub fn width(mut self, width: usize) -> Self {
        self.settings_mut().width = width;
        self
    }

    /// When printing spans & events, only render the following fields
    pub fn only_show_fields(mut self, fields: &[&'static str]) -> Self {
        self.settings_mut().only_show_fields(fields);
        self
    }

    /// Only render spans longer than `duration`
    pub fn min_duration(mut self, duration: Duration) -> Self {
        self.settings_mut().min_duration(duration);
        self
    }

    /// Update the settings of this [`TexRayLayer`]
    pub fn update_settings(mut self, f: impl FnOnce(&mut Settings) -> &mut Settings) -> Self {
        // TODO: assert!(self.tracked_spans_v2.is_empty());
        f(self.settings_mut().deref_mut());
        self
    }

    /// Updates the `writer` used to dump output
    pub fn writer(self, writer: impl Write + Send + 'static) -> Self {
        self.update_settings(move |s| s.writer(writer))
    }

    fn for_tracker<'a, S>(
        &self,
        span: &Id,
        ctx: &Context<'a, S>,
        f: impl Fn(&mut InterestTracker, Vec<Id>),
    ) where
        S: Subscriber + for<'span> LookupSpan<'span> + Send + Sync,
    {
        if let Some(path) = ctx.span_scope(span) {
            self.tracker
                .if_interested(path.from_root().map(|s| s.id()), |tracker, path| {
                    f(tracker, path.collect::<Vec<_>>())
                });
        }
    }

    fn dump_on_exit(&self, span: &Span, settings: Option<SpanSettings>) {
        check_initialized!(self);
        if let Some(id) = span.id() {
            self.tracker.register_interest(
                id,
                settings.unwrap_or(self.settings.lock().lock_settings().clone()),
            );
        }
    }

    fn initialized(&self) -> bool {
        self.initialized.load(Ordering::Relaxed)
    }
}

fn pretty_duration(duration: Duration) -> String {
    const NANOS_PER_SEC: u128 = 1_000_000_000;
    let divisors = [
        ("m ", (60 * NANOS_PER_SEC)),
        ("s ", NANOS_PER_SEC),
        ("ms", NANOS_PER_SEC / 1000),
        ("μs", NANOS_PER_SEC / 1000 / 1000),
        ("ns", 1),
    ];
    let nanos = duration.as_nanos();
    if nanos == 0 {
        return "0ns".to_string();
    }
    for (unit, div) in divisors {
        if nanos / div >= 1 {
            return format!("{}{}", nanos / div, unit);
        }
    }
    unreachable!("{:?}", duration)
}

impl<S> Layer<S> for TeXRayLayer
where
    S: Subscriber + for<'span> LookupSpan<'span> + Send + Sync,
{
    fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>) {
        check_initialized!(self);
        self.for_tracker(id, &ctx, |tracker, path| {
            tracker.new_span(path).record_metadata(attrs);
        });
    }

    fn on_record(&self, id: &Id, values: &Record<'_>, ctx: Context<'_, S>) {
        check_initialized!(self);
        self.for_tracker(id, &ctx, |tracker, path| {
            tracker.record_metadata(&path, values)
        })
    }

    fn on_event(&self, event: &TracingEvent<'_>, ctx: Context<'_, S>) {
        check_initialized!(self);
        if let Some(span) = ctx.current_span().id() {
            self.for_tracker(span, &ctx, |tracker, path| {
                let mut metadata = TrackedMetadata::default();

                event.record(&mut tracker.field_recorder(&mut metadata));
                let tracked_event = EventInfo::now(metadata);
                tracker.add_event(path, tracked_event);
            });
        }
    }

    fn on_enter(&self, id: &Id, ctx: Context<'_, S>) {
        check_initialized!(self);
        self.for_tracker(id, &ctx, |tracker, path| {
            tracker.open(path, SpanInfo::for_span(id, &ctx));
        });
    }

    fn on_close(&self, id: Id, ctx: Context<'_, S>) {
        check_initialized!(self);
        self.for_tracker(&id, &ctx, |tracker, path| {
            tracker.exit(path, SystemTime::now());
            if self.tracker.end_tracking(id.clone()) {
                let _ = tracker
                    .dump()
                    .map_err(|err| eprintln!("failed to dump output: {}", err));
            }
        });
    }
}