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:
Eliza Weisman
2019-05-24 15:24:13 -07:00
committed by GitHub
parent 1b498e8aa2
commit 84d5a7f5a0
12 changed files with 284 additions and 131 deletions
+2 -2
View File
@@ -125,7 +125,7 @@ fn main() {
tokio_trace::subscriber::with_default(subscriber, || {
let mut foo: u64 = 2;
span!(Level::TRACE, "my_great_span", foo_count = &foo).enter(|| {
span!(Level::TRACE, "my_great_span", foo_count = &foo).in_scope(|| {
foo += 1;
info!({ yak_shaved = true, yak_count = 1 }, "hi from inside my span");
span!(
@@ -134,7 +134,7 @@ fn main() {
foo_count = &foo,
baz_count = 5
)
.enter(|| {
.in_scope(|| {
warn!({ yak_shaved = false, yak_count = -1 }, "failed to shave yak");
});
});
+6 -6
View File
@@ -22,25 +22,25 @@ fn main() {
let subscriber = SloggishSubscriber::new(2);
tokio_trace::subscriber::with_default(subscriber, || {
span!(Level::TRACE, "", version = &field::display(5.0)).enter(|| {
span!(Level::TRACE, "server", host = "localhost", port = 8080).enter(|| {
span!(Level::TRACE, "", version = &field::display(5.0)).in_scope(|| {
span!(Level::TRACE, "server", host = "localhost", port = 8080).in_scope(|| {
info!("starting");
info!("listening");
let peer1 = span!(Level::TRACE, "conn", peer_addr = "82.9.9.9", port = 42381);
peer1.enter(|| {
peer1.in_scope(|| {
debug!("connected");
debug!({ length = 2 }, "message received");
});
let peer2 = span!(Level::TRACE, "conn", peer_addr = "8.8.8.8", port = 18230);
peer2.enter(|| {
peer2.in_scope(|| {
debug!("connected");
});
peer1.enter(|| {
peer1.in_scope(|| {
warn!({ algo = "xor" }, "weak encryption requested");
debug!({ length = 8 }, "response sent");
debug!("disconnected");
});
peer2.enter(|| {
peer2.in_scope(|| {
debug!({ length = 5 }, "message received");
debug!({ length = 8 }, "response sent");
debug!("disconnected");