-
Notifications
You must be signed in to change notification settings - Fork 3
feat: client-side sampleRate to Android session replay #660
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
abelonogov-ld
merged 3 commits into
main
from
andrey/android-session-replay-sample-rate
Jun 30, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
39 changes: 39 additions & 0 deletions
39
...ndroid/lib/src/main/kotlin/com/launchdarkly/observability/replay/SessionReplaySampling.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package com.launchdarkly.observability.replay | ||
|
|
||
| import kotlin.random.Random | ||
|
|
||
| internal object SessionReplaySampling { | ||
| fun shouldSample( | ||
| sampleRate: Double, | ||
| randomValue: () -> Double = { Random.nextDouble() }, | ||
| ): Boolean { | ||
| if (sampleRate <= 0.0) return false | ||
| if (sampleRate >= 1.0) return true | ||
| return randomValue() < sampleRate | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Tracks whether sampling has been decided for the current enable cycle. | ||
| * Mirrors `SessionReplaySamplingSession` in the Swift session replay SDK. | ||
| */ | ||
| internal class SessionReplaySamplingSession { | ||
| private var decisionMade = false | ||
|
|
||
| @Synchronized | ||
| fun shouldStartCapture( | ||
| ignoreSampling: Boolean, | ||
| sampleRate: Double, | ||
| randomValue: () -> Double = { Random.nextDouble() }, | ||
| ): Boolean { | ||
| if (ignoreSampling) return true | ||
| if (decisionMade) return false | ||
| decisionMade = true | ||
| return SessionReplaySampling.shouldSample(sampleRate, randomValue) | ||
| } | ||
|
|
||
| @Synchronized | ||
| fun reset() { | ||
| decisionMade = false | ||
| } | ||
| } |
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
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
72 changes: 72 additions & 0 deletions
72
...id/lib/src/test/kotlin/com/launchdarkly/observability/replay/SessionReplaySamplingTest.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package com.launchdarkly.observability.replay | ||
|
|
||
| import org.junit.jupiter.api.Assertions.assertEquals | ||
| import org.junit.jupiter.api.Assertions.assertFalse | ||
| import org.junit.jupiter.api.Assertions.assertTrue | ||
| import org.junit.jupiter.api.Test | ||
|
|
||
| class SessionReplaySamplingTest { | ||
|
|
||
| @Test | ||
| fun `sampleRate defaults to always sample`() { | ||
| assertEquals(1.0, ReplayOptions().sampleRate) | ||
| assertTrue(SessionReplaySampling.shouldSample(sampleRate = 1.0) { 0.99 }) | ||
| } | ||
|
|
||
| @Test | ||
| fun `sampleRate zero disables session replay`() { | ||
| assertFalse(SessionReplaySampling.shouldSample(sampleRate = 0.0) { 0.0 }) | ||
| } | ||
|
|
||
| @Test | ||
| fun `sampleRate samples when random value is below rate`() { | ||
| assertTrue(SessionReplaySampling.shouldSample(sampleRate = 0.5) { 0.49 }) | ||
| assertFalse(SessionReplaySampling.shouldSample(sampleRate = 0.5) { 0.5 }) | ||
| } | ||
|
|
||
| @Test | ||
| fun `sampling decision is not re-evaluated after sampled out`() { | ||
| val session = SessionReplaySamplingSession() | ||
| assertFalse( | ||
| session.shouldStartCapture( | ||
| ignoreSampling = false, | ||
| sampleRate = 0.25, | ||
| randomValue = { 0.99 }, | ||
| ) | ||
| ) | ||
| assertFalse( | ||
| session.shouldStartCapture( | ||
| ignoreSampling = false, | ||
| sampleRate = 0.25, | ||
| randomValue = { 0.0 }, | ||
| ) | ||
| ) | ||
| session.reset() | ||
| assertTrue( | ||
| session.shouldStartCapture( | ||
| ignoreSampling = false, | ||
| sampleRate = 0.25, | ||
| randomValue = { 0.0 }, | ||
| ) | ||
| ) | ||
| } | ||
|
|
||
| @Test | ||
| fun `ignoreSampling bypasses persisted sampled-out decision`() { | ||
| val session = SessionReplaySamplingSession() | ||
| assertFalse( | ||
| session.shouldStartCapture( | ||
| ignoreSampling = false, | ||
| sampleRate = 0.25, | ||
| randomValue = { 0.99 }, | ||
| ) | ||
| ) | ||
| assertTrue( | ||
| session.shouldStartCapture( | ||
| ignoreSampling = true, | ||
| sampleRate = 0.25, | ||
| randomValue = { 0.99 }, | ||
| ) | ||
| ) | ||
| } | ||
| } |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sampled-out replay drops identify
Medium Severity
When replay is enabled but excluded by
sampleRate,identifySessionreturns immediately without caching the payload. That differs from the disabled path, which stores pending identify for the next start. After a later stop/start that wins sampling, recording can begin without the user context that was already identified.Reviewed by Cursor Bugbot for commit 9e399f7. Configure here.