-
Notifications
You must be signed in to change notification settings - Fork 713
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
core, subscriber: add
event_enabled
to filter events on fields (#2008)
## Motivation Allow filter layers to filter on the contents of events (see #2007). ## Solution This branch adds a new `Subscriber::event_enabled` method, taking an `Event` and returning `bool`. This is called before the `Subscriber::event` method, and if it returns `false`, `Subscriber::event` is not called. For simple subscriber (e.g. not using `Layer`s), the `event_enabled` method is not particulary necessary, as the subscriber can just skip the `event` call. However, this branch also adds a new `Layer::event_enabled` method, with the signature: ```rust fn event_enabled(&self, event: &Event<'_>, ctx: Context<'_, S>) -> bool; ``` This is called before `Layer::on_event`, and if `event_enabled` returns `false`, `on_event` is not called (nor is `Subscriber::event`). This allows filter `Layer`s to filter out an `Event` based on its fields. Closes #2007
- Loading branch information
Showing
7 changed files
with
206 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#![cfg(feature = "registry")] | ||
|
||
use std::sync::{Arc, Mutex}; | ||
use tracing::{subscriber::with_default, Event, Metadata, Subscriber}; | ||
use tracing_subscriber::{layer::Context, prelude::*, registry, Layer}; | ||
|
||
struct TrackingLayer { | ||
enabled: bool, | ||
event_enabled_count: Arc<Mutex<usize>>, | ||
event_enabled: bool, | ||
on_event_count: Arc<Mutex<usize>>, | ||
} | ||
|
||
impl<C> Layer<C> for TrackingLayer | ||
where | ||
C: Subscriber + Send + Sync + 'static, | ||
{ | ||
fn enabled(&self, _metadata: &Metadata<'_>, _ctx: Context<'_, C>) -> bool { | ||
self.enabled | ||
} | ||
|
||
fn event_enabled(&self, _event: &Event<'_>, _ctx: Context<'_, C>) -> bool { | ||
*self.event_enabled_count.lock().unwrap() += 1; | ||
self.event_enabled | ||
} | ||
|
||
fn on_event(&self, _event: &Event<'_>, _ctx: Context<'_, C>) { | ||
*self.on_event_count.lock().unwrap() += 1; | ||
} | ||
} | ||
|
||
#[test] | ||
fn event_enabled_is_only_called_once() { | ||
let event_enabled_count = Arc::new(Mutex::default()); | ||
let count = event_enabled_count.clone(); | ||
let subscriber = registry().with(TrackingLayer { | ||
enabled: true, | ||
event_enabled_count, | ||
event_enabled: true, | ||
on_event_count: Arc::new(Mutex::default()), | ||
}); | ||
with_default(subscriber, || { | ||
tracing::error!("hiya!"); | ||
}); | ||
|
||
assert_eq!(1, *count.lock().unwrap()); | ||
} | ||
|
||
#[test] | ||
fn event_enabled_not_called_when_not_enabled() { | ||
let event_enabled_count = Arc::new(Mutex::default()); | ||
let count = event_enabled_count.clone(); | ||
let subscriber = registry().with(TrackingLayer { | ||
enabled: false, | ||
event_enabled_count, | ||
event_enabled: true, | ||
on_event_count: Arc::new(Mutex::default()), | ||
}); | ||
with_default(subscriber, || { | ||
tracing::error!("hiya!"); | ||
}); | ||
|
||
assert_eq!(0, *count.lock().unwrap()); | ||
} | ||
|
||
#[test] | ||
fn event_disabled_does_disable_event() { | ||
let on_event_count = Arc::new(Mutex::default()); | ||
let count = on_event_count.clone(); | ||
let subscriber = registry().with(TrackingLayer { | ||
enabled: true, | ||
event_enabled_count: Arc::new(Mutex::default()), | ||
event_enabled: false, | ||
on_event_count, | ||
}); | ||
with_default(subscriber, || { | ||
tracing::error!("hiya!"); | ||
}); | ||
|
||
assert_eq!(0, *count.lock().unwrap()); | ||
} |