Skip to content

Commit 0d03553

Browse files
committed
add a new "dirty" flag to session
1 parent b51a7a9 commit 0d03553

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

packages/replay-internal/src/coreHandlers/handleGlobalEvent.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,16 @@ export function handleGlobalEventListener(replay: ReplayContainer): (event: Even
7171
}
7272

7373
// If we sampled this error in buffer mode, immediately mark the session as "sampled"
74-
// by changing the sampled state from 'buffer' to 'session'. Otherwise, if the application is interrupted before `afterSendEvent` occurs, then the session would remain as "buffer" but we have an error event that is tagged with a replay id. This could end up creating replays w/ excessive durations because of the linked error.
75-
if (isErrorEventSampled && replay.recordingMode === 'buffer') {
74+
// by changing the sampled state from 'buffer' to 'session'. Otherwise, if the application is interrupte
75+
// before `afterSendEvent` occurs, then the session would remain as "buffer" but we have an error event
76+
// that is tagged with a replay id. This could end up creating replays w/ excessive durations because
77+
// of the linked error.
78+
if (isErrorEventSampled && replay.recordingMode === 'buffer' && replay.session?.sampled === 'buffer') {
7679
const session = replay.session;
77-
if (session?.sampled === 'buffer') {
78-
session.sampled = 'session';
79-
// Save the session if sticky sessions are enabled to persist the state change
80-
if (replay.getOptions().stickySession) {
81-
saveSession(session);
82-
}
80+
session.dirty = true;
81+
// Save the session if sticky sessions are enabled to persist the state change
82+
if (replay.getOptions().stickySession) {
83+
saveSession(session);
8384
}
8485
}
8586

packages/replay-internal/src/types/replay.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,13 @@ export interface Session {
383383
* Is the session sampled? `false` if not sampled, otherwise, `session` or `buffer`
384384
*/
385385
sampled: Sampled;
386+
387+
/**
388+
* Session is dirty when its id has been linked to an event (e.g. error event).
389+
* This is helpful when a session is mistakenly stuck in "buffer" mode (e.g. network issues preventing it from being converted to "session" mode).
390+
* The dirty flag is used to prevent updating the session start time to the earliest event in the buffer so that it can be refreshed if it's been expired.
391+
*/
392+
dirty?: boolean;
386393
}
387394

388395
export type EventBufferType = 'sync' | 'worker';

packages/replay-internal/src/util/handleRecordingEmit.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ export function getHandleRecordingEmit(replay: ReplayContainer): RecordingEmitCa
7272

7373
// When in buffer mode, make sure we adjust the session started date to the current earliest event of the buffer
7474
// this should usually be the timestamp of the checkout event, but to be safe...
75-
if (replay.recordingMode === 'buffer' && session && replay.eventBuffer) {
75+
if (replay.recordingMode === 'buffer' && session && replay.eventBuffer && !session.dirty) {
7676
const earliestEvent = replay.eventBuffer.getEarliestTimestamp();
7777
if (earliestEvent) {
7878
DEBUG_BUILD &&

0 commit comments

Comments
 (0)