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
tracingspans to Sentry as transactions and spans. - Reporting
tracingevents to Sentry as logs. - Reporting errors and panics with the correct trace correlation.
- Optionally, reporting
tracingevents 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[dependencies]
tracing = "0.1.41"
tracing-subscriber = "0.3.19"
sentry = { version = "0.49.5", features = [
"tracing"
] }
tokio = { version = "1.45.0", features = ["full"] }
[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:
Macros like #[tokio::main] and #[actix_web::main] are not supported. The Sentry client must be initialized before the async runtime is started, as shown below.
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
}
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: Capturestracingspans as Sentry transactions and spans.LogLayer: Capturestracingevents as Sentry logs. Requires thelogsfeature, 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.
sentry::integrations::tracing::layer() and SentryLayer are deprecated as of version 0.49.4. When migrating, replace the legacy layer instead of adding the new layers alongside it: combining SpanLayer with the legacy layer can panic. See the migration guide for details.
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:
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();
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.
Events captured by ErrorLayer count toward your error quota, which is typically more expensive than logs. Always add a level filter: without one, ErrorLayer sends every tracing event as an error, including DEBUG and TRACE.
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.
Learn more about manually capturing an error or message in our Usage documentation.
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
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").