-
Notifications
You must be signed in to change notification settings - Fork 792
Add Subscribe::event_enabled to conditionally dis/enable events based on fields #2008
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
029be0e
Let Subscribe disable specific events
CAD97 2f69943
Explain how various interest enablers work together
CAD97 30f0644
Rescue event_enabled impls from the rebase wastes
CAD97 ce016e1
Improve event_enabled order independence
CAD97 53ed1dc
Properly call Collect::event_enabled in more places
CAD97 f5f1758
Apply code review documentation improvements
CAD97 952538f
Ensure event_enabled is not called extra times
CAD97 d400cf6
Gate event_enabling test for registry presence
CAD97 0eda53b
Merge branch 'master' into on_event_control_flow
hawkw 57e2a47
Update tracing-subscriber/src/subscribe/mod.rs
CAD97 536ed44
More event_enabled tests
CAD97 4620bd2
Merge branch 'master' into on_event_control_flow
hawkw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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")] | ||
|
||
CAD97 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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()); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.