Repository navigation
fix(okhttp): Capture network details only while Session Replay is recording - #6486
Merged
Merged
Conversation
Capturing a response body for session replay peeked it up front with `Response.peekBody(MAX_NETWORK_BODY_SIZE + 1)`. OkHttp implements a peek as `request(byteCount)`, which keeps reading until that many bytes are buffered or the stream ends. A response with no Content-Length never satisfies either condition, so for server-sent events, long-poll and any chunked endpoint that stays open the interceptor never returned and the caller never received the response at all. Bodies with a known length still end on their own, so they keep the existing up-front capture. Bodies of unknown length are now wrapped in a body that copies what the application consumes into a capped buffer and reports it once no more bytes can arrive: when the stream ends, when the application closes the body, or when the cap is reached. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Split the capture wrapping into its own function to keep a single return, and suppress TooManyFunctions on the interceptor as done elsewhere in this module.
A response body has a single consumer, and neither Http1ExchangeCodec.cancel() nor Http2ExchangeCodec.cancel() closes the body, so nothing reaches this class from another thread. The AtomicBoolean already guarantees the capture is reported exactly once.
A streamed body is only known once it has been consumed, which can be after the NetworkRequestData carrying it was handed to the scope, so the replay thread can read it while it is still being written. Keeping the three response values behind one volatile reference means a reader sees either nothing or the complete set.
…shepherd] Co-Authored-By: Markus Hintersteiner <markus.hintersteiner@sentry.io>
…shepherd] # Conflicts: # CHANGELOG.md Co-Authored-By: Markus Hintersteiner <markus.hintersteiner@sentry.io>
…getsentry/sentry-java into fix/okhttp-unknown-length-response-body
📲 Install BuildsAndroid
|
romtsn
approved these changes
Nov 6, 2026
markushi
marked this pull request as ready for review
November 6, 2026 08:34
markushi
requested review from
0xadam-brown,
adinauer and
runningcode
as code owners
November 6, 2026 08:34
Base automatically changed from
fix/okhttp-unknown-length-response-body
to
main
November 6, 2026 11:45
# Conflicts: # sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorUnknownContentLengthTest.kt
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.
Follow-up to #6231. This PR targets that branch and must merge after it.
📜 Description
SentryOkHttpInterceptornow captures Session Replay network details (request and response bodies and headers) only while a replay is recording or buffering, so only whenoptions.replayController.isRecordingis true. When no replay is active, the interceptor does not buffer the request body and does not wrap the response body. Spans, breadcrumbs, and failed-request events do not change.The gate covers headers as well as bodies. Like the bodies, the headers are stored in the
SENTRY_REPLAY_NETWORK_DETAILShint, and onlyDefaultReplayBreadcrumbConverterreads them.💡 Motivation and Context
Before this change, the SDK captured bodies for allowlisted URLs even when Session Replay was off. This work had no use, because only replay reads those bodies. #6231 fixes the SSE / unknown-length blocking. This PR removes the remaining overhead when replay is not active.
💚 How did you test it?
./gradlew :sentry-okhttp:test📝 Checklist
sendDefaultPIIis enabled.🔮 Next steps
🤖 Generated with Claude Code