Skip to content

Commit f9062f5

Browse files
romtsncodex
andcommitted
fix(replay): Bypass sampling for manual buffers
Track whether a buffered replay was started automatically so only automatic buffers apply per-error sampling. Manually started buffers now capture on errors as documented. Refs JAVA-325 Co-Authored-By: Codex <noreply@openai.com>
1 parent 4ce579a commit f9062f5

2 files changed

Lines changed: 36 additions & 8 deletions

File tree

‎sentry-android-replay/src/main/java/io/sentry/android/replay/ReplayIntegration.kt‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,11 @@ public class ReplayIntegration(
159159
override fun isRecording(): Boolean = state.get().isRecording
160160

161161
override fun start() {
162-
enqueueOnMainThread { startInternal(isFullSession = true) }
162+
enqueueOnMainThread { startInternal(isFullSession = true, shouldSampleOnError = false) }
163163
}
164164

165165
override fun startBuffering() {
166-
enqueueOnMainThread { startInternal(isFullSession = false) }
166+
enqueueOnMainThread { startInternal(isFullSession = false, shouldSampleOnError = false) }
167167
}
168168

169169
override fun onAppForegrounded(startNewSession: Boolean) {
@@ -179,7 +179,7 @@ public class ReplayIntegration(
179179
"Session replay is not started, full session was not sampled and onErrorSampleRate is not specified",
180180
)
181181
} else {
182-
startInternal(isFullSession)
182+
startInternal(isFullSession, shouldSampleOnError = !isFullSession)
183183
}
184184
}
185185
resumeInternal()
@@ -190,7 +190,7 @@ public class ReplayIntegration(
190190
enqueueOnMainThread { pauseInternal() }
191191
}
192192

193-
private fun startInternal(isFullSession: Boolean) {
193+
private fun startInternal(isFullSession: Boolean, shouldSampleOnError: Boolean) {
194194
if (!isEnabled.get()) {
195195
return
196196
}
@@ -235,6 +235,7 @@ public class ReplayIntegration(
235235
lifecycleState = STARTED,
236236
replayId = replayId ?: SentryId.EMPTY_ID,
237237
captureStrategy = strategy,
238+
shouldSampleOnError = shouldSampleOnError,
238239
)
239240
)
240241

@@ -279,7 +280,11 @@ public class ReplayIntegration(
279280
return SentryId.EMPTY_ID
280281
}
281282

282-
if (current.isBuffering && !sample(options.sessionReplay.onErrorSampleRate)) {
283+
if (
284+
current.isBuffering &&
285+
current.shouldSampleOnError &&
286+
!sample(options.sessionReplay.onErrorSampleRate)
287+
) {
283288
options.logger.log(
284289
INFO,
285290
"Replay wasn't sampled by onErrorSampleRate, not capturing for event",
@@ -355,7 +360,7 @@ public class ReplayIntegration(
355360
enqueueOnMainThread {
356361
val current = state.get()
357362
if (!current.isRecording) {
358-
startInternal(isFullSession = true)
363+
startInternal(isFullSession = true, shouldSampleOnError = false)
359364
} else {
360365
captureReplayInternal(current.generation, current.replayId, false)
361366
}
@@ -729,6 +734,7 @@ public class ReplayIntegration(
729734
val lifecycleState: ReplayLifecycleState = ReplayLifecycleState.INITIAL,
730735
val replayId: SentryId = SentryId.EMPTY_ID,
731736
val captureStrategy: CaptureStrategy? = null,
737+
val shouldSampleOnError: Boolean = false,
732738
) {
733739
val isBuffering: Boolean
734740
get() = captureStrategy is BufferCaptureStrategy

‎sentry-android-replay/src/test/java/io/sentry/android/replay/ReplayIntegrationTest.kt‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,16 +576,38 @@ class ReplayIntegrationTest {
576576
fixture.getSut(
577577
context,
578578
sessionSampleRate = 0.0,
579-
onErrorSampleRate = 0.0,
579+
onErrorSampleRate = 1.0,
580580
replayCaptureStrategyProvider = { captureStrategy },
581581
)
582582
replay.register(fixture.scopes, fixture.options)
583-
replay.start()
583+
replay.onAppForegrounded(true)
584+
fixture.options.sessionReplay.onErrorSampleRate = 0.0
584585

585586
assertThat(replay.captureReplay(false)).isEqualTo(SentryId.EMPTY_ID)
586587
verify(captureStrategy, never()).captureReplay(any(), any())
587588
}
588589

590+
@Test
591+
fun `manual buffer capture bypasses error sampling`() {
592+
val replayId = SentryId()
593+
val captureStrategy = mock<BufferCaptureStrategy>()
594+
whenever(captureStrategy.currentReplayId).thenReturn(replayId)
595+
whenever(captureStrategy.convert()).thenReturn(captureStrategy)
596+
val replay =
597+
fixture.getSut(
598+
context,
599+
sessionSampleRate = 0.0,
600+
onErrorSampleRate = 0.0,
601+
replayCaptureStrategyProvider = { captureStrategy },
602+
)
603+
replay.register(fixture.scopes, fixture.options)
604+
replay.startBuffering()
605+
606+
assertThat(replay.captureReplay(false)).isEqualTo(replayId)
607+
verify(captureStrategy).captureReplay(eq(false), any())
608+
verify(captureStrategy).convert()
609+
}
610+
589611
@Test
590612
fun `capture queued after stop cannot resurrect replay`() {
591613
val replayId = SentryId()

0 commit comments

Comments
 (0)