diff --git a/sentry-tower/Cargo.toml b/sentry-tower/Cargo.toml index cec600d0..8e0d9c1b 100644 --- a/sentry-tower/Cargo.toml +++ b/sentry-tower/Cargo.toml @@ -14,8 +14,10 @@ rust-version = "1.66" [features] http = ["dep:http", "pin-project", "url"] +axum-matched-path = ["axum/matched-path"] [dependencies] +axum = { version = "0.6", optional = true } tower-layer = "0.3" tower-service = "0.3" http = { version = "0.2.6", optional = true } diff --git a/sentry-tower/src/http.rs b/sentry-tower/src/http.rs index a0b84b97..82417d2b 100644 --- a/sentry-tower/src/http.rs +++ b/sentry-tower/src/http.rs @@ -154,7 +154,7 @@ where let headers = request.headers().into_iter().flat_map(|(header, value)| { value.to_str().ok().map(|value| (header.as_str(), value)) }); - let tx_name = format!("{} {}", request.method(), request.uri().path()); + let tx_name = format!("{} {}", request.method(), path_from_request(&request)); Some(sentry_core::TransactionContext::continue_from_headers( &tx_name, "http.server", @@ -172,6 +172,15 @@ where } } +fn path_from_request(request: &Request) -> &str { + #[cfg(feature = "axum-matched-path")] + if let Some(matched_path) = request.extensions().get::() { + return matched_path.as_str(); + } + + request.uri().path() +} + fn map_status(status: StatusCode) -> protocol::SpanStatus { match status { StatusCode::UNAUTHORIZED => protocol::SpanStatus::Unauthenticated,