Skip to content

fix(http): Finish transaction on drop #727

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 1 commit into from
Jan 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion sentry-tower/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::pin::Pin;
use std::task::{Context, Poll};

use http::{header, uri, Request, Response, StatusCode};
use pin_project::pinned_drop;
use sentry_core::protocol;
use tower_layer::Layer;
use tower_service::Service;
Expand Down Expand Up @@ -62,7 +63,7 @@ impl<S> Layer<S> for SentryHttpLayer {
}

/// The Future returned from [`SentryHttpService`].
#[pin_project::pin_project]
#[pin_project::pin_project(PinnedDrop)]
pub struct SentryHttpFuture<F> {
on_first_poll: Option<(
sentry_core::protocol::Request,
Expand Down Expand Up @@ -123,6 +124,23 @@ where
}
}

#[pinned_drop]
impl<F> PinnedDrop for SentryHttpFuture<F> {
fn drop(self: Pin<&mut Self>) {
let slf = self.project();

// If the future gets dropped without being polled to completion,
// still finish the transaction to make sure this is not lost.
if let Some((transaction, parent_span)) = slf.transaction.take() {
if transaction.get_status().is_none() {
transaction.set_status(protocol::SpanStatus::Aborted);
}
transaction.finish();
sentry_core::configure_scope(|scope| scope.set_span(parent_span));
}
}
}

impl<S, ReqBody, ResBody> Service<Request<ReqBody>> for SentryHttpService<S>
where
S: Service<Request<ReqBody>, Response = Response<ResBody>>,
Expand Down