Skip to content

Commit

Permalink
sentry-tower: Use MatchedPath for transaction names (#570)
Browse files Browse the repository at this point in the history
If the `axum-matched-path` feature of `sentry-tower` is used, it will enable the corresponding `matched-path` feature of `axum` and use the `MatchedPath` request extension to generate the transaction name. As before, the HTTP method is also still included in the transaction name.
  • Loading branch information
Turbo87 authored Apr 24, 2023
1 parent 8a33e31 commit cf7d956
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions sentry-tower/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
11 changes: 10 additions & 1 deletion sentry-tower/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -172,6 +172,15 @@ where
}
}

fn path_from_request<B>(request: &Request<B>) -> &str {
#[cfg(feature = "axum-matched-path")]
if let Some(matched_path) = request.extensions().get::<axum::extract::MatchedPath>() {
return matched_path.as_str();
}

request.uri().path()
}

fn map_status(status: StatusCode) -> protocol::SpanStatus {
match status {
StatusCode::UNAUTHORIZED => protocol::SpanStatus::Unauthenticated,
Expand Down

0 comments on commit cf7d956

Please sign in to comment.