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): Ensure buffer sessions end after capturing an error #8713

Merged
merged 1 commit into from
Aug 3, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion packages/replay/src/session/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ export function makeSession(session: Partial<Session> & { sampled: Sampled }): S
const lastActivity = session.lastActivity || now;
const segmentId = session.segmentId || 0;
const sampled = session.sampled;
const shouldRefresh = typeof session.shouldRefresh === 'boolean' ? session.shouldRefresh : true;

return {
id,
started,
lastActivity,
segmentId,
sampled,
shouldRefresh: true,
shouldRefresh,
};
}
79 changes: 79 additions & 0 deletions packages/replay/test/integration/errorSampleRate.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -819,6 +819,85 @@ describe('Integration | errorSampleRate', () => {
});
});

/**
* If an error happens, we switch the recordingMode to `session`, set `shouldRefresh=false` on the session,
* but keep `sampled=buffer`.
* This test should verify that if we load such a session from sessionStorage, the session is eventually correctly ended.
*/
it('handles buffer sessions that previously had an error', async () => {
// Pretend that a session is already saved before loading replay
WINDOW.sessionStorage.setItem(
REPLAY_SESSION_KEY,
`{"segmentId":0,"id":"fd09adfc4117477abc8de643e5a5798a","sampled":"buffer","started":${BASE_TIMESTAMP},"lastActivity":${BASE_TIMESTAMP},"shouldRefresh":false}`,
);
const { mockRecord, replay, integration } = await resetSdkMock({
replayOptions: {
stickySession: true,
},
sentryOptions: {
replaysOnErrorSampleRate: 1.0,
},
autoStart: false,
});
integration['_initialize']();

jest.runAllTimers();

await new Promise(process.nextTick);
const TEST_EVENT = { data: {}, timestamp: BASE_TIMESTAMP, type: 3 };
mockRecord._emitter(TEST_EVENT);

expect(replay).not.toHaveLastSentReplay();

// Waiting for max life should eventually stop recording
// We simulate a full checkout which would otherwise be done automatically
for (let i = 0; i < MAX_SESSION_LIFE / 60_000; i++) {
jest.advanceTimersByTime(60_000);
await new Promise(process.nextTick);
mockRecord.takeFullSnapshot(true);
}

expect(replay).not.toHaveLastSentReplay();
expect(replay.isEnabled()).toBe(false);
});

it('handles buffer sessions that never had an error', async () => {
// Pretend that a session is already saved before loading replay
WINDOW.sessionStorage.setItem(
REPLAY_SESSION_KEY,
`{"segmentId":0,"id":"fd09adfc4117477abc8de643e5a5798a","sampled":"buffer","started":${BASE_TIMESTAMP},"lastActivity":${BASE_TIMESTAMP}}`,
);
const { mockRecord, replay, integration } = await resetSdkMock({
replayOptions: {
stickySession: true,
},
sentryOptions: {
replaysOnErrorSampleRate: 1.0,
},
autoStart: false,
});
integration['_initialize']();

jest.runAllTimers();

await new Promise(process.nextTick);
const TEST_EVENT = { data: {}, timestamp: BASE_TIMESTAMP, type: 3 };
mockRecord._emitter(TEST_EVENT);

expect(replay).not.toHaveLastSentReplay();

// Waiting for max life should eventually stop recording
// We simulate a full checkout which would otherwise be done automatically
for (let i = 0; i < MAX_SESSION_LIFE / 60_000; i++) {
jest.advanceTimersByTime(60_000);
await new Promise(process.nextTick);
mockRecord.takeFullSnapshot(true);
}

expect(replay).not.toHaveLastSentReplay();
expect(replay.isEnabled()).toBe(true);
});

/**
* This is testing a case that should only happen with error-only sessions.
* Previously we had assumed that loading a session from session storage meant
Expand Down