Skip to content

Commit

Permalink
fix(http): Finish transaction on drop
Browse files Browse the repository at this point in the history
  • Loading branch information
Dav1dde committed Jan 28, 2025
1 parent 135f2be commit 36f885c
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion sentry-tower/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ 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 sentry_core::types::protocol;
use tower_layer::Layer;
use tower_service::Service;

Expand Down Expand Up @@ -62,7 +64,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 +125,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

0 comments on commit 36f885c

Please sign in to comment.