pub struct TracingRateLimitLayer<S = Arc<ShardedStorage<EventSignature, EventState>>>{ /* private fields */ }Expand description
A tracing::Layer that applies rate limiting to events.
This layer intercepts events, computes their signature, and decides whether to allow or suppress them based on the configured policy.
Optionally emits periodic summaries of suppressed events when active
emission is enabled (requires async feature).
Implementations§
Source§impl<S> TracingRateLimitLayer<S>
impl<S> TracingRateLimitLayer<S>
Sourcepub fn should_allow(&self, signature: EventSignature) -> bool
pub fn should_allow(&self, signature: EventSignature) -> bool
Check if an event should be allowed through.
Sourcepub fn should_allow_with_metadata(
&self,
signature: EventSignature,
metadata: EventMetadata,
) -> bool
pub fn should_allow_with_metadata( &self, signature: EventSignature, metadata: EventMetadata, ) -> bool
Check if an event should be allowed through and capture metadata.
This method stores event metadata on first occurrence so summaries can show human-readable event details instead of just signature hashes.
Note: Only available with the human-readable feature flag.
Sourcepub fn limiter(&self) -> &RateLimiter<S>
pub fn limiter(&self) -> &RateLimiter<S>
Get a reference to the underlying limiter.
Sourcepub fn metrics(&self) -> &Metrics
pub fn metrics(&self) -> &Metrics
Get a reference to the metrics.
Returns metrics about rate limiting behavior including:
- Events allowed
- Events suppressed
- Signatures evicted
Sourcepub fn signature_count(&self) -> usize
pub fn signature_count(&self) -> usize
Get the current number of tracked signatures.
Sourcepub fn circuit_breaker(&self) -> &Arc<CircuitBreaker>
pub fn circuit_breaker(&self) -> &Arc<CircuitBreaker>
Get a reference to the circuit breaker.
Use this to check the circuit breaker state and health:
circuit_breaker().state()- Current circuit statecircuit_breaker().consecutive_failures()- Failure count
Sourcepub async fn shutdown(&self) -> Result<(), ShutdownError>
pub async fn shutdown(&self) -> Result<(), ShutdownError>
Shutdown the active suppression summary emitter, if running.
This method gracefully stops the background emission task. If active emission is not enabled, this method does nothing.
Requires the async feature.
§Errors
Returns an error if the emitter task fails to shut down gracefully.
§Example
let layer = TracingRateLimitLayer::builder()
.with_active_emission(true)
.build()
.unwrap();
// Use the layer...
// Shutdown before dropping
layer.shutdown().await.expect("shutdown failed");Source§impl TracingRateLimitLayer<Arc<ShardedStorage<EventSignature, EventState>>>
impl TracingRateLimitLayer<Arc<ShardedStorage<EventSignature, EventState>>>
Sourcepub fn builder() -> TracingRateLimitLayerBuilder
pub fn builder() -> TracingRateLimitLayerBuilder
Create a builder for configuring the layer.
Defaults:
- Policy: token bucket (50 burst capacity, 1 token/sec refill rate)
- Max signatures: 10,000 (with LRU eviction)
- Summary interval: 30 seconds
- Active emission: disabled
- Summary formatter: default (WARN level with signature and count)
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a layer with default settings.
Equivalent to TracingRateLimitLayer::builder().build().unwrap().
Defaults:
- Policy: token bucket (50 burst capacity, 1 token/sec refill rate = 60/min)
- Max signatures: 10,000 (with LRU eviction)
- Summary interval: 30 seconds
§Panics
This method cannot panic because all default values are valid.
Sourcepub fn with_storage<ST>(
storage: ST,
policy: Policy,
clock: Arc<dyn Clock>,
) -> TracingRateLimitLayer<ST>
pub fn with_storage<ST>( storage: ST, policy: Policy, clock: Arc<dyn Clock>, ) -> TracingRateLimitLayer<ST>
Create a layer with custom storage backend.
This allows using alternative storage implementations like Redis for distributed rate limiting across multiple application instances.
§Arguments
storage- Custom storage implementation (must implementStorage<EventSignature, EventState>)policy- Rate limiting policy to applyclock- Clock implementation (useSystemClock::new()for production)
§Example with Redis
use tracing_throttle::{TracingRateLimitLayer, RedisStorage, Policy, SystemClock};
use std::sync::Arc;
#[tokio::main]
async fn main() {
let storage = Arc::new(
RedisStorage::connect("redis://127.0.0.1/")
.await
.expect("Failed to connect")
);
let policy = Policy::token_bucket(100.0, 10.0).unwrap();
let clock = Arc::new(SystemClock::new());
let layer = TracingRateLimitLayer::with_storage(storage, policy, clock);
}Trait Implementations§
Source§impl<S> Clone for TracingRateLimitLayer<S>
impl<S> Clone for TracingRateLimitLayer<S>
Source§fn clone(&self) -> TracingRateLimitLayer<S>
fn clone(&self) -> TracingRateLimitLayer<S>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl<S, Sub> Filter<Sub> for TracingRateLimitLayer<S>where
S: Storage<EventSignature, EventState> + Clone,
Sub: Subscriber + for<'lookup> LookupSpan<'lookup>,
impl<S, Sub> Filter<Sub> for TracingRateLimitLayer<S>where
S: Storage<EventSignature, EventState> + Clone,
Sub: Subscriber + for<'lookup> LookupSpan<'lookup>,
Source§fn enabled(&self, _meta: &Metadata<'_>, _cx: &Context<'_, Sub>) -> bool
fn enabled(&self, _meta: &Metadata<'_>, _cx: &Context<'_, Sub>) -> bool
true if this layer is interested in a span or event with the
given Metadata in the current Context, similarly to
Subscriber::enabled. Read moreSource§fn event_enabled(&self, event: &Event<'_>, cx: &Context<'_, Sub>) -> bool
fn event_enabled(&self, event: &Event<'_>, cx: &Context<'_, Sub>) -> bool
Layer]'s [on_event], to determine if on_event` should be called. Read moreSource§fn callsite_enabled(&self, meta: &'static Metadata<'static>) -> Interest
fn callsite_enabled(&self, meta: &'static Metadata<'static>) -> Interest
Source§fn max_level_hint(&self) -> Option<LevelFilter>
fn max_level_hint(&self) -> Option<LevelFilter>
Source§fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>)
fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, S>)
Source§fn on_enter(&self, id: &Id, ctx: Context<'_, S>)
fn on_enter(&self, id: &Id, ctx: Context<'_, S>)
Source§impl<S, Sub> Layer<Sub> for TracingRateLimitLayer<S>where
S: Storage<EventSignature, EventState> + Clone + 'static,
Sub: Subscriber + for<'lookup> LookupSpan<'lookup>,
impl<S, Sub> Layer<Sub> for TracingRateLimitLayer<S>where
S: Storage<EventSignature, EventState> + Clone + 'static,
Sub: Subscriber + for<'lookup> LookupSpan<'lookup>,
Source§fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, Sub>)
fn on_new_span(&self, attrs: &Attributes<'_>, id: &Id, ctx: Context<'_, Sub>)
Attributes and Id.Source§fn on_register_dispatch(&self, subscriber: &Dispatch)
fn on_register_dispatch(&self, subscriber: &Dispatch)
Subscriber. Read moreSource§fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest
fn register_callsite(&self, metadata: &'static Metadata<'static>) -> Interest
Subscriber::register_callsite. Read moreSource§fn enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool
fn enabled(&self, metadata: &Metadata<'_>, ctx: Context<'_, S>) -> bool
true if this layer is interested in a span or event with the
given metadata in the current Context, similarly to
Subscriber::enabled. Read moreSource§fn on_record(&self, _span: &Id, _values: &Record<'_>, _ctx: Context<'_, S>)
fn on_record(&self, _span: &Id, _values: &Record<'_>, _ctx: Context<'_, S>)
Id recorded the given
values.Source§fn on_follows_from(&self, _span: &Id, _follows: &Id, _ctx: Context<'_, S>)
fn on_follows_from(&self, _span: &Id, _follows: &Id, _ctx: Context<'_, S>)
span recorded that it
follows from the span with the ID follows.Source§fn on_event(&self, _event: &Event<'_>, _ctx: Context<'_, S>)
fn on_event(&self, _event: &Event<'_>, _ctx: Context<'_, S>)
Source§fn on_enter(&self, _id: &Id, _ctx: Context<'_, S>)
fn on_enter(&self, _id: &Id, _ctx: Context<'_, S>)
Source§fn on_exit(&self, _id: &Id, _ctx: Context<'_, S>)
fn on_exit(&self, _id: &Id, _ctx: Context<'_, S>)
Source§fn on_close(&self, _id: Id, _ctx: Context<'_, S>)
fn on_close(&self, _id: Id, _ctx: Context<'_, S>)
Source§fn on_id_change(&self, _old: &Id, _new: &Id, _ctx: Context<'_, S>)
fn on_id_change(&self, _old: &Id, _new: &Id, _ctx: Context<'_, S>)
Source§fn and_then<L>(self, layer: L) -> Layered<L, Self, S>
fn and_then<L>(self, layer: L) -> Layered<L, Self, S>
Layer, returning a Layered
struct implementing Layer. Read moreSource§fn with_subscriber(self, inner: S) -> Layered<Self, S>where
Self: Sized,
fn with_subscriber(self, inner: S) -> Layered<Self, S>where
Self: Sized,
Layer with the given Subscriber, returning a
Layered struct that implements Subscriber. Read more