From 51a0b8c9efa0769d323353cca3ccd60c8cff3ab5 Mon Sep 17 00:00:00 2001 From: Gus Wynn Date: Thu, 9 Jun 2022 15:54:58 -0700 Subject: [PATCH] implement for the layer/subscriber --- tracing-subscriber/src/reload.rs | 53 +++++++++++++++++++++++++++ tracing-subscriber/tests/reload.rs | 59 ++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) diff --git a/tracing-subscriber/src/reload.rs b/tracing-subscriber/src/reload.rs index f2c577b32f..bab7767c59 100644 --- a/tracing-subscriber/src/reload.rs +++ b/tracing-subscriber/src/reload.rs @@ -138,6 +138,59 @@ impl Subscriber { } } +#[cfg(all(feature = "registry", feature = "std"))] +#[cfg_attr(docsrs, doc(cfg(all(feature = "registry", feature = "std"))))] +impl crate::subscribe::Filter for Subscriber +where + S: crate::subscribe::Filter + 'static, + C: Collect, +{ + #[inline] + fn callsite_enabled(&self, metadata: &'static Metadata<'static>) -> Interest { + try_lock!(self.inner.read(), else return Interest::sometimes()).callsite_enabled(metadata) + } + + #[inline] + fn enabled(&self, metadata: &Metadata<'_>, ctx: &subscribe::Context<'_, C>) -> bool { + try_lock!(self.inner.read(), else return false).enabled(metadata, ctx) + } + + #[inline] + fn on_new_span( + &self, + attrs: &span::Attributes<'_>, + id: &span::Id, + ctx: subscribe::Context<'_, C>, + ) { + try_lock!(self.inner.read()).on_new_span(attrs, id, ctx) + } + + #[inline] + fn on_record( + &self, + span: &span::Id, + values: &span::Record<'_>, + ctx: subscribe::Context<'_, C>, + ) { + try_lock!(self.inner.read()).on_record(span, values, ctx) + } + + #[inline] + fn on_enter(&self, id: &span::Id, ctx: subscribe::Context<'_, C>) { + try_lock!(self.inner.read()).on_enter(id, ctx) + } + + #[inline] + fn on_exit(&self, id: &span::Id, ctx: subscribe::Context<'_, C>) { + try_lock!(self.inner.read()).on_exit(id, ctx) + } + + #[inline] + fn on_close(&self, id: span::Id, ctx: subscribe::Context<'_, C>) { + try_lock!(self.inner.read()).on_close(id, ctx) + } +} + // ===== impl Handle ===== impl Handle { diff --git a/tracing-subscriber/tests/reload.rs b/tracing-subscriber/tests/reload.rs index ee133366dd..f979087afd 100644 --- a/tracing-subscriber/tests/reload.rs +++ b/tracing-subscriber/tests/reload.rs @@ -32,6 +32,17 @@ impl Collect for NopCollector { } } +pub struct NopSubscriber; +impl tracing_subscriber::Subscribe for NopSubscriber { + fn register_callsite(&self, _m: &Metadata<'_>) -> Interest { + Interest::sometimes() + } + + fn enabled(&self, _m: &Metadata<'_>, _: subscribe::Context<'_, S>) -> bool { + true + } +} + #[test] fn reload_handle() { static FILTER1_CALLS: AtomicUsize = AtomicUsize::new(0); @@ -82,3 +93,51 @@ fn reload_handle() { assert_eq!(FILTER2_CALLS.load(Ordering::SeqCst), 1); }) } + +#[test] +fn reload_filter() { + static FILTER1_CALLS: AtomicUsize = AtomicUsize::new(0); + static FILTER2_CALLS: AtomicUsize = AtomicUsize::new(0); + + enum Filter { + One, + Two, + } + + impl tracing_subscriber::subscribe::Filter for Filter { + fn enabled(&self, m: &Metadata<'_>, _: &subscribe::Context<'_, S>) -> bool { + println!("ENABLED: {:?}", m); + match self { + Filter::One => FILTER1_CALLS.fetch_add(1, Ordering::SeqCst), + Filter::Two => FILTER2_CALLS.fetch_add(1, Ordering::SeqCst), + }; + true + } + } + fn event() { + tracing::trace!("my event"); + } + + let (filter, handle) = Subscriber::new(Filter::One); + + let dispatcher = tracing_core::dispatch::Dispatch::new( + tracing_subscriber::registry().with(NopSubscriber.with_filter(filter)), + ); + + tracing_core::dispatch::with_default(&dispatcher, || { + assert_eq!(FILTER1_CALLS.load(Ordering::SeqCst), 0); + assert_eq!(FILTER2_CALLS.load(Ordering::SeqCst), 0); + + event(); + + assert_eq!(FILTER1_CALLS.load(Ordering::SeqCst), 1); + assert_eq!(FILTER2_CALLS.load(Ordering::SeqCst), 0); + + handle.reload(Filter::Two).expect("should reload"); + + event(); + + assert_eq!(FILTER1_CALLS.load(Ordering::SeqCst), 1); + assert_eq!(FILTER2_CALLS.load(Ordering::SeqCst), 1); + }) +}