Skip to content

Commit d4493a8

Browse files
Fishrock123hawkw
andcommitted
docs: document span.in_scope() at top-level (#1344)
## Motivation `span.in_scope()` had a link def in the main tracing docs which was unused, this function is quite handy to know about and I almost re-implemented something similar to it. I almost reimplemented this as a macro. ## Solution Document it at top-level! Co-authored-by: Eliza Weisman <[email protected]>
1 parent 4946123 commit d4493a8

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

tracing/src/lib.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,25 @@
173173
//! # fn main() {}
174174
//! ```
175175
//!
176+
//! For functions which don't have built-in tracing support and can't have
177+
//! the `#[instrument]` attribute applied (such as from an external crate,
178+
//! the [`Span` struct][`Span`] has a [`in_scope()` method][`in_scope`]
179+
//! which can be used to easily wrap synchonous code in a span.
180+
//!
181+
//! For example:
182+
//! ```rust
183+
//! use tracing::info_span;
184+
//!
185+
//! # fn doc() -> Result<(), ()> {
186+
//! # mod serde_json {
187+
//! # pub(crate) fn from_slice(buf: &[u8]) -> Result<(), ()> { Ok(()) }
188+
//! # }
189+
//! # let buf: [u8; 0] = [];
190+
//! let json = info_span!("json.parse").in_scope(|| serde_json::from_slice(&buf))?;
191+
//! # let _ = json; // suppress unused variable warning
192+
//! # Ok(())
193+
//! # }
194+
//! ```
176195
//!
177196
//! You can find more examples showing how to use this crate [here][examples].
178197
//!

0 commit comments

Comments
 (0)