-
Notifications
You must be signed in to change notification settings - Fork 0
feat: session replay control with sampling and state management #200
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
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6dfd692
enhance session replay control with sampling and state management
abelonogov-ld a7cdcaa
update readmy
abelonogov-ld 46e6e21
fix isEnabled
abelonogov-ld 9caea5c
remeber decision
abelonogov-ld 9d48b9b
Merge branch 'main' into andrey/new-sr-options
abelonogov-ld 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
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
19 changes: 19 additions & 0 deletions
19
Sources/LaunchDarklySessionReplay/API/SessionReplayStartResult.swift
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,19 @@ | ||
| public enum SessionReplayStartResult: Equatable { | ||
| /// Session Replay was not installed or has not finished registering. | ||
| case unavailable | ||
| /// Session Replay is now running because this call started it. | ||
| case started | ||
| /// Session Replay was already running before this call. | ||
| case alreadyStarted | ||
| /// Session Replay stayed stopped because the session was sampled out. | ||
| case sampledOut | ||
|
|
||
| public var isRunning: Bool { | ||
| switch self { | ||
| case .started, .alreadyStarted: | ||
| return true | ||
| case .unavailable, .sampledOut: | ||
| return 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
35 changes: 35 additions & 0 deletions
35
Sources/LaunchDarklySessionReplay/SessionReplaySampling.swift
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,35 @@ | ||
| import Foundation | ||
|
|
||
| enum SessionReplaySampling { | ||
| static func shouldSample(sampleRate: Double, randomValue: () -> Double = { Double.random(in: 0..<1) }) -> Bool { | ||
| guard sampleRate > 0 else { return false } | ||
| guard sampleRate < 1 else { return true } | ||
|
|
||
| return randomValue() < sampleRate | ||
| } | ||
| } | ||
|
|
||
| /// Tracks whether sampling has been decided for the current enable cycle. | ||
| internal struct SessionReplaySamplingSession { | ||
| private(set) var decisionMade = false | ||
|
|
||
| /// Returns `true` when capture should start; `false` when the session stays sampled out. | ||
| mutating func shouldStartCapture( | ||
| ignoreSampling: Bool, | ||
| sampleRate: Double, | ||
| randomValue: () -> Double = { Double.random(in: 0..<1) } | ||
| ) -> Bool { | ||
| if ignoreSampling { | ||
| return true | ||
| } | ||
| if decisionMade { | ||
| return false | ||
| } | ||
| decisionMade = true | ||
| return SessionReplaySampling.shouldSample(sampleRate: sampleRate, randomValue: randomValue) | ||
| } | ||
|
|
||
| mutating func 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import Testing | ||
| @testable import LaunchDarklySessionReplay | ||
|
|
||
| struct SessionReplaySamplingTests { | ||
| @Test("sampleRate defaults to always sample") | ||
| func sampleRateDefaultsToAlwaysSample() { | ||
| #expect(SessionReplayOptions().sampleRate == 1.0) | ||
| #expect(SessionReplaySampling.shouldSample(sampleRate: 1.0, randomValue: { 0.99 })) | ||
| } | ||
|
|
||
| @Test("sampleRate zero disables session replay") | ||
| func sampleRateZeroDisablesSessionReplay() { | ||
| #expect(SessionReplaySampling.shouldSample(sampleRate: 0.0, randomValue: { 0.0 }) == false) | ||
| } | ||
|
|
||
| @Test("sampleRate samples when random value is below rate") | ||
| func sampleRateSamplesBelowRate() { | ||
| #expect(SessionReplaySampling.shouldSample(sampleRate: 0.5, randomValue: { 0.49 })) | ||
| #expect(SessionReplaySampling.shouldSample(sampleRate: 0.5, randomValue: { 0.5 }) == false) | ||
| } | ||
|
|
||
| @Test("start result indicates whether session replay is running") | ||
| func startResultIndicatesRunningState() { | ||
| #expect(SessionReplayStartResult.started.isRunning) | ||
| #expect(SessionReplayStartResult.alreadyStarted.isRunning) | ||
| #expect(SessionReplayStartResult.sampledOut.isRunning == false) | ||
| #expect(SessionReplayStartResult.unavailable.isRunning == false) | ||
| } | ||
|
|
||
| @Test("sampling decision is not re-evaluated after sampled out") | ||
| func samplingDecisionIsPersistedUntilReset() { | ||
| var session = SessionReplaySamplingSession() | ||
| #expect(session.shouldStartCapture(ignoreSampling: false, sampleRate: 0.25, randomValue: { 0.99 }) == false) | ||
| #expect(session.shouldStartCapture(ignoreSampling: false, sampleRate: 0.25, randomValue: { 0.0 }) == false) | ||
| session.reset() | ||
| #expect(session.shouldStartCapture(ignoreSampling: false, sampleRate: 0.25, randomValue: { 0.0 })) | ||
| } | ||
|
|
||
| @Test("ignoreSampling bypasses persisted sampled-out decision") | ||
| func ignoreSamplingBypassesPersistedDecision() { | ||
| var session = SessionReplaySamplingSession() | ||
| #expect(session.shouldStartCapture(ignoreSampling: false, sampleRate: 0.25, randomValue: { 0.99 }) == false) | ||
| #expect(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.
Uh oh!
There was an error while loading. Please reload this page.