Expand description
Periodic and throttled logging macros for the tracing ecosystem.
This crate provides macros that extend tracing with rate-limited logging capabilities:
*_once!- Log only the first time the macro is reached*_every!(duration, ...)- Log at most once per specified duration*_every_n!(n, ...)- Log every N occurrences*_first_n!(n, ...)- Log only the first N occurrences*_backoff!(initial, max, ...)- Log with exponential backoff*_on_change!(value, ...)- Log only when the tracked value changes*_once_per_value!(key, ...)- Log once per unique key value*_sample!(probability, ...)- Log with probability sampling
§Examples
use throttled_tracing::{info_once, debug_every, warn_every_n};
use std::time::Duration;
fn process_item(item: u32) {
// Only logs the first time this line is reached
info_once!("Processing started");
// Logs at most once per second
debug_every!(Duration::from_secs(1), "Processing item {}", item);
// Logs every 100th call
warn_every_n!(100, "Processed {} items so far", item);
}use throttled_tracing::{error_backoff, info_on_change, debug_once_per_value, trace_sample};
use std::time::Duration;
fn handle_error(err: &str) {
// Exponential backoff: logs at 1s, 2s, 4s, 8s... up to 60s
error_backoff!(Duration::from_secs(1), Duration::from_secs(60), "Error: {}", err);
}
fn monitor_status(status: u32) {
// Only logs when status changes
info_on_change!(status, "Status changed to {}", status);
}
fn process_user(user_id: u64) {
// Logs once per unique user_id
debug_once_per_value!(user_id, "First time seeing user {}", user_id);
}
fn high_volume_operation() {
// Logs ~1% of calls
trace_sample!(0.01, "Sampled operation");
}Re-exports§
pub use parking_lot;pub use tracing;pub use fastrand;
Macros§
- debug_
backoff - Logs a message at the DEBUG level with exponential backoff.
Starts at
initialinterval, doubles after each log, caps atmax. - debug_
every - Logs a message at the DEBUG level at most once per specified duration.
- debug_
every_ n - Logs a message at the DEBUG level every N occurrences.
- debug_
first_ n - Logs a message at the DEBUG level only for the first N occurrences.
- debug_
on_ change - Logs a message at the DEBUG level only when the tracked value changes.
- debug_
once - Logs a message at the DEBUG level only once.
- debug_
once_ per_ value - Logs a message at the DEBUG level once per unique value.
- debug_
sample - Logs a message at the DEBUG level with the given probability (0.0 to 1.0).
- error_
backoff - Logs a message at the ERROR level with exponential backoff.
Starts at
initialinterval, doubles after each log, caps atmax. - error_
every - Logs a message at the ERROR level at most once per specified duration.
- error_
every_ n - Logs a message at the ERROR level every N occurrences.
- error_
first_ n - Logs a message at the ERROR level only for the first N occurrences.
- error_
on_ change - Logs a message at the ERROR level only when the tracked value changes.
- error_
once - Logs a message at the ERROR level only once.
- error_
once_ per_ value - Logs a message at the ERROR level once per unique value.
- error_
sample - Logs a message at the ERROR level with the given probability (0.0 to 1.0).
- info_
backoff - Logs a message at the INFO level with exponential backoff.
Starts at
initialinterval, doubles after each log, caps atmax. - info_
every - Logs a message at the INFO level at most once per specified duration.
- info_
every_ n - Logs a message at the INFO level every N occurrences.
- info_
first_ n - Logs a message at the INFO level only for the first N occurrences.
- info_
on_ change - Logs a message at the INFO level only when the tracked value changes.
- info_
once - Logs a message at the INFO level only once.
- info_
once_ per_ value - Logs a message at the INFO level once per unique value.
- info_
sample - Logs a message at the INFO level with the given probability (0.0 to 1.0).
- trace_
backoff - Logs a message at the TRACE level with exponential backoff.
Starts at
initialinterval, doubles after each log, caps atmax. - trace_
every - Logs a message at the TRACE level at most once per specified duration.
- trace_
every_ n - Logs a message at the TRACE level every N occurrences.
- trace_
first_ n - Logs a message at the TRACE level only for the first N occurrences.
- trace_
on_ change - Logs a message at the TRACE level only when the tracked value changes.
- trace_
once - Logs a message at the TRACE level only once.
- trace_
once_ per_ value - Logs a message at the TRACE level once per unique value.
- trace_
sample - Logs a message at the TRACE level with the given probability (0.0 to 1.0).
- warn_
backoff - Logs a message at the WARN level with exponential backoff.
Starts at
initialinterval, doubles after each log, caps atmax. - warn_
every - Logs a message at the WARN level at most once per specified duration.
- warn_
every_ n - Logs a message at the WARN level every N occurrences.
- warn_
first_ n - Logs a message at the WARN level only for the first N occurrences.
- warn_
on_ change - Logs a message at the WARN level only when the tracked value changes.
- warn_
once - Logs a message at the WARN level only once.
- warn_
once_ per_ value - Logs a message at the WARN level once per unique value.
- warn_
sample - Logs a message at the WARN level with the given probability (0.0 to 1.0).
Structs§
- Backoff
State - State for exponential backoff logging.
- Count
State - State for count-based throttling.
- FirstN
State - State for logging only the first N occurrences.
- OnChange
State - State for logging only when a value changes.
Uses hash comparison, so values must implement
Hash. - Once
PerValue State - State for logging once per unique value.
Uses hash-based deduplication, so values must implement
Hash. Note: Memory grows with each unique value seen. - Once
State - State for one-time logging.
- Sample
State - State for probabilistic sampling.
- Throttle
State - State for duration-based throttling.