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 onError sampling when loading an expired buffered session #13962

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
2 changes: 1 addition & 1 deletion .size-limit.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ module.exports = [
path: 'packages/browser/build/npm/esm/index.js',
import: createImport('init', 'browserTracingIntegration', 'replayIntegration', 'feedbackIntegration'),
gzip: true,
limit: '91 KB',
limit: '95 KB',
},
{
name: '@sentry/browser (incl. Tracing, Replay, Feedback, metrics)',
Expand Down
20 changes: 10 additions & 10 deletions packages/replay-internal/src/util/handleRecordingEmit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,6 @@ export function getHandleRecordingEmit(replay: ReplayContainer): RecordingEmitCa
// only be added for checkouts
addSettingsEvent(replay, isCheckout);

// If there is a previousSessionId after a full snapshot occurs, then
// the replay session was started due to session expiration. The new session
// is started before triggering a new checkout and contains the id
// of the previous session. Do not immediately flush in this case
// to avoid capturing only the checkout and instead the replay will
// be captured if they perform any follow-up actions.
if (session && session.previousSessionId) {
return true;
}
Comment on lines -74 to -82
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 bug was so simple and right there this entire time 😭


// When in buffer mode, make sure we adjust the session started date to the current earliest event of the buffer
// this should usually be the timestamp of the checkout event, but to be safe...
if (replay.recordingMode === 'buffer' && session && replay.eventBuffer) {
Expand All @@ -97,6 +87,16 @@ export function getHandleRecordingEmit(replay: ReplayContainer): RecordingEmitCa
}
}

// If there is a previousSessionId after a full snapshot occurs, then
// the replay session was started due to session expiration. The new session
// is started before triggering a new checkout and contains the id
// of the previous session. Do not immediately flush in this case
// to avoid capturing only the checkout and instead the replay will
// be captured if they perform any follow-up actions.
if (session && session.previousSessionId) {
return true;
}

if (replay.recordingMode === 'session') {
// If the full snapshot is due to an initial load, we will not have
// a previous session ID. In this case, we want to buffer events
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,90 @@ describe('Integration | errorSampleRate', () => {
});
});

it('loads an old session with a previousSessionId set and can send buffered replay', async () => {
vi.mock('../../src/session/fetchSession', () => ({
fetchSession: () => ({
id: 'newreplayid',
started: BASE_TIMESTAMP,
lastActivity: BASE_TIMESTAMP,
segmentId: 0,
sampled: 'buffer',
previousSessionId: 'previoussessionid',
}),
}));

const ADVANCED_TIME = 86400000;
const optionsEvent = createOptionsEvent(replay);

expect(replay.session.started).toBe(BASE_TIMESTAMP);

// advance time to make sure replay duration is invalid
vi.advanceTimersByTime(ADVANCED_TIME);

// full snapshot should update session start time
mockRecord.takeFullSnapshot(true);
expect(replay.session.started).toBe(BASE_TIMESTAMP + ADVANCED_TIME);
expect(replay.recordingMode).toBe('buffer');

// advance so we can flush
vi.advanceTimersByTime(DEFAULT_FLUSH_MIN_DELAY);

captureException(new Error('testing'));
await vi.advanceTimersToNextTimerAsync();

// Converts to session mode
expect(replay.recordingMode).toBe('session');
expect(replay).toHaveLastSentReplay({
recordingPayloadHeader: { segment_id: 0 },
replayEventPayload: expect.objectContaining({
replay_type: 'buffer',
}),
recordingData: JSON.stringify([
{ data: { isCheckout: true }, timestamp: BASE_TIMESTAMP + ADVANCED_TIME, type: 2 },
{ ...optionsEvent, timestamp: BASE_TIMESTAMP + ADVANCED_TIME },
]),
});

// capture next event
domHandler({
name: 'click',
event: new Event('click'),
});

vi.advanceTimersByTime(DEFAULT_FLUSH_MIN_DELAY);
await vi.advanceTimersToNextTimerAsync();

expect(replay).toHaveLastSentReplay({
recordingPayloadHeader: { segment_id: 1 },
replayEventPayload: expect.objectContaining({
// We don't change replay_type as it starts in buffer mode and that's
// what we're interested in, even though recordingMode changes to
// 'session'
replay_type: 'buffer',
}),
recordingData: JSON.stringify([
// There's a new checkout because we convert to session mode
{ data: { isCheckout: true }, timestamp: BASE_TIMESTAMP + ADVANCED_TIME + DEFAULT_FLUSH_MIN_DELAY, type: 2 },
{
type: 5,
timestamp: BASE_TIMESTAMP + ADVANCED_TIME + DEFAULT_FLUSH_MIN_DELAY,
data: {
tag: 'breadcrumb',
payload: {
timestamp: (BASE_TIMESTAMP + ADVANCED_TIME + DEFAULT_FLUSH_MIN_DELAY) / 1000,
type: 'default',
category: 'ui.click',
message: '<unknown>',
data: {},
},
},
},
]),
});
vi.unmock('../../src/session/fetchSession');
await waitForFlush();
});

it('manually flushes replay and does not continue to record', async () => {
const TEST_EVENT = getTestEventIncremental({ timestamp: BASE_TIMESTAMP });
mockRecord._emitter(TEST_EVENT);
Expand Down
Loading