Skip to content
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 4 commits into from
Oct 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tracing-subscriber/src/filter/layer_filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,7 @@ where
id if id == TypeId::of::<MagicPlfDowncastMarker>() => {
Some(&self.id as *const _ as *const ())
}
_ => None,
_ => self.layer.downcast_raw(id),
}
}
}
Expand Down
68 changes: 68 additions & 0 deletions tracing-subscriber/tests/layer_filters/downcast_raw.rs
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;

Copy link
Member

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 of downcast_raw tests. could also be added in a follow-up change!

Copy link
Contributor Author

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.

#[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());
}
1 change: 1 addition & 0 deletions tracing-subscriber/tests/layer_filters/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
mod support;
use self::support::*;
mod boxed;
mod downcast_raw;
mod filter_scopes;
mod targets;
mod trees;
Expand Down