diff --git a/tracing-core/src/collect.rs b/tracing-core/src/collect.rs index f852380a82..4a31be87a4 100644 --- a/tracing-core/src/collect.rs +++ b/tracing-core/src/collect.rs @@ -53,6 +53,9 @@ use core::ptr::NonNull; /// Additionally, collectors which wish to perform a behaviour once for each /// callsite, such as allocating storage for data related to that callsite, /// can perform it in `register_callsite`. +/// - [`event_enabled`] is called once before every [`event`] is recorded. This +/// can be used to implement filtering on events once their field values are +/// known but before any processing is done in `event`. /// - [`clone_span`] is called every time a span ID is cloned, and [`try_close`] /// is called when a span ID is dropped. By default, these functions do /// nothing. However, they can be used to implement reference counting for @@ -67,6 +70,8 @@ use core::ptr::NonNull; /// [`enabled`]: Collect::enabled /// [`clone_span`]: Collect::clone_span /// [`try_close`]: Collect::try_close +/// [`event`]: Collect::event +/// [`event_enabled`]: Collect::event_enabled pub trait Collect: 'static { // === Span registry methods ============================================== @@ -277,6 +282,17 @@ pub trait Collect: 'static { /// follow from _b_), it may silently do nothing. fn record_follows_from(&self, span: &span::Id, follows: &span::Id); + /// Determine if an [`Event`] should be recorded. + /// + /// By default, this returns `true` and collectors can filter events in + /// [`event`][Self::event] without any penalty. However, when `event` is + /// more complicated, this can be used to determine if `event` should be + /// called at all, separating out the decision from the processing. + fn event_enabled(&self, event: &Event<'_>) -> bool { + let _ = event; + true + } + /// Records that an [`Event`] has occurred. /// /// This method will be invoked when an Event is constructed by diff --git a/tracing-subscriber/src/subscribe/layered.rs b/tracing-subscriber/src/subscribe/layered.rs index 9cdf719d61..a316cab424 100644 --- a/tracing-subscriber/src/subscribe/layered.rs +++ b/tracing-subscriber/src/subscribe/layered.rs @@ -113,13 +113,21 @@ where self.subscriber.on_follows_from(span, follows, self.ctx()); } - fn event(&self, event: &Event<'_>) { + fn event_enabled(&self, event: &Event<'_>) -> bool { if self.subscriber.event_enabled(event, self.ctx()) { - self.inner.event(event); - self.subscriber.on_event(event, self.ctx()); + // if the outer subscriber enables the event, ask the inner collector. + self.inner.event_enabled(event) + } else { + // otherwise, the event is disabled by this subscriber + false } } + fn event(&self, event: &Event<'_>) { + self.inner.event(event); + self.subscriber.on_event(event, self.ctx()); + } + fn enter(&self, span: &span::Id) { self.inner.enter(span); self.subscriber.on_enter(span, self.ctx()); @@ -260,10 +268,10 @@ where #[inline] fn event_enabled(&self, event: &Event<'_>, ctx: Context<'_, C>) -> bool { if self.subscriber.event_enabled(event, ctx.clone()) { - // if the outer subscriber enables the event, ask the inner subscriber. + // if the outer subscriber enables the event, ask the inner collector. self.inner.event_enabled(event, ctx) } else { - // otherwise, the callsite is disabled by this subscriber + // otherwise, the event is disabled by this subscriber false } }