Skip to content

Commit 6f33254

Browse files
committed
log trying to figure out why session start time is not being updated
1 parent 23f514e commit 6f33254

File tree

3 files changed

+61
-27
lines changed

3 files changed

+61
-27
lines changed

packages/replay-internal/src/eventBuffer/EventBufferProxy.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export class EventBufferProxy implements EventBuffer {
115115
// Wait for original events to be re-added before resolving
116116
try {
117117
await Promise.all(addEventPromises);
118+
DEBUG_BUILD && logger.info('Finished switching to compression worker');
118119

119120
// Can now clear fallback buffer as it's no longer necessary
120121
this._fallback.clear();

packages/replay-internal/src/replay.ts

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ export class ReplayContainer implements ReplayContainerInterface {
524524
}
525525

526526
const activityTime = Date.now();
527+
const earliestEvent = this.eventBuffer && this.eventBuffer.getEarliestTimestamp();
527528

528529
DEBUG_BUILD && logger.info('Converting buffer to session');
529530

@@ -549,6 +550,9 @@ export class ReplayContainer implements ReplayContainerInterface {
549550

550551
// Once this session ends, we do not want to refresh it
551552
if (this.session) {
553+
if (earliestEvent) {
554+
this.session.started = earliestEvent;
555+
}
552556
this._updateUserActivity(activityTime);
553557
this._updateSessionActivity(activityTime);
554558
this._maybeSaveSession();
@@ -1222,35 +1226,14 @@ export class ReplayContainer implements ReplayContainerInterface {
12221226
return;
12231227
}
12241228

1225-
const start = this.session.started;
1226-
const now = Date.now();
1227-
const duration = now - start;
1228-
12291229
// A flush is about to happen, cancel any queued flushes
12301230
this._debouncedFlush.cancel();
12311231

1232-
// If session is too short, or too long (allow some wiggle room over maxReplayDuration), do not send it
1233-
// This _should_ not happen, but it may happen if flush is triggered due to a page activity change or similar
1234-
const tooShort = duration < this._options.minReplayDuration;
1235-
const tooLong = duration > this._options.maxReplayDuration + 5_000;
1236-
if (tooShort || tooLong) {
1237-
DEBUG_BUILD &&
1238-
logger.info(
1239-
`Session duration (${Math.floor(duration / 1000)}s) is too ${
1240-
tooShort ? 'short' : 'long'
1241-
}, not sending replay.`,
1242-
);
1243-
1244-
if (tooShort) {
1245-
this._debouncedFlush();
1246-
}
1232+
const isValidDuration = this._checkReplayDurationDuringFlush();
12471233

1248-
// XXX: disregard durations for buffer mode for debug purposes
1249-
if (this.recordingMode !== 'buffer') {
1250-
return;
1251-
} else {
1252-
setTag(`replay.${tooShort ? 'tooShort' : 'tooLong'}`, true);
1253-
}
1234+
// XXX: disregard durations for buffer mode for debug purposes
1235+
if (!isValidDuration && this.recordingMode !== 'buffer') {
1236+
return;
12541237
}
12551238

12561239
const eventBuffer = this.eventBuffer;
@@ -1285,6 +1268,52 @@ export class ReplayContainer implements ReplayContainerInterface {
12851268
}
12861269
};
12871270

1271+
/**
1272+
* Checks to see if replay duration is within bounds during a flush. If it is
1273+
* too short, will queue up a new flush to prevent short replays.
1274+
*
1275+
* Returns true if duration is ok, false otherwise
1276+
*/
1277+
private _checkReplayDurationDuringFlush(): boolean {
1278+
if (!this.session) {
1279+
return false;
1280+
}
1281+
1282+
const earliestTimestampFromBuffer = this.eventBuffer && this.eventBuffer.getEarliestTimestamp();
1283+
const start =
1284+
this.recordingMode === 'buffer' && earliestTimestampFromBuffer
1285+
? earliestTimestampFromBuffer
1286+
: this.session.started;
1287+
const now = Date.now();
1288+
const duration = now - start;
1289+
1290+
// If session is too short, or too long (allow some wiggle room over maxReplayDuration), do not send it
1291+
// This _should_ not happen, but it may happen if flush is triggered due to a page activity change or similar
1292+
const tooShort = duration < this._options.minReplayDuration;
1293+
const tooLong = duration > this._options.maxReplayDuration + 5_000;
1294+
if (tooShort || tooLong) {
1295+
DEBUG_BUILD &&
1296+
logger.info(
1297+
`Session duration (${Math.floor(duration / 1000)}s) is too ${
1298+
tooShort ? 'short' : 'long'
1299+
}, not sending replay.`,
1300+
);
1301+
1302+
if (tooShort) {
1303+
this._debouncedFlush();
1304+
}
1305+
1306+
// XXX: disregard durations for buffer mode for debug purposes
1307+
if (this.recordingMode === 'buffer') {
1308+
setTag(`replay.${tooShort ? 'tooShort' : 'tooLong'}`, true);
1309+
}
1310+
1311+
return false;
1312+
}
1313+
1314+
return true;
1315+
}
1316+
12881317
/** Save the session, if it is sticky */
12891318
private _maybeSaveSession(): void {
12901319
if (this.session && this._options.stickySession) {

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export function getHandleRecordingEmit(replay: ReplayContainer): RecordingEmitCa
4343
// dependent on this reset.
4444
if (replay.recordingMode === 'buffer' && isCheckout) {
4545
replay.setInitialState();
46+
DEBUG_BUILD && logger.info('Adding checkout event while in buffer mode');
4647
}
4748

4849
// If the event is not added (e.g. due to being paused, disabled, or out of the max replay duration),
@@ -83,8 +84,8 @@ export function getHandleRecordingEmit(replay: ReplayContainer): RecordingEmitCa
8384

8485
// When in buffer mode, make sure we adjust the session started date to the current earliest event of the buffer
8586
// this should usually be the timestamp of the checkout event, but to be safe...
86-
if (replay.recordingMode === 'buffer' && session && replay.eventBuffer) {
87-
const earliestEvent = replay.eventBuffer.getEarliestTimestamp();
87+
if (replay.recordingMode === 'buffer' && session) {
88+
const earliestEvent = replay.eventBuffer && replay.eventBuffer.getEarliestTimestamp();
8889
if (earliestEvent) {
8990
DEBUG_BUILD &&
9091
logger.info(`Updating session start time to earliest event in buffer to ${new Date(earliestEvent)}`);
@@ -94,6 +95,9 @@ export function getHandleRecordingEmit(replay: ReplayContainer): RecordingEmitCa
9495
if (replay.getOptions().stickySession) {
9596
saveSession(session);
9697
}
98+
} else {
99+
// XXX(billy): debugging
100+
DEBUG_BUILD && logger.warn('Unable to find earliest event in event buffer');
97101
}
98102
}
99103

0 commit comments

Comments
 (0)