Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix sentry-tower URL generation #460

Merged
merged 2 commits into from May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion sentry-tower/Cargo.toml
Expand Up @@ -12,14 +12,15 @@ Sentry integration for tower-based crates.
edition = "2018"

[features]
http = ["http_", "pin-project"]
http = ["http_", "pin-project", "url"]

[dependencies]
tower-layer = "0.3"
tower-service = "0.3"
http_ = { package = "http", version = "0.2.6", optional = true }
pin-project = { version = "1.0.10", optional = true }
sentry-core = { version = "0.25.0", path = "../sentry-core", default-features = false, features = ["client"] }
url = { version = "2.2.2", optional = true }

[dev-dependencies]
anyhow = "1"
Expand Down
17 changes: 15 additions & 2 deletions sentry-tower/src/http.rs
@@ -1,8 +1,9 @@
use std::convert::TryInto;
use std::future::Future;
use std::pin::Pin;
use std::task::{Context, Poll};

use http_::{Request, Response, StatusCode};
use http_::{header, uri, Request, Response, StatusCode};
use sentry_core::protocol;
use tower_layer::Layer;
use tower_service::Service;
Expand Down Expand Up @@ -136,7 +137,7 @@ where
fn call(&mut self, request: Request<ReqBody>) -> Self::Future {
let sentry_req = sentry_core::protocol::Request {
method: Some(request.method().to_string()),
url: request.uri().to_string().parse().ok(),
url: get_url_from_request(&request),
headers: request
.headers()
.into_iter()
Expand Down Expand Up @@ -186,3 +187,15 @@ fn map_status(status: StatusCode) -> protocol::SpanStatus {
_ => protocol::SpanStatus::UnknownError,
}
}

fn get_url_from_request<B>(request: &Request<B>) -> Option<url::Url> {
let uri = request.uri().clone();
let mut uri_parts = uri.into_parts();
uri_parts.scheme.get_or_insert(uri::Scheme::HTTP);
if uri_parts.authority.is_none() {
let host = request.headers().get(header::HOST)?.as_bytes();
uri_parts.authority = Some(host.try_into().ok()?);
}
let uri = uri::Uri::from_parts(uri_parts).ok()?;
uri.to_string().parse().ok()
}