Skip to content

Commit

Permalink
implement for the layer/subscriber
Browse files Browse the repository at this point in the history
  • Loading branch information
guswynn committed Jun 9, 2022
1 parent 388fff8 commit 51a0b8c
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 0 deletions.
53 changes: 53 additions & 0 deletions tracing-subscriber/src/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,59 @@ impl<S> Subscriber<S> {
}
}

#[cfg(all(feature = "registry", feature = "std"))]
#[cfg_attr(docsrs, doc(cfg(all(feature = "registry", feature = "std"))))]
impl<S, C> crate::subscribe::Filter<C> for Subscriber<S>
where
S: crate::subscribe::Filter<C> + '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<S> Handle<S> {
Expand Down
59 changes: 59 additions & 0 deletions tracing-subscriber/tests/reload.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,17 @@ impl Collect for NopCollector {
}
}

pub struct NopSubscriber;
impl<S: Collect> tracing_subscriber::Subscribe<S> 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);
Expand Down Expand Up @@ -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<S: Collect> tracing_subscriber::subscribe::Filter<S> 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);
})
}

0 comments on commit 51a0b8c

Please sign in to comment.