blob: 4d13fc02a228a7e34c5622bd27c6975e546df8ff [file] [log] [blame]
<!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">&#9776;</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">&#x2212;</span>]</a></span></div><div class="item-decl"><pre class="rust trait"><code>pub trait Instrument: <a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a> {
fn <a href="#method.instrument" class="fnname">instrument</a>(self, span: <a class="struct" href="../struct.Span.html" title="struct tracing::Span">Span</a>) -&gt; <a class="struct" href="struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a>&lt;Self&gt;<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>&lt;T&gt;</span><code class="content"><span class="where fmt-newline">impl&lt;T:&nbsp;<a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a>&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a> for <a class="struct" href="struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a>&lt;T&gt;</span><span class="where fmt-newline"> type <a href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = T::<a class="associatedtype" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" title="type core::future::future::Future::Output">Output</a>;</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) -&gt; <a class="struct" href="struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a>&lt;Self&gt;<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>&lt;T&gt;</span><code class="content"><span class="where fmt-newline">impl&lt;T:&nbsp;<a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a>&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a> for <a class="struct" href="struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a>&lt;T&gt;</span><span class="where fmt-newline"> type <a href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = T::<a class="associatedtype" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" title="type core::future::future::Future::Output">Output</a>;</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 <a href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="std::future::Future"><code>std::future::Future</code></a>.</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>) -&gt; <a class="struct" href="struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a>&lt;Self&gt;<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>&lt;T&gt;</span><code class="content"><span class="where fmt-newline">impl&lt;T:&nbsp;<a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a>&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a> for <a class="struct" href="struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a>&lt;T&gt;</span><span class="where fmt-newline"> type <a href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = T::<a class="associatedtype" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" title="type core::future::future::Future::Output">Output</a>;</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="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html"><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">&quot;my_future&quot;</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">&quot;outer&quot;</span>).entered();
<span class="comment">// If the &quot;my_future&quot; span is enabled, then the spawned task will
// be within both &quot;my_future&quot; *and* &quot;outer&quot;, since &quot;outer&quot; is
// &quot;my_future&quot;&#39;s parent. However, if &quot;my_future&quot; 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">&quot;my_future&quot;</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 &quot;my_future&quot; span is disabled,
// the spawned task will still be instrumented with the &quot;outer&quot; span:
</span>tokio::spawn(
my_future
.instrument(<span class="macro">tracing::debug_span!</span>(<span class="string">&quot;my_future&quot;</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) -&gt; <a class="struct" href="struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a>&lt;Self&gt;<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>&lt;T&gt;</span><code class="content"><span class="where fmt-newline">impl&lt;T:&nbsp;<a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a>&gt; <a class="trait" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a> for <a class="struct" href="struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a>&lt;T&gt;</span><span class="where fmt-newline"> type <a href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = T::<a class="associatedtype" href="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html#associatedtype.Output" title="type core::future::future::Future::Output">Output</a>;</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="https://doc.rust-lang.org/nightly/core/future/future/trait.Future.html"><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">&quot;my_span&quot;</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">&quot;this event will occur inside `my_span`&quot;</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&lt;T:&nbsp;<a class="trait" href="https://doc.rust-lang.org/nightly/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>&gt; <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>