Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
mydea committed Jul 13, 2023
1 parent d35aa65 commit 6887454
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 4 deletions.
6 changes: 3 additions & 3 deletions packages/replay/src/replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/src/util/sampleSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@ export function sampleSession({
return 'session';
}

return 'buffer';
return errorSampleRate > 0 ? 'buffer' : false;
}
28 changes: 28 additions & 0 deletions packages/replay/test/unit/util/sampleSession.test.ts
Original file line number Diff line number Diff line change
@@ -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);
},
);
});

0 comments on commit 6887454

Please sign in to comment.