| <!DOCTYPE html><html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><meta name="generator" content="rustdoc"><meta name="description" content="Attaches spans to a [`std::future::Future`]."><meta name="keywords" content="rust, rustlang, rust-lang, Instrument"><title>Instrument in tracing::instrument - Rust</title><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Regular.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../FiraSans-Medium.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Regular.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceSerif4-Bold.ttf.woff2"><link rel="preload" as="font" type="font/woff2" crossorigin href="../../SourceCodePro-Semibold.ttf.woff2"><link rel="stylesheet" href="../../normalize.css"><link rel="stylesheet" href="../../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" href="../../ayu.css" disabled><link rel="stylesheet" href="../../dark.css" disabled><link rel="stylesheet" href="../../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../../storage.js"></script><script defer src="sidebar-items.js"></script><script defer src="../../main.js"></script><noscript><link rel="stylesheet" href="../../noscript.css"></noscript><link rel="alternate icon" type="image/png" href="../../favicon-16x16.png"><link rel="alternate icon" type="image/png" href="../../favicon-32x32.png"><link rel="icon" type="image/svg+xml" href="../../favicon.svg"></head><body class="rustdoc trait"><!--[if lte IE 11]><div class="warning">This old browser is unsupported and will most likely display funky things.</div><![endif]--><nav class="mobile-topbar"><button class="sidebar-menu-toggle">☰</button><a class="sidebar-logo" href="../../tracing/index.html"><div class="logo-container"><img src="https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png" alt="logo"></div></a><h2></h2></nav><nav class="sidebar"><a class="sidebar-logo" href="../../tracing/index.html"><div class="logo-container"> |
| <img src="https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png" alt="logo"></div></a><h2 class="location"><a href="#">Instrument</a></h2><div class="sidebar-elems"><section><h3><a href="#provided-methods">Provided Methods</a></h3><ul class="block"><li><a href="#method.in_current_span">in_current_span</a></li><li><a href="#method.instrument">instrument</a></li></ul><h3><a href="#implementors">Implementors</a></h3></section><h2><a href="index.html">In tracing::instrument</a></h2></div></nav><main><div class="width-limiter"><nav class="sub"><form class="search-form"><div class="search-container"><span></span><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"><div id="help-button" title="help" tabindex="-1"><a href="../../help.html">?</a></div><div id="settings-menu" tabindex="-1"><a href="../../settings.html" title="settings"><img width="22" height="22" alt="Change settings" src="../../wheel.svg"></a></div></div></form></nav><section id="main-content" class="content"><div class="main-heading"><h1 class="fqn">Trait <a href="../index.html">tracing</a>::<wbr><a href="index.html">instrument</a>::<wbr><a class="trait" href="#">Instrument</a><button id="copy-path" onclick="copy_path(this)" title="Copy item path to clipboard"><img src="../../clipboard.svg" width="19" height="18" alt="Copy item path"></button></h1><span class="out-of-band"><a class="srclink" href="../../src/tracing/instrument.rs.html#16-124">source</a> · <a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span></div><div class="item-decl"><pre class="rust trait"><code>pub trait Instrument: Sized { |
| fn <a href="#method.instrument" class="fnname">instrument</a>(self, span: <a class="struct" href="../struct.Span.html" title="struct tracing::Span">Span</a>) -> <a class="struct" href="struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><T></span><code class="content"><span class="where fmt-newline">impl<T: Future> Future for <a class="struct" href="struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><T></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = T::Output;</span></code></span></span></span></span> { ... } |
| <span class="item-spacer"></span> fn <a href="#method.in_current_span" class="fnname">in_current_span</a>(self) -> <a class="struct" href="struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><T></span><code class="content"><span class="where fmt-newline">impl<T: Future> Future for <a class="struct" href="struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><T></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = T::Output;</span></code></span></span></span></span> { ... } |
| }</code></pre></div><details class="rustdoc-toggle top-doc" open><summary class="hideme"><span>Expand description</span></summary><div class="docblock"><p>Attaches spans to a [<code>std::future::Future</code>].</p> |
| <p>Extension trait allowing futures to be |
| instrumented with a <code>tracing</code> <a href="../struct.Span.html">span</a>.</p> |
| </div></details><h2 id="provided-methods" class="small-section-header">Provided Methods<a href="#provided-methods" class="anchor"></a></h2><div class="methods"><details class="rustdoc-toggle method-toggle" open><summary><section id="method.instrument" class="method has-srclink"><a class="srclink rightside" href="../../src/tracing/instrument.rs.html#82-84">source</a><h4 class="code-header">fn <a href="#method.instrument" class="fnname">instrument</a>(self, span: <a class="struct" href="../struct.Span.html" title="struct tracing::Span">Span</a>) -> <a class="struct" href="struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><T></span><code class="content"><span class="where fmt-newline">impl<T: Future> Future for <a class="struct" href="struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><T></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = T::Output;</span></code></span></span></span></span></h4></section></summary><div class="docblock"><p>Instruments this type with the provided <a href="../struct.Span.html" title="Span"><code>Span</code></a>, returning an |
| <code>Instrumented</code> wrapper.</p> |
| <p>The attached <a href="../struct.Span.html" title="Span"><code>Span</code></a> will be <a href="../struct.Span.html#method.enter">entered</a> every time the instrumented |
| <a href="std::future::Future"><code>Future</code></a> is polled.</p> |
| <h5 id="examples"><a href="#examples">Examples</a></h5> |
| <p>Instrumenting a future:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tracing::Instrument; |
| |
| <span class="kw">let </span>my_future = <span class="kw">async </span>{ |
| <span class="comment">// ... |
| </span>}; |
| |
| my_future |
| .instrument(<span class="macro">tracing::info_span!</span>(<span class="string">"my_future"</span>)) |
| .<span class="kw">await</span></code></pre></div> |
| <p>The <a href="../struct.Span.html#method.or_current"><code>Span::or_current</code></a> combinator can be used in combination with |
| <code>instrument</code> to ensure that the <a href="../struct.Span.html#method.current">current span</a> is attached to the |
| future if the span passed to <code>instrument</code> is <a href="../struct.Span.html#method.is_disabled">disabled</a>:</p> |
| |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tracing::Instrument; |
| |
| <span class="kw">let </span>my_future = <span class="kw">async </span>{ |
| <span class="comment">// ... |
| </span>}; |
| |
| <span class="kw">let </span>outer_span = <span class="macro">tracing::info_span!</span>(<span class="string">"outer"</span>).entered(); |
| |
| <span class="comment">// If the "my_future" span is enabled, then the spawned task will |
| // be within both "my_future" *and* "outer", since "outer" is |
| // "my_future"'s parent. However, if "my_future" is disabled, |
| // the spawned task will *not* be in any span. |
| </span>tokio::spawn( |
| my_future |
| .instrument(<span class="macro">tracing::debug_span!</span>(<span class="string">"my_future"</span>)) |
| ); |
| |
| <span class="comment">// Using `Span::or_current` ensures the spawned task is instrumented |
| // with the current span, if the new span passed to `instrument` is |
| // not enabled. This means that if the "my_future" span is disabled, |
| // the spawned task will still be instrumented with the "outer" span: |
| </span>tokio::spawn( |
| my_future |
| .instrument(<span class="macro">tracing::debug_span!</span>(<span class="string">"my_future"</span>).or_current()) |
| );</code></pre></div> |
| </div></details><details class="rustdoc-toggle method-toggle" open><summary><section id="method.in_current_span" class="method has-srclink"><a class="srclink rightside" href="../../src/tracing/instrument.rs.html#121-123">source</a><h4 class="code-header">fn <a href="#method.in_current_span" class="fnname">in_current_span</a>(self) -> <a class="struct" href="struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<span class="notable-traits-tooltiptext"><span class="docblock"><span class="notable">Notable traits for <a class="struct" href="struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><T></span><code class="content"><span class="where fmt-newline">impl<T: Future> Future for <a class="struct" href="struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><T></span><span class="where fmt-newline"> type <a class="associatedtype">Output</a> = T::Output;</span></code></span></span></span></span></h4></section></summary><div class="docblock"><p>Instruments this type with the <a href="../struct.Span.html#method.current">current</a> <a href="../struct.Span.html"><code>Span</code></a>, returning an |
| <code>Instrumented</code> wrapper.</p> |
| <p>The attached <a href="../struct.Span.html"><code>Span</code></a> will be <a href="../struct.Span.html#method.enter">entered</a> every time the instrumented |
| <a href="std::future::Future"><code>Future</code></a> is polled.</p> |
| <p>This can be used to propagate the current span when spawning a new future.</p> |
| <h5 id="examples-1"><a href="#examples-1">Examples</a></h5> |
| <div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use </span>tracing::Instrument; |
| |
| <span class="kw">let </span>span = <span class="macro">tracing::info_span!</span>(<span class="string">"my_span"</span>); |
| <span class="kw">let </span>_enter = span.enter(); |
| |
| <span class="comment">// ... |
| |
| </span><span class="kw">let </span>future = <span class="kw">async </span>{ |
| <span class="macro">tracing::debug!</span>(<span class="string">"this event will occur inside `my_span`"</span>); |
| <span class="comment">// ... |
| </span>}; |
| tokio::spawn(future.in_current_span());</code></pre></div> |
| </div></details></div><h2 id="implementors" class="small-section-header">Implementors<a href="#implementors" class="anchor"></a></h2><div id="implementors-list"><section id="impl-Instrument-for-T" class="impl has-srclink"><a class="srclink rightside" href="../../src/tracing/instrument.rs.html#276">source</a><a href="#impl-Instrument-for-T" class="anchor"></a><h3 class="code-header">impl<T: Sized> <a class="trait" href="../trait.Instrument.html" title="trait tracing::Instrument">Instrument</a> for T</h3></section></div><script src="../../implementors/tracing/instrument/trait.Instrument.js" async></script></section></div></main><div id="rustdoc-vars" data-root-path="../../" data-current-crate="tracing" data-themes="ayu,dark,light" data-resource-suffix="" data-rustdoc-version="1.66.0-nightly (5c8bff74b 2022-10-21)" ></div></body></html> |