Skip to content

Commit eb289cf

Browse files
committed
implement for the layer/subscriber
1 parent 0d23925 commit eb289cf

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

tracing-subscriber/src/reload.rs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,59 @@ impl<S> Subscriber<S> {
138138
}
139139
}
140140

141+
#[cfg(all(feature = "registry", feature = "std"))]
142+
#[cfg_attr(docsrs, doc(cfg(all(feature = "registry", feature = "std"))))]
143+
impl<S, C> crate::subscribe::Filter<C> for Subscriber<S>
144+
where
145+
S: crate::subscribe::Filter<C> + 'static,
146+
C: Collect,
147+
{
148+
#[inline]
149+
fn callsite_enabled(&self, metadata: &'static Metadata<'static>) -> Interest {
150+
try_lock!(self.inner.read(), else return Interest::sometimes()).callsite_enabled(metadata)
151+
}
152+
153+
#[inline]
154+
fn enabled(&self, metadata: &Metadata<'_>, ctx: &subscribe::Context<'_, C>) -> bool {
155+
try_lock!(self.inner.read(), else return false).enabled(metadata, ctx)
156+
}
157+
158+
#[inline]
159+
fn on_new_span(
160+
&self,
161+
attrs: &span::Attributes<'_>,
162+
id: &span::Id,
163+
ctx: subscribe::Context<'_, C>,
164+
) {
165+
try_lock!(self.inner.read()).on_new_span(attrs, id, ctx)
166+
}
167+
168+
#[inline]
169+
fn on_record(
170+
&self,
171+
span: &span::Id,
172+
values: &span::Record<'_>,
173+
ctx: subscribe::Context<'_, C>,
174+
) {
175+
try_lock!(self.inner.read()).on_record(span, values, ctx)
176+
}
177+
178+
#[inline]
179+
fn on_enter(&self, id: &span::Id, ctx: subscribe::Context<'_, C>) {
180+
try_lock!(self.inner.read()).on_enter(id, ctx)
181+
}
182+
183+
#[inline]
184+
fn on_exit(&self, id: &span::Id, ctx: subscribe::Context<'_, C>) {
185+
try_lock!(self.inner.read()).on_exit(id, ctx)
186+
}
187+
188+
#[inline]
189+
fn on_close(&self, id: span::Id, ctx: subscribe::Context<'_, C>) {
190+
try_lock!(self.inner.read()).on_close(id, ctx)
191+
}
192+
}
193+
141194
// ===== impl Handle =====
142195

143196
impl<S> Handle<S> {

tracing-subscriber/tests/reload.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,17 @@ impl Collect for NopCollector {
3232
}
3333
}
3434

35+
pub struct NopSubscriber;
36+
impl<S: Collect> tracing_subscriber::Subscribe<S> for NopSubscriber {
37+
fn register_callsite(&self, _m: &Metadata<'_>) -> Interest {
38+
Interest::sometimes()
39+
}
40+
41+
fn enabled(&self, _m: &Metadata<'_>, _: subscribe::Context<'_, S>) -> bool {
42+
true
43+
}
44+
}
45+
3546
#[test]
3647
fn reload_handle() {
3748
static FILTER1_CALLS: AtomicUsize = AtomicUsize::new(0);
@@ -82,3 +93,51 @@ fn reload_handle() {
8293
assert_eq!(FILTER2_CALLS.load(Ordering::SeqCst), 1);
8394
})
8495
}
96+
97+
#[test]
98+
fn reload_filter() {
99+
static FILTER1_CALLS: AtomicUsize = AtomicUsize::new(0);
100+
static FILTER2_CALLS: AtomicUsize = AtomicUsize::new(0);
101+
102+
enum Filter {
103+
One,
104+
Two,
105+
}
106+
107+
impl<S: Collect> tracing_subscriber::subscribe::Filter<S> for Filter {
108+
fn enabled(&self, m: &Metadata<'_>, _: &subscribe::Context<'_, S>) -> bool {
109+
println!("ENABLED: {:?}", m);
110+
match self {
111+
Filter::One => FILTER1_CALLS.fetch_add(1, Ordering::SeqCst),
112+
Filter::Two => FILTER2_CALLS.fetch_add(1, Ordering::SeqCst),
113+
};
114+
true
115+
}
116+
}
117+
fn event() {
118+
tracing::trace!("my event");
119+
}
120+
121+
let (filter, handle) = Subscriber::new(Filter::One);
122+
123+
let dispatcher = tracing_core::dispatch::Dispatch::new(
124+
tracing_subscriber::registry().with(NopSubscriber.with_filter(filter)),
125+
);
126+
127+
tracing_core::dispatch::with_default(&dispatcher, || {
128+
assert_eq!(FILTER1_CALLS.load(Ordering::SeqCst), 0);
129+
assert_eq!(FILTER2_CALLS.load(Ordering::SeqCst), 0);
130+
131+
event();
132+
133+
assert_eq!(FILTER1_CALLS.load(Ordering::SeqCst), 1);
134+
assert_eq!(FILTER2_CALLS.load(Ordering::SeqCst), 0);
135+
136+
handle.reload(Filter::Two).expect("should reload");
137+
138+
event();
139+
140+
assert_eq!(FILTER1_CALLS.load(Ordering::SeqCst), 1);
141+
assert_eq!(FILTER2_CALLS.load(Ordering::SeqCst), 1);
142+
})
143+
}

0 commit comments

Comments
 (0)