-
Notifications
You must be signed in to change notification settings - Fork 721
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 `Collect::event_enabled` method, taking an `Event` and returning `bool`. This is called before the `Collect::event` method, and if it returns `false`, `Collect::event` is not called. For simple collectors (e.g. not using `Subscribe` layers), the `event_enabled` method is not particulary necessary, as the collector can just skip the `event` call. However, this branch also adds a new `Subscribe::event_enabled` method, with the signature: ```rust fn event_enabled(&self, event: &Event<'_>, ctx: Context<'_, C>) -> bool; ``` This is called before `Subscribe::on_event`, and if `event_enabled` returns `false`, `on_event` is not called (nor is `Collect::event`). This allows filter subscribers to filter out an `Event` based on its fields. Closes #2007
- Loading branch information
Showing
7 changed files
with
203 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::{collect::with_default, Collect, Event, Metadata}; | ||
use tracing_subscriber::{prelude::*, registry, subscribe::Context, Subscribe}; | ||
|
||
struct TrackingLayer { | ||
enabled: bool, | ||
event_enabled_count: Arc<Mutex<usize>>, | ||
event_enabled: bool, | ||
on_event_count: Arc<Mutex<usize>>, | ||
} | ||
|
||
impl<C> Subscribe<C> for TrackingLayer | ||
where | ||
C: Collect + 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 collector = registry().with(TrackingLayer { | ||
enabled: true, | ||
event_enabled_count, | ||
event_enabled: true, | ||
on_event_count: Arc::new(Mutex::default()), | ||
}); | ||
with_default(collector, || { | ||
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 collector = registry().with(TrackingLayer { | ||
enabled: false, | ||
event_enabled_count, | ||
event_enabled: true, | ||
on_event_count: Arc::new(Mutex::default()), | ||
}); | ||
with_default(collector, || { | ||
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 collector = registry().with(TrackingLayer { | ||
enabled: true, | ||
event_enabled_count: Arc::new(Mutex::default()), | ||
event_enabled: false, | ||
on_event_count, | ||
}); | ||
with_default(collector, || { | ||
tracing::error!("hiya!"); | ||
}); | ||
|
||
assert_eq!(0, *count.lock().unwrap()); | ||
} |