-
Notifications
You must be signed in to change notification settings - Fork 709
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
Forward Filtered::downcast_raw
to wrapped Layer
#1619
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
65ae64c
Forward `Filtered::downcast_raw` to wrapped Layer
bryanburgers 683dbcb
Merge branch 'v0.1.x' into filtered-forward-downcast
hawkw fb2ffa1
Simplify downcast_raw test and add basic test
bryanburgers 6ed452b
Merge branch 'v0.1.x' into filtered-forward-downcast
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 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,68 @@ | ||
use tracing::Subscriber; | ||
use tracing_subscriber::filter::Targets; | ||
use tracing_subscriber::prelude::*; | ||
use tracing_subscriber::Layer; | ||
|
||
#[test] | ||
fn downcast_ref_to_inner_layer_and_filter() { | ||
// Test that a filtered layer gives downcast_ref access to | ||
// both the layer and the filter. | ||
|
||
struct WrappedLayer; | ||
|
||
impl<S> Layer<S> for WrappedLayer | ||
where | ||
S: Subscriber, | ||
S: for<'lookup> tracing_subscriber::registry::LookupSpan<'lookup>, | ||
{ | ||
} | ||
|
||
let layer = WrappedLayer; | ||
let filter = Targets::new().with_default(tracing::Level::INFO); | ||
let registry = tracing_subscriber::registry().with(layer.with_filter(filter)); | ||
let dispatch = tracing::dispatcher::Dispatch::new(registry); | ||
|
||
// The filter is available | ||
assert!(dispatch.downcast_ref::<Targets>().is_some()); | ||
// The wrapped layer is available | ||
assert!(dispatch.downcast_ref::<WrappedLayer>().is_some()); | ||
} | ||
|
||
#[test] | ||
fn forward_downcast_raw_to_layer() { | ||
// Test that a filtered layer still gives its wrapped layer a chance to | ||
// return a custom struct from downcast_raw. | ||
// https://github.com/tokio-rs/tracing/issues/1618 | ||
|
||
struct WrappedLayer { | ||
with_context: WithContext, | ||
} | ||
|
||
struct WithContext; | ||
|
||
impl<S> Layer<S> for WrappedLayer | ||
where | ||
S: Subscriber, | ||
S: for<'lookup> tracing_subscriber::registry::LookupSpan<'lookup>, | ||
{ | ||
unsafe fn downcast_raw(&self, id: std::any::TypeId) -> Option<*const ()> { | ||
match id { | ||
id if id == std::any::TypeId::of::<Self>() => Some(self as *const _ as *const ()), | ||
id if id == std::any::TypeId::of::<WithContext>() => { | ||
Some(&self.with_context as *const _ as *const ()) | ||
} | ||
_ => None, | ||
} | ||
} | ||
} | ||
|
||
let layer = WrappedLayer { | ||
with_context: WithContext, | ||
}; | ||
let filter = Targets::new().with_default(tracing::Level::INFO); | ||
let registry = tracing_subscriber::registry().with(layer.with_filter(filter)); | ||
let dispatch = tracing::dispatcher::Dispatch::new(registry); | ||
|
||
// Types from a custom implementation of `downcast_raw` are available | ||
assert!(dispatch.downcast_ref::<WithContext>().is_some()); | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it may also be nice to add a test for downcasting to the
Layer
type itself, ensuring that normal downcasting works? this PR doesn't affect that, so we certainly don't need to also add it, but it might be nice to have if we're going to have a module ofdowncast_raw
tests. could also be added in a follow-up change!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, I added
downcast_ref_to_inner_layer_and_filter
for this. Let me know if it needs anything more.