Skip to content

Commit 893330a

Browse files
mydeabillyvg
andauthored
fix(replay): Use session.started for min/max duration check (#8617)
We've been using `context.initialTimestamp` to check for min/max session duration, but actually this is always updated when a tab is refreshed, for example. Instead, we now use `session.started`, which should also always be updated for a buffer session. --------- Co-authored-by: Billy Vong <[email protected]>
1 parent d4fce08 commit 893330a

File tree

5 files changed

+103
-4
lines changed

5 files changed

+103
-4
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
window.Replay = new Sentry.Replay({
5+
flushMinDelay: 200,
6+
flushMaxDelay: 200,
7+
minReplayDuration: 0,
8+
});
9+
10+
Sentry.init({
11+
dsn: 'https://[email protected]/1337',
12+
sampleRate: 0,
13+
replaysSessionSampleRate: 1.0,
14+
replaysOnErrorSampleRate: 0.0,
15+
16+
integrations: [window.Replay],
17+
});
18+
19+
window.Replay._replay.timeouts = {
20+
sessionIdlePause: 1000, // this is usually 5min, but we want to test this with shorter times
21+
sessionIdleExpire: 2000, // this is usually 15min, but we want to test this with shorter times
22+
maxSessionLife: 2000, // default: 60min
23+
};
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
</head>
6+
<body>
7+
<button onclick="console.log('Test log 1')" id="button1">Click me</button>
8+
<button onclick="console.log('Test log 2')" id="button2">Click me</button>
9+
</body>
10+
</html>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
import { expect } from '@playwright/test';
2+
3+
import { sentryTest } from '../../../utils/fixtures';
4+
import { getExpectedReplayEvent } from '../../../utils/replayEventTemplates';
5+
import { getReplayEvent, shouldSkipReplayTest, waitForReplayRequest } from '../../../utils/replayHelpers';
6+
7+
const SESSION_MAX_AGE = 2000;
8+
9+
sentryTest('keeps track of max duration across reloads', async ({ getLocalTestPath, page }) => {
10+
if (shouldSkipReplayTest()) {
11+
sentryTest.skip();
12+
}
13+
14+
const reqPromise0 = waitForReplayRequest(page, 0);
15+
const reqPromise1 = waitForReplayRequest(page, 1);
16+
17+
await page.route('https://dsn.ingest.sentry.io/**/*', route => {
18+
return route.fulfill({
19+
status: 200,
20+
contentType: 'application/json',
21+
body: JSON.stringify({ id: 'test-id' }),
22+
});
23+
});
24+
25+
const url = await getLocalTestPath({ testDir: __dirname });
26+
27+
await page.goto(url);
28+
29+
await new Promise(resolve => setTimeout(resolve, SESSION_MAX_AGE / 2));
30+
31+
await page.reload();
32+
await page.click('#button1');
33+
34+
// After the second reload, we should have a new session (because we exceeded max age)
35+
const reqPromise3 = waitForReplayRequest(page, 0);
36+
37+
await new Promise(resolve => setTimeout(resolve, SESSION_MAX_AGE / 2 + 100));
38+
39+
void page.click('#button1');
40+
await page.evaluate(`Object.defineProperty(document, 'visibilityState', {
41+
configurable: true,
42+
get: function () {
43+
return 'hidden';
44+
},
45+
});
46+
document.dispatchEvent(new Event('visibilitychange'));`);
47+
48+
const replayEvent0 = getReplayEvent(await reqPromise0);
49+
expect(replayEvent0).toEqual(getExpectedReplayEvent({}));
50+
51+
const replayEvent1 = getReplayEvent(await reqPromise1);
52+
expect(replayEvent1).toEqual(
53+
getExpectedReplayEvent({
54+
segment_id: 1,
55+
}),
56+
);
57+
58+
const replayEvent3 = getReplayEvent(await reqPromise3);
59+
expect(replayEvent3).toEqual(getExpectedReplayEvent({}));
60+
});

packages/replay/src/replay.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,10 @@ export class ReplayContainer implements ReplayContainerInterface {
426426

427427
const activityTime = Date.now();
428428

429+
// eslint-disable-next-line no-console
430+
const log = this.getOptions()._experiments.traceInternals ? console.info : logger.info;
431+
__DEBUG_BUILD__ && log(`[Replay] Converting buffer to session, starting at ${activityTime}`);
432+
429433
// Allow flush to complete before resuming as a session recording, otherwise
430434
// the checkout from `startRecording` may be included in the payload.
431435
// Prefer to keep the error replay as a separate (and smaller) segment
@@ -981,9 +985,6 @@ export class ReplayContainer implements ReplayContainerInterface {
981985

982986
const earliestEvent = eventBuffer.getEarliestTimestamp();
983987
if (earliestEvent && earliestEvent < this._context.initialTimestamp) {
984-
// eslint-disable-next-line no-console
985-
const log = this.getOptions()._experiments.traceInternals ? console.info : logger.info;
986-
__DEBUG_BUILD__ && log(`[Replay] Updating initial timestamp to ${earliestEvent}`);
987988
this._context.initialTimestamp = earliestEvent;
988989
}
989990
}
@@ -1103,7 +1104,7 @@ export class ReplayContainer implements ReplayContainerInterface {
11031104
return;
11041105
}
11051106

1106-
const start = this._context.initialTimestamp;
1107+
const start = this.session.started;
11071108
const now = Date.now();
11081109
const duration = now - start;
11091110

packages/replay/src/util/handleRecordingEmit.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ export function getHandleRecordingEmit(replay: ReplayContainer): RecordingEmitCa
7272
if (replay.recordingMode === 'buffer' && replay.session && replay.eventBuffer) {
7373
const earliestEvent = replay.eventBuffer.getEarliestTimestamp();
7474
if (earliestEvent) {
75+
// eslint-disable-next-line no-console
76+
const log = replay.getOptions()._experiments.traceInternals ? console.info : logger.info;
77+
__DEBUG_BUILD__ &&
78+
log(`[Replay] Updating session start time to earliest event in buffer at ${earliestEvent}`);
79+
7580
replay.session.started = earliestEvent;
7681

7782
if (replay.getOptions().stickySession) {

0 commit comments

Comments
 (0)