Crate rootcause_tracing

Crate rootcause_tracing 

Source
Expand description

Tracing span capture for rootcause error reports.

This crate automatically captures tracing span context when errors occur, helping you understand which operation was being performed.

§How It Works

You add RootcauseLayer to your tracing subscriber alongside your existing layers (formatting, filtering, log forwarding, etc.). While your other layers do their work, RootcauseLayer quietly captures span field values in the background for use in error reports.

§Quick Start

use rootcause::hooks::Hooks;
use rootcause_tracing::{RootcauseLayer, SpanCollector};
use tracing_subscriber::{Registry, layer::SubscriberExt};

// 1. Set up tracing with RootcauseLayer (required)
let subscriber = Registry::default()
    .with(RootcauseLayer) // Captures span field values for error reports
    .with(tracing_subscriber::fmt::layer()); // Your normal console output
tracing::subscriber::set_global_default(subscriber).expect("failed to set subscriber");

// 2. Install hook to capture spans for all errors (optional)
Hooks::new()
    .report_creation_hook(SpanCollector::new())
    .install()
    .expect("failed to install hooks");

// 3. Use normally - spans are captured automatically
#[tracing::instrument(fields(user_id = 42))]
fn example() -> rootcause::Report {
    rootcause::report!("something went wrong")
}
println!("{}", example());

Output:

 ● something went wrong
 ├ src/main.rs:10
 ╰ Tracing spans
   │ example{user_id=42}
   ╰─

§Manual Attachment

To attach spans selectively instead of automatically:

use rootcause::{Report, report};
use rootcause_tracing::SpanExt;

#[tracing::instrument]
fn operation() -> Result<(), Report> {
    Err(report!("operation failed"))
}

let result = operation().attach_span();

Note: RootcauseLayer must be in your subscriber setup either way.

§Environment Variables

  • ROOTCAUSE_TRACING - Comma-separated options:
    • leafs - Only capture tracing spans for leaf errors (errors without children)

Structs§

RootcauseLayer
A tracing layer that captures span field values for error reports.
SpanCollector
Attachment collector for capturing tracing spans.
SpanHandler
Handler for formatting Span attachments.

Traits§

SpanExt
Extension trait for attaching tracing spans to reports.