Skip to main content

Crate tracing_flight_recorder

Crate tracing_flight_recorder 

Source
Expand description

In-memory flight recorder for tracing events.

Inspired by Go 1.25’s trace.FlightRecorder, this crate provides a tracing_subscriber::Layer that continuously buffers tracing events in a bounded ring buffer. When something goes wrong — a sync failure, a circuit breaker opening, a panic — you snapshot the buffer and get the last N seconds of verbose (DEBUG/TRACE) context that would otherwise be lost.

Events capture their full span hierarchy (names + fields), so snapshots preserve request context like request_id, user_id, and method. Sensitive fields are automatically redacted in both events and spans.

The recorder pays zero I/O cost until a snapshot is triggered.

§Quick Start

use tracing_flight_recorder::{FlightRecorder, FlightRecorderLayer};
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::Layer;

let recorder = FlightRecorder::new(1000);
// CRITICAL: apply per-layer filtering so the recorder sees DEBUG+
// while the console stays at INFO. A global EnvFilter would block
// DEBUG/TRACE events from reaching the recorder layer entirely.
let fr_filter = tracing_subscriber::EnvFilter::new("my_app=debug,warn");
let console_filter = tracing_subscriber::EnvFilter::new("info");
tracing_subscriber::registry()
    .with(FlightRecorderLayer::new(recorder.clone()).with_filter(fr_filter))
    .with(tracing_subscriber::fmt::layer().with_filter(console_filter))
    .init();

// ... application code ...

// On error: dump the last 1000 events to disk
recorder.dump_to_file(std::path::Path::new("flight-recorder.json")).ok();

§Minimal Example

use tracing_flight_recorder::FlightRecorder;

let recorder = FlightRecorder::new(50);
assert!(recorder.is_empty());
assert_eq!(recorder.capacity(), 50);

Structs§

CapturedEvent
A single tracing event captured into a serializable form.
DumpEvent
Payload delivered to an on_dump callback after a snapshot is attempted.
FlightRecorder
A bounded in-memory ring buffer of tracing events.
FlightRecorderDump
A complete flight-recorder dump: diagnostic metadata + the captured events.
FlightRecorderLayer
A tracing_subscriber::Layer that feeds every event into a FlightRecorder.
LevelTrigger
Fires when an event’s severity is at or above a configured Level.
OnceTrigger
Decorator that allows an inner trigger to fire at most once until reset.
SpanContext
The context of a single span in the hierarchy when an event fires.

Enums§

DumpSource
What kind of code path triggered a dump.

Constants§

DEFAULT_CAPACITY
Default ring-buffer capacity (number of events).
DUMP_SCHEMA_VERSION
Current FlightRecorderDump envelope schema version.

Traits§

Trigger
A predicate that decides whether the buffer should be dumped for a given event.