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

Conversation

bryanburgers
Copy link
Contributor

Motivation

I'm trying to implement a Layer that has something very similar to tracing-error's WithContext to support erasing types and getting access to the current span's state.

To do that, I implement Layer::downcast_raw in the same way that tracing-error does.

This works great when the layer is not filtered. However, once I filter the layer

let filter = tracing_subscriber::filter::Targets::new().with_default(tracing::Level::INFO);
let layer = MyLayer::new();
tracing_subscriber::registry().with(layer.with_filter(filter)).init();

I'm not able to get a WithContext instance anymore, because Filtered handles downcast_raw, and doesn't forward it to MyLayer::downcast_raw.

Solution

If Filtered::downcast_raw does not know how to handle the given type, forward it to the wrapped layer's Layer::downcast_raw implementation.


Fixes #1618

If `Filtered::downcast_raw` can't resolve the type given to it, forward
the type to the wrapped `Layer`'s `downcast_raw`, in case that `Layer`
has overridden that method.
Copy link
Member

@hawkw hawkw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks great! i had a couple minor suggestions regarding the test, but take it or leave it!

tracing-subscriber/tests/layer_filters/downcast_raw.rs Outdated Show resolved Hide resolved
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.

Copy link
Member

@hawkw hawkw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this looks great, thanks again!

@bryanburgers
Copy link
Contributor Author

bryanburgers commented Oct 5, 2021

this looks great, thanks again!

Not to push my luck too far (so tell me to buzz off if you want), but any idea when this would make it into a crates.io release?

@hawkw
Copy link
Member

hawkw commented Oct 5, 2021

this looks great, thanks again!

Not to push my luck too far (so tell me to buzz off if you want), but any idea when this would make it into a crates.io release?

I want to publish a new tracing-subscriber release with this change today (hopefully within the hour!)

@hawkw hawkw merged commit 29ffdbc into tokio-rs:v0.1.x Oct 5, 2021
hawkw added a commit that referenced this pull request Oct 5, 2021
# 0.2.25 (October 5, 2021)

This release fixes an issue where a `Layer` implementation's custom
`downcast_raw` implementation was lost when wrapping that layer with a
per-layer filter.

### Fixed

- **registry**: Forward `Filtered::downcast_raw` to wrapped `Layer`
  ([#1619])

### Added

- Documentation improvements ([#1596], [#1601])

Thanks to @bryanburgers for contributing to this release!

[#1619]: #1619
[#1601]: #1601
[#1596]: #1596
hawkw added a commit that referenced this pull request Oct 5, 2021
# 0.2.25 (October 5, 2021)

This release fixes an issue where a `Layer` implementation's custom
`downcast_raw` implementation was lost when wrapping that layer with a
per-layer filter.

### Fixed

- **registry**: Forward `Filtered::downcast_raw` to wrapped `Layer`
  ([#1619])

### Added

- Documentation improvements ([#1596], [#1601])

Thanks to @bryanburgers for contributing to this release!

[#1619]: #1619
[#1601]: #1601
[#1596]: #1596
@bryanburgers bryanburgers deleted the filtered-forward-downcast branch October 6, 2021 11:54
davidbarsky added a commit that referenced this pull request Nov 29, 2021
## Motivation

I'm trying to implement a `Layer` that has something very similar to
tracing-error's
[`WithContext`](https://github.com/tokio-rs/tracing/blob/66cd79f72af5ebcb6f21a1017b6ce33bea05558d/tracing-error/src/layer.rs#L32)
to support erasing types and getting access to the current span's state.

To do that, I implement `Layer::downcast_raw` in [the same way that
tracing-error
does](https://github.com/tokio-rs/tracing/blob/66cd79f72af5ebcb6f21a1017b6ce33bea05558d/tracing-error/src/layer.rs#L55-L63).

This works great when the layer is not filtered. However, once I filter
the layer

```rust
let filter = tracing_subscriber::filter::Targets::new().with_default(tracing::Level::INFO);
let layer = MyLayer::new();
tracing_subscriber::registry().with(layer.with_filter(filter)).init();
```

I'm not able to get a `WithContext` instance anymore, because `Filtered`
[handles `downcast_raw`, and doesn't forward
it](https://github.com/tokio-rs/tracing/blob/66cd79f72af5ebcb6f21a1017b6ce33bea05558d/tracing-subscriber/src/filter/layer_filters.rs#L379-L391)
to `MyLayer::downcast_raw`.

## Solution

If `Filtered::downcast_raw` does not know how to handle the given type,
forward it to the wrapped layer's `Layer::downcast_raw` implementation.

Fixes #1618
kaffarell pushed a commit to kaffarell/tracing that referenced this pull request May 22, 2024
…rs#1619)

## Motivation

I'm trying to implement a `Layer` that has something very similar to
tracing-error's
[`WithContext`](https://github.com/tokio-rs/tracing/blob/66cd79f72af5ebcb6f21a1017b6ce33bea05558d/tracing-error/src/layer.rs#L32)
to support erasing types and getting access to the current span's state.

To do that, I implement `Layer::downcast_raw` in [the same way that
tracing-error
does](https://github.com/tokio-rs/tracing/blob/66cd79f72af5ebcb6f21a1017b6ce33bea05558d/tracing-error/src/layer.rs#L55-L63).

This works great when the layer is not filtered. However, once I filter
the layer

```rust
let filter = tracing_subscriber::filter::Targets::new().with_default(tracing::Level::INFO);
let layer = MyLayer::new();
tracing_subscriber::registry().with(layer.with_filter(filter)).init();
```

I'm not able to get a `WithContext` instance anymore, because `Filtered`
[handles `downcast_raw`, and doesn't forward
it](https://github.com/tokio-rs/tracing/blob/66cd79f72af5ebcb6f21a1017b6ce33bea05558d/tracing-subscriber/src/filter/layer_filters.rs#L379-L391)
to `MyLayer::downcast_raw`.

## Solution

If `Filtered::downcast_raw` does not know how to handle the given type,
forward it to the wrapped layer's `Layer::downcast_raw` implementation.


Fixes tokio-rs#1618
kaffarell pushed a commit to kaffarell/tracing that referenced this pull request May 22, 2024
# 0.2.25 (October 5, 2021)

This release fixes an issue where a `Layer` implementation's custom
`downcast_raw` implementation was lost when wrapping that layer with a
per-layer filter.

### Fixed

- **registry**: Forward `Filtered::downcast_raw` to wrapped `Layer`
  ([tokio-rs#1619])

### Added

- Documentation improvements ([tokio-rs#1596], [tokio-rs#1601])

Thanks to @bryanburgers for contributing to this release!

[tokio-rs#1619]: tokio-rs#1619
[tokio-rs#1601]: tokio-rs#1601
[tokio-rs#1596]: tokio-rs#1596
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants