diff --git a/packages/replay/src/replay.ts b/packages/replay/src/replay.ts index 4db9a7aae92d..df4b398ac112 100644 --- a/packages/replay/src/replay.ts +++ b/packages/replay/src/replay.ts @@ -398,7 +398,7 @@ export class ReplayContainer implements ReplayContainerInterface { this.recordingMode = 'session'; if (this.session) { - this._updateSessionActivity(activityTime); + this.session.lastActivity = activityTime; this.session.started = activityTime; this._maybeSaveSession(); } @@ -537,7 +537,7 @@ export class ReplayContainer implements ReplayContainerInterface { /** * Check the state/expiration of the session. - * The callback is used when the session is neither paused nor expired. + * The callback is called when the session is neither paused nor expired. */ public checkSessionState(onContinue: () => void): void { if (!this.session || !this.session.sampled) { @@ -608,7 +608,7 @@ export class ReplayContainer implements ReplayContainerInterface { // this method is generally called on page load or manually - in both cases // we should treat it as an activity - this._updateSessionActivity(); + this.updateUserActivity(); this.eventBuffer = createEventBuffer({ useCompression: this._options.useCompression, diff --git a/packages/replay/src/util/sampleSession.ts b/packages/replay/src/util/sampleSession.ts index cff29c23c082..4a9223574e02 100644 --- a/packages/replay/src/util/sampleSession.ts +++ b/packages/replay/src/util/sampleSession.ts @@ -19,5 +19,5 @@ export function sampleSession({ return 'session'; } - return 'buffer'; + return errorSampleRate > 0 ? 'buffer' : false; } diff --git a/packages/replay/test/unit/util/sampleSession.test.ts b/packages/replay/test/unit/util/sampleSession.test.ts new file mode 100644 index 000000000000..321632fac0d0 --- /dev/null +++ b/packages/replay/test/unit/util/sampleSession.test.ts @@ -0,0 +1,28 @@ +import type { Sampled } from '../../../src/types'; +import { sampleSession } from '../../../src/util/sampleSession'; + +// Note: We "fix" Math.random() to always return 0.2 +const cases: [number, number, Sampled][] = [ + [0, 0, false], + [-1, -1, false], + [1, 0, 'session'], + [0, 1, 'buffer'], + [0.1, 0.1, 'buffer'], + [0.1, 0, false], + [0.3, 0.1, 'session'], + [0.3, 0, 'session'], +]; + +describe('Unit | util | sampleSession', () => { + const mockRandom = jest.spyOn(Math, 'random'); + + test.each(cases)( + 'given sessionSampleRate=%p and errorSampleRate=%p, result should be %p', + (sessionSampleRate: number, errorSampleRate: number, expectedResult: Sampled) => { + mockRandom.mockImplementationOnce(() => 0.2); + + const result = sampleSession({ sessionSampleRate, errorSampleRate }); + expect(result).toEqual(expectedResult); + }, + ); +});