tracing/
span.rs

1//! Spans represent periods of time in which a program was executing in a
2//! particular context.
3//!
4//! A span consists of [fields], user-defined key-value pairs of arbitrary data
5//! that describe the context the span represents, and a set of fixed attributes
6//! that describe all `tracing` spans and events. Attributes describing spans
7//! include:
8//!
9//! - An [`Id`] assigned by the subscriber that uniquely identifies it in relation
10//!   to other spans.
11//! - The span's [parent] in the trace tree.
12//! - [Metadata] that describes static characteristics of all spans
13//!   originating from that callsite, such as its name, source code location,
14//!   [verbosity level], and the names of its fields.
15//!
16//! # Creating Spans
17//!
18//! Spans are created using the [`span!`] macro. This macro is invoked with the
19//! following arguments, in order:
20//!
21//! - The [`target`] and/or [`parent`][parent] attributes, if the user wishes to
22//!   override their default values.
23//! - The span's [verbosity level]
24//! - A string literal providing the span's name.
25//! - Finally, zero or more arbitrary key/value fields.
26//!
27//! [`target`]: super::Metadata::target
28//!
29//! For example:
30//! ```rust
31//! use tracing::{span, Level};
32//!
33//! /// Construct a new span at the `INFO` level named "my_span", with a single
34//! /// field named answer , with the value `42`.
35//! let my_span = span!(Level::INFO, "my_span", answer = 42);
36//! ```
37//!
38//! The documentation for the [`span!`] macro provides additional examples of
39//! the various options that exist when creating spans.
40//!
41//! The [`trace_span!`], [`debug_span!`], [`info_span!`], [`warn_span!`], and
42//! [`error_span!`] exist as shorthand for constructing spans at various
43//! verbosity levels.
44//!
45//! ## Recording Span Creation
46//!
47//! The [`Attributes`] type contains data associated with a span, and is
48//! provided to the [`Subscriber`] when a new span is created. It contains
49//! the span's metadata, the ID of [the span's parent][parent] if one was
50//! explicitly set, and any fields whose values were recorded when the span was
51//! constructed. The subscriber, which is responsible for recording `tracing`
52//! data, can then store or record these values.
53//!
54//! # The Span Lifecycle
55//!
56//! ## Entering a Span
57//!
58//! A thread of execution is said to _enter_ a span when it begins executing,
59//! and _exit_ the span when it switches to another context. Spans may be
60//! entered through the [`enter`], [`entered`], and [`in_scope`] methods.
61//!
62//! The [`enter`] method enters a span, returning a [guard] that exits the span
63//! when dropped
64//! ```
65//! # use tracing::{span, Level};
66//! let my_var: u64 = 5;
67//! let my_span = span!(Level::TRACE, "my_span", my_var);
68//!
69//! // `my_span` exists but has not been entered.
70//!
71//! // Enter `my_span`...
72//! let _enter = my_span.enter();
73//!
74//! // Perform some work inside of the context of `my_span`...
75//! // Dropping the `_enter` guard will exit the span.
76//!```
77//!
78//! <div class="example-wrap" style="display:inline-block"><pre class="compile_fail" style="white-space:normal;font:inherit;">
79//!     <strong>Warning</strong>: In asynchronous code that uses async/await syntax,
80//!     <code>Span::enter</code> may produce incorrect traces if the returned drop
81//!     guard is held across an await point. See
82//!     <a href="struct.Span.html#in-asynchronous-code">the method documentation</a>
83//!     for details.
84//! </pre></div>
85//!
86//! The [`entered`] method is analogous to [`enter`], but moves the span into
87//! the returned guard, rather than borrowing it. This allows creating and
88//! entering a span in a single expression:
89//!
90//! ```
91//! # use tracing::{span, Level};
92//! // Create a span and enter it, returning a guard:
93//! let span = span!(Level::INFO, "my_span").entered();
94//!
95//! // We are now inside the span! Like `enter()`, the guard returned by
96//! // `entered()` will exit the span when it is dropped...
97//!
98//! // ...but, it can also be exited explicitly, returning the `Span`
99//! // struct:
100//! let span = span.exit();
101//! ```
102//!
103//! Finally, [`in_scope`] takes a closure or function pointer and executes it
104//! inside the span:
105//!
106//! ```
107//! # use tracing::{span, Level};
108//! let my_var: u64 = 5;
109//! let my_span = span!(Level::TRACE, "my_span", my_var = &my_var);
110//!
111//! my_span.in_scope(|| {
112//!     // perform some work in the context of `my_span`...
113//! });
114//!
115//! // Perform some work outside of the context of `my_span`...
116//!
117//! my_span.in_scope(|| {
118//!     // Perform some more work in the context of `my_span`.
119//! });
120//! ```
121//!
122//! <pre class="ignore" style="white-space:normal;font:inherit;">
123//!     <strong>Note</strong>: Since entering a span takes <code>&self</code>, and
124//!     <code>Span</code>s are <code>Clone</code>, <code>Send</code>, and
125//!     <code>Sync</code>, it is entirely valid for multiple threads to enter the
126//!     same span concurrently.
127//! </pre>
128//!
129//! ## Span Relationships
130//!
131//! Spans form a tree structure — unless it is a root span, all spans have a
132//! _parent_, and may have one or more _children_. When a new span is created,
133//! the current span becomes the new span's parent. The total execution time of
134//! a span consists of the time spent in that span and in the entire subtree
135//! represented by its children. Thus, a parent span always lasts for at least
136//! as long as the longest-executing span in its subtree.
137//!
138//! ```
139//! # use tracing::{Level, span};
140//! // this span is considered the "root" of a new trace tree:
141//! span!(Level::INFO, "root").in_scope(|| {
142//!     // since we are now inside "root", this span is considered a child
143//!     // of "root":
144//!     span!(Level::DEBUG, "outer_child").in_scope(|| {
145//!         // this span is a child of "outer_child", which is in turn a
146//!         // child of "root":
147//!         span!(Level::TRACE, "inner_child").in_scope(|| {
148//!             // and so on...
149//!         });
150//!     });
151//!     // another span created here would also be a child of "root".
152//! });
153//!```
154//!
155//! In addition, the parent of a span may be explicitly specified in
156//! the `span!` macro. For example:
157//!
158//! ```rust
159//! # use tracing::{Level, span};
160//! // Create, but do not enter, a span called "foo".
161//! let foo = span!(Level::INFO, "foo");
162//!
163//! // Create and enter a span called "bar".
164//! let bar = span!(Level::INFO, "bar");
165//! let _enter = bar.enter();
166//!
167//! // Although we have currently entered "bar", "baz"'s parent span
168//! // will be "foo".
169//! let baz = span!(parent: &foo, Level::INFO, "baz");
170//! ```
171//!
172//! A child span should typically be considered _part_ of its parent. For
173//! example, if a subscriber is recording the length of time spent in various
174//! spans, it should generally include the time spent in a span's children as
175//! part of that span's duration.
176//!
177//! In addition to having zero or one parent, a span may also _follow from_ any
178//! number of other spans. This indicates a causal relationship between the span
179//! and the spans that it follows from, but a follower is *not* typically
180//! considered part of the duration of the span it follows. Unlike the parent, a
181//! span may record that it follows from another span after it is created, using
182//! the [`follows_from`] method.
183//!
184//! As an example, consider a listener task in a server. As the listener accepts
185//! incoming connections, it spawns new tasks that handle those connections. We
186//! might want to have a span representing the listener, and instrument each
187//! spawned handler task with its own span. We would want our instrumentation to
188//! record that the handler tasks were spawned as a result of the listener task.
189//! However, we might not consider the handler tasks to be _part_ of the time
190//! spent in the listener task, so we would not consider those spans children of
191//! the listener span. Instead, we would record that the handler tasks follow
192//! from the listener, recording the causal relationship but treating the spans
193//! as separate durations.
194//!
195//! ## Closing Spans
196//!
197//! Execution may enter and exit a span multiple times before that span is
198//! _closed_. Consider, for example, a future which has an associated
199//! span and enters that span every time it is polled:
200//! ```rust
201//! # use std::future::Future;
202//! # use std::task::{Context, Poll};
203//! # use std::pin::Pin;
204//! struct MyFuture {
205//!    // data
206//!    span: tracing::Span,
207//! }
208//!
209//! impl Future for MyFuture {
210//!     type Output = ();
211//!
212//!     fn poll(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Self::Output> {
213//!         let _enter = self.span.enter();
214//!         // Do actual future work...
215//! # Poll::Ready(())
216//!     }
217//! }
218//! ```
219//!
220//! If this future was spawned on an executor, it might yield one or more times
221//! before `poll` returns [`Poll::Ready`]. If the future were to yield, then
222//! the executor would move on to poll the next future, which may _also_ enter
223//! an associated span or series of spans. Therefore, it is valid for a span to
224//! be entered repeatedly before it completes. Only the time when that span or
225//! one of its children was the current span is considered to be time spent in
226//! that span. A span which is not executing and has not yet been closed is said
227//! to be _idle_.
228//!
229//! Because spans may be entered and exited multiple times before they close,
230//! [`Subscriber`]s have separate trait methods which are called to notify them
231//! of span exits and when span handles are dropped. When execution exits a
232//! span, [`exit`] will always be called with that span's ID to notify the
233//! subscriber that the span has been exited. When span handles are dropped, the
234//! [`drop_span`] method is called with that span's ID. The subscriber may use
235//! this to determine whether or not the span will be entered again.
236//!
237//! If there is only a single handle with the capacity to exit a span, dropping
238//! that handle "closes" the span, since the capacity to enter it no longer
239//! exists. For example:
240//! ```
241//! # use tracing::{Level, span};
242//! {
243//!     span!(Level::TRACE, "my_span").in_scope(|| {
244//!         // perform some work in the context of `my_span`...
245//!     }); // --> Subscriber::exit(my_span)
246//!
247//!     // The handle to `my_span` only lives inside of this block; when it is
248//!     // dropped, the subscriber will be informed via `drop_span`.
249//!
250//! } // --> Subscriber::drop_span(my_span)
251//! ```
252//!
253//! However, if multiple handles exist, the span can still be re-entered even if
254//! one or more is dropped. For determining when _all_ handles to a span have
255//! been dropped, `Subscriber`s have a [`clone_span`] method, which is called
256//! every time a span handle is cloned. Combined with `drop_span`, this may be
257//! used to track the number of handles to a given span — if `drop_span` has
258//! been called one more time than the number of calls to `clone_span` for a
259//! given ID, then no more handles to the span with that ID exist. The
260//! subscriber may then treat it as closed.
261//!
262//! # When to use spans
263//!
264//! As a rule of thumb, spans should be used to represent discrete units of work
265//! (e.g., a given request's lifetime in a server) or periods of time spent in a
266//! given context (e.g., time spent interacting with an instance of an external
267//! system, such as a database).
268//!
269//! Which scopes in a program correspond to new spans depend somewhat on user
270//! intent. For example, consider the case of a loop in a program. Should we
271//! construct one span and perform the entire loop inside of that span, like:
272//!
273//! ```rust
274//! # use tracing::{Level, span};
275//! # let n = 1;
276//! let span = span!(Level::TRACE, "my_loop");
277//! let _enter = span.enter();
278//! for i in 0..n {
279//!     # let _ = i;
280//!     // ...
281//! }
282//! ```
283//! Or, should we create a new span for each iteration of the loop, as in:
284//! ```rust
285//! # use tracing::{Level, span};
286//! # let n = 1u64;
287//! for i in 0..n {
288//!     let span = span!(Level::TRACE, "my_loop", iteration = i);
289//!     let _enter = span.enter();
290//!     // ...
291//! }
292//! ```
293//!
294//! Depending on the circumstances, we might want to do either, or both. For
295//! example, if we want to know how long was spent in the loop overall, we would
296//! create a single span around the entire loop; whereas if we wanted to know how
297//! much time was spent in each individual iteration, we would enter a new span
298//! on every iteration.
299//!
300//! [fields]: super::field
301//! [Metadata]: super::Metadata
302//! [verbosity level]: super::Level
303//! [`Poll::Ready`]: std::task::Poll::Ready
304//! [`span!`]: super::span!
305//! [`trace_span!`]: super::trace_span!
306//! [`debug_span!`]: super::debug_span!
307//! [`info_span!`]: super::info_span!
308//! [`warn_span!`]: super::warn_span!
309//! [`error_span!`]: super::error_span!
310//! [`clone_span`]: super::subscriber::Subscriber::clone_span()
311//! [`drop_span`]: super::subscriber::Subscriber::drop_span()
312//! [`exit`]: super::subscriber::Subscriber::exit
313//! [`Subscriber`]: super::subscriber::Subscriber
314//! [`enter`]: Span::enter()
315//! [`entered`]: Span::entered()
316//! [`in_scope`]: Span::in_scope()
317//! [`follows_from`]: Span::follows_from()
318//! [guard]: Entered
319//! [parent]: #span-relationships
320pub use tracing_core::span::{Attributes, Id, Record};
321
322use crate::stdlib::{
323    cmp, fmt,
324    hash::{Hash, Hasher},
325    marker::PhantomData,
326    mem,
327    ops::Deref,
328};
329use crate::{
330    dispatcher::{self, Dispatch},
331    field, Metadata,
332};
333
334/// Trait implemented by types which have a span `Id`.
335pub trait AsId: crate::sealed::Sealed {
336    /// Returns the `Id` of the span that `self` corresponds to, or `None` if
337    /// this corresponds to a disabled span.
338    fn as_id(&self) -> Option<&Id>;
339}
340
341/// A handle representing a span, with the capability to enter the span if it
342/// exists.
343///
344/// If the span was rejected by the current `Subscriber`'s filter, entering the
345/// span will silently do nothing. Thus, the handle can be used in the same
346/// manner regardless of whether or not the trace is currently being collected.
347#[derive(Clone)]
348pub struct Span {
349    /// A handle used to enter the span when it is not executing.
350    ///
351    /// If this is `None`, then the span has either closed or was never enabled.
352    inner: Option<Inner>,
353    /// Metadata describing the span.
354    ///
355    /// This might be `Some` even if `inner` is `None`, in the case that the
356    /// span is disabled but the metadata is needed for `log` support.
357    meta: Option<&'static Metadata<'static>>,
358}
359
360/// A handle representing the capacity to enter a span which is known to exist.
361///
362/// Unlike `Span`, this type is only constructed for spans which _have_ been
363/// enabled by the current filter. This type is primarily used for implementing
364/// span handles; users should typically not need to interact with it directly.
365#[derive(Debug)]
366pub(crate) struct Inner {
367    /// The span's ID, as provided by `subscriber`.
368    id: Id,
369
370    /// The subscriber that will receive events relating to this span.
371    ///
372    /// This should be the same subscriber that provided this span with its
373    /// `id`.
374    subscriber: Dispatch,
375}
376
377/// A guard representing a span which has been entered and is currently
378/// executing.
379///
380/// When the guard is dropped, the span will be exited.
381///
382/// This is returned by the [`Span::enter`] function.
383///
384/// [`Span::enter`]: super::Span::enter
385#[derive(Debug)]
386#[must_use = "once a span has been entered, it should be exited"]
387pub struct Entered<'a> {
388    span: &'a Span,
389}
390
391/// An owned version of [`Entered`], a guard representing a span which has been
392/// entered and is currently executing.
393///
394/// When the guard is dropped, the span will be exited.
395///
396/// This is returned by the [`Span::entered`] function.
397///
398/// [`Span::entered`]: super::Span::entered()
399#[derive(Debug)]
400#[must_use = "once a span has been entered, it should be exited"]
401pub struct EnteredSpan {
402    span: Span,
403
404    /// ```compile_fail
405    /// use tracing::span::*;
406    /// trait AssertSend: Send {}
407    ///
408    /// impl AssertSend for EnteredSpan {}
409    /// ```
410    _not_send: PhantomNotSend,
411}
412
413/// `log` target for all span lifecycle (creation/enter/exit/close) records.
414#[cfg(feature = "log")]
415const LIFECYCLE_LOG_TARGET: &str = "tracing::span";
416/// `log` target for span activity (enter/exit) records.
417#[cfg(feature = "log")]
418const ACTIVITY_LOG_TARGET: &str = "tracing::span::active";
419
420// ===== impl Span =====
421
422impl Span {
423    /// Constructs a new `Span` with the given [metadata] and set of
424    /// [field values].
425    ///
426    /// The new span will be constructed by the currently-active [`Subscriber`],
427    /// with the current span as its parent (if one exists).
428    ///
429    /// After the span is constructed, [field values] and/or [`follows_from`]
430    /// annotations may be added to it.
431    ///
432    /// [metadata]: super::Metadata
433    /// [`Subscriber`]: super::subscriber::Subscriber
434    /// [field values]: super::field::ValueSet
435    /// [`follows_from`]: super::Span::follows_from
436    pub fn new(meta: &'static Metadata<'static>, values: &field::ValueSet<'_>) -> Span {
437        dispatcher::get_default(|dispatch| Self::new_with(meta, values, dispatch))
438    }
439
440    #[inline]
441    #[doc(hidden)]
442    pub fn new_with(
443        meta: &'static Metadata<'static>,
444        values: &field::ValueSet<'_>,
445        dispatch: &Dispatch,
446    ) -> Span {
447        let new_span = Attributes::new(meta, values);
448        Self::make_with(meta, new_span, dispatch)
449    }
450
451    /// Constructs a new `Span` as the root of its own trace tree, with the
452    /// given [metadata] and set of [field values].
453    ///
454    /// After the span is constructed, [field values] and/or [`follows_from`]
455    /// annotations may be added to it.
456    ///
457    /// [metadata]: super::Metadata
458    /// [field values]: super::field::ValueSet
459    /// [`follows_from`]: super::Span::follows_from
460    pub fn new_root(meta: &'static Metadata<'static>, values: &field::ValueSet<'_>) -> Span {
461        dispatcher::get_default(|dispatch| Self::new_root_with(meta, values, dispatch))
462    }
463
464    #[inline]
465    #[doc(hidden)]
466    pub fn new_root_with(
467        meta: &'static Metadata<'static>,
468        values: &field::ValueSet<'_>,
469        dispatch: &Dispatch,
470    ) -> Span {
471        let new_span = Attributes::new_root(meta, values);
472        Self::make_with(meta, new_span, dispatch)
473    }
474
475    /// Constructs a new `Span` as child of the given parent span, with the
476    /// given [metadata] and set of [field values].
477    ///
478    /// After the span is constructed, [field values] and/or [`follows_from`]
479    /// annotations may be added to it.
480    ///
481    /// [metadata]: super::Metadata
482    /// [field values]: super::field::ValueSet
483    /// [`follows_from`]: super::Span::follows_from
484    pub fn child_of(
485        parent: impl Into<Option<Id>>,
486        meta: &'static Metadata<'static>,
487        values: &field::ValueSet<'_>,
488    ) -> Span {
489        let mut parent = parent.into();
490        dispatcher::get_default(move |dispatch| {
491            Self::child_of_with(Option::take(&mut parent), meta, values, dispatch)
492        })
493    }
494
495    #[inline]
496    #[doc(hidden)]
497    pub fn child_of_with(
498        parent: impl Into<Option<Id>>,
499        meta: &'static Metadata<'static>,
500        values: &field::ValueSet<'_>,
501        dispatch: &Dispatch,
502    ) -> Span {
503        let new_span = match parent.into() {
504            Some(parent) => Attributes::child_of(parent, meta, values),
505            None => Attributes::new_root(meta, values),
506        };
507        Self::make_with(meta, new_span, dispatch)
508    }
509
510    /// Constructs a new disabled span with the given `Metadata`.
511    ///
512    /// This should be used when a span is constructed from a known callsite,
513    /// but the subscriber indicates that it is disabled.
514    ///
515    /// Entering, exiting, and recording values on this span will not notify the
516    /// `Subscriber` but _may_ record log messages if the `log` feature flag is
517    /// enabled.
518    #[inline(always)]
519    pub fn new_disabled(meta: &'static Metadata<'static>) -> Span {
520        Self {
521            inner: None,
522            meta: Some(meta),
523        }
524    }
525
526    /// Constructs a new span that is *completely disabled*.
527    ///
528    /// This can be used rather than `Option<Span>` to represent cases where a
529    /// span is not present.
530    ///
531    /// Entering, exiting, and recording values on this span will do nothing.
532    #[inline(always)]
533    pub const fn none() -> Span {
534        Self {
535            inner: None,
536            meta: None,
537        }
538    }
539
540    /// Returns a handle to the span [considered by the `Subscriber`] to be the
541    /// current span.
542    ///
543    /// If the subscriber indicates that it does not track the current span, or
544    /// that the thread from which this function is called is not currently
545    /// inside a span, the returned span will be disabled.
546    ///
547    /// [considered by the `Subscriber`]:
548    ///     super::subscriber::Subscriber::current_span
549    pub fn current() -> Span {
550        dispatcher::get_default(|dispatch| {
551            if let Some((id, meta)) = dispatch.current_span().into_inner() {
552                let id = dispatch.clone_span(&id);
553                Self {
554                    inner: Some(Inner::new(id, dispatch)),
555                    meta: Some(meta),
556                }
557            } else {
558                Self::none()
559            }
560        })
561    }
562
563    fn make_with(
564        meta: &'static Metadata<'static>,
565        new_span: Attributes<'_>,
566        dispatch: &Dispatch,
567    ) -> Span {
568        let attrs = &new_span;
569        let id = dispatch.new_span(attrs);
570        let inner = Some(Inner::new(id, dispatch));
571
572        let span = Self {
573            inner,
574            meta: Some(meta),
575        };
576
577        if_log_enabled! { *meta.level(), {
578            let target = if attrs.is_empty() {
579                LIFECYCLE_LOG_TARGET
580            } else {
581                meta.target()
582            };
583            let values = attrs.values();
584            span.log(
585                target,
586                level_to_log!(*meta.level()),
587                format_args!("++ {};{}", meta.name(), crate::log::LogValueSet { values, is_first: false }),
588            );
589        }}
590
591        span
592    }
593
594    /// Enters this span, returning a guard that will exit the span when dropped.
595    ///
596    /// If this span is enabled by the current subscriber, then this function will
597    /// call [`Subscriber::enter`] with the span's [`Id`], and dropping the guard
598    /// will call [`Subscriber::exit`]. If the span is disabled, this does
599    /// nothing.
600    ///
601    /// # In Asynchronous Code
602    ///
603    /// **Warning**: in asynchronous code that uses [async/await syntax][syntax],
604    /// `Span::enter` should be used very carefully or avoided entirely. Holding
605    /// the drop guard returned by `Span::enter` across `.await` points will
606    /// result in incorrect traces. For example,
607    ///
608    /// ```
609    /// # use tracing::info_span;
610    /// # async fn some_other_async_function() {}
611    /// async fn my_async_function() {
612    ///     let span = info_span!("my_async_function");
613    ///
614    ///     // WARNING: This span will remain entered until this
615    ///     // guard is dropped...
616    ///     let _enter = span.enter();
617    ///     // ...but the `await` keyword may yield, causing the
618    ///     // runtime to switch to another task, while remaining in
619    ///     // this span!
620    ///     some_other_async_function().await
621    ///
622    ///     // ...
623    /// }
624    /// ```
625    ///
626    /// The drop guard returned by `Span::enter` exits the span when it is
627    /// dropped. When an async function or async block yields at an `.await`
628    /// point, the current scope is _exited_, but values in that scope are
629    /// **not** dropped (because the async block will eventually resume
630    /// execution from that await point). This means that _another_ task will
631    /// begin executing while _remaining_ in the entered span. This results in
632    /// an incorrect trace.
633    ///
634    /// Instead of using `Span::enter` in asynchronous code, prefer the
635    /// following:
636    ///
637    /// * To enter a span for a synchronous section of code within an async
638    ///   block or function, prefer [`Span::in_scope`]. Since `in_scope` takes a
639    ///   synchronous closure and exits the span when the closure returns, the
640    ///   span will always be exited before the next await point. For example:
641    ///   ```
642    ///   # use tracing::info_span;
643    ///   # async fn some_other_async_function(_: ()) {}
644    ///   async fn my_async_function() {
645    ///       let span = info_span!("my_async_function");
646    ///
647    ///       let some_value = span.in_scope(|| {
648    ///           // run some synchronous code inside the span...
649    ///       });
650    ///
651    ///       // This is okay! The span has already been exited before we reach
652    ///       // the await point.
653    ///       some_other_async_function(some_value).await;
654    ///
655    ///       // ...
656    ///   }
657    ///   ```
658    /// * For instrumenting asynchronous code, `tracing` provides the
659    ///   [`Future::instrument` combinator][instrument] for
660    ///   attaching a span to a future (async function or block). This will
661    ///   enter the span _every_ time the future is polled, and exit it whenever
662    ///   the future yields.
663    ///
664    ///   `Instrument` can be used with an async block inside an async function:
665    ///   ```ignore
666    ///   # use tracing::info_span;
667    ///   use tracing::Instrument;
668    ///
669    ///   # async fn some_other_async_function() {}
670    ///   async fn my_async_function() {
671    ///       let span = info_span!("my_async_function");
672    ///       async move {
673    ///          // This is correct! If we yield here, the span will be exited,
674    ///          // and re-entered when we resume.
675    ///          some_other_async_function().await;
676    ///
677    ///          //more asynchronous code inside the span...
678    ///
679    ///       }
680    ///         // instrument the async block with the span...
681    ///         .instrument(span)
682    ///         // ...and await it.
683    ///         .await
684    ///   }
685    ///   ```
686    ///
687    ///   It can also be used to instrument calls to async functions at the
688    ///   callsite:
689    ///   ```ignore
690    ///   # use tracing::debug_span;
691    ///   use tracing::Instrument;
692    ///
693    ///   # async fn some_other_async_function() {}
694    ///   async fn my_async_function() {
695    ///       let some_value = some_other_async_function()
696    ///          .instrument(debug_span!("some_other_async_function"))
697    ///          .await;
698    ///
699    ///       // ...
700    ///   }
701    ///   ```
702    ///
703    /// * The [`#[instrument]` attribute macro][attr] can automatically generate
704    ///   correct code when used on an async function:
705    ///
706    ///   ```ignore
707    ///   # async fn some_other_async_function() {}
708    ///   #[tracing::instrument(level = "info")]
709    ///   async fn my_async_function() {
710    ///
711    ///       // This is correct! If we yield here, the span will be exited,
712    ///       // and re-entered when we resume.
713    ///       some_other_async_function().await;
714    ///
715    ///       // ...
716    ///
717    ///   }
718    ///   ```
719    ///
720    /// [syntax]: https://rust-lang.github.io/async-book/01_getting_started/04_async_await_primer.html
721    /// [`Span::in_scope`]: Span::in_scope()
722    /// [instrument]: crate::Instrument
723    /// [attr]: macro@crate::instrument
724    ///
725    /// # Examples
726    ///
727    /// ```
728    /// # use tracing::{span, Level};
729    /// let span = span!(Level::INFO, "my_span");
730    /// let guard = span.enter();
731    ///
732    /// // code here is within the span
733    ///
734    /// drop(guard);
735    ///
736    /// // code here is no longer within the span
737    ///
738    /// ```
739    ///
740    /// Guards need not be explicitly dropped:
741    ///
742    /// ```
743    /// # use tracing::trace_span;
744    /// fn my_function() -> String {
745    ///     // enter a span for the duration of this function.
746    ///     let span = trace_span!("my_function");
747    ///     let _enter = span.enter();
748    ///
749    ///     // anything happening in functions we call is still inside the span...
750    ///     my_other_function();
751    ///
752    ///     // returning from the function drops the guard, exiting the span.
753    ///     return "Hello world".to_owned();
754    /// }
755    ///
756    /// fn my_other_function() {
757    ///     // ...
758    /// }
759    /// ```
760    ///
761    /// Sub-scopes may be created to limit the duration for which the span is
762    /// entered:
763    ///
764    /// ```
765    /// # use tracing::{info, info_span};
766    /// let span = info_span!("my_great_span");
767    ///
768    /// {
769    ///     let _enter = span.enter();
770    ///
771    ///     // this event occurs inside the span.
772    ///     info!("i'm in the span!");
773    ///
774    ///     // exiting the scope drops the guard, exiting the span.
775    /// }
776    ///
777    /// // this event is not inside the span.
778    /// info!("i'm outside the span!")
779    /// ```
780    ///
781    /// [`Subscriber::enter`]: super::subscriber::Subscriber::enter()
782    /// [`Subscriber::exit`]: super::subscriber::Subscriber::exit()
783    /// [`Id`]: super::Id
784    #[inline(always)]
785    pub fn enter(&self) -> Entered<'_> {
786        self.do_enter();
787        Entered { span: self }
788    }
789
790    /// Enters this span, consuming it and returning a [guard][`EnteredSpan`]
791    /// that will exit the span when dropped.
792    ///
793    /// <pre class="compile_fail" style="white-space:normal;font:inherit;">
794    ///     <strong>Warning</strong>: In asynchronous code that uses async/await syntax,
795    ///     <code>Span::entered</code> may produce incorrect traces if the returned drop
796    ///     guard is held across an await point. See <a href="#in-asynchronous-code">the
797    ///     <code>Span::enter</code> documentation</a> for details.
798    /// </pre>
799    ///
800    ///
801    /// If this span is enabled by the current subscriber, then this function will
802    /// call [`Subscriber::enter`] with the span's [`Id`], and dropping the guard
803    /// will call [`Subscriber::exit`]. If the span is disabled, this does
804    /// nothing.
805    ///
806    /// This is similar to the [`Span::enter`] method, except that it moves the
807    /// span by value into the returned guard, rather than borrowing it.
808    /// Therefore, this method can be used to create and enter a span in a
809    /// single expression, without requiring a `let`-binding. For example:
810    ///
811    /// ```
812    /// # use tracing::info_span;
813    /// let _span = info_span!("something_interesting").entered();
814    /// ```
815    /// rather than:
816    /// ```
817    /// # use tracing::info_span;
818    /// let span = info_span!("something_interesting");
819    /// let _e = span.enter();
820    /// ```
821    ///
822    /// Furthermore, `entered` may be used when the span must be stored in some
823    /// other struct or be passed to a function while remaining entered.
824    ///
825    /// <pre class="ignore" style="white-space:normal;font:inherit;">
826    ///     <strong>Note</strong>: The returned <a href="../struct.EnteredSpan.html">
827    ///     <code>EnteredSpan</code></a> guard does not implement <code>Send</code>.
828    ///     Dropping the guard will exit <em>this</em> span, and if the guard is sent
829    ///     to another thread and dropped there, that thread may never have entered
830    ///     this span. Thus, <code>EnteredSpan</code>s should not be sent between threads.
831    /// </pre>
832    ///
833    /// [syntax]: https://rust-lang.github.io/async-book/01_getting_started/04_async_await_primer.html
834    ///
835    /// # Examples
836    ///
837    /// The returned guard can be [explicitly exited][EnteredSpan::exit],
838    /// returning the un-entered span:
839    ///
840    /// ```
841    /// # use tracing::{Level, span};
842    /// let span = span!(Level::INFO, "doing_something").entered();
843    ///
844    /// // code here is within the span
845    ///
846    /// // explicitly exit the span, returning it
847    /// let span = span.exit();
848    ///
849    /// // code here is no longer within the span
850    ///
851    /// // enter the span again
852    /// let span = span.entered();
853    ///
854    /// // now we are inside the span once again
855    /// ```
856    ///
857    /// Guards need not be explicitly dropped:
858    ///
859    /// ```
860    /// # use tracing::trace_span;
861    /// fn my_function() -> String {
862    ///     // enter a span for the duration of this function.
863    ///     let span = trace_span!("my_function").entered();
864    ///
865    ///     // anything happening in functions we call is still inside the span...
866    ///     my_other_function();
867    ///
868    ///     // returning from the function drops the guard, exiting the span.
869    ///     return "Hello world".to_owned();
870    /// }
871    ///
872    /// fn my_other_function() {
873    ///     // ...
874    /// }
875    /// ```
876    ///
877    /// Since the [`EnteredSpan`] guard can dereference to the [`Span`] itself,
878    /// the span may still be accessed while entered. For example:
879    ///
880    /// ```rust
881    /// # use tracing::info_span;
882    /// use tracing::field;
883    ///
884    /// // create the span with an empty field, and enter it.
885    /// let span = info_span!("my_span", some_field = field::Empty).entered();
886    ///
887    /// // we can still record a value for the field while the span is entered.
888    /// span.record("some_field", &"hello world!");
889    /// ```
890    ///
891    /// [`Subscriber::enter`]: super::subscriber::Subscriber::enter()
892    /// [`Subscriber::exit`]: super::subscriber::Subscriber::exit()
893    /// [`Id`]: super::Id
894    #[inline(always)]
895    pub fn entered(self) -> EnteredSpan {
896        self.do_enter();
897        EnteredSpan {
898            span: self,
899            _not_send: PhantomNotSend,
900        }
901    }
902
903    /// Returns this span, if it was [enabled] by the current [`Subscriber`], or
904    /// the [current span] (whose lexical distance may be further than expected),
905    ///  if this span [is disabled].
906    ///
907    /// This method can be useful when propagating spans to spawned threads or
908    /// [async tasks]. Consider the following:
909    ///
910    /// ```
911    /// let _parent_span = tracing::info_span!("parent").entered();
912    ///
913    /// // ...
914    ///
915    /// let child_span = tracing::debug_span!("child");
916    ///
917    /// std::thread::spawn(move || {
918    ///     let _entered = child_span.entered();
919    ///
920    ///     tracing::info!("spawned a thread!");
921    ///
922    ///     // ...
923    /// });
924    /// ```
925    ///
926    /// If the current [`Subscriber`] enables the [`DEBUG`] level, then both
927    /// the "parent" and "child" spans will be enabled. Thus, when the "spawaned
928    /// a thread!" event occurs, it will be inside of the "child" span. Because
929    /// "parent" is the parent of "child", the event will _also_ be inside of
930    /// "parent".
931    ///
932    /// However, if the [`Subscriber`] only enables the [`INFO`] level, the "child"
933    /// span will be disabled. When the thread is spawned, the
934    /// `child_span.entered()` call will do nothing, since "child" is not
935    /// enabled. In this case, the "spawned a thread!" event occurs outside of
936    /// *any* span, since the "child" span was responsible for propagating its
937    /// parent to the spawned thread.
938    ///
939    /// If this is not the desired behavior, `Span::or_current` can be used to
940    /// ensure that the "parent" span is propagated in both cases, either as a
941    /// parent of "child" _or_ directly. For example:
942    ///
943    /// ```
944    /// let _parent_span = tracing::info_span!("parent").entered();
945    ///
946    /// // ...
947    ///
948    /// // If DEBUG is enabled, then "child" will be enabled, and `or_current`
949    /// // returns "child". Otherwise, if DEBUG is not enabled, "child" will be
950    /// // disabled, and `or_current` returns "parent".
951    /// let child_span = tracing::debug_span!("child").or_current();
952    ///
953    /// std::thread::spawn(move || {
954    ///     let _entered = child_span.entered();
955    ///
956    ///     tracing::info!("spawned a thread!");
957    ///
958    ///     // ...
959    /// });
960    /// ```
961    ///
962    /// When spawning [asynchronous tasks][async tasks], `Span::or_current` can
963    /// be used similarly, in combination with [`instrument`]:
964    ///
965    /// ```
966    /// use tracing::Instrument;
967    /// # // lol
968    /// # mod tokio {
969    /// #     pub(super) fn spawn(_: impl std::future::Future) {}
970    /// # }
971    ///
972    /// let _parent_span = tracing::info_span!("parent").entered();
973    ///
974    /// // ...
975    ///
976    /// let child_span = tracing::debug_span!("child");
977    ///
978    /// tokio::spawn(
979    ///     async {
980    ///         tracing::info!("spawned a task!");
981    ///
982    ///         // ...
983    ///
984    ///     }.instrument(child_span.or_current())
985    /// );
986    /// ```
987    ///
988    /// In general, `or_current` should be preferred over nesting an
989    /// [`instrument`]  call inside of an [`in_current_span`] call, as using
990    /// `or_current` will be more efficient.
991    ///
992    /// ```
993    /// use tracing::Instrument;
994    /// # // lol
995    /// # mod tokio {
996    /// #     pub(super) fn spawn(_: impl std::future::Future) {}
997    /// # }
998    /// async fn my_async_fn() {
999    ///     // ...
1000    /// }
1001    ///
1002    /// let _parent_span = tracing::info_span!("parent").entered();
1003    ///
1004    /// // Do this:
1005    /// tokio::spawn(
1006    ///     my_async_fn().instrument(tracing::debug_span!("child").or_current())
1007    /// );
1008    ///
1009    /// // ...rather than this:
1010    /// tokio::spawn(
1011    ///     my_async_fn()
1012    ///         .instrument(tracing::debug_span!("child"))
1013    ///         .in_current_span()
1014    /// );
1015    /// ```
1016    ///
1017    /// [enabled]: crate::Subscriber::enabled
1018    /// [`Subscriber`]: crate::Subscriber
1019    /// [current span]: Span::current
1020    /// [is disabled]: Span::is_disabled
1021    /// [`INFO`]: crate::Level::INFO
1022    /// [`DEBUG`]: crate::Level::DEBUG
1023    /// [async tasks]: std::task
1024    /// [`instrument`]: crate::instrument::Instrument::instrument
1025    /// [`in_current_span`]: crate::instrument::Instrument::in_current_span
1026    pub fn or_current(self) -> Self {
1027        if self.is_disabled() {
1028            return Self::current();
1029        }
1030        self
1031    }
1032
1033    #[inline(always)]
1034    fn do_enter(&self) {
1035        if let Some(inner) = self.inner.as_ref() {
1036            inner.subscriber.enter(&inner.id);
1037        }
1038
1039        if_log_enabled! { crate::Level::TRACE, {
1040            if let Some(_meta) = self.meta {
1041                self.log(ACTIVITY_LOG_TARGET, log::Level::Trace, format_args!("-> {};", _meta.name()));
1042            }
1043        }}
1044    }
1045
1046    // Called from [`Entered`] and [`EnteredSpan`] drops.
1047    //
1048    // Running this behaviour on drop rather than with an explicit function
1049    // call means that spans may still be exited when unwinding.
1050    #[inline(always)]
1051    fn do_exit(&self) {
1052        if let Some(inner) = self.inner.as_ref() {
1053            inner.subscriber.exit(&inner.id);
1054        }
1055
1056        if_log_enabled! { crate::Level::TRACE, {
1057            if let Some(_meta) = self.meta {
1058                self.log(ACTIVITY_LOG_TARGET, log::Level::Trace, format_args!("<- {};", _meta.name()));
1059            }
1060        }}
1061    }
1062
1063    /// Executes the given function in the context of this span.
1064    ///
1065    /// If this span is enabled, then this function enters the span, invokes `f`
1066    /// and then exits the span. If the span is disabled, `f` will still be
1067    /// invoked, but in the context of the currently-executing span (if there is
1068    /// one).
1069    ///
1070    /// Returns the result of evaluating `f`.
1071    ///
1072    /// # Examples
1073    ///
1074    /// ```
1075    /// # use tracing::{trace, span, Level};
1076    /// let my_span = span!(Level::TRACE, "my_span");
1077    ///
1078    /// my_span.in_scope(|| {
1079    ///     // this event occurs within the span.
1080    ///     trace!("i'm in the span!");
1081    /// });
1082    ///
1083    /// // this event occurs outside the span.
1084    /// trace!("i'm not in the span!");
1085    /// ```
1086    ///
1087    /// Calling a function and returning the result:
1088    /// ```
1089    /// # use tracing::{info_span, Level};
1090    /// fn hello_world() -> String {
1091    ///     "Hello world!".to_owned()
1092    /// }
1093    ///
1094    /// let span = info_span!("hello_world");
1095    /// // the span will be entered for the duration of the call to
1096    /// // `hello_world`.
1097    /// let a_string = span.in_scope(hello_world);
1098    ///
1099    pub fn in_scope<F: FnOnce() -> T, T>(&self, f: F) -> T {
1100        let _enter = self.enter();
1101        f()
1102    }
1103
1104    /// Returns a [`Field`][super::field::Field] for the field with the
1105    /// given `name`, if one exists,
1106    pub fn field<Q: field::AsField + ?Sized>(&self, field: &Q) -> Option<field::Field> {
1107        self.metadata().and_then(|meta| field.as_field(meta))
1108    }
1109
1110    /// Returns true if this `Span` has a field for the given
1111    /// [`Field`][super::field::Field] or field name.
1112    #[inline]
1113    pub fn has_field<Q: field::AsField + ?Sized>(&self, field: &Q) -> bool {
1114        self.field(field).is_some()
1115    }
1116
1117    /// Records that the field described by `field` has the value `value`.
1118    ///
1119    /// This may be used with [`field::Empty`] to declare fields whose values
1120    /// are not known when the span is created, and record them later:
1121    /// ```
1122    /// use tracing::{trace_span, field};
1123    ///
1124    /// // Create a span with two fields: `greeting`, with the value "hello world", and
1125    /// // `parting`, without a value.
1126    /// let span = trace_span!("my_span", greeting = "hello world", parting = field::Empty);
1127    ///
1128    /// // ...
1129    ///
1130    /// // Now, record a value for parting as well.
1131    /// // (note that the field name is passed as a string slice)
1132    /// span.record("parting", "goodbye world!");
1133    /// ```
1134    /// However, it may also be used to record a _new_ value for a field whose
1135    /// value was already recorded:
1136    /// ```
1137    /// use tracing::info_span;
1138    /// # fn do_something() -> Result<(), ()> { Err(()) }
1139    ///
1140    /// // Initially, let's assume that our attempt to do something is going okay...
1141    /// let span = info_span!("doing_something", is_okay = true);
1142    /// let _e = span.enter();
1143    ///
1144    /// match do_something() {
1145    ///     Ok(something) => {
1146    ///         // ...
1147    ///     }
1148    ///     Err(_) => {
1149    ///         // Things are no longer okay!
1150    ///         span.record("is_okay", false);
1151    ///     }
1152    /// }
1153    /// ```
1154    ///
1155    /// <pre class="ignore" style="white-space:normal;font:inherit;">
1156    ///     <strong>Note</strong>: The fields associated with a span are part
1157    ///     of its <a href="../struct.Metadata.html"><code>Metadata</code></a>.
1158    ///     The <a href="../struct.Metadata.html"><code>Metadata</code></a>
1159    ///     describing a particular span is constructed statically when the span
1160    ///     is created and cannot be extended later to add new fields. Therefore,
1161    ///     you cannot record a value for a field that was not specified when the
1162    ///     span was created:
1163    /// </pre>
1164    ///
1165    /// ```
1166    /// use tracing::{trace_span, field};
1167    ///
1168    /// // Create a span with two fields: `greeting`, with the value "hello world", and
1169    /// // `parting`, without a value.
1170    /// let span = trace_span!("my_span", greeting = "hello world", parting = field::Empty);
1171    ///
1172    /// // ...
1173    ///
1174    /// // Now, you try to record a value for a new field, `new_field`, which was not
1175    /// // declared as `Empty` or populated when you created `span`.
1176    /// // You won't get any error, but the assignment will have no effect!
1177    /// span.record("new_field", "interesting_value_you_really_need");
1178    ///
1179    /// // Instead, all fields that may be recorded after span creation should be declared up front,
1180    /// // using field::Empty when a value is not known, as we did for `parting`.
1181    /// // This `record` call will indeed replace field::Empty with "you will be remembered".
1182    /// span.record("parting", "you will be remembered");
1183    /// ```
1184    ///
1185    /// <div class="example-wrap" style="display:inline-block">
1186    /// <pre class="ignore" style="white-space:normal;font:inherit;">
1187    /// **Note**: To record several values in just one call, see the [`record_all!`](crate::record_all!) macro.
1188    /// </pre></div>
1189    ///
1190    /// [`field::Empty`]: super::field::Empty
1191    /// [`Metadata`]: super::Metadata
1192    pub fn record<Q: field::AsField + ?Sized, V: field::Value>(
1193        &self,
1194        field: &Q,
1195        value: V,
1196    ) -> &Self {
1197        if let Some(meta) = self.meta {
1198            if let Some(field) = field.as_field(meta) {
1199                self.record_all(
1200                    &meta
1201                        .fields()
1202                        .value_set(&[(&field, Some(&value as &dyn field::Value))]),
1203                );
1204            }
1205        }
1206
1207        self
1208    }
1209
1210    /// Records all the fields in the provided `ValueSet`.
1211    #[doc(hidden)]
1212    pub fn record_all(&self, values: &field::ValueSet<'_>) -> &Self {
1213        let record = Record::new(values);
1214        if let Some(ref inner) = self.inner {
1215            inner.record(&record);
1216        }
1217
1218        if let Some(_meta) = self.meta {
1219            if_log_enabled! { *_meta.level(), {
1220                let target = if record.is_empty() {
1221                    LIFECYCLE_LOG_TARGET
1222                } else {
1223                    _meta.target()
1224                };
1225                self.log(
1226                    target,
1227                    level_to_log!(*_meta.level()),
1228                    format_args!("{};{}", _meta.name(), crate::log::LogValueSet { values, is_first: false }),
1229                );
1230            }}
1231        }
1232
1233        self
1234    }
1235
1236    /// Returns `true` if this span was disabled by the subscriber and does not
1237    /// exist.
1238    ///
1239    /// See also [`is_none`].
1240    ///
1241    /// [`is_none`]: Span::is_none()
1242    #[inline]
1243    pub fn is_disabled(&self) -> bool {
1244        self.inner.is_none()
1245    }
1246
1247    /// Returns `true` if this span was constructed by [`Span::none`] and is
1248    /// empty.
1249    ///
1250    /// If `is_none` returns `true` for a given span, then [`is_disabled`] will
1251    /// also return `true`. However, when a span is disabled by the subscriber
1252    /// rather than constructed by `Span::none`, this method will return
1253    /// `false`, while `is_disabled` will return `true`.
1254    ///
1255    /// [`Span::none`]: Span::none()
1256    /// [`is_disabled`]: Span::is_disabled()
1257    #[inline]
1258    pub fn is_none(&self) -> bool {
1259        self.is_disabled() && self.meta.is_none()
1260    }
1261
1262    /// Indicates that the span with the given ID has an indirect causal
1263    /// relationship with this span.
1264    ///
1265    /// This relationship differs somewhat from the parent-child relationship: a
1266    /// span may have any number of prior spans, rather than a single one; and
1267    /// spans are not considered to be executing _inside_ of the spans they
1268    /// follow from. This means that a span may close even if subsequent spans
1269    /// that follow from it are still open, and time spent inside of a
1270    /// subsequent span should not be included in the time its precedents were
1271    /// executing. This is used to model causal relationships such as when a
1272    /// single future spawns several related background tasks, et cetera.
1273    ///
1274    /// If this span is disabled, or the resulting follows-from relationship
1275    /// would be invalid, this function will do nothing.
1276    ///
1277    /// # Examples
1278    ///
1279    /// Setting a `follows_from` relationship with a `Span`:
1280    /// ```
1281    /// # use tracing::{span, Id, Level, Span};
1282    /// let span1 = span!(Level::INFO, "span_1");
1283    /// let span2 = span!(Level::DEBUG, "span_2");
1284    /// span2.follows_from(span1);
1285    /// ```
1286    ///
1287    /// Setting a `follows_from` relationship with the current span:
1288    /// ```
1289    /// # use tracing::{span, Id, Level, Span};
1290    /// let span = span!(Level::INFO, "hello!");
1291    /// span.follows_from(Span::current());
1292    /// ```
1293    ///
1294    /// Setting a `follows_from` relationship with a `Span` reference:
1295    /// ```
1296    /// # use tracing::{span, Id, Level, Span};
1297    /// let span = span!(Level::INFO, "hello!");
1298    /// let curr = Span::current();
1299    /// span.follows_from(&curr);
1300    /// ```
1301    ///
1302    /// Setting a `follows_from` relationship with an `Id`:
1303    /// ```
1304    /// # use tracing::{span, Id, Level, Span};
1305    /// let span = span!(Level::INFO, "hello!");
1306    /// let id = span.id();
1307    /// span.follows_from(id);
1308    /// ```
1309    pub fn follows_from(&self, from: impl Into<Option<Id>>) -> &Self {
1310        if let Some(ref inner) = self.inner {
1311            if let Some(from) = from.into() {
1312                inner.follows_from(&from);
1313            }
1314        }
1315        self
1316    }
1317
1318    /// Returns this span's `Id`, if it is enabled.
1319    pub fn id(&self) -> Option<Id> {
1320        self.inner.as_ref().map(Inner::id)
1321    }
1322
1323    /// Returns this span's `Metadata`, if it is enabled.
1324    pub fn metadata(&self) -> Option<&'static Metadata<'static>> {
1325        self.meta
1326    }
1327
1328    #[cfg(feature = "log")]
1329    #[inline]
1330    fn log(&self, target: &str, level: log::Level, message: fmt::Arguments<'_>) {
1331        if let Some(meta) = self.meta {
1332            if level_to_log!(*meta.level()) <= log::max_level() {
1333                let logger = log::logger();
1334                let log_meta = log::Metadata::builder().level(level).target(target).build();
1335                if logger.enabled(&log_meta) {
1336                    if let Some(ref inner) = self.inner {
1337                        logger.log(
1338                            &log::Record::builder()
1339                                .metadata(log_meta)
1340                                .module_path(meta.module_path())
1341                                .file(meta.file())
1342                                .line(meta.line())
1343                                .args(format_args!("{} span={}", message, inner.id.into_u64()))
1344                                .build(),
1345                        );
1346                    } else {
1347                        logger.log(
1348                            &log::Record::builder()
1349                                .metadata(log_meta)
1350                                .module_path(meta.module_path())
1351                                .file(meta.file())
1352                                .line(meta.line())
1353                                .args(message)
1354                                .build(),
1355                        );
1356                    }
1357                }
1358            }
1359        }
1360    }
1361
1362    /// Invokes a function with a reference to this span's ID and subscriber.
1363    ///
1364    /// if this span is enabled, the provided function is called, and the result is returned.
1365    /// If the span is disabled, the function is not called, and this method returns `None`
1366    /// instead.
1367    pub fn with_subscriber<T>(&self, f: impl FnOnce((&Id, &Dispatch)) -> T) -> Option<T> {
1368        self.inner
1369            .as_ref()
1370            .map(|inner| f((&inner.id, &inner.subscriber)))
1371    }
1372}
1373
1374impl cmp::PartialEq for Span {
1375    fn eq(&self, other: &Self) -> bool {
1376        match (&self.meta, &other.meta) {
1377            (Some(this), Some(that)) => {
1378                this.callsite() == that.callsite() && self.inner == other.inner
1379            }
1380            _ => false,
1381        }
1382    }
1383}
1384
1385impl Hash for Span {
1386    fn hash<H: Hasher>(&self, hasher: &mut H) {
1387        self.inner.hash(hasher);
1388    }
1389}
1390
1391impl fmt::Debug for Span {
1392    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
1393        let mut span = f.debug_struct("Span");
1394        if let Some(meta) = self.meta {
1395            span.field("name", &meta.name())
1396                .field("level", &meta.level())
1397                .field("target", &meta.target());
1398
1399            if let Some(ref inner) = self.inner {
1400                span.field("id", &inner.id());
1401            } else {
1402                span.field("disabled", &true);
1403            }
1404
1405            if let Some(ref path) = meta.module_path() {
1406                span.field("module_path", &path);
1407            }
1408
1409            if let Some(ref line) = meta.line() {
1410                span.field("line", &line);
1411            }
1412
1413            if let Some(ref file) = meta.file() {
1414                span.field("file", &file);
1415            }
1416        } else {
1417            span.field("none", &true);
1418        }
1419
1420        span.finish()
1421    }
1422}
1423
1424impl<'a> From<&'a Span> for Option<&'a Id> {
1425    fn from(span: &'a Span) -> Self {
1426        span.inner.as_ref().map(|inner| &inner.id)
1427    }
1428}
1429
1430impl<'a> From<&'a Span> for Option<Id> {
1431    fn from(span: &'a Span) -> Self {
1432        span.inner.as_ref().map(Inner::id)
1433    }
1434}
1435
1436impl From<Span> for Option<Id> {
1437    fn from(span: Span) -> Self {
1438        span.inner.as_ref().map(Inner::id)
1439    }
1440}
1441
1442impl<'a> From<&'a EnteredSpan> for Option<&'a Id> {
1443    fn from(span: &'a EnteredSpan) -> Self {
1444        span.inner.as_ref().map(|inner| &inner.id)
1445    }
1446}
1447
1448impl<'a> From<&'a EnteredSpan> for Option<Id> {
1449    fn from(span: &'a EnteredSpan) -> Self {
1450        span.inner.as_ref().map(Inner::id)
1451    }
1452}
1453
1454impl Drop for Span {
1455    #[inline(always)]
1456    fn drop(&mut self) {
1457        if let Some(Inner {
1458            ref id,
1459            ref subscriber,
1460        }) = self.inner
1461        {
1462            subscriber.try_close(id.clone());
1463        }
1464
1465        if_log_enabled! { crate::Level::TRACE, {
1466            if let Some(meta) = self.meta {
1467                self.log(
1468                    LIFECYCLE_LOG_TARGET,
1469                    log::Level::Trace,
1470                    format_args!("-- {};", meta.name()),
1471                );
1472            }
1473        }}
1474    }
1475}
1476
1477// ===== impl Inner =====
1478
1479impl Inner {
1480    /// Indicates that the span with the given ID has an indirect causal
1481    /// relationship with this span.
1482    ///
1483    /// This relationship differs somewhat from the parent-child relationship: a
1484    /// span may have any number of prior spans, rather than a single one; and
1485    /// spans are not considered to be executing _inside_ of the spans they
1486    /// follow from. This means that a span may close even if subsequent spans
1487    /// that follow from it are still open, and time spent inside of a
1488    /// subsequent span should not be included in the time its precedents were
1489    /// executing. This is used to model causal relationships such as when a
1490    /// single future spawns several related background tasks, et cetera.
1491    ///
1492    /// If this span is disabled, this function will do nothing. Otherwise, it
1493    /// returns `Ok(())` if the other span was added as a precedent of this
1494    /// span, or an error if this was not possible.
1495    fn follows_from(&self, from: &Id) {
1496        self.subscriber.record_follows_from(&self.id, from)
1497    }
1498
1499    /// Returns the span's ID.
1500    fn id(&self) -> Id {
1501        self.id.clone()
1502    }
1503
1504    fn record(&self, values: &Record<'_>) {
1505        self.subscriber.record(&self.id, values)
1506    }
1507
1508    fn new(id: Id, subscriber: &Dispatch) -> Self {
1509        Inner {
1510            id,
1511            subscriber: subscriber.clone(),
1512        }
1513    }
1514}
1515
1516impl cmp::PartialEq for Inner {
1517    fn eq(&self, other: &Self) -> bool {
1518        self.id == other.id
1519    }
1520}
1521
1522impl Hash for Inner {
1523    fn hash<H: Hasher>(&self, state: &mut H) {
1524        self.id.hash(state);
1525    }
1526}
1527
1528impl Clone for Inner {
1529    fn clone(&self) -> Self {
1530        Inner {
1531            id: self.subscriber.clone_span(&self.id),
1532            subscriber: self.subscriber.clone(),
1533        }
1534    }
1535}
1536
1537// ===== impl Entered =====
1538
1539impl EnteredSpan {
1540    /// Returns this span's `Id`, if it is enabled.
1541    pub fn id(&self) -> Option<Id> {
1542        self.inner.as_ref().map(Inner::id)
1543    }
1544
1545    /// Exits this span, returning the underlying [`Span`].
1546    #[inline]
1547    pub fn exit(mut self) -> Span {
1548        // One does not simply move out of a struct with `Drop`.
1549        let span = mem::replace(&mut self.span, Span::none());
1550        span.do_exit();
1551        span
1552    }
1553}
1554
1555impl Deref for EnteredSpan {
1556    type Target = Span;
1557
1558    #[inline]
1559    fn deref(&self) -> &Span {
1560        &self.span
1561    }
1562}
1563
1564impl Drop for Entered<'_> {
1565    #[inline(always)]
1566    fn drop(&mut self) {
1567        self.span.do_exit()
1568    }
1569}
1570
1571impl Drop for EnteredSpan {
1572    #[inline(always)]
1573    fn drop(&mut self) {
1574        self.span.do_exit()
1575    }
1576}
1577
1578/// Technically, `EnteredSpan` _can_ implement both `Send` *and*
1579/// `Sync` safely. It doesn't, because it has a `PhantomNotSend` field,
1580/// specifically added in order to make it `!Send`.
1581///
1582/// Sending an `EnteredSpan` guard between threads cannot cause memory unsafety.
1583/// However, it *would* result in incorrect behavior, so we add a
1584/// `PhantomNotSend` to prevent it from being sent between threads. This is
1585/// because it must be *dropped* on the same thread that it was created;
1586/// otherwise, the span will never be exited on the thread where it was entered,
1587/// and it will attempt to exit the span on a thread that may never have entered
1588/// it. However, we still want them to be `Sync` so that a struct holding an
1589/// `Entered` guard can be `Sync`.
1590///
1591/// Thus, this is totally safe.
1592#[derive(Debug)]
1593struct PhantomNotSend {
1594    ghost: PhantomData<*mut ()>,
1595}
1596
1597#[allow(non_upper_case_globals)]
1598const PhantomNotSend: PhantomNotSend = PhantomNotSend { ghost: PhantomData };
1599
1600/// # Safety
1601///
1602/// Trivially safe, as `PhantomNotSend` doesn't have any API.
1603unsafe impl Sync for PhantomNotSend {}
1604
1605#[cfg(test)]
1606mod test {
1607    use super::*;
1608
1609    #[test]
1610    fn test_record_backwards_compat() {
1611        Span::current().record("some-key", "some text");
1612        Span::current().record("some-key", false);
1613    }
1614}