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§
- Sampling
Layer - A
tracing_subscriber::Layerthat samples events into time-bucketed reservoirs. - Sampling
Layer Builder - Builder for
SamplingLayer. - Stats
- Shared handle for reading layer event counters.