From 9ff04fd2f046bbb5dd35a7c8c1646b3dcf1157a3 Mon Sep 17 00:00:00 2001 From: Eliza Weisman Date: Fri, 17 Sep 2021 10:12:43 -0700 Subject: [PATCH] subscriber: clear `enabled` filter map when short circuiting This is essentially the same change as #1569, but for `enabled` states rather than `register_callsite`. When a global filter returns `false` from `enabled`, ensure that the per-layer filter `FilterMap` and debug counters are cleared, so that they are empty on the next `enabled` call. See #1563 --- tracing-subscriber/src/filter/layer_filters.rs | 14 ++++++++++++++ tracing-subscriber/src/layer/layered.rs | 7 +++++++ 2 files changed, 21 insertions(+) diff --git a/tracing-subscriber/src/filter/layer_filters.rs b/tracing-subscriber/src/filter/layer_filters.rs index bc59125a9e..e1cee05089 100644 --- a/tracing-subscriber/src/filter/layer_filters.rs +++ b/tracing-subscriber/src/filter/layer_filters.rs @@ -45,6 +45,7 @@ use tracing_core::{ subscriber::{Interest, Subscriber}, Event, Metadata, }; +use tracing_futures::Instrument; /// A [`Layer`] that wraps an inner [`Layer`] and adds a [`Filter`] which /// controls what spans and events are enabled for that layer. @@ -700,6 +701,19 @@ impl FilterState { } } + /// Clears the current in-progress filter state. + /// + /// This resets the [`FilterMap`] and current [`Interest`] as well as + /// clearing the debug counters. + pub(crate) fn clear_enabled() { + FILTERING.try_with(|filtering| { + filtering.enabled.set(FilterMap::default()); + + #[cfg(debug_assertions)] + filtering.counters.in_filter_pass.set(0); + }); + } + pub(crate) fn take_interest() -> Option { FILTERING .try_with(|filtering| { diff --git a/tracing-subscriber/src/layer/layered.rs b/tracing-subscriber/src/layer/layered.rs index dae885269d..def6c0ce16 100644 --- a/tracing-subscriber/src/layer/layered.rs +++ b/tracing-subscriber/src/layer/layered.rs @@ -80,6 +80,13 @@ where self.inner.enabled(metadata) } else { // otherwise, the callsite is disabled by the layer + + // If per-layer filters are in use, and we are short-circuiting + // (rather than calling into the inner type), clear the current + // per-layer filter `enabled` state. + #[cfg(feature = "registry")] + filter::FilterState::clear_enabled(); + false } }