17 releases

new 0.3.1 Nov 24, 2021
0.2.6 Sep 11, 2021
0.2.4 May 2, 2021
0.2.0 Mar 20, 2021
0.1.6 May 12, 2020

#22 in Debugging

Download history 6141/week @ 2021-08-10 5783/week @ 2021-08-17 5210/week @ 2021-08-24 6226/week @ 2021-08-31 5529/week @ 2021-09-07 7552/week @ 2021-09-14 5976/week @ 2021-09-21 6394/week @ 2021-09-28 7509/week @ 2021-10-05 7355/week @ 2021-10-12 8561/week @ 2021-10-19 7623/week @ 2021-10-26 7516/week @ 2021-11-02 6964/week @ 2021-11-09 9029/week @ 2021-11-16 8898/week @ 2021-11-23

33,755 downloads per month
Used in less than 10 crates

MIT/Apache

220KB
326 lines

tracing-bunyan-formatter

Bunyan formatting for tokio-rs/tracing.


tracing-bunyan-formatter provides two Layers implementation to be used on top of a tracing Subscriber:

Important: each span will inherit all fields and properties attached to its parent - this is currently not the behaviour provided by tracing_subscriber::fmt::Layer.

Example

use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
use tracing::instrument;
use tracing::info;
use tracing_subscriber::Registry;
use tracing_subscriber::layer::SubscriberExt;

#[instrument]
pub fn a_unit_of_work(first_parameter: u64) {
    for i in 0..2 {
        a_sub_unit_of_work(i);
    }
    info!(excited = "true", "Tracing is quite cool!");
}

#[instrument]
pub fn a_sub_unit_of_work(sub_parameter: u64) {
    info!("Events have the full context of their parent span!");
}

fn main() {
    let formatting_layer = BunyanFormattingLayer::new("tracing_demo".into(), std::io::stdout);
    let subscriber = Registry::default()
        .with(JsonStorageLayer)
        .with(formatting_layer);
    tracing::subscriber::set_global_default(subscriber).unwrap();

    info!("Orphan event without a parent span");
    a_unit_of_work(2);
}

Console output


If you pipe the output in the bunyan CLI:


Implementation strategy

The layered approach we have pursued is not necessarily the most efficient, but it makes it easier to separate different concerns and re-use common logic across multiple Layers.

While the current crate has no ambition to provide any sort of general purpose framework on top of [tracing-subscriber]'s Layer trait, the information collected by JsonStorageLayer can be leveraged via its public API by other downstream layers outside of this crate whose main concern is formatting. It significantly lowers the amount of complexity you have to deal with if you are interested in implementing your own formatter, for whatever reason or purpose.

You can also add another enrichment layer following the JsonStorageLayer to collect additional information about each span and store it in JsonStorage. We could have pursued this compositional approach to add elapsed_milliseconds to each span instead of baking it in JsonStorage itself.

Optional features

You can enable the arbitrary_precision feature to handle numbers of arbitrary size losslessly. Be aware of a known issue with untagged deserialization.

Testing

Currently the tests only support being run sequentially, so the number of threads needs to be restricted:

cargo test -- --test-threads 1

Dependencies

~2.5MB
~47K SLoC