Skip to content

Commit 7f6fbad

Browse files
committed
feat(replay): Allow to configure maxReplayDuration
This defaults to 60min, and is capped at max. 60min (=you cannot specify a longer max duration than 60min).
1 parent 797d906 commit 7f6fbad

File tree

22 files changed

+248
-233
lines changed

22 files changed

+248
-233
lines changed

packages/browser-integration-tests/suites/replay/errors/errorsInSession/init.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,4 @@ Sentry.init({
1919
return event;
2020
},
2121
integrations: [window.Replay],
22-
debug: true,
2322
});

packages/browser-integration-tests/suites/replay/largeMutations/mutationLimit/init.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ Sentry.init({
1313
sampleRate: 0,
1414
replaysSessionSampleRate: 1.0,
1515
replaysOnErrorSampleRate: 0.0,
16-
debug: true,
1716

1817
integrations: [window.Replay],
1918
});

packages/browser-integration-tests/suites/replay/maxReplayDuration/init.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ window.Replay = new Sentry.Replay({
55
flushMinDelay: 200,
66
flushMaxDelay: 200,
77
minReplayDuration: 0,
8+
maxReplayDuration: 2000,
89
});
910

1011
Sentry.init({
@@ -19,5 +20,4 @@ Sentry.init({
1920
window.Replay._replay.timeouts = {
2021
sessionIdlePause: 1000, // this is usually 5min, but we want to test this with shorter times
2122
sessionIdleExpire: 2000, // this is usually 15min, but we want to test this with shorter times
22-
maxSessionLife: 2000, // default: 60min
2323
};

packages/browser-integration-tests/suites/replay/maxReplayDuration/test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { sentryTest } from '../../../utils/fixtures';
44
import { getExpectedReplayEvent } from '../../../utils/replayEventTemplates';
55
import { getReplayEvent, shouldSkipReplayTest, waitForReplayRequest } from '../../../utils/replayHelpers';
66

7-
const SESSION_MAX_AGE = 2000;
7+
const MAX_REPLAY_DURATION = 2000;
88

99
sentryTest('keeps track of max duration across reloads', async ({ getLocalTestPath, page }) => {
1010
if (shouldSkipReplayTest()) {
@@ -26,15 +26,15 @@ sentryTest('keeps track of max duration across reloads', async ({ getLocalTestPa
2626

2727
await page.goto(url);
2828

29-
await new Promise(resolve => setTimeout(resolve, SESSION_MAX_AGE / 2));
29+
await new Promise(resolve => setTimeout(resolve, MAX_REPLAY_DURATION / 2));
3030

3131
await page.reload();
3232
await page.click('#button1');
3333

3434
// After the second reload, we should have a new session (because we exceeded max age)
3535
const reqPromise3 = waitForReplayRequest(page, 0);
3636

37-
await new Promise(resolve => setTimeout(resolve, SESSION_MAX_AGE / 2 + 100));
37+
await new Promise(resolve => setTimeout(resolve, MAX_REPLAY_DURATION / 2 + 100));
3838

3939
void page.click('#button1');
4040
await page.evaluate(`Object.defineProperty(document, 'visibilityState', {

packages/browser-integration-tests/suites/replay/minReplayDuration/init.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ Sentry.init({
1212
sampleRate: 0,
1313
replaysSessionSampleRate: 1.0,
1414
replaysOnErrorSampleRate: 0.0,
15-
debug: true,
1615

1716
integrations: [window.Replay],
1817
});

packages/browser-integration-tests/suites/replay/sessionExpiry/init.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ Sentry.init({
1212
sampleRate: 0,
1313
replaysSessionSampleRate: 1.0,
1414
replaysOnErrorSampleRate: 0.0,
15-
debug: true,
1615

1716
integrations: [window.Replay],
1817
});
1918

2019
window.Replay._replay.timeouts = {
2120
sessionIdlePause: 1000, // this is usually 5min, but we want to test this with shorter times
2221
sessionIdleExpire: 2000, // this is usually 15min, but we want to test this with shorter times
23-
maxSessionLife: 3600000, // default: 60min
2422
};

packages/browser-integration-tests/suites/replay/sessionInactive/init.js

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@ Sentry.init({
1212
sampleRate: 0,
1313
replaysSessionSampleRate: 1.0,
1414
replaysOnErrorSampleRate: 0.0,
15-
debug: true,
1615

1716
integrations: [window.Replay],
1817
});
1918

2019
window.Replay._replay.timeouts = {
2120
sessionIdlePause: 1000, // this is usually 5min, but we want to test this with shorter times
2221
sessionIdleExpire: 900000, // defayult: 15min
23-
maxSessionLife: 3600000, // default: 60min
2422
};

packages/browser-integration-tests/suites/replay/sessionMaxAge/init.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@ window.Replay = new Sentry.Replay({
55
flushMinDelay: 200,
66
flushMaxDelay: 200,
77
minReplayDuration: 0,
8+
maxReplayDuration: 4000,
89
});
910

1011
Sentry.init({
1112
dsn: 'https://[email protected]/1337',
1213
sampleRate: 0,
1314
replaysSessionSampleRate: 1.0,
1415
replaysOnErrorSampleRate: 0.0,
15-
debug: true,
1616

1717
integrations: [window.Replay],
1818
});
1919

2020
window.Replay._replay.timeouts = {
2121
sessionIdlePause: 300000, // default: 5min
2222
sessionIdleExpire: 900000, // default: 15min
23-
maxSessionLife: 4000, // this is usually 60min, but we want to test this with shorter times
2423
};

packages/browser-integration-tests/suites/replay/sessionMaxAge/test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from '../../../utils/replayHelpers';
1313

1414
// Session should be max. 4s long
15-
const SESSION_MAX_AGE = 4000;
15+
const MAX_REPLAY_DURATION = 4000;
1616

1717
/*
1818
The main difference between this and sessionExpiry test, is that here we wait for the overall time (4s)
@@ -58,7 +58,7 @@ sentryTest('handles session that exceeds max age', async ({ getLocalTestPath, pa
5858
// Wait for an incremental snapshot
5959
// Wait half of the session max age (after initial flush), but account for potentially slow runners
6060
const timePassed1 = Date.now() - startTimestamp;
61-
await new Promise(resolve => setTimeout(resolve, Math.max(SESSION_MAX_AGE / 2 - timePassed1, 0)));
61+
await new Promise(resolve => setTimeout(resolve, Math.max(MAX_REPLAY_DURATION / 2 - timePassed1, 0)));
6262
await page.click('#button1');
6363

6464
const req1 = await reqPromise1;
@@ -71,7 +71,7 @@ sentryTest('handles session that exceeds max age', async ({ getLocalTestPath, pa
7171

7272
// Wait for session to expire
7373
const timePassed2 = Date.now() - startTimestamp;
74-
await new Promise(resolve => setTimeout(resolve, Math.max(SESSION_MAX_AGE - timePassed2, 0)));
74+
await new Promise(resolve => setTimeout(resolve, Math.max(MAX_REPLAY_DURATION - timePassed2, 0)));
7575
await page.click('#button2');
7676

7777
const req2 = await reqPromise2;

packages/replay/src/constants.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@ export const SESSION_IDLE_PAUSE_DURATION = 300_000; // 5 minutes in ms
1717
// The idle limit for a session after which the session expires.
1818
export const SESSION_IDLE_EXPIRE_DURATION = 900_000; // 15 minutes in ms
1919

20-
// The maximum length of a session
21-
export const MAX_SESSION_LIFE = 3_600_000; // 60 minutes in ms
22-
2320
/** Default flush delays */
2421
export const DEFAULT_FLUSH_MIN_DELAY = 5_000;
2522
// XXX: Temp fix for our debounce logic where `maxWait` would never occur if it
@@ -50,3 +47,6 @@ export const REPLAY_MAX_EVENT_BUFFER_SIZE = 20_000_000; // ~20MB
5047
export const MIN_REPLAY_DURATION = 4_999;
5148
/* The max. allowed value that the minReplayDuration can be set to. */
5249
export const MIN_REPLAY_DURATION_LIMIT = 15_000;
50+
51+
/** The max. length of a replay. */
52+
export const MAX_REPLAY_DURATION = 3_600_000; // 60 minutes in ms;

packages/replay/src/integration.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { dropUndefinedKeys } from '@sentry/utils';
55
import {
66
DEFAULT_FLUSH_MAX_DELAY,
77
DEFAULT_FLUSH_MIN_DELAY,
8+
MAX_REPLAY_DURATION,
89
MIN_REPLAY_DURATION,
910
MIN_REPLAY_DURATION_LIMIT,
1011
} from './constants';
@@ -57,6 +58,7 @@ export class Replay implements Integration {
5758
flushMinDelay = DEFAULT_FLUSH_MIN_DELAY,
5859
flushMaxDelay = DEFAULT_FLUSH_MAX_DELAY,
5960
minReplayDuration = MIN_REPLAY_DURATION,
61+
maxReplayDuration = MAX_REPLAY_DURATION,
6062
stickySession = true,
6163
useCompression = true,
6264
_experiments = {},
@@ -136,6 +138,7 @@ export class Replay implements Integration {
136138
flushMinDelay,
137139
flushMaxDelay,
138140
minReplayDuration: Math.min(minReplayDuration, MIN_REPLAY_DURATION_LIMIT),
141+
maxReplayDuration: Math.min(maxReplayDuration, MAX_REPLAY_DURATION),
139142
stickySession,
140143
sessionSampleRate,
141144
errorSampleRate,

packages/replay/src/replay.ts

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import { logger } from '@sentry/utils';
66

77
import {
88
BUFFER_CHECKOUT_TIME,
9-
MAX_SESSION_LIFE,
109
SESSION_IDLE_EXPIRE_DURATION,
1110
SESSION_IDLE_PAUSE_DURATION,
1211
SLOW_CLICK_SCROLL_TIMEOUT,
@@ -148,7 +147,6 @@ export class ReplayContainer implements ReplayContainerInterface {
148147
this.timeouts = {
149148
sessionIdlePause: SESSION_IDLE_PAUSE_DURATION,
150149
sessionIdleExpire: SESSION_IDLE_EXPIRE_DURATION,
151-
maxSessionLife: MAX_SESSION_LIFE,
152150
} as const;
153151
this._lastActivity = Date.now();
154152
this._isEnabled = false;
@@ -278,14 +276,10 @@ export class ReplayContainer implements ReplayContainerInterface {
278276

279277
const previousSessionId = this.session && this.session.id;
280278

281-
const { session } = getSession({
282-
timeouts: this.timeouts,
283-
stickySession: Boolean(this._options.stickySession),
284-
currentSession: this.session,
279+
const { session } = getSession(this, {
285280
// This is intentional: create a new session-based replay when calling `start()`
286281
sessionSampleRate: 1,
287282
allowBuffering: false,
288-
traceInternals: this._options._experiments.traceInternals,
289283
});
290284

291285
session.previousSessionId = previousSessionId;
@@ -307,13 +301,9 @@ export class ReplayContainer implements ReplayContainerInterface {
307301

308302
const previousSessionId = this.session && this.session.id;
309303

310-
const { session } = getSession({
311-
timeouts: this.timeouts,
312-
stickySession: Boolean(this._options.stickySession),
313-
currentSession: this.session,
304+
const { session } = getSession(this, {
314305
sessionSampleRate: 0,
315306
allowBuffering: true,
316-
traceInternals: this._options._experiments.traceInternals,
317307
});
318308

319309
session.previousSessionId = previousSessionId;
@@ -483,7 +473,7 @@ export class ReplayContainer implements ReplayContainerInterface {
483473
// `shouldRefresh`, the session could be considered expired due to
484474
// lifespan, which is not what we want. Update session start date to be
485475
// the current timestamp, so that session is not considered to be
486-
// expired. This means that max replay duration can be MAX_SESSION_LIFE +
476+
// expired. This means that max replay duration can be MAX_REPLAY_DURATION +
487477
// (length of buffer), which we are ok with.
488478
this._updateUserActivity(activityTime);
489479
this._updateSessionActivity(activityTime);
@@ -754,13 +744,9 @@ export class ReplayContainer implements ReplayContainerInterface {
754744
* Returns false if session is not recorded.
755745
*/
756746
private _loadAndCheckSession(): boolean {
757-
const { type, session } = getSession({
758-
timeouts: this.timeouts,
759-
stickySession: Boolean(this._options.stickySession),
760-
currentSession: this.session,
747+
const { type, session } = getSession(this, {
761748
sessionSampleRate: this._options.sessionSampleRate,
762749
allowBuffering: this._options.errorSampleRate > 0 || this.recordingMode === 'buffer',
763-
traceInternals: this._options._experiments.traceInternals,
764750
});
765751

766752
// If session was newly created (i.e. was not loaded from storage), then
@@ -893,7 +879,10 @@ export class ReplayContainer implements ReplayContainerInterface {
893879
return;
894880
}
895881

896-
const expired = isSessionExpired(this.session, this.timeouts);
882+
const expired = isSessionExpired(this.session, {
883+
maxReplayDuration: this._options.maxReplayDuration,
884+
...this.timeouts,
885+
});
897886

898887
if (breadcrumb && !expired) {
899888
this._createCustomBreadcrumb(breadcrumb);
@@ -1072,7 +1061,7 @@ export class ReplayContainer implements ReplayContainerInterface {
10721061
// Check total duration again, to avoid sending outdated stuff
10731062
// We leave 30s wiggle room to accomodate late flushing etc.
10741063
// This _could_ happen when the browser is suspended during flushing, in which case we just want to stop
1075-
if (timestamp - this._context.initialTimestamp > this.timeouts.maxSessionLife + 30_000) {
1064+
if (timestamp - this._context.initialTimestamp > this._options.maxReplayDuration + 30_000) {
10761065
throw new Error('Session is too long, not sending replay');
10771066
}
10781067

@@ -1145,10 +1134,10 @@ export class ReplayContainer implements ReplayContainerInterface {
11451134
// A flush is about to happen, cancel any queued flushes
11461135
this._debouncedFlush.cancel();
11471136

1148-
// If session is too short, or too long (allow some wiggle room over maxSessionLife), do not send it
1137+
// If session is too short, or too long (allow some wiggle room over maxReplayDuration), do not send it
11491138
// This _should_ not happen, but it may happen if flush is triggered due to a page activity change or similar
11501139
const tooShort = duration < this._options.minReplayDuration;
1151-
const tooLong = duration > this.timeouts.maxSessionLife + 5_000;
1140+
const tooLong = duration > this._options.maxReplayDuration + 5_000;
11521141
if (tooShort || tooLong) {
11531142
logInfo(
11541143
`[Replay] Session duration (${Math.floor(duration / 1000)}s) is too ${

packages/replay/src/session/getSession.ts

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,32 @@
1-
import type { Session, SessionOptions, Timeouts } from '../types';
1+
import type { ReplayContainer, Session } from '../types';
22
import { isSessionExpired } from '../util/isSessionExpired';
33
import { logInfoNextTick } from '../util/log';
44
import { createSession } from './createSession';
55
import { fetchSession } from './fetchSession';
66
import { makeSession } from './Session';
77

8-
interface GetSessionParams extends SessionOptions {
9-
timeouts: Timeouts;
10-
11-
/**
12-
* The current session (e.g. if stickySession is off)
13-
*/
14-
currentSession?: Session;
15-
16-
traceInternals?: boolean;
17-
}
18-
198
/**
209
* Get or create a session
2110
*/
22-
export function getSession({
23-
timeouts,
24-
currentSession,
25-
stickySession,
26-
sessionSampleRate,
27-
allowBuffering,
28-
traceInternals,
29-
}: GetSessionParams): { type: 'new' | 'saved'; session: Session } {
11+
export function getSession(
12+
replay: ReplayContainer,
13+
{ sessionSampleRate, allowBuffering }: { sessionSampleRate: number; allowBuffering: boolean },
14+
): { type: 'new' | 'saved'; session: Session } {
15+
const currentSession = replay.session;
16+
const stickySession = replay.getOptions().stickySession;
17+
const { traceInternals } = replay.getOptions()._experiments;
18+
3019
// If session exists and is passed, use it instead of always hitting session storage
3120
const session = currentSession || (stickySession && fetchSession(traceInternals));
3221

3322
if (session) {
3423
// If there is a session, check if it is valid (e.g. "last activity" time
3524
// should be within the "session idle time", and "session started" time is
3625
// within "max session time").
37-
const isExpired = isSessionExpired(session, timeouts);
26+
const isExpired = isSessionExpired(session, {
27+
maxReplayDuration: replay.getOptions().maxReplayDuration,
28+
...replay.timeouts,
29+
});
3830

3931
if (!isExpired || (allowBuffering && session.shouldRefresh)) {
4032
return { type: 'saved', session };

packages/replay/src/types/replay.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export interface SendReplayData {
3131
export interface Timeouts {
3232
sessionIdlePause: number;
3333
sessionIdleExpire: number;
34-
maxSessionLife: number;
3534
}
3635

3736
/**
@@ -187,6 +186,12 @@ export interface ReplayPluginOptions extends ReplayNetworkOptions {
187186
*/
188187
minReplayDuration: number;
189188

189+
/**
190+
* The max. duration (in ms) a replay session may be.
191+
* This is capped at max. 60min.
192+
*/
193+
maxReplayDuration: number;
194+
190195
/**
191196
* Callback before adding a custom recording event
192197
*

packages/replay/src/util/addEvent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ export async function addEvent(
4141
}
4242

4343
// Throw out events that are +60min from the initial timestamp
44-
if (timestampInMs > replay.getContext().initialTimestamp + replay.timeouts.maxSessionLife) {
44+
if (timestampInMs > replay.getContext().initialTimestamp + replay.getOptions().maxReplayDuration) {
4545
logInfo(
46-
`[Replay] Skipping event with timestamp ${timestampInMs} because it is after maxSessionLife`,
46+
`[Replay] Skipping event with timestamp ${timestampInMs} because it is after maxReplayDuration`,
4747
replay.getOptions()._experiments.traceInternals,
4848
);
4949
return null;
Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,22 @@
1-
import type { Session, Timeouts } from '../types';
1+
import type { Session } from '../types';
22
import { isExpired } from './isExpired';
33

44
/**
55
* Checks to see if session is expired
66
*/
7-
export function isSessionExpired(session: Session, timeouts: Timeouts, targetTime: number = +new Date()): boolean {
7+
export function isSessionExpired(
8+
session: Session,
9+
{
10+
maxReplayDuration,
11+
sessionIdleExpire,
12+
targetTime = Date.now(),
13+
}: { maxReplayDuration: number; sessionIdleExpire: number; targetTime?: number },
14+
): boolean {
815
return (
916
// First, check that maximum session length has not been exceeded
10-
isExpired(session.started, timeouts.maxSessionLife, targetTime) ||
17+
isExpired(session.started, maxReplayDuration, targetTime) ||
1118
// check that the idle timeout has not been exceeded (i.e. user has
1219
// performed an action within the last `sessionIdleExpire` ms)
13-
isExpired(session.lastActivity, timeouts.sessionIdleExpire, targetTime)
20+
isExpired(session.lastActivity, sessionIdleExpire, targetTime)
1421
);
1522
}

0 commit comments

Comments
 (0)