Sentry for tokio-rs/tracing

Learn about monitoring your Rust application with Sentry's tokio-rs/tracing integration.

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

  • Reporting tracing spans to Sentry as transactions and spans.
  • Reporting tracing events to Sentry as logs.
  • Reporting errors and panics with the correct trace correlation.
  • Optionally, reporting tracing events to Sentry as errors, which create issues.

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.49.5", features = [
  "tracing"
] }
tokio = { version = "1.45.0", features = ["full"] }

Initialize the Sentry SDK and register the Sentry layers to start sending tracing spans and logs to Sentry:

Copied
use sentry::integrations::tracing::{LogLayer, SpanLayer};
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::prelude::*;

fn main() {
    // Initialize Sentry
    let _guard = sentry::init(
        sentry::ClientOptions::new()
            .dsn("___PUBLIC_DSN___")
            .maybe_release(sentry::release_name!())
            // Capture all traces and spans. Set to a lower value in production
            .traces_sample_rate(1.0)
    );

    // Register the Sentry tracing layers
    tracing_subscriber::registry()
        .with(tracing_subscriber::fmt::layer())
        // Capture INFO, WARN, and ERROR spans as Sentry transactions and spans
        .with(SpanLayer::new().with_filter(LevelFilter::INFO))
        // Capture INFO, WARN, and ERROR events as Sentry logs
        .with(LogLayer::new().with_filter(LevelFilter::INFO))
        .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;
        });
}

#[tracing::instrument] // Captures a root span (transaction) around this function execution
async fn fail() {
    tracing::info!("Doing work"); // Captured as a log
    let _span = tracing::info_span("Child span").entered(); // Captures a child span
    tracing::error!("Everything is on fire!"); // Captured as a log, linked to the child span
}

The integration provides separate layers, so you can choose which tracing data to send to Sentry. We recommend enabling the span and log layers, as shown above:

  • SpanLayer: Captures tracing spans as Sentry transactions and spans.
  • LogLayer: Captures tracing events as Sentry logs. Requires the logs feature, which is enabled by default.

The layers don't filter by level on their own, and without a filter they capture every level, including DEBUG and TRACE. We recommend giving each layer a tracing_subscriber filter, as shown above. LevelFilter::INFO is a sensible starting point for most applications.

If you'd like Sentry issues for error-level tracing events, for example, to use them in issue alerts, you can also enable the error layer, ErrorLayer, with an ERROR level filter:

Copied
use sentry::integrations::tracing::{ErrorLayer, LogLayer, SpanLayer};
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::prelude::*;

tracing_subscriber::registry()
    .with(tracing_subscriber::fmt::layer())
    .with(SpanLayer::new().with_filter(LevelFilter::INFO))
    .with(LogLayer::new().with_filter(LevelFilter::INFO))
    // Capture ERROR events as Sentry errors
    .with(ErrorLayer::new().with_filter(LevelFilter::ERROR))
    .init();

With this setup, ERROR events are sent to Sentry as both logs and errors.

The SDK also provides BreadcrumbLayer, which adds tracing events as breadcrumbs to subsequent Sentry errors. It's mainly there to support legacy setups. We recommend LogLayer instead, since logs are sent whether or not an error occurs.

To view the recorded traces and spans, log into sentry.io, select your project, and then select Explore > Traces in the sidebar.

To view the recorded logs, select Explore > Logs in the sidebar.

  • Explore practical guides on what to monitor, log, track, and investigate after setup
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").