tracing_error

Struct SpanTrace

Source
pub struct SpanTrace { /* private fields */ }
Expand description

A captured trace of tracing spans.

This type can be thought of as a relative of std::backtrace::Backtrace. However, rather than capturing the current call stack when it is constructed, a SpanTrace instead captures the current span and its parents.

In many cases, span traces may be as useful as stack backtraces useful in pinpointing where an error occurred and why, if not moreso:

  • A span trace captures only the user-defined, human-readable tracing spans, rather than every frame in the call stack, often cutting out a lot of noise.
  • Span traces include the fields recorded by each span in the trace, as well as their names and source code location, so different invocations of a function can be distinguished,
  • In asynchronous code, backtraces for errors that occur in futures often consist not of the stack frames that spawned a future, but the stack frames of the executor that is responsible for running that future. This means that if an async fn calls another async fn which generates an error, the calling async function will not appear in the stack trace (and often, the callee won’t either!). On the other hand, when the tracing-futures crate is used to instrument async code, the span trace will represent the logical application context a future was running in, rather than the stack trace of the executor that was polling a future when an error occurred.

Finally, unlike stack Backtraces, capturing a SpanTrace is fairly lightweight, and the resulting struct is not large. The SpanTrace struct is formatted lazily; instead, it simply stores a copy of the current span, and allows visiting the spans in that span’s trace tree by calling the with_spans method.

§Formatting

The SpanTrace type implements fmt::Display, formatting the span trace similarly to how Rust formats panics. For example:

   0: custom_error::do_another_thing
        with answer=42 will_succeed=false
          at examples/examples/custom_error.rs:42
   1: custom_error::do_something
        with foo="hello world"
          at examples/examples/custom_error.rs:37

Additionally, if custom formatting is desired, the with_spans method can be used to visit each span in the trace, formatting them in order.

Implementations§

Source§

impl SpanTrace

Source

pub fn new(span: Span) -> Self

Create a new span trace with the given span as the innermost span.

Source

pub fn capture() -> Self

Capture the current span trace.

§Examples
use tracing_error::SpanTrace;

pub struct MyError {
    span_trace: SpanTrace,
    // ...
}


#[tracing::instrument]
pub fn my_function(arg: &str) -> Result<(), MyError> {
    if some_error_condition() {
        return Err(MyError {
            span_trace: SpanTrace::capture(),
            // ...
        });
    }

    // ...
}
Source

pub fn with_spans( &self, f: impl FnMut(&'static Metadata<'static>, &str) -> bool, )

Apply a function to all captured spans in the trace until it returns false.

This will call the provided function with a reference to the Metadata and a formatted representation of the fields of each span captured in the trace, starting with the span that was current when the trace was captured. The function may return true or false to indicate whether to continue iterating over spans; if it returns false, no additional spans will be visited.

Source

pub fn status(&self) -> SpanTraceStatus

Returns the status of this SpanTrace.

The status indicates one of the following:

  • the current subscriber does not support capturing SpanTraces
  • there was no current span, so a trace was not captured
  • a span trace was successfully captured

Trait Implementations§

Source§

impl Clone for SpanTrace

Source§

fn clone(&self) -> SpanTrace

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SpanTrace

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for SpanTrace

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit #126799)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more