mirror of
https://github.com/tokio-rs/tokio.git
synced 2026-09-08 00:00:13 +02:00
trace: Change Span::enter to return a guard, add Span::in_scope (#1076)
## Motivation Currently, the primary way to use a span is to use `.enter` and pass a closure to be executed under the span. While that is convenient in many settings, it also comes with two decently inconvenient drawbacks: - It breaks control flow statements like `return`, `?`, `break`, and `continue` - It require re-indenting a potentially large chunk of code if you wish it to appear under a span ## Solution This branch changes the `Span::enter` function to return a scope guard that exits the span when dropped, as in: ```rust let guard = span.enter(); // code here is within the span drop(guard); // code here is no longer within the span ``` The method previously called `enter`, which takes a closure and executes it in the span's context, is now called `Span::in_scope`, and was reimplemented on top of the new `enter` method. This is a breaking change to `tokio-trace` that will be part of the upcoming 0.2 release. Closes #1075 Signed-off-by: Eliza Weisman <[email protected]>
This commit is contained in:
@@ -56,7 +56,7 @@ to indicate that some code takes place within the context of that `Span`:
|
||||
```rust
|
||||
// Construct a new span named "my span".
|
||||
let mut span = span!("my span");
|
||||
span.enter(|| {
|
||||
span.in_scope(|| {
|
||||
// Any trace events in this closure or code called by it will occur within
|
||||
// the span.
|
||||
});
|
||||
@@ -88,7 +88,7 @@ use tokio_trace::field;
|
||||
pub fn shave_the_yak(yak: &mut Yak) {
|
||||
// Create a new span for this invocation of `shave_the_yak`, annotated
|
||||
// with the yak being shaved as a *field* on the span.
|
||||
span!("shave_the_yak", yak = field::debug(&yak)).enter(|| {
|
||||
span!("shave_the_yak", yak = field::debug(&yak)).in_scope(|| {
|
||||
// Since the span is annotated with the yak, it is part of the context
|
||||
// for everything happening inside the span. Therefore, we don't need
|
||||
// to add it to the message for this event, as the `log` crate does.
|
||||
|
||||
Reference in New Issue
Block a user