Skip to content

Commit 6d05845

Browse files
committed
tracing: highlight Span::entered in more docs
Signed-off-by: Eliza Weisman <[email protected]>
1 parent a358728 commit 6d05845

File tree

3 files changed

+34
-13
lines changed

3 files changed

+34
-13
lines changed

tracing/README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,7 @@ pub fn shave_all(yaks: usize) -> usize {
174174
//
175175
// local variables (`yaks`) can be used as field values
176176
// without an assignment, similar to struct initializers.
177-
let span = span!(Level::TRACE, "shaving_yaks", yaks);
178-
let _enter = span.enter();
177+
let _span_ = span!(Level::TRACE, "shaving_yaks", yaks).entered();
179178

180179
info!("shaving yaks");
181180

@@ -212,14 +211,20 @@ If you are instrumenting code that make use of
212211
[`std::future::Future`](https://doc.rust-lang.org/stable/std/future/trait.Future.html)
213212
or async/await, be sure to use the
214213
[`tracing-futures`](https://docs.rs/tracing-futures) crate. This is needed
215-
because the following example _will not_ work:
214+
because the following examples _will not_ work:
216215

217216
```rust
218217
async {
219218
let _s = span.enter();
220219
// ...
221220
}
222221
```
222+
```rust
223+
async {
224+
let _s = tracing::span!(...).entered();
225+
// ...
226+
}
227+
```
223228

224229
The span guard `_s` will not exit until the future generated by the `async` block is complete.
225230
Since futures and spans can be entered and exited _multiple_ times without them completing,

tracing/src/lib.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -495,8 +495,7 @@
495495
//! //
496496
//! // local variables (`yaks`) can be used as field values
497497
//! // without an assignment, similar to struct initializers.
498-
//! let span = span!(Level::TRACE, "shaving_yaks", yaks);
499-
//! let _enter = span.enter();
498+
//! let _span = span!(Level::TRACE, "shaving_yaks", yaks).entered();
500499
//!
501500
//! info!("shaving yaks");
502501
//!

tracing/src/span.rs

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,12 @@
5757
//!
5858
//! A thread of execution is said to _enter_ a span when it begins executing,
5959
//! and _exit_ the span when it switches to another context. Spans may be
60-
//! entered through the [`enter`] and [`in_scope`] methods.
60+
//! entered through the [`enter`], [`entered`], and [`in_scope`] methods.
6161
//!
62-
//! The `enter` method enters a span, returning a [guard] that exits the span
62+
//! The [`enter`] method enters a span, returning a [guard] that exits the span
6363
//! when dropped
6464
//! ```
65-
//! # #[macro_use] extern crate tracing;
66-
//! # use tracing::Level;
65+
//! # use tracing::{span, Level};
6766
//! let my_var: u64 = 5;
6867
//! let my_span = span!(Level::TRACE, "my_span", my_var);
6968
//!
@@ -86,11 +85,28 @@
8685
//! for details.
8786
//! </pre></div>
8887
//!
89-
//! `in_scope` takes a closure or function pointer and executes it inside the
90-
//! span.
88+
//! The [`entered`] method is analogous to [`enter`], but moves the span into
89+
//! the returned guard, rather than borrowing it. This allows creating and
90+
//! entering a span in a single expression:
91+
//!
9192
//! ```
92-
//! # #[macro_use] extern crate tracing;
93-
//! # use tracing::Level;
93+
//! # use tracing::{span, Level};
94+
//! // Create a span and enter it, returning a guard:
95+
//! let span = span!(Level::INFO, "my_span").entered();
96+
//!
97+
//! // We are now inside the span! Like `enter()`, the guard returned by
98+
//! // `entered()` will exit the span when it is dropped...
99+
//!
100+
//! // ...but, it can also be exited explicitly, returning the `Span`
101+
//! // struct:
102+
//! let span = span.exit();
103+
//! ```
104+
//!
105+
//! Finally, [`in_scope`] takes a closure or function pointer and executes it
106+
//! inside the span:
107+
//!
108+
//! ```
109+
//! # use tracing::{span, level};
94110
//! let my_var: u64 = 5;
95111
//! let my_span = span!(Level::TRACE, "my_span", my_var = &my_var);
96112
//!
@@ -309,6 +325,7 @@
309325
//! [`Subscriber`]: ../subscriber/trait.Subscriber.html
310326
//! [`Attributes`]: struct.Attributes.html
311327
//! [`enter`]: struct.Span.html#method.enter
328+
//! [`entered`]: struct.Span.html#method.entered
312329
//! [`in_scope`]: struct.Span.html#method.in_scope
313330
//! [`follows_from`]: struct.Span.html#method.follows_from
314331
//! [guard]: struct.Entered.html

0 commit comments

Comments
 (0)