-
Notifications
You must be signed in to change notification settings - Fork 847
Add Filter::event_enabled
#2245
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
7 commits
Select commit
Hold shift + click to select a range
f7c189b
Allow per-subscribe filters to participate in event_enabled
CAD97 a88270f
Test per-subscribe filters on event fields
CAD97 31b7d30
Apply review suggestions
CAD97 4b627ef
Merge branch 'master' into filter-event-enabled
hawkw 9270c95
Improve documentation w.r.t. Filter::event_enabled
CAD97 280cc0d
Fix intra-doc link
CAD97 826afa7
Apply docs suggestions from code review
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1382,6 +1382,26 @@ pub trait Filter<S> { | |
| None | ||
| } | ||
|
|
||
| /// Called before the filtered subscribers' [`on_event`], to determine if | ||
| /// `on_event` should be called. | ||
| /// | ||
| /// This gives a chance to filter events based on their fields. Note, | ||
| /// however, that this *does not* override [`enabled`], and is not even | ||
| /// called if [`enabled`] returns `false`. | ||
| /// | ||
CAD97 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /// ## Default Implementation | ||
| /// | ||
| /// By default, this method returns `true`, indicating that no events are | ||
| /// filtered out based on their fields. | ||
| /// | ||
| /// [`enabled`]: crate::subscribe::Filter::enabled | ||
| /// [`on_event`]: crate::subscribe::Subscribe::on_event | ||
| #[inline] // collapse this to a constant please mrs optimizer | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i like how the optimizer is, apparently, married :)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| fn event_enabled(&self, event: &Event<'_>, cx: &Context<'_, S>) -> bool { | ||
| let _ = (event, cx); | ||
| true | ||
| } | ||
|
|
||
| /// Notifies this filter that a new span was constructed with the given | ||
| /// `Attributes` and `Id`. | ||
| /// | ||
|
|
||
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 |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |
| mod support; | ||
| use self::support::*; | ||
| mod filter_scopes; | ||
| mod per_event; | ||
| mod targets; | ||
| mod trees; | ||
| mod vec; | ||
|
|
||
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,61 @@ | ||
| use crate::support::*; | ||
| use tracing::Level; | ||
| use tracing_subscriber::{field::Visit, prelude::*, subscribe::Filter}; | ||
|
|
||
| struct FilterEvent; | ||
|
|
||
| impl<S> Filter<S> for FilterEvent { | ||
| fn enabled( | ||
| &self, | ||
| _meta: &tracing::Metadata<'_>, | ||
| _cx: &tracing_subscriber::subscribe::Context<'_, S>, | ||
| ) -> bool { | ||
| true | ||
| } | ||
|
|
||
| fn event_enabled( | ||
| &self, | ||
| event: &tracing::Event<'_>, | ||
| _cx: &tracing_subscriber::subscribe::Context<'_, S>, | ||
| ) -> bool { | ||
| struct ShouldEnable(bool); | ||
| impl Visit for ShouldEnable { | ||
| fn record_bool(&mut self, field: &tracing_core::Field, value: bool) { | ||
| if field.name() == "enable" { | ||
| self.0 = value; | ||
| } | ||
| } | ||
|
|
||
| fn record_debug( | ||
| &mut self, | ||
| _field: &tracing_core::Field, | ||
| _value: &dyn core::fmt::Debug, | ||
| ) { | ||
| } | ||
| } | ||
| let mut should_enable = ShouldEnable(false); | ||
| event.record(&mut should_enable); | ||
| should_enable.0 | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn per_subscriber_event_field_filtering() { | ||
| let (expect, handle) = subscriber::mock() | ||
| .event(event::mock().at_level(Level::TRACE)) | ||
| .event(event::mock().at_level(Level::INFO)) | ||
| .done() | ||
| .run_with_handle(); | ||
|
|
||
| let _subscriber = tracing_subscriber::registry() | ||
| .with(expect.with_filter(FilterEvent)) | ||
| .set_default(); | ||
|
|
||
| tracing::trace!(enable = true, "hello trace"); | ||
| tracing::debug!("hello debug"); | ||
| tracing::info!(enable = true, "hello info"); | ||
| tracing::warn!(enable = false, "hello warn"); | ||
| tracing::error!("hello error"); | ||
|
|
||
| handle.assert_finished(); | ||
| } |
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.