-
-
Notifications
You must be signed in to change notification settings - Fork 167
feat: Add a Http Service integration with trace propagation (NATIVE-314) #397
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6a479ff
feat: Add a Http Service integration with trace propagation (NATIVE-314)
Swatinem 80ed382
make the tower-http code actually work
Swatinem ee4ac46
use a proper request obj and an event processor
Swatinem 648c4f4
rm unused imports
Swatinem e3d9755
add a transaction name
Swatinem 8c79a32
improve wording
Swatinem File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
use std::future::Future; | ||
use std::pin::Pin; | ||
use std::task::{Context, Poll}; | ||
|
||
use http_::Request; | ||
use tower_layer::Layer; | ||
use tower_service::Service; | ||
|
||
/// Tower Layer that logs Http Request Headers. | ||
/// | ||
/// The Service created by this Layer can also optionally start a new | ||
/// performance monitoring transaction for each incoming request, | ||
/// continuing the trace based on incoming distributed tracing headers. | ||
#[derive(Clone, Default)] | ||
pub struct SentryHttpLayer { | ||
start_transaction: bool, | ||
} | ||
|
||
impl SentryHttpLayer { | ||
/// Creates a new Layer that only logs Request Headers. | ||
pub fn new() -> Self { | ||
Self::default() | ||
} | ||
|
||
/// Creates a new Layer which starts a new performance monitoring transaction | ||
/// for each incoming request. | ||
pub fn with_transaction() -> Self { | ||
Self { | ||
start_transaction: true, | ||
} | ||
} | ||
} | ||
|
||
/// Tower Service that logs Http Request Headers. | ||
/// | ||
/// The Service can also optionally start a new performance monitoring transaction | ||
/// for each incoming request, continuing the trace based on incoming | ||
/// distributed tracing headers. | ||
#[derive(Clone)] | ||
pub struct SentryHttpService<S> { | ||
service: S, | ||
start_transaction: bool, | ||
} | ||
|
||
impl<S> Layer<S> for SentryHttpLayer { | ||
type Service = SentryHttpService<S>; | ||
|
||
fn layer(&self, service: S) -> Self::Service { | ||
Self::Service { | ||
service, | ||
start_transaction: self.start_transaction, | ||
} | ||
} | ||
} | ||
|
||
/// The Future returned from [`SentryHttpService`]. | ||
#[pin_project::pin_project] | ||
pub struct SentryHttpFuture<F> { | ||
transaction: Option<( | ||
sentry_core::TransactionOrSpan, | ||
Option<sentry_core::TransactionOrSpan>, | ||
)>, | ||
#[pin] | ||
future: F, | ||
} | ||
|
||
impl<F> Future for SentryHttpFuture<F> | ||
where | ||
F: Future, | ||
{ | ||
type Output = F::Output; | ||
|
||
fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> { | ||
let slf = self.project(); | ||
match slf.future.poll(cx) { | ||
Poll::Ready(res) => { | ||
if let Some((transaction, parent_span)) = slf.transaction.take() { | ||
transaction.finish(); | ||
sentry_core::configure_scope(|scope| scope.set_span(parent_span)); | ||
} | ||
Poll::Ready(res) | ||
} | ||
Poll::Pending => Poll::Pending, | ||
} | ||
} | ||
} | ||
|
||
impl<S, Body> Service<Request<Body>> for SentryHttpService<S> | ||
where | ||
S: Service<Request<Body>>, | ||
{ | ||
type Response = S::Response; | ||
type Error = S::Error; | ||
type Future = SentryHttpFuture<S::Future>; | ||
|
||
fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> { | ||
self.service.poll_ready(cx) | ||
} | ||
|
||
fn call(&mut self, request: Request<Body>) -> Self::Future { | ||
let transaction = sentry_core::configure_scope(|scope| { | ||
let sentry_req = sentry_core::protocol::Request { | ||
method: Some(request.method().to_string()), | ||
url: request.uri().to_string().parse().ok(), | ||
headers: request | ||
.headers() | ||
.into_iter() | ||
.map(|(header, value)| { | ||
( | ||
header.to_string(), | ||
value.to_str().unwrap_or_default().into(), | ||
) | ||
}) | ||
.collect(), | ||
Swatinem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
..Default::default() | ||
}; | ||
scope.add_event_processor(move |mut event| { | ||
if event.request.is_none() { | ||
event.request = Some(sentry_req.clone()); | ||
} | ||
Some(event) | ||
}); | ||
|
||
if self.start_transaction { | ||
let headers = request.headers().into_iter().flat_map(|(header, value)| { | ||
value.to_str().ok().map(|value| (header.as_str(), value)) | ||
}); | ||
Swatinem marked this conversation as resolved.
Show resolved
Hide resolved
|
||
let tx_name = format!("{} {}", request.method(), request.uri().path()); | ||
let tx_ctx = sentry_core::TransactionContext::continue_from_headers( | ||
&tx_name, | ||
"http.server", | ||
headers, | ||
); | ||
let transaction: sentry_core::TransactionOrSpan = | ||
sentry_core::start_transaction(tx_ctx).into(); | ||
let parent_span = scope.get_span(); | ||
scope.set_span(Some(transaction.clone())); | ||
Some((transaction, parent_span)) | ||
} else { | ||
None | ||
} | ||
}); | ||
|
||
SentryHttpFuture { | ||
transaction, | ||
future: self.service.call(request), | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.