tracing

Learn about monitoring your Rust application with Sentry's tracing integration.

The Sentry SDK offers an integration for tokio's tracing ecosystem that supports:

  • Reporting tracing events to Sentry as events, breadcrumbs, or logs.
  • Reporting tracing spans to Sentry.
  • Reporting errors and panics with the correct trace correlation.

To add Sentry with the tracing integration to your Rust project, add the necessary dependencies to your Cargo.toml:

Cargo.toml
Copied
[dependencies]
tracing = "0.1.41"
tracing-subscriber = "0.3.19"
sentry = { version = "0.42.0", features = [
  "tracing",
  #  logs
  "logs",
  #  logs
] }
tokio = { version = "1.45.0", features = ["full"] }

Initialize the Sentry SDK and register the Sentry layer to start sending tracing events and spans to Sentry:

Copied
use tracing_subscriber::prelude::*;

fn main() {
    // Initialize Sentry
    let _guard = sentry::init((
        "https://examplePublicKey@o0.ingest.sentry.io/0",
        sentry::ClientOptions {
            release: sentry::release_name!(),
            #  performance
            // Capture all traces and spans. Set to a lower value in production
            traces_sample_rate: 1.0,
            #  performance
            #  logs
            enable_logs:true
            #  logs
            ..sentry::ClientOptions::default()
        },
    });

    // Register the Sentry tracing layer
    tracing_subscriber::registry()
        .with(tracing_subscriber::fmt::layer())
        .with(sentry::integrations::tracing::layer())
        .init();
        
    tokio::runtime::Builder::new_multi_thread()
        .enable_all()
        .build()?
        .block_on(async {
            // Futures should to be bound to a Hub
            // Learn more at https://docs.rs/sentry-core/latest/sentry_core/#parallelism-concurrency-and-async
            fail().bind_hub(sentry::Hub::current()).await;
        });
}

#  performance
#[tracing::instrument] // Captures a root span (transaction) around this function execution
#  performance
async fn fail() {
    tracing::debug!("Doing work"); // Adds a breadrcumb
    #  performance
    let _span = tracing::info_span("Child span").entered(); // Captures a child span
    #  performance
    tracing::error!("Everything is on fire!");
}

By default, error level events are captured as Sentry events, while anything at or above info is added as a breadcrumb.

To capture structured logs for tracing events instead, you need to set up the Sentry layer with a custom event filter that maps to logs, like so:

Copied
use sentry::integrations::tracing::EventFilter;

sentry::integrations::tracing::layer().event_filter(|md| match *md.level() {
    // Capture error level events as Sentry events
    tracing::Level::ERROR => EventFilter::Event,
    // Ignore trace level events, as they're too verbose
    tracing::Level::TRACE => EventFilter::Ignore,
    // Capture everything else as a structured log
    _ => EventFilter::Log,
});

To view and resolve the recorded error, log into sentry.io and select your project. Select Issues, and then Errors & Outages in the sidebar, where you will find the newly created issue. Clicking on the issue's title will open a page where you can see detailed information and mark it as resolved.

Was this helpful?
Help improve this content
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").