Skip to main content

Crate tracing_log_sample

Crate tracing_log_sample 

Source
Expand description

A tracing_subscriber::Layer that rate-limits log output using reservoir sampling.

Events are collected into fixed-duration time buckets and uniformly sampled, guaranteeing every event has an equal chance of being kept. Multiple sampling budgets can be configured with EnvFilter patterns — events displaced from one budget’s reservoir cascade to the next matching budget.

Sampled events are released gradually over the following bucket via adaptive smearing, avoiding write bursts at rotation boundaries.

Formatting is delegated to tracing_subscriber::fmt::Layer, so all the usual formatting options (compact, pretty, JSON, timestamps, etc.) work out of the box.

§Example

use std::time::Duration;
use tracing_subscriber::{Registry, filter::EnvFilter, layer::SubscriberExt};
use tracing_log_sample::SamplingLayer;

let (layer, stats) = SamplingLayer::<Registry>::builder()
    .bucket_duration(Duration::from_millis(50))
    .budget(EnvFilter::new("error"), 1000)
    .budget(EnvFilter::new("info"), 5000)
    .build();

let subscriber = Registry::default().with(layer);
// stats.received(), stats.sampled(), stats.dropped()
// tracing::subscriber::set_global_default(subscriber).unwrap();

Structs§

SamplingLayer
A tracing_subscriber::Layer that samples events into time-bucketed reservoirs.
SamplingLayerBuilder
Builder for SamplingLayer.
Stats
Shared handle for reading layer event counters.