FutureSpanExt

Trait FutureSpanExt 

Source
pub trait FutureSpanExt<T, E>: Future<Output = Result<T, E>> + Sized {
    // Provided methods
    fn with_span_context(self) -> SpanContextFuture<Self>  { ... }
    fn with_span(self, span: Span) -> SpanContextFuture<Self>  { ... }
}
Expand description

Extension trait for futures that adds tracing span context to errors.

This trait provides methods to automatically capture the current tracing span’s metadata and attach it as error context when errors occur.

§Example

use error_rail::async_ext::FutureSpanExt;
use tracing::info_span;

async fn fetch_user(id: u64) -> Result<User, ApiError> {
    let span = info_span!("fetch_user", user_id = id);
     
    async {
        database.get_user(id).await
    }
    .with_span_context()
    .instrument(span)
    .await
}

Provided Methods§

Source

fn with_span_context(self) -> SpanContextFuture<Self>

Captures the current span’s metadata as error context on failure.

When the future resolves to an error, the current span’s name and metadata are attached as context to the error.

Source

fn with_span(self, span: Span) -> SpanContextFuture<Self>

Captures a specific span’s metadata as error context on failure.

Unlike with_span_context(), this method uses the provided span instead of the current span.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§

Source§

impl<F, T, E> FutureSpanExt<T, E> for F
where F: Future<Output = Result<T, E>>,