diff --git a/CHANGELOG.md b/CHANGELOG.md index 01fb497ebb..3f04617ce7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,7 @@ ### Fixes - 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)) +- 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)) - 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)) ### Dependencies diff --git a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt index a10e462453..bdd680ac66 100644 --- a/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt +++ b/sentry-okhttp/src/main/java/io/sentry/okhttp/SentryOkHttpInterceptor.kt @@ -117,13 +117,19 @@ public open class SentryOkHttpInterceptor( var response: Response? = null var code: Int? = null + // Network details only end up in a replay, so capturing them while no replay is recording or + // buffering is wasted work. val networkDetailData = - NetworkDetailCaptureUtils.initializeForUrl( - request.url.toString(), - request.method, - scopes.options.sessionReplay.networkDetailAllowUrls, - scopes.options.sessionReplay.networkDetailDenyUrls, - ) + if (scopes.options.replayController.isRecording) { + NetworkDetailCaptureUtils.initializeForUrl( + request.url.toString(), + request.method, + scopes.options.sessionReplay.networkDetailAllowUrls, + scopes.options.sessionReplay.networkDetailDenyUrls, + ) + } else { + null + } try { val requestBuilder = request.newBuilder() diff --git a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorTest.kt b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorTest.kt index d6c4c56c37..baa37a7716 100644 --- a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorTest.kt +++ b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorTest.kt @@ -57,6 +57,7 @@ import org.mockito.kotlin.anyOrNull import org.mockito.kotlin.argumentCaptor import org.mockito.kotlin.check import org.mockito.kotlin.doAnswer +import org.mockito.kotlin.doReturn import org.mockito.kotlin.mock import org.mockito.kotlin.never import org.mockito.kotlin.verify @@ -90,10 +91,12 @@ class SentryOkHttpInterceptorTest { sendDefaultPii: Boolean = false, eventListener: EventListener? = null, additionalInterceptors: List = emptyList(), + isReplayRecording: Boolean = true, optionsConfiguration: Sentry.OptionsConfiguration? = null, ): OkHttpClient { options = SentryOptions().also { + it.setReplayController(mock { on { isRecording } doReturn isReplayRecording }) optionsConfiguration?.configure(it) it.dsn = "https://key@sentry.io/proj" if (includeMockServerInTracePropagationTargets) { @@ -381,6 +384,27 @@ class SentryOkHttpInterceptorTest { assertNotNull(networkDetails.response) } + @Test + fun `does not capture Session Replay network details when replay is not recording`() { + val sut = + fixture.getSut( + isReplayRecording = false, + optionsConfiguration = { it.sessionReplay.setNetworkDetailAllowUrls(listOf(".*")) }, + ) + + sut.newCall(postRequest()).execute().close() + + val hint = argumentCaptor() + verify(fixture.scopes).addBreadcrumb(any(), hint.capture()) + assertThat( + hint.firstValue.getAs( + TypeCheckHint.SENTRY_REPLAY_NETWORK_DETAILS, + NetworkRequestData::class.java, + ) + ) + .isNull() + } + @SuppressWarnings("SwallowedException") @Test fun `adds breadcrumb when http calls results in exception`() { diff --git a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorUnknownContentLengthTest.kt b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorUnknownContentLengthTest.kt index 810f780cda..b6257bfc50 100644 --- a/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorUnknownContentLengthTest.kt +++ b/sentry-okhttp/src/test/java/io/sentry/okhttp/SentryOkHttpInterceptorUnknownContentLengthTest.kt @@ -37,6 +37,7 @@ import okio.GzipSink import okio.buffer import org.mockito.kotlin.any import org.mockito.kotlin.argumentCaptor +import org.mockito.kotlin.doReturn import org.mockito.kotlin.mock import org.mockito.kotlin.verify import org.mockito.kotlin.whenever @@ -59,6 +60,7 @@ class SentryOkHttpInterceptorUnknownContentLengthTest { options = SentryOptions().apply { dsn = "https://key@sentry.io/proj" + setReplayController(mock { on { isRecording } doReturn true }) sessionReplay.setNetworkDetailAllowUrls(listOf(".*")) sessionReplay.setNetworkCaptureBodies(captureBodies) }