#tracing #assertions #predicate

tracing-capture

Capturing tracing spans and events, e.g. for testing

2 unstable releases

0.2.0-beta.1 Mar 3, 2024
0.1.0 Dec 9, 2022

#118 in Testing

Download history 2878/week @ 2024-07-22 3380/week @ 2024-07-29 3668/week @ 2024-08-05 3753/week @ 2024-08-12 3116/week @ 2024-08-19 3566/week @ 2024-08-26 2966/week @ 2024-09-02 2349/week @ 2024-09-09 3227/week @ 2024-09-16 2288/week @ 2024-09-23 2605/week @ 2024-09-30 3016/week @ 2024-10-07 3149/week @ 2024-10-14 3278/week @ 2024-10-21 3412/week @ 2024-10-28 2854/week @ 2024-11-04

12,743 downloads per month
Used in 3 crates

MIT/Apache

165KB
3K SLoC

Capturing Tracing Spans and Events

Build Status License: MIT OR Apache-2.0 rust 1.70+ required

Documentation: Docs.rs crate docs (main)

This crate provides a tracing Layer to capture tracing spans and events as they occur. The captured spans and events can then be used for testing assertions (e.g., "Did a span with a specific name / target / … occur? What were its fields? Was the span closed? How many times the span was entered?" and so on).

The crate supports both straightforward assertions on the captured data, and more fluent assertions based on predicates.

Usage

Add this to your Crate.toml:

[dependencies]
tracing-capture = "0.2.0-beta.1"

Capturing spans for test assertions

use tracing::Level;
use tracing_subscriber::layer::SubscriberExt;
use tracing_capture::{CaptureLayer, SharedStorage};

let subscriber = tracing_subscriber::fmt()
    .pretty()
    .with_max_level(Level::INFO)
    .finish();
// Add the capturing layer.
let storage = SharedStorage::default();
let subscriber = subscriber.with(CaptureLayer::new(&storage));

// Capture tracing information.
tracing::subscriber::with_default(subscriber, || {
    tracing::info_span!("test", num = 42_i64).in_scope(|| {
        tracing::warn!("I feel disturbance in the Force...");
    });
});

// Inspect the only captured span.
let storage = storage.lock();
assert_eq!(storage.all_spans().len(), 1);
let span = storage.all_spans().next().unwrap();
assert_eq!(span["num"], 42_i64);
assert_eq!(span.stats().entered, 1);
assert!(span.stats().is_closed);

Predicate-based assertions

use predicates::str::contains;
use tracing::Level;
use tracing_subscriber::{layer::SubscriberExt, Registry};
use tracing_capture::{predicates::*, CaptureLayer, SharedStorage};

let storage = SharedStorage::default();
let subscriber = Registry::default().with(CaptureLayer::new(&storage));
tracing::subscriber::with_default(subscriber, || {
    tracing::info_span!("test_spans").in_scope(|| {
        tracing::warn!(result = 42_i64, "computed");
    });
});

let storage = storage.lock();
let predicate = level(Level::WARN)
    & message(contains("compute"))
    & field("result", 42_i64);
// Checks that there is a single event satisfying `predicate`.
storage.scan_events().single(&predicate);
// ...and that none of spans satisfy similar predicate.
storage.scan_spans().none(&level(Level::WARN));

Alternatives / similar tools

  • tracing-test is a lower-level alternative.
  • tracing-fluent-assertions is more similar in intended goals, but differs significantly in API design; the assertions need to be declared before the capture.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in tracing-toolbox by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

Dependencies

~1.7–2.4MB
~45K SLoC