fix(sentry): Bind hub to response bodies via middleware#526
Open
jan-auer wants to merge 2 commits into
Open
Conversation
Axum stream bodies are polled by hyper after the middleware stack unwinds, so Hub::current() inside a stream producer resolves to the tokio worker thread's ambient hub rather than the request's hub. Introduce SentryStream<S>, a pin-projected stream wrapper that re-activates a captured Arc<Hub> via HubSwitchGuard on every poll_next call, mirroring how SentryFuture works for futures. Bind the hub at stream construction time in the two affected endpoints: complete (multipart) and batch. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Replace per-endpoint SentryStream wrapping with a centralized middleware that operates at the http_body::Body level instead of the Stream level, preserving size hints for buffered responses.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #526 +/- ##
==========================================
+ Coverage 87.35% 87.38% +0.02%
==========================================
Files 87 88 +1
Lines 14086 14111 +25
==========================================
+ Hits 12305 12331 +26
+ Misses 1781 1780 -1
☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
When an axum handler returns a streaming body via
Body::from_stream(), theIntoResponseconversion runs inside the handler (within the sentry middleware), but the body's frames are polled by hyper after middleware has unwound. This meansHub::current()inside a stream producer resolves to whatever hub the tokio worker thread happens to have — not the request's hub.Three endpoints are affected:
object_get,complete(multipart), andbatch.Instead of wrapping individual streams per-endpoint, this PR introduces a middleware that wraps every response body in a
SentryBodythat re-activates the request's hub on eachpoll_framecall. This still allows for plain bodies to provide acontent-length. It effectively double-boxes every body, but the overhead of the indirection is negligible.Alternative to #524.