-
Notifications
You must be signed in to change notification settings - Fork 721
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
tracing-core, tracing-subscriber: Add {Collect,Subscriber}::on_register_dispatch
#2269
Conversation
405c060
to
6c67cce
Compare
…er_dispatch` The `on_register_dispatch` method is invoked when a `Collect` is registered as a `Dispatch`. This method should be overridden to perform actions upon the installation of a collector/subscriber; for instance, to send a copy of the collector's `Dispatch` to a worker thread.
6c67cce
to
6023a0a
Compare
I think it can't be For the |
@jswrenn I think it's failing CI cause of clippy |
I'm not opposed to this change (it is rather small...), but I'd like a little more detail about the motivations for this use-case, as I'm somewhat hesitant to make changes to |
it should not take |
@hawkw, whoops, I definitely meant to write:
An argument in favor of the method consuming |
I think the receiver should probably be It's a bummer that we can't just use an |
It occurs to me that adding an API like this has the unfortunate consequence of making it quite easy to accidentally write memory leaks. In your specific use-case in Currently, there's no public API for turning a |
My feeling is that it's probably uncommon to have many complex subscribers initialized and installed over a program's lifetime, so the risk is low. In the near-term, it's sufficient to document this risk. In the long-term, we could add |
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.
stamped; needed for console, but this might be removed in 0.2 because of other collector changes.
@hawkw mentioned in discord that she'd like a way to get a weak ref to a dispatcher, but noted that can be done in followup PR. |
Yeah. I would prefer to not release this feature without some way of breaking |
The `on_register_dispatch` method is invoked when a `Collect` is registered as a `Dispatch`. This method should be overridden to perform actions upon the installation of a collector/subscriber; for instance, to send a copy of the collector's `Dispatch` to a worker thread.
The `on_register_dispatch` method is invoked when a `Subscriber` is registered as a `Dispatch`. This method should be overridden to perform actions upon the installation of a subscriber/layer; for instance, to send a copy of the subscriber's `Dispatch` to a worker thread.
The `on_register_dispatch` method is invoked when a `Subscriber` is registered as a `Dispatch`. This method should be overridden to perform actions upon the installation of a subscriber/layer; for instance, to send a copy of the subscriber's `Dispatch` to a worker thread.
Allows collectors and subscribers to stash their own `Dispatch` without causing a memory leak. ## Motivation Resolves a shortcoming of #2269: that it's impossible for collectors or subscribers to stash a copy of their own `Dispatch` without creating a reference cycle that would prevent them from being dropped. ## Solution Introduces `WeakDispatch` (analogous to `std::sync::Weak`) that holds a weak pointer to a collector. `WeakDispatch` can be created via `Dispatch::downgrade`, and can be transformed into a `Dispatch` via `WeakDispatch::upgrade`. Co-authored-by: Eliza Weisman <eliza@buoyant.io>
Allows `Subscriber`s and `Layer`s to stash their own `Dispatch` without causing a memory leak. ## Motivation Resolves a shortcoming of #2269: that it's impossible for `Subscriber`s or `Layer`s to stash a copy of their own `Dispatch` without creating a reference cycle that would prevent them from being dropped. ## Solution Introduces `WeakDispatch` (analogous to `std::sync::Weak`) that holds a weak pointer to a `Subscriber`. `WeakDispatch` can be created via `Dispatch::downgrade`, and can be transformed into a `Dispatch` via `WeakDispatch::upgrade`. Co-authored-by: Eliza Weisman <eliza@buoyant.io>
Allows `Subscriber`s and `Layer`s to stash their own `Dispatch` without causing a memory leak. ## Motivation Resolves a shortcoming of #2269: that it's impossible for `Subscriber`s or `Layer`s to stash a copy of their own `Dispatch` without creating a reference cycle that would prevent them from being dropped. ## Solution Introduces `WeakDispatch` (analogous to `std::sync::Weak`) that holds a weak pointer to a `Subscriber`. `WeakDispatch` can be created via `Dispatch::downgrade`, and can be transformed into a `Dispatch` via `WeakDispatch::upgrade`. Co-authored-by: Eliza Weisman <eliza@buoyant.io>
Allows `Subscriber`s and `Layer`s to stash their own `Dispatch` without causing a memory leak. ## Motivation Resolves a shortcoming of #2269: that it's impossible for `Subscriber`s or `Layer`s to stash a copy of their own `Dispatch` without creating a reference cycle that would prevent them from being dropped. ## Solution Introduces `WeakDispatch` (analogous to `std::sync::Weak`) that holds a weak pointer to a `Subscriber`. `WeakDispatch` can be created via `Dispatch::downgrade`, and can be transformed into a `Dispatch` via `WeakDispatch::upgrade`. Co-authored-by: Eliza Weisman <eliza@buoyant.io>
Allows `Subscriber`s and `Layer`s to stash their own `Dispatch` without causing a memory leak. ## Motivation Resolves a shortcoming of #2269: that it's impossible for `Subscriber`s or `Layer`s to stash a copy of their own `Dispatch` without creating a reference cycle that would prevent them from being dropped. ## Solution Introduces `WeakDispatch` (analogous to `std::sync::Weak`) that holds a weak pointer to a `Subscriber`. `WeakDispatch` can be created via `Dispatch::downgrade`, and can be transformed into a `Dispatch` via `WeakDispatch::upgrade`. Co-authored-by: Eliza Weisman <eliza@buoyant.io>
# 0.1.30 (October 6, 2022) This release of `tracing-core` adds a new `on_register_dispatch` method to the `Subscriber` trait to allow the `Subscriber` to perform initialization after being registered as a `Dispatch`, and a `WeakDispatch` type to allow a `Subscriber` to store its own `Dispatch` without creating reference count cycles. ### Added - `Subscriber::on_register_dispatch` method ([#2269]) - `WeakDispatch` type and `Dispatch::downgrade()` function ([#2293]) Thanks to @jswrenn for contributing to this release! [#2269]: #2269 [#2293]: #2293
# 0.1.30 (October 6, 2022) This release of `tracing-core` adds a new `on_register_dispatch` method to the `Subscriber` trait to allow the `Subscriber` to perform initialization after being registered as a `Dispatch`, and a `WeakDispatch` type to allow a `Subscriber` to store its own `Dispatch` without creating reference count cycles. ### Added - `Subscriber::on_register_dispatch` method ([#2269]) - `WeakDispatch` type and `Dispatch::downgrade()` function ([#2293]) Thanks to @jswrenn for contributing to this release! [#2269]: #2269 [#2293]: #2293
# 0.1.37 (October 6, 2022) This release of `tracing` incorporates changes from `tracing-core` [v0.1.30][core-0.1.30] and `tracing-attributes` [v0.1.23][0.1.23], including the new `Subscriber::on_register_dispatch` method for performing late initialization after a `Subscriber` is registered as a `Dispatch`, and bugfixes for the `#[instrument]` attribute. Additionally, it fixes instances of the `bare_trait_objects` lint, which is now a warning on `tracing`'s MSRV and will become an error in the next edition. ### Fixed - **attributes**: Incorrect handling of inner attributes in `#[instrument]`ed functions (#2307) - **attributes**: Incorrect location of compiler diagnostic spans generated for type errors in `#[instrument]`ed `async fn`s (#2270) - **attributes**: Updated `syn` dependency to fix compilation with `-Z minimal-versions` (#2246) - `bare_trait_objects` warning in `valueset!` macro expansion (#2308) ### Added - **core**: `Subscriber::on_register_dispatch` method (#2269) - **core**: `WeakDispatch` type and `Dispatch::downgrade()` function (#2293) ### Changed - `tracing-core`: updated to [0.1.30][core-0.1.30] - `tracing-attributes`: updated to [0.1.23][attrs-0.1.23] ### Documented - Added [`tracing-web`] and [`reqwest-tracing`] to related crates (#2283], [#2331) Thanks to new contributors @compiler-errors, @e-nomem, @WorldSEnder, @Xiami2012, and @tl-rodrigo-gryzinski, as well as @jswrenn and @CAD97, for contributing to this release! [core-0.1.30]: https://github.com/tokio-rs/tracing/releases/tag/tracing-core-0.1.30 [attrs-0.1.23]: https://github.com/tokio-rs/tracing/releases/tag/tracing-attributes-0.1.23 [`tracing-web`]: https://crates.io/crates/tracing-web/ [`reqwest-tracing`]: https://crates.io/crates/reqwest-tracing/
# 0.1.37 (October 6, 2022) This release of `tracing` incorporates changes from `tracing-core` [v0.1.30][core-0.1.30] and `tracing-attributes` [v0.1.23][attrs-0.1.23], including the new `Subscriber::on_register_dispatch` method for performing late initialization after a `Subscriber` is registered as a `Dispatch`, and bugfixes for the `#[instrument]` attribute. Additionally, it fixes instances of the `bare_trait_objects` lint, which is now a warning on `tracing`'s MSRV and will become an error in the next edition. ### Fixed - **attributes**: Incorrect handling of inner attributes in `#[instrument]`ed functions (#2307) - **attributes**: Incorrect location of compiler diagnostic spans generated for type errors in `#[instrument]`ed `async fn`s (#2270) - **attributes**: Updated `syn` dependency to fix compilation with `-Z minimal-versions` (#2246) - `bare_trait_objects` warning in `valueset!` macro expansion (#2308) ### Added - **core**: `Subscriber::on_register_dispatch` method (#2269) - **core**: `WeakDispatch` type and `Dispatch::downgrade()` function (#2293) ### Changed - `tracing-core`: updated to [0.1.30][core-0.1.30] - `tracing-attributes`: updated to [0.1.23][attrs-0.1.23] ### Documented - Added [`tracing-web`] and [`reqwest-tracing`] to related crates (#2283, #2331) Thanks to new contributors @compiler-errors, @e-nomem, @WorldSEnder, @Xiami2012, and @tl-rodrigo-gryzinski, as well as @jswrenn and @CAD97, for contributing to this release! [core-0.1.30]: https://github.com/tokio-rs/tracing/releases/tag/tracing-core-0.1.30 [attrs-0.1.23]: https://github.com/tokio-rs/tracing/releases/tag/tracing-attributes-0.1.23 [`tracing-web`]: https://crates.io/crates/tracing-web/ [`reqwest-tracing`]: https://crates.io/crates/reqwest-tracing/
)" This reverts commit a0824d3 (PR #2247). As discussed in [this comment][1], the implementation for `Arc`s may cause subtly incorrect behavior if actually used, due to the `&mut self` receiver of the `LookupSpan::register_filter` method, since the `Arc` cannot be mutably borrowed if any clones of it exist. The APIs added in PRs #2269 and #2293 offer an alternative solution to the same problems this change was intended to solve, and --- since this change hasn't been published yet --- it can safely be reverted. [1]: https://giethub.com/tokio-rs/tracing/pull/2247#issuecomment-1199924876
The `on_register_dispatch` method is invoked when a `Subscriber` is registered as a `Dispatch`. This method should be overridden to perform actions upon the installation of a subscriber/layer; for instance, to send a copy of the subscriber's `Dispatch` to a worker thread.
Allows `Subscriber`s and `Layer`s to stash their own `Dispatch` without causing a memory leak. ## Motivation Resolves a shortcoming of tokio-rs#2269: that it's impossible for `Subscriber`s or `Layer`s to stash a copy of their own `Dispatch` without creating a reference cycle that would prevent them from being dropped. ## Solution Introduces `WeakDispatch` (analogous to `std::sync::Weak`) that holds a weak pointer to a `Subscriber`. `WeakDispatch` can be created via `Dispatch::downgrade`, and can be transformed into a `Dispatch` via `WeakDispatch::upgrade`. Co-authored-by: Eliza Weisman <eliza@buoyant.io>
# 0.1.30 (October 6, 2022) This release of `tracing-core` adds a new `on_register_dispatch` method to the `Subscriber` trait to allow the `Subscriber` to perform initialization after being registered as a `Dispatch`, and a `WeakDispatch` type to allow a `Subscriber` to store its own `Dispatch` without creating reference count cycles. ### Added - `Subscriber::on_register_dispatch` method ([tokio-rs#2269]) - `WeakDispatch` type and `Dispatch::downgrade()` function ([tokio-rs#2293]) Thanks to @jswrenn for contributing to this release! [tokio-rs#2269]: tokio-rs#2269 [tokio-rs#2293]: tokio-rs#2293
# 0.1.37 (October 6, 2022) This release of `tracing` incorporates changes from `tracing-core` [v0.1.30][core-0.1.30] and `tracing-attributes` [v0.1.23][attrs-0.1.23], including the new `Subscriber::on_register_dispatch` method for performing late initialization after a `Subscriber` is registered as a `Dispatch`, and bugfixes for the `#[instrument]` attribute. Additionally, it fixes instances of the `bare_trait_objects` lint, which is now a warning on `tracing`'s MSRV and will become an error in the next edition. ### Fixed - **attributes**: Incorrect handling of inner attributes in `#[instrument]`ed functions (tokio-rs#2307) - **attributes**: Incorrect location of compiler diagnostic spans generated for type errors in `#[instrument]`ed `async fn`s (tokio-rs#2270) - **attributes**: Updated `syn` dependency to fix compilation with `-Z minimal-versions` (tokio-rs#2246) - `bare_trait_objects` warning in `valueset!` macro expansion (tokio-rs#2308) ### Added - **core**: `Subscriber::on_register_dispatch` method (tokio-rs#2269) - **core**: `WeakDispatch` type and `Dispatch::downgrade()` function (tokio-rs#2293) ### Changed - `tracing-core`: updated to [0.1.30][core-0.1.30] - `tracing-attributes`: updated to [0.1.23][attrs-0.1.23] ### Documented - Added [`tracing-web`] and [`reqwest-tracing`] to related crates (tokio-rs#2283, tokio-rs#2331) Thanks to new contributors @compiler-errors, @e-nomem, @WorldSEnder, @Xiami2012, and @tl-rodrigo-gryzinski, as well as @jswrenn and @CAD97, for contributing to this release! [core-0.1.30]: https://github.com/tokio-rs/tracing/releases/tag/tracing-core-0.1.30 [attrs-0.1.23]: https://github.com/tokio-rs/tracing/releases/tag/tracing-attributes-0.1.23 [`tracing-web`]: https://crates.io/crates/tracing-web/ [`reqwest-tracing`]: https://crates.io/crates/reqwest-tracing/
…kio-rs#2247)" This reverts commit a0824d3 (PR tokio-rs#2247). As discussed in [this comment][1], the implementation for `Arc`s may cause subtly incorrect behavior if actually used, due to the `&mut self` receiver of the `LookupSpan::register_filter` method, since the `Arc` cannot be mutably borrowed if any clones of it exist. The APIs added in PRs tokio-rs#2269 and tokio-rs#2293 offer an alternative solution to the same problems this change was intended to solve, and --- since this change hasn't been published yet --- it can safely be reverted. [1]: https://giethub.com/tokio-rs/tracing/pull/2247#issuecomment-1199924876
The
on_register_dispatch
method is invoked when aCollect
is registered as aDispatch
. This method can be overridden to perform actions upon the installation of a collector/subscriber; for instance, to send a copy of the collector'sDispatch
to a worker thread.Unresolved questions:
on_register_dispatch
correct?&self
, or&mut self
?&self
, or only the&Dispatch
parameter (which can then, if desired, be downcasted to&self
)?