tracing: add Span::or_current to help with efficient propagation#1538
Merged
tracing: add Span::or_current to help with efficient propagation#1538
Span::or_current to help with efficient propagation#1538Conversation
This should provide a more efficient alternative to writing
```rust
tokio::spawn(
future
.instrument(some_span)
.in_current_span()
);
```
since it doesn't wrap the future in two different spans which are
(potentially) both entered on polls.
As a follow-up, we may want to also consider adding an `Instrument`-like
function that automatically calls `or_current`.
davidbarsky
approved these changes
Sep 4, 2021
Member
davidbarsky
left a comment
There was a problem hiding this comment.
Two notes:
- I feel like this the necessity of this combinator illustrates why don't really like levels in spans.
- When backporting, be sure to swap out "collector" for "subscriber"!
tracing/src/span.rs
Outdated
| /// the [current span], if this span [is disabled]. | ||
| /// | ||
| /// This method can be useful when propagating spans to spawned threads or | ||
| /// [async tasks]. For example, consider the following: |
Member
There was a problem hiding this comment.
Suggested change
| /// [async tasks]. For example, consider the following: | |
| /// [async tasks]. Consider the following: |
davidbarsky
reviewed
Sep 4, 2021
davidbarsky
reviewed
Sep 4, 2021
Co-authored-by: David Barsky <me@davidbarsky.com>
Member
Author
Sure...but it's also not exclusively due to span levels. Even if spans don't have levels, you could still have a similar scenario if you're enabling spans based on their targets. For example, a parent span might be propagated by a child in a module you disable by target. It's a consequence of any kind of filtering of spans, not just by level. Although, I will admit that it's easier to end up in this situation with level-based filtering, I think that's more a consequence of most people doing level-based filtering. |
hawkw
commented
Sep 4, 2021
Member
Author
|
@davidbarsky couple quick questions before I merge this:
|
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
Signed-off-by: Eliza Weisman <eliza@buoyant.io>
hawkw
added a commit
that referenced
this pull request
Sep 4, 2021
…1538) This adds a new `Span::or_current` method that returns the span it's called on, if that span is enabled, or the current span if that span is not enabled. This should provide a more efficient alternative to writing code like ```rust tokio::spawn( future .instrument(some_span) .in_current_span() ); ``` since it doesn't wrap the future in two different spans which are (potentially) both entered on polls. It's also a little more concise to write, which (hopefully) will encourage people to use it instead of the less efficient alternative. `Span::or_current` may be useful in other cases as well. Signed-off-by: Eliza Weisman <eliza@buoyant.io>
hawkw
added a commit
that referenced
this pull request
Sep 4, 2021
…1538) This adds a new `Span::or_current` method that returns the span it's called on, if that span is enabled, or the current span if that span is not enabled. This should provide a more efficient alternative to writing code like ```rust tokio::spawn( future .instrument(some_span) .in_current_span() ); ``` since it doesn't wrap the future in two different spans which are (potentially) both entered on polls. It's also a little more concise to write, which (hopefully) will encourage people to use it instead of the less efficient alternative. `Span::or_current` may be useful in other cases as well. Signed-off-by: Eliza Weisman <eliza@buoyant.io>
hawkw
added a commit
that referenced
this pull request
Sep 13, 2021
# 0.1.27 (September 13, 2021) This release adds a new [`Span::or_current`] method to aid in efficiently propagating span contexts to spawned threads or tasks. Additionally, it updates the [`tracing-core`] version to [0.1.20] and the [`tracing-attributes`] version to [0.1.16], ensuring that a number of new features in those crates are present. ### Fixed - **instrument**: Added missing `WithSubscriber` implementations for futures and other types ([#1424]) ### Added - `Span::or_current` method, to help with efficient span context propagation ([#1538]) - **attributes**: add `skip_all` option to `#[instrument]` ([#1548]) - **attributes**: record primitive types as primitive values rather than as `fmt::Debug` ([#1378]) - **core**: `NoSubscriber`, a no-op `Subscriber` implementation ([#1549]) - **core**: Added `Visit::record_f64` and support for recording floating-point values ([#1507], [#1522]) - A large number of documentation improvements and fixes ([#1369], [#1398], [#1435], [#1442], [#1524], [#1556]) Thanks to new contributors @dzvon and @mbergkvist, as well as @teozkr, @maxburke, @LukeMathWalker, and @jsgf, for contributing to this release! [`Span::or_current`]: https://docs.rs/tracing/0.1.27/tracing/struct.Span.html#method.or_current [`tracing-core`]: https://crates.io/crates/tracing-core [`tracing-attributes`]: https://crates.io/crates/tracing-attributes [`tracing-core`]: https://crates.io/crates/tracing-core [0.1.20]: https://github.com/tokio-rs/tracing/releases/tag/tracing-core-0.1.20 [0.1.16]: https://github.com/tokio-rs/tracing/releases/tag/tracing-attributes-0.1.16 [#1424]: #1424 [#1538]: #1538 [#1548]: #1548 [#1378]: #1378 [#1507]: #1507 [#1522]: #1522 [#1369]: #1369 [#1398]: #1398 [#1435]: #1435 [#1442]: #1442
hawkw
added a commit
that referenced
this pull request
Sep 13, 2021
# 0.1.27 (September 13, 2021) This release adds a new [`Span::or_current`] method to aid in efficiently propagating span contexts to spawned threads or tasks. Additionally, it updates the [`tracing-core`] version to [0.1.20] and the [`tracing-attributes`] version to [0.1.16], ensuring that a number of new features in those crates are present. ### Fixed - **instrument**: Added missing `WithSubscriber` implementations for futures and other types (#1424) ### Added - `Span::or_current` method, to help with efficient span context propagation (#1538) - **attributes**: add `skip_all` option to `#[instrument]` (#1548) - **attributes**: record primitive types as primitive values rather than as `fmt::Debug` (#1378) - **core**: `NoSubscriber`, a no-op `Subscriber` implementation (#1549) - **core**: Added `Visit::record_f64` and support for recording floating-point values (#1507, #1522) - A large number of documentation improvements and fixes (#1369, #1398, #1435, #1442, #1524, #1556) Thanks to new contributors @dzvon and @mbergkvist, as well as @teozkr, @maxburke, @LukeMathWalker, and @jsgf, for contributing to this release! [`Span::or_current`]: https://docs.rs/tracing/0.1.27/tracing/struct.Span.html#method.or_current [`tracing-core`]: https://crates.io/crates/tracing-core [`tracing-attributes`]: https://crates.io/crates/tracing-attributes [`tracing-core`]: https://crates.io/crates/tracing-core [0.1.20]: https://github.com/tokio-rs/tracing/releases/tag/tracing-core-0.1.20 [0.1.16]: https://github.com/tokio-rs/tracing/releases/tag/tracing-attributes-0.1.16
kaffarell
pushed a commit
to kaffarell/tracing
that referenced
this pull request
May 22, 2024
# 0.1.27 (September 13, 2021) This release adds a new [`Span::or_current`] method to aid in efficiently propagating span contexts to spawned threads or tasks. Additionally, it updates the [`tracing-core`] version to [0.1.20] and the [`tracing-attributes`] version to [0.1.16], ensuring that a number of new features in those crates are present. ### Fixed - **instrument**: Added missing `WithSubscriber` implementations for futures and other types (tokio-rs#1424) ### Added - `Span::or_current` method, to help with efficient span context propagation (tokio-rs#1538) - **attributes**: add `skip_all` option to `#[instrument]` (tokio-rs#1548) - **attributes**: record primitive types as primitive values rather than as `fmt::Debug` (tokio-rs#1378) - **core**: `NoSubscriber`, a no-op `Subscriber` implementation (tokio-rs#1549) - **core**: Added `Visit::record_f64` and support for recording floating-point values (tokio-rs#1507, tokio-rs#1522) - A large number of documentation improvements and fixes (tokio-rs#1369, tokio-rs#1398, tokio-rs#1435, tokio-rs#1442, tokio-rs#1524, tokio-rs#1556) Thanks to new contributors @dzvon and @mbergkvist, as well as @teozkr, @maxburke, @LukeMathWalker, and @jsgf, for contributing to this release! [`Span::or_current`]: https://docs.rs/tracing/0.1.27/tracing/struct.Span.html#method.or_current [`tracing-core`]: https://crates.io/crates/tracing-core [`tracing-attributes`]: https://crates.io/crates/tracing-attributes [`tracing-core`]: https://crates.io/crates/tracing-core [0.1.20]: https://github.com/tokio-rs/tracing/releases/tag/tracing-core-0.1.20 [0.1.16]: https://github.com/tokio-rs/tracing/releases/tag/tracing-attributes-0.1.16
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This adds a new
Span::or_currentmethod that returns the span it'scalled on, if that span is enabled, or the current span if that span
is not enabled.
This should provide a more efficient alternative to writing code like
since it doesn't wrap the future in two different spans which are
(potentially) both entered on polls. It's also a little more concise
to write, which (hopefully) will encourage people to use it instead
of the less efficient alternative.
Span::or_currentmay be useful in other cases as well.