Skip to content

Commit

Permalink
Improve event_enabled order independence
Browse files Browse the repository at this point in the history
  • Loading branch information
CAD97 committed Apr 11, 2022
1 parent 5256829 commit df4b068
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
16 changes: 16 additions & 0 deletions tracing-core/src/collect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 ==============================================

Expand Down Expand Up @@ -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
Expand Down
18 changes: 13 additions & 5 deletions tracing-subscriber/src/subscribe/layered.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit df4b068

Please sign in to comment.