73 lines
18 KiB
HTML
73 lines
18 KiB
HTML
<!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 - 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" type="text/css" href="../normalize.css"><link rel="stylesheet" type="text/css" href="../rustdoc.css" id="mainThemeStyle"><link rel="stylesheet" type="text/css" href="../ayu.css" disabled><link rel="stylesheet" type="text/css" href="../dark.css" disabled><link rel="stylesheet" type="text/css" href="../light.css" id="themeStyle"><script id="default-settings" ></script><script src="../storage.js"></script><script src="../crates.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="sidebar"><div class="sidebar-menu" role="button">☰</div><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">Trait Instrument</h2><div class="sidebar-elems"><div class="block items"><h3 class="sidebar-title"><a href="#provided-methods">Provided Methods</a></h3><div class="sidebar-links"><a href="#method.in_current_span">in_current_span</a><a href="#method.instrument">instrument</a></div><h3 class="sidebar-title"><a href="#implementors">Implementors</a></h3></div><h2 class="location">Other items in<br><a href="index.html">tracing</a></h2><div id="sidebar-vars" data-name="Instrument" data-ty="trait" data-relpath=""></div><script defer src="sidebar-items.js"></script></div></nav><main><div class="width-limiter"><div class="sub-container"><a class="sub-logo-container" href="../tracing/index.html"><img src="https://raw.githubusercontent.com/tokio-rs/tracing/master/assets/logo-type.png" alt="logo"></a><nav class="sub"><div class="theme-picker"><button id="theme-picker" aria-label="Pick another theme!" aria-haspopup="menu" title="themes"><img width="18" height="18" alt="Pick another theme!" src="../brush.svg"></button><div id="theme-choices" role="menu"></div></div><form class="search-form"><div class="search-container"><div><select id="crate-search"><option value="All crates">All crates</option></select><input class="search-input" name="search" autocomplete="off" spellcheck="false" placeholder="Click or press ‘S’ to search, ‘?’ for more options…" type="search"></div><button type="button" id="help-button" title="help">?</button><a id="settings-menu" href="../settings.html" title="settings"><img width="18" height="18" alt="Change settings" src="../wheel.svg"></a></div></form></nav></div><section id="main-content" class="content"><h1 class="fqn"><span class="in-band">Trait <a href="index.html">tracing</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></span><span class="out-of-band"><span id="render-detail"><a id="toggle-all-docs" href="javascript:void(0)" title="collapse all docs">[<span class="inner">−</span>]</a></span><a class="srclink" href="../src/tracing/instrument.rs.html#16-124" title="goto source code">[src]</a></span></h1><div class="docblock item-decl"><pre class="rust trait"><code>pub trait Instrument: <a class="trait" href="https://doc.rust-lang.org/1.59.0/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>) -> <a class="struct" href="instrument/struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<div class="notable-traits-tooltiptext"><span class="docblock"><div class="notable">Notable traits for <a class="struct" href="instrument/struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><T></div><code class="content"><span class="where fmt-newline">impl<T: <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a>> <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a> for <a class="struct" href="instrument/struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><T></span><span class="where fmt-newline"> type <a href="https://doc.rust-lang.org/1.59.0/core/future/future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = T::<a class="associatedtype" href="https://doc.rust-lang.org/1.59.0/core/future/future/trait.Future.html#associatedtype.Output" title="type core::future::future::Future::Output">Output</a>;</span></code></span></div></span></span> { ... }
|
||
<div class="item-spacer"></div> fn <a href="#method.in_current_span" class="fnname">in_current_span</a>(self) -> <a class="struct" href="instrument/struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<div class="notable-traits-tooltiptext"><span class="docblock"><div class="notable">Notable traits for <a class="struct" href="instrument/struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><T></div><code class="content"><span class="where fmt-newline">impl<T: <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a>> <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a> for <a class="struct" href="instrument/struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><T></span><span class="where fmt-newline"> type <a href="https://doc.rust-lang.org/1.59.0/core/future/future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = T::<a class="associatedtype" href="https://doc.rust-lang.org/1.59.0/core/future/future/trait.Future.html#associatedtype.Output" title="type core::future::future::Future::Output">Output</a>;</span></code></span></div></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/1.59.0/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" open><summary><div id="method.instrument" class="method has-srclink"><div class="rightside"><a class="srclink" href="../src/tracing/instrument.rs.html#82-84" title="goto source code">[src]</a></div><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="instrument/struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<div class="notable-traits-tooltiptext"><span class="docblock"><div class="notable">Notable traits for <a class="struct" href="instrument/struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><T></div><code class="content"><span class="where fmt-newline">impl<T: <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a>> <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a> for <a class="struct" href="instrument/struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><T></span><span class="where fmt-newline"> type <a href="https://doc.rust-lang.org/1.59.0/core/future/future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = T::<a class="associatedtype" href="https://doc.rust-lang.org/1.59.0/core/future/future/trait.Future.html#associatedtype.Output" title="type core::future::future::Future::Output">Output</a>;</span></code></span></div></span></span></h4></div></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/1.59.0/core/future/future/trait.Future.html"><code>Future</code></a> is polled.</p>
|
||
<h5 id="examples" class="section-header"><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> <span class="ident">tracing::Instrument</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">my_future</span> <span class="op">=</span> <span class="kw">async</span> {
|
||
<span class="comment">// ...</span>
|
||
};
|
||
|
||
<span class="ident">my_future</span>
|
||
.<span class="ident">instrument</span>(<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> <span class="ident">tracing::Instrument</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">my_future</span> <span class="op">=</span> <span class="kw">async</span> {
|
||
<span class="comment">// ...</span>
|
||
};
|
||
|
||
<span class="kw">let</span> <span class="ident">outer_span</span> <span class="op">=</span> <span class="macro">tracing::info_span!</span>(<span class="string">"outer"</span>).<span class="ident">entered</span>();
|
||
|
||
<span class="comment">// If the "my_future" span is enabled, then the spawned task will</span>
|
||
<span class="comment">// be within both "my_future" *and* "outer", since "outer" is</span>
|
||
<span class="comment">// "my_future"'s parent. However, if "my_future" is disabled,</span>
|
||
<span class="comment">// the spawned task will *not* be in any span.</span>
|
||
<span class="ident">tokio::spawn</span>(
|
||
<span class="ident">my_future</span>
|
||
.<span class="ident">instrument</span>(<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</span>
|
||
<span class="comment">// with the current span, if the new span passed to `instrument` is</span>
|
||
<span class="comment">// not enabled. This means that if the "my_future" span is disabled,</span>
|
||
<span class="comment">// the spawned task will still be instrumented with the "outer" span:</span>
|
||
<span class="ident">tokio::spawn</span>(
|
||
<span class="ident">my_future</span>
|
||
.<span class="ident">instrument</span>(<span class="macro">tracing::debug_span!</span>(<span class="string">"my_future"</span>).<span class="ident">or_current</span>())
|
||
);</code></pre></div>
|
||
</div></details><details class="rustdoc-toggle" open><summary><div id="method.in_current_span" class="method has-srclink"><div class="rightside"><a class="srclink" href="../src/tracing/instrument.rs.html#121-123" title="goto source code">[src]</a></div><h4 class="code-header">fn <a href="#method.in_current_span" class="fnname">in_current_span</a>(self) -> <a class="struct" href="instrument/struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><Self><span class="notable-traits"><span class="notable-traits-tooltip">ⓘ<div class="notable-traits-tooltiptext"><span class="docblock"><div class="notable">Notable traits for <a class="struct" href="instrument/struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><T></div><code class="content"><span class="where fmt-newline">impl<T: <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a>> <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/future/future/trait.Future.html" title="trait core::future::future::Future">Future</a> for <a class="struct" href="instrument/struct.Instrumented.html" title="struct tracing::instrument::Instrumented">Instrumented</a><T></span><span class="where fmt-newline"> type <a href="https://doc.rust-lang.org/1.59.0/core/future/future/trait.Future.html#associatedtype.Output" class="associatedtype">Output</a> = T::<a class="associatedtype" href="https://doc.rust-lang.org/1.59.0/core/future/future/trait.Future.html#associatedtype.Output" title="type core::future::future::Future::Output">Output</a>;</span></code></span></div></span></span></h4></div></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/1.59.0/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" class="section-header"><a href="#examples-1">Examples</a></h5>
|
||
<div class="example-wrap"><pre class="rust rust-example-rendered"><code><span class="kw">use</span> <span class="ident">tracing::Instrument</span>;
|
||
|
||
<span class="kw">let</span> <span class="ident">span</span> <span class="op">=</span> <span class="macro">tracing::info_span!</span>(<span class="string">"my_span"</span>);
|
||
<span class="kw">let</span> <span class="ident">_enter</span> <span class="op">=</span> <span class="ident">span</span>.<span class="ident">enter</span>();
|
||
|
||
<span class="comment">// ...</span>
|
||
|
||
<span class="kw">let</span> <span class="ident">future</span> <span class="op">=</span> <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>
|
||
};
|
||
<span class="ident">tokio::spawn</span>(<span class="ident">future</span>.<span class="ident">in_current_span</span>());</code></pre></div>
|
||
</div></details></div><h2 id="implementors" class="small-section-header">Implementors<a href="#implementors" class="anchor"></a></h2><div class="item-list" id="implementors-list"><div id="impl-Instrument" class="impl has-srclink"><div class="rightside"><a class="srclink" href="../src/tracing/instrument.rs.html#276" title="goto source code">[src]</a></div><a href="#impl-Instrument" class="anchor"></a><h3 class="code-header in-band">impl<T: <a class="trait" href="https://doc.rust-lang.org/1.59.0/core/marker/trait.Sized.html" title="trait core::marker::Sized">Sized</a>> <a class="trait" href="trait.Instrument.html" title="trait tracing::Instrument">Instrument</a> for T</h3></div></div><script type="text/javascript" src="../implementors/tracing/trait.Instrument.js" async></script></section><section id="search" class="content hidden"></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.59.0 (9d1b2106e 2022-02-23)" ></div>
|
||
</body></html> |