Skip to content
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

fix(replay): Fix buffered replays creating replay w/o error occuring #8168

Merged
merged 4 commits into from
May 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 24 additions & 2 deletions packages/replay/jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,24 @@ function checkCallForSentReplay(
};
}

/**
* Only want calls that send replay events, i.e. ignore error events
*/
function getReplayCalls(calls: any[][][]): any[][][] {
return calls.map(call => {
const arg = call[0];
if (arg.length !== 2) {
return [];
}

if (!arg[1][0].find(({type}: {type: string}) => ['replay_event', 'replay_recording'].includes(type))) {
return [];
}

return [ arg ];
}).filter(Boolean);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

filter(Boolean) - TIL 😅

}

/**
* Checks all calls to `fetch` and ensures a replay was uploaded by
* checking the `fetch()` request's body.
Expand All @@ -143,7 +161,9 @@ const toHaveSentReplay = function (

const expectedKeysLength = expected ? ('sample' in expected ? Object.keys(expected.sample) : Object.keys(expected)).length : 0;

for (const currentCall of calls) {
const replayCalls = getReplayCalls(calls)

for (const currentCall of replayCalls) {
result = checkCallForSentReplay.call(this, currentCall[0], expected);
if (result.pass) {
break;
Expand Down Expand Up @@ -193,7 +213,9 @@ const toHaveLastSentReplay = function (
expected?: SentReplayExpected | { sample: SentReplayExpected; inverse: boolean },
) {
const { calls } = (getCurrentHub().getClient()?.getTransport()?.send as MockTransport).mock;
const lastCall = calls[calls.length - 1]?.[0];
const replayCalls = getReplayCalls(calls)

const lastCall = replayCalls[calls.length - 1]?.[0];

const { results, call, pass } = checkCallForSentReplay.call(this, lastCall, expected);

Expand Down
4 changes: 3 additions & 1 deletion packages/replay/src/replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,9 @@ export class ReplayContainer implements ReplayContainerInterface {
this._debouncedFlush.cancel();
// See comment above re: `_isEnabled`, we "force" a flush, ignoring the
// `_isEnabled` state of the plugin since it was disabled above.
await this._flush({ force: true });
if (this.recordingMode === 'session') {
await this._flush({ force: true });
}

// After flush, destroy event buffer
this.eventBuffer && this.eventBuffer.destroy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,21 @@ describe('Integration | errorSampleRate with delayed flush', () => {
});
});

// This tests a regression where we were calling flush indiscriminantly in `stop()`
it('does not upload a replay event if error is not sampled', async () => {
// We are trying to replicate the case where error rate is 0 and session
// rate is > 0, we can't set them both to 0 otherwise
// `_loadAndCheckSession` is not called when initializing the plugin.
replay.stop();
replay['_options']['errorSampleRate'] = 0;
replay['_loadAndCheckSession']();

jest.runAllTimers();
await new Promise(process.nextTick);
expect(mockRecord.takeFullSnapshot).not.toHaveBeenCalled();
expect(replay).not.toHaveLastSentReplay();
});

it('does not send a replay when triggering a full dom snapshot when document becomes visible after [SESSION_IDLE_EXPIRE_DURATION]ms', async () => {
Object.defineProperty(document, 'visibilityState', {
configurable: true,
Expand Down Expand Up @@ -664,7 +679,7 @@ describe('Integration | errorSampleRate with delayed flush', () => {
jest.advanceTimersByTime(DEFAULT_FLUSH_MIN_DELAY);
await new Promise(process.nextTick);

expect(replay).toHaveLastSentReplay();
expect(replay).not.toHaveLastSentReplay();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was previously checking that an error event was sent.


// Wait a bit, shortly before session expires
jest.advanceTimersByTime(MAX_SESSION_LIFE - 1000);
Expand Down
18 changes: 16 additions & 2 deletions packages/replay/test/integration/errorSampleRate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,21 @@ describe('Integration | errorSampleRate', () => {
});
});

// This tests a regression where we were calling flush indiscriminantly in `stop()`
it('does not upload a replay event if error is not sampled', async () => {
// We are trying to replicate the case where error rate is 0 and session
// rate is > 0, we can't set them both to 0 otherwise
// `_loadAndCheckSession` is not called when initializing the plugin.
replay.stop();
replay['_options']['errorSampleRate'] = 0;
replay['_loadAndCheckSession']();

jest.runAllTimers();
await new Promise(process.nextTick);
expect(mockRecord.takeFullSnapshot).not.toHaveBeenCalled();
expect(replay).not.toHaveLastSentReplay();
});

it('does not send a replay when triggering a full dom snapshot when document becomes visible after [SESSION_IDLE_EXPIRE_DURATION]ms', async () => {
Object.defineProperty(document, 'visibilityState', {
configurable: true,
Expand Down Expand Up @@ -668,8 +683,7 @@ describe('Integration | errorSampleRate', () => {

jest.advanceTimersByTime(DEFAULT_FLUSH_MIN_DELAY);
await new Promise(process.nextTick);

expect(replay).toHaveLastSentReplay();
expect(replay).not.toHaveLastSentReplay();

// Wait a bit, shortly before session expires
jest.advanceTimersByTime(MAX_SESSION_LIFE - 1000);
Expand Down