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 Jun 14, 2022
1 parent 30f0644 commit ce016e1
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 @@ -57,6 +57,9 @@ use core::ptr::NonNull;
/// See also the [documentation on the callsite registry][cs-reg] for details
/// on [`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 @@ -72,6 +75,8 @@ use core::ptr::NonNull;
/// [`clone_span`]: Collect::clone_span
/// [`try_close`]: Collect::try_close
/// [cs-reg]: crate::callsite#registering-callsites
/// [`event`]: Collect::event
/// [`event_enabled`]: Collect::event_enabled
pub trait Collect: 'static {
// === Span registry methods ==============================================

Expand Down Expand Up @@ -288,6 +293,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 @@ -138,13 +138,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 @@ -285,10 +293,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 ce016e1

Please sign in to comment.