Expand description
Capturing tracing spans and events, e.g. for testing purposes.
The core type in this crate is CaptureLayer
, a tracing Layer
that can be used
to capture tracing spans and events.
§Examples
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);
// Inspect the only event in the span.
let event = span.events().next().unwrap();
assert_eq!(*event.metadata().level(), Level::WARN);
assert_eq!(
event.message(),
Some("I feel disturbance in the Force...")
);
§Alternatives / similar tools
tracing-test
is a lower-level alternative.tracing-fluent-assertions
is more similar in its goals, but differs significantly in the API design; e.g., the assertions need to be declared before the capture.
Modules§
- Predicates for
CapturedSpan
s andCapturedEvent
s.
Structs§
- Tracing [
Layer
] that captures (optionally filtered) spans and events. - Captured tracing event containing a reference to its [
Metadata
] and values that the event was created with. - Iterator over
CapturedEvent
s returned fromStorage::all_events()
etc. - Captured tracing span containing a reference to its [
Metadata
], values that the span was created with, stats, and descendantCapturedEvent
s. - Iterator over
CapturedSpan
s returned fromStorage::all_spans()
etc. - Iterator over the descendant events of a
CapturedSpan
. Returned byCapturedSpan::descendant_events()
. - Iterator over descendant
CapturedSpan
s of a span. Returned byCapturedSpan::descendants()
. - Shared wrapper for tracing
Storage
. - Statistics about a
CapturedSpan
. - Storage of captured tracing information.
Traits§
- Uniting trait for
CapturedSpan
s andCapturedEvent
s that allows writing generic code in cases both should be supported.