tokio-rs / tracing

Application level tracing for Rust.
https://tracing.rs
MIT License
5.52k stars 725 forks source link

subscriber: move timing into separate subscriber layer #3063

Open joshka opened 3 months ago

joshka commented 3 months ago

Moves the Timing logic out from fmt::Subcriber into its own layer.

Motivation

This allows for the timing subscriber logic to be composed and used by any subscriber rather than just users of the fmt subscriber. I'd like this info to be available as part of some ideas for a new Ratatui tracing layer I'm thinking about.

Solution

A new subscriber (TimingSubscriber) that stores Timing in the span extensions Updated the on_close logic in fmt::Subsciber to use this and removed all the code that otherwise dealt with timing.

The expectation is that this subscriber would be composed when there's need to record any timing data.

TODO: work out how to properly configure the fmt subscriber to use the timing subscriber as a layer (or otherwise make it easy to compose this layer)

Questions:

Fixes: https://github.com/tokio-rs/tracing/issues/2946 Replaces: https://github.com/tokio-rs/tracing/pull/3038

joshka commented 3 months ago

Added an example which uses this:

    tracing_subscriber::registry()
        .with(tracing_subscriber::fmt::timing::TimingSubscriber::default())
        .with(tracing_subscriber::FmtSubscriber::new().with_span_events(FmtSpan::FULL))
        .init();
image