Docs
Error monitoring and tracing
Send application errors to Sentry and request spans to an OpenTelemetry collector.
Keep the generated request tracing, with the span change below, and install one subscriber at startup. Sentry can capture errors from tracing::error!, and OpenTelemetry can send the same request tree to a collector. Set SENTRY_DSN and OTEL_EXPORTER_OTLP_ENDPOINT in the server environment.
[dependencies]
sentry = { version = "0.49", features = ["tracing", "tower"] }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing-opentelemetry = "0.34"
opentelemetry = "0.33"
opentelemetry_sdk = "0.33"
opentelemetry-otlp = { version = "0.33", features = ["grpc-tonic"] }
use opentelemetry::trace::TracerProvider as _;
use opentelemetry_otlp::SpanExporter;
use opentelemetry_sdk::{trace::SdkTracerProvider, Resource};
use tracing_subscriber::{filter::LevelFilter, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter, Layer};
// Call inside the Tokio runtime, before serving requests.
pub fn install_tracing() -> Result<SdkTracerProvider, Box<dyn std::error::Error + Send + Sync>> {
let exporter = SpanExporter::builder().with_tonic().build()?;
let provider = SdkTracerProvider::builder()
.with_resource(Resource::builder().with_service_name("proa-site").build())
.with_batch_exporter(exporter)
.build();
tracing_subscriber::registry()
.with(EnvFilter::try_from_default_env().unwrap_or_else(|_| EnvFilter::new("info")))
.with(tracing_subscriber::fmt::layer())
.with(sentry::integrations::tracing::ErrorLayer::new().with_filter(LevelFilter::ERROR))
.with(tracing_opentelemetry::layer().with_tracer(provider.tracer("http")))
.try_init()?;
Ok(provider)
}
Sentry must be initialized before the Tokio runtime starts, so replace the generated #[tokio::main] entry point with a plain main that initializes Sentry and then runs the generated body on a runtime it builds. The guard stays alive until the server has drained, and the tracer provider shuts down before the runtime drops:
fn main() -> anyhow::Result<()> {
let _sentry = sentry::init(());
tokio::runtime::Runtime::new()?.block_on(run())
}
/// The generated `main` body, with `install_tracing()` in place of its
/// `tracing_subscriber` initialization.
async fn run() -> anyhow::Result<()> {
let provider = telemetry::install_tracing().map_err(anyhow::Error::from_boxed)?;
// Load the config, build the router, and serve, as generated.
provider.shutdown()?;
Ok(())
}
Add sentry::integrations::tower::NewSentryLayer::<axum::http::Request<axum::body::Body>>::new_from_top() to the completed router to isolate each request’s Sentry scope.
The generated TraceLayer::new_for_http() records the full request URI, query string included, so values such as OAuth callback codes would reach the collector. Record the matched route template instead, falling back to the path for static files:
use axum::{extract::MatchedPath, http::Request};
use tower_http::trace::TraceLayer;
let trace = TraceLayer::new_for_http().make_span_with(|request: &Request<_>| {
let route = request
.extensions()
.get::<MatchedPath>()
.map_or_else(|| request.uri().path(), MatchedPath::as_str);
tracing::info_span!("request", method = %request.method(), route)
});
Pass trace to app.layer(...) in place of TraceLayer::new_for_http(). Add the response status and request ID as fields, and keep authorization headers, cookies, and request bodies out of spans. Exercise a controlled application error in staging and confirm one Sentry event and a completed request span at the collector. Sentry for Axum · OpenTelemetry exporters