#spans #testing #snapshot #tracing #macro #test-span #usint

macro test-span-macro

macro to do snapshot tests on tracing spans, usint test-span

9 releases (breaking)

0.8.0 May 4, 2024
0.7.0 Aug 5, 2022
0.6.0 May 5, 2022
0.5.0 May 3, 2022
0.1.1 Jan 10, 2022

#2233 in Procedural macros

Download history 135/week @ 2024-07-29 173/week @ 2024-08-05 115/week @ 2024-08-12 385/week @ 2024-08-19 395/week @ 2024-08-26 265/week @ 2024-09-02 568/week @ 2024-09-09 226/week @ 2024-09-16 183/week @ 2024-09-23 60/week @ 2024-09-30 72/week @ 2024-10-07 65/week @ 2024-10-14 73/week @ 2024-10-21 9/week @ 2024-10-28 81/week @ 2024-11-04 19/week @ 2024-11-11

182 downloads per month
Used in 3 crates (via test-span)

MIT/Apache

9KB
104 lines

test-span

A macro and utilities to do snapshot tests on tracing spans.

docs Build status Apache 2.0 License MIT License

How to use

Refer to the tests for a more exhaustive list of features and behaviors:

use test_span::prelude::*;

#[test_span]
fn a_test() {
    do_something();

    // test_span provides your with three functions:
    let spans = get_spans();
    let logs = get_logs();
    // you can get both in one call
    let (spans, logs) = get_telemetry();

    // This plays well with insta snapshots:
    insta::assert_json_snapshot!(logs);
    insta::assert_json_snapshot!(spans);
}

// Test span plays well with async
#[test_span(tokio::test)]
// you can specify the span / log level
// you would like to track like this:
#[level(tracing::Level::INFO)]
async fn an_sync_test() {
    do_something_async().await;
    // You still get access to each function
    let spans = get_spans();
    let logs = get_logs();
    let (spans, logs) = get_telemetry();
}

Limitations

Spans and logs are hard to track across thread spawns. However we're providing you with a log dump you can check:

#[test_span]
fn track_across_threads() {
    std::thread::spawn(|| {
        tracing::info!("only in get_all_logs!");
    })
    .join()
    .unwrap();

    let logs = get_logs();
    // not in get_logs()
    assert!(!logs.contains_message("only in get_all_logs!");

    // get_all_logs takes a filter Level
    let all_logs = test_span::get_all_logs(&tracing::Level::INFO);
    assert!(all_logs.contains_message("only in get_all_logs!"));
}

Contributing

More information can be found in the contribution docs

License

Licensed under either of [Apache License, Version 2.0](LICENSE-APACHE) or [MIT license](LICENSE-MIT) at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~205–630KB
~15K SLoC