Skip to content

Commit b79aabf

Browse files
markushicarlonzoclaudesentry-junior[bot]
authored
fix(okhttp): Capture network details only while Session Replay is recording (#6486)
* fix(okhttp): Don't block on response bodies of unknown length 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> * changelog: reference the pull request * fix(okhttp): Satisfy detekt for the streaming body capture 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. * refactor(okhttp): Drop unnecessary locking from the capturing source 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. * fix: Publish late response details safely 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. * test: Cover late-filled network request data * fix(okhttp): Harden the capture of response bodies with no known length * test(okhttp): Cover async, cancelled and HTTP/2 bodies of unknown length * ref: Publish both sides of NetworkRequestData the same way * fix(okhttp): Keep the bytes received before a stream breaks * ref: Read the response details of a network breadcrumb as one snapshot * ref(okhttp): Capture every response body through one path * changelog * test: Silence R8 for the okhttp types the new response body extends * ref(okhttp): Rename NetworkBodyCapturingResponseBody to CapturedResponseBody * ref(okhttp): Capture only the response bytes the application reads * test(okhttp): Wait for the capture, not for the source handing bytes over * ref: Keep the status code inside the response details * ref(okhttp): Restore the original response flow and rethrow fatal capture errors * ref(okhttp): Drop comments that restate the code * ref: Drop the constructor comment on ReplayNetworkRequestOrResponse * Address PR comments * address PR comments * test(replay): Pin the response body a late capture leaves out of the replay * fix(okhttp): Capture network details only while Session Replay is recording * changelog --------- Co-authored-by: Carlo Marinangeli <carlo.marinangeli@gmail.com> Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Co-authored-by: sentry-junior[bot] <264270552+sentry-junior[bot]@users.noreply.github.com>
1 parent 3d1f55e commit b79aabf

4 files changed

Lines changed: 39 additions & 6 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
### Fixes
2323

2424
- Session Replay: Fix `SentryOkHttpInterceptor` hanging forever on responses whose body has no known length, such as Server-Sent Events or a gzipped response, when network details are enabled for the URL ([#6231](https://github.com/getsentry/sentry-java/pull/6231))
25+
- Session Replay: `SentryOkHttpInterceptor` now captures network request and response details only while Session Replay is recording or buffering ([#6486](https://github.com/getsentry/sentry-java/pull/6486))
2526
- Stop marking an OkHttp call span as failed when a connection attempt to one of the host's addresses fails but OkHttp connects on another ([#6472](https://github.com/getsentry/sentry-java/pull/6472))
2627

2728
### Dependencies

‎sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,19 @@ public open class SentryOkHttpInterceptor(
117117
var response: Response? = null
118118
var code: Int? = null
119119

120+
// Network details only end up in a replay, so capturing them while no replay is recording or
121+
// buffering is wasted work.
120122
val networkDetailData =
121-
NetworkDetailCaptureUtils.initializeForUrl(
122-
request.url.toString(),
123-
request.method,
124-
scopes.options.sessionReplay.networkDetailAllowUrls,
125-
scopes.options.sessionReplay.networkDetailDenyUrls,
126-
)
123+
if (scopes.options.replayController.isRecording) {
124+
NetworkDetailCaptureUtils.initializeForUrl(
125+
request.url.toString(),
126+
request.method,
127+
scopes.options.sessionReplay.networkDetailAllowUrls,
128+
scopes.options.sessionReplay.networkDetailDenyUrls,
129+
)
130+
} else {
131+
null
132+
}
127133

128134
try {
129135
val requestBuilder = request.newBuilder()

‎sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorTest.kt‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import org.mockito.kotlin.anyOrNull
5757
import org.mockito.kotlin.argumentCaptor
5858
import org.mockito.kotlin.check
5959
import org.mockito.kotlin.doAnswer
60+
import org.mockito.kotlin.doReturn
6061
import org.mockito.kotlin.mock
6162
import org.mockito.kotlin.never
6263
import org.mockito.kotlin.verify
@@ -90,10 +91,12 @@ class SentryOkHttpInterceptorTest {
9091
sendDefaultPii: Boolean = false,
9192
eventListener: EventListener? = null,
9293
additionalInterceptors: List<Interceptor> = emptyList(),
94+
isReplayRecording: Boolean = true,
9395
optionsConfiguration: Sentry.OptionsConfiguration<SentryOptions>? = null,
9496
): OkHttpClient {
9597
options =
9698
SentryOptions().also {
99+
it.setReplayController(mock { on { isRecording } doReturn isReplayRecording })
97100
optionsConfiguration?.configure(it)
98101
it.dsn = "https://key@sentry.io/proj"
99102
if (includeMockServerInTracePropagationTargets) {
@@ -381,6 +384,27 @@ class SentryOkHttpInterceptorTest {
381384
assertNotNull(networkDetails.response)
382385
}
383386

387+
@Test
388+
fun `does not capture Session Replay network details when replay is not recording`() {
389+
val sut =
390+
fixture.getSut(
391+
isReplayRecording = false,
392+
optionsConfiguration = { it.sessionReplay.setNetworkDetailAllowUrls(listOf(".*")) },
393+
)
394+
395+
sut.newCall(postRequest()).execute().close()
396+
397+
val hint = argumentCaptor<Hint>()
398+
verify(fixture.scopes).addBreadcrumb(any(), hint.capture())
399+
assertThat(
400+
hint.firstValue.getAs(
401+
TypeCheckHint.SENTRY_REPLAY_NETWORK_DETAILS,
402+
NetworkRequestData::class.java,
403+
)
404+
)
405+
.isNull()
406+
}
407+
384408
@SuppressWarnings("SwallowedException")
385409
@Test
386410
fun `adds breadcrumb when http calls results in exception`() {

‎sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorUnknownContentLengthTest.kt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import okio.GzipSink
3737
import okio.buffer
3838
import org.mockito.kotlin.any
3939
import org.mockito.kotlin.argumentCaptor
40+
import org.mockito.kotlin.doReturn
4041
import org.mockito.kotlin.mock
4142
import org.mockito.kotlin.verify
4243
import org.mockito.kotlin.whenever
@@ -59,6 +60,7 @@ class SentryOkHttpInterceptorUnknownContentLengthTest {
5960
options =
6061
SentryOptions().apply {
6162
dsn = "https://key@sentry.io/proj"
63+
setReplayController(mock { on { isRecording } doReturn true })
6264
sessionReplay.setNetworkDetailAllowUrls(listOf(".*"))
6365
sessionReplay.setNetworkCaptureBodies(captureBodies)
6466
}

0 commit comments

Comments
 (0)