Skip to content

Commit

Permalink
fix(replay): Fix onError sampling when loading an expired buffered se…
Browse files Browse the repository at this point in the history
…ssion (#13962)

This fixes a bug where an older, saved session (that has a
`previousSessionId`, i.e. session was recorded and expired) would cause
buffered replays to not send when an error happens. This is because the
session start timestamp would never update, causing our flush logic to
skip sending the replay due to incorrect replay duration calculation.
  • Loading branch information
billyvg authored Oct 15, 2024
1 parent 8249a5e commit 5f68d4d
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 11 deletions.
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;
}

// 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
84 changes: 84 additions & 0 deletions packages/replay-internal/test/integration/errorSampleRate.test.ts
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

0 comments on commit 5f68d4d

Please sign in to comment.