Skip to content

Commit b03a32b

Browse files
authored
ref(replay): Extract flush min and max delay default values to constants (#6612)
Extract the default values of flush min and max delays into constants
1 parent b674c26 commit b03a32b

File tree

6 files changed

+49
-36
lines changed

6 files changed

+49
-36
lines changed

packages/replay/src/constants.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,8 @@ export const DEFAULT_ERROR_SAMPLE_RATE = 1.0;
2828

2929
/** The select to use for the `maskAllText` option */
3030
export const MASK_ALL_TEXT_SELECTOR = 'body *:not(style), body *:not(script)';
31+
32+
/** Default flush delays */
33+
export const DEFAULT_FLUSH_MIN_DELAY = 5_000;
34+
export const DEFAULT_FLUSH_MAX_DELAY = 15_000;
35+
export const INITIAL_FLUSH_DELAY = 5_000;

packages/replay/src/integration.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,14 @@ import { getCurrentHub } from '@sentry/core';
22
import type { BrowserClientReplayOptions } from '@sentry/types';
33
import { Integration } from '@sentry/types';
44

5-
import { DEFAULT_ERROR_SAMPLE_RATE, DEFAULT_SESSION_SAMPLE_RATE, MASK_ALL_TEXT_SELECTOR } from './constants';
5+
import {
6+
DEFAULT_ERROR_SAMPLE_RATE,
7+
DEFAULT_FLUSH_MAX_DELAY,
8+
DEFAULT_FLUSH_MIN_DELAY,
9+
DEFAULT_SESSION_SAMPLE_RATE,
10+
INITIAL_FLUSH_DELAY,
11+
MASK_ALL_TEXT_SELECTOR,
12+
} from './constants';
613
import { ReplayContainer } from './replay';
714
import type { RecordingOptions, ReplayConfiguration, ReplayPluginOptions } from './types';
815
import { isBrowser } from './util/isBrowser';
@@ -40,9 +47,9 @@ export class Replay implements Integration {
4047
private _replay?: ReplayContainer;
4148

4249
constructor({
43-
flushMinDelay = 5000,
44-
flushMaxDelay = 15000,
45-
initialFlushDelay = 5000,
50+
flushMinDelay = DEFAULT_FLUSH_MIN_DELAY,
51+
flushMaxDelay = DEFAULT_FLUSH_MAX_DELAY,
52+
initialFlushDelay = INITIAL_FLUSH_DELAY,
4653
stickySession = true,
4754
useCompression = true,
4855
sessionSampleRate,

packages/replay/test/unit/flush.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as SentryUtils from '@sentry/utils';
22

3-
import { SESSION_IDLE_DURATION, WINDOW } from '../../src/constants';
3+
import { DEFAULT_FLUSH_MIN_DELAY, SESSION_IDLE_DURATION, WINDOW } from '../../src/constants';
44
import * as AddMemoryEntry from '../../src/util/addMemoryEntry';
55
import { createPerformanceSpans } from '../../src/util/createPerformanceSpans';
66
import { createPerformanceEntries } from './../../src/createPerformanceEntry';
@@ -145,7 +145,7 @@ it('long first flush enqueues following events', async () => {
145145
domHandler({
146146
name: 'click',
147147
});
148-
await advanceTimers(5000);
148+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
149149
// flush #2 @ t=5s - due to click
150150
expect(replay.flush).toHaveBeenCalledTimes(2);
151151

@@ -212,7 +212,7 @@ it('long first flush enqueues following events', async () => {
212212
});
213213
// flush #5 @ t=25s - debounced flush calls `flush`
214214
// 20s + `flushMinDelay` which is 5 seconds
215-
await advanceTimers(5000);
215+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
216216
expect(replay.flush).toHaveBeenCalledTimes(5);
217217
expect(replay.runFlush).toHaveBeenCalledTimes(2);
218218
expect(mockSendReplay).toHaveBeenLastCalledWith({

packages/replay/test/unit/index-errorSampleRate.test.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { captureException } from '@sentry/core';
22

3-
import { REPLAY_SESSION_KEY, VISIBILITY_CHANGE_TIMEOUT, WINDOW } from '../../src/constants';
3+
import { DEFAULT_FLUSH_MIN_DELAY, REPLAY_SESSION_KEY, VISIBILITY_CHANGE_TIMEOUT, WINDOW } from '../../src/constants';
44
import { addEvent } from '../../src/util/addEvent';
55
import { ReplayContainer } from './../../src/replay';
66
import { PerformanceEntryResource } from './../fixtures/performanceEntry/resource';
@@ -54,7 +54,7 @@ describe('Replay (errorSampleRate)', () => {
5454
expect(replay).not.toHaveLastSentReplay();
5555

5656
captureException(new Error('testing'));
57-
jest.advanceTimersByTime(5000);
57+
jest.advanceTimersByTime(DEFAULT_FLUSH_MIN_DELAY);
5858
await new Promise(process.nextTick);
5959

6060
expect(replay).toHaveSentReplay({
@@ -99,15 +99,15 @@ describe('Replay (errorSampleRate)', () => {
9999
events: JSON.stringify([{ data: { isCheckout: true }, timestamp: BASE_TIMESTAMP + 5020, type: 2 }]),
100100
});
101101

102-
jest.advanceTimersByTime(5000);
102+
jest.advanceTimersByTime(DEFAULT_FLUSH_MIN_DELAY);
103103

104104
// New checkout when we call `startRecording` again after uploading segment
105105
// after an error occurs
106106
expect(replay).toHaveLastSentReplay({
107107
events: JSON.stringify([
108108
{
109109
data: { isCheckout: true },
110-
timestamp: BASE_TIMESTAMP + 5000 + 20,
110+
timestamp: BASE_TIMESTAMP + DEFAULT_FLUSH_MIN_DELAY + 20,
111111
type: 2,
112112
},
113113
]),
@@ -118,7 +118,7 @@ describe('Replay (errorSampleRate)', () => {
118118
name: 'click',
119119
});
120120

121-
jest.advanceTimersByTime(5000);
121+
jest.advanceTimersByTime(DEFAULT_FLUSH_MIN_DELAY);
122122
await new Promise(process.nextTick);
123123

124124
expect(replay).toHaveLastSentReplay({
@@ -245,12 +245,12 @@ describe('Replay (errorSampleRate)', () => {
245245
expect(replay).not.toHaveLastSentReplay();
246246

247247
// There should also not be another attempt at an upload 5 seconds after the last replay event
248-
await advanceTimers(5000);
248+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
249249
expect(replay).not.toHaveLastSentReplay();
250250

251251
// Let's make sure it continues to work
252252
mockRecord._emitter(TEST_EVENT);
253-
await advanceTimers(5000);
253+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
254254
jest.runAllTimers();
255255
await new Promise(process.nextTick);
256256
expect(replay).not.toHaveLastSentReplay();
@@ -294,11 +294,11 @@ describe('Replay (errorSampleRate)', () => {
294294
jest.runAllTimers();
295295
await new Promise(process.nextTick);
296296

297-
jest.advanceTimersByTime(5000);
297+
jest.advanceTimersByTime(DEFAULT_FLUSH_MIN_DELAY);
298298

299299
captureException(new Error('testing'));
300300

301-
jest.advanceTimersByTime(5000);
301+
jest.advanceTimersByTime(DEFAULT_FLUSH_MIN_DELAY);
302302
await new Promise(process.nextTick);
303303

304304
expect(replay).toHaveSentReplay({
@@ -309,7 +309,7 @@ describe('Replay (errorSampleRate)', () => {
309309
// (advance timers + waiting for flush after the checkout) and
310310
// extra time is likely due to async of `addMemoryEntry()`
311311

312-
timestamp: (BASE_TIMESTAMP + 5000 + 5000 + 20) / 1000,
312+
timestamp: (BASE_TIMESTAMP + DEFAULT_FLUSH_MIN_DELAY + DEFAULT_FLUSH_MIN_DELAY + 20) / 1000,
313313
error_ids: [expect.any(String)],
314314
trace_ids: [],
315315
urls: ['http://localhost/'],
@@ -400,7 +400,7 @@ it('sends a replay after loading the session multiple times', async () => {
400400

401401
captureException(new Error('testing'));
402402

403-
jest.advanceTimersByTime(5000);
403+
jest.advanceTimersByTime(DEFAULT_FLUSH_MIN_DELAY);
404404
await new Promise(process.nextTick);
405405

406406
expect(replay).toHaveSentReplay({

packages/replay/test/unit/index-noSticky.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { getCurrentHub } from '@sentry/core';
22
import { Transport } from '@sentry/types';
33
import * as SentryUtils from '@sentry/utils';
44

5-
import { SESSION_IDLE_DURATION, VISIBILITY_CHANGE_TIMEOUT } from '../../src/constants';
5+
import { DEFAULT_FLUSH_MIN_DELAY, SESSION_IDLE_DURATION, VISIBILITY_CHANGE_TIMEOUT } from '../../src/constants';
66
import { addEvent } from '../../src/util/addEvent';
77
import { ReplayContainer } from './../../src/replay';
88
import { BASE_TIMESTAMP, mockRrweb, mockSdk } from './../index';
@@ -197,7 +197,7 @@ describe('Replay (no sticky)', () => {
197197

198198
// There should also not be another attempt at an upload 5 seconds after the last replay event
199199
mockTransport.mockClear();
200-
await advanceTimers(5000);
200+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
201201
expect(replay).not.toHaveLastSentReplay();
202202

203203
expect(replay.session?.lastActivity).toBe(BASE_TIMESTAMP);
@@ -208,7 +208,7 @@ describe('Replay (no sticky)', () => {
208208
// Let's make sure it continues to work
209209
mockTransport.mockClear();
210210
mockRecord._emitter(TEST_EVENT);
211-
await advanceTimers(5000);
211+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
212212
expect(replay).toHaveLastSentReplay({ events: JSON.stringify([TEST_EVENT]) });
213213
});
214214

@@ -252,7 +252,7 @@ describe('Replay (no sticky)', () => {
252252
name: 'click',
253253
});
254254

255-
await advanceTimers(5000);
255+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
256256

257257
const newTimestamp = BASE_TIMESTAMP + FIFTEEN_MINUTES;
258258
const breadcrumbTimestamp = newTimestamp + 20; // I don't know where this 20ms comes from

packages/replay/test/unit/index.test.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Event, Scope } from '@sentry/types';
33
import { EventType } from 'rrweb';
44

55
import {
6+
DEFAULT_FLUSH_MIN_DELAY,
67
MASK_ALL_TEXT_SELECTOR,
78
MAX_SESSION_LIFE,
89
REPLAY_SESSION_KEY,
@@ -336,7 +337,7 @@ describe('Replay', () => {
336337

337338
// There should also not be another attempt at an upload 5 seconds after the last replay event
338339
mockTransportSend.mockClear();
339-
await advanceTimers(5000);
340+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
340341

341342
expect(replay).not.toHaveLastSentReplay();
342343

@@ -348,7 +349,7 @@ describe('Replay', () => {
348349
// Let's make sure it continues to work
349350
mockTransportSend.mockClear();
350351
mockRecord._emitter(TEST_EVENT);
351-
await advanceTimers(5000);
352+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
352353
expect(replay).toHaveLastSentReplay({ events: JSON.stringify([TEST_EVENT]) });
353354
});
354355

@@ -402,7 +403,7 @@ describe('Replay', () => {
402403
name: 'click',
403404
});
404405

405-
await advanceTimers(5000);
406+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
406407

407408
const newTimestamp = BASE_TIMESTAMP + FIFTEEN_MINUTES;
408409
const breadcrumbTimestamp = newTimestamp + 20; // I don't know where this 20ms comes from
@@ -479,7 +480,7 @@ describe('Replay', () => {
479480
});
480481

481482
WINDOW.dispatchEvent(new Event('blur'));
482-
await advanceTimers(5000);
483+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
483484

484485
expect(mockRecord.takeFullSnapshot).not.toHaveBeenCalled();
485486
expect(replay).not.toHaveLastSentReplay();
@@ -498,7 +499,7 @@ describe('Replay', () => {
498499

499500
const NEW_TEST_EVENT = {
500501
data: { name: 'test' },
501-
timestamp: BASE_TIMESTAMP + MAX_SESSION_LIFE + 5000 + 20,
502+
timestamp: BASE_TIMESTAMP + MAX_SESSION_LIFE + DEFAULT_FLUSH_MIN_DELAY + 20,
502503
type: 3,
503504
};
504505

@@ -509,9 +510,9 @@ describe('Replay', () => {
509510
await new Promise(process.nextTick);
510511

511512
expect(replay).not.toHaveSameSession(initialSession);
512-
await advanceTimers(5000);
513+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
513514

514-
const newTimestamp = BASE_TIMESTAMP + MAX_SESSION_LIFE + 5000 + 20; // I don't know where this 20ms comes from
515+
const newTimestamp = BASE_TIMESTAMP + MAX_SESSION_LIFE + DEFAULT_FLUSH_MIN_DELAY + 20; // I don't know where this 20ms comes from
515516
const breadcrumbTimestamp = newTimestamp;
516517

517518
jest.runAllTimers();
@@ -591,13 +592,13 @@ describe('Replay', () => {
591592
throw new Error('Something bad happened');
592593
});
593594
mockRecord._emitter(TEST_EVENT);
594-
await advanceTimers(5000);
595+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
595596

596597
expect(mockRecord.takeFullSnapshot).not.toHaveBeenCalled();
597598
mockTransportSend.mockImplementationOnce(() => {
598599
throw new Error('Something bad happened');
599600
});
600-
await advanceTimers(5000);
601+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
601602

602603
// next tick should retry and succeed
603604
mockConsole.mockRestore();
@@ -625,7 +626,7 @@ describe('Replay', () => {
625626
expect(replay.session?.segmentId).toBe(1);
626627

627628
// next tick should do nothing
628-
await advanceTimers(5000);
629+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
629630
expect(replay).not.toHaveLastSentReplay();
630631
});
631632

@@ -648,12 +649,12 @@ describe('Replay', () => {
648649
});
649650
mockRecord._emitter(TEST_EVENT);
650651

651-
await advanceTimers(5000);
652+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
652653

653654
expect(mockRecord.takeFullSnapshot).not.toHaveBeenCalled();
654655
expect(replay.sendReplayRequest).toHaveBeenCalledTimes(1);
655656

656-
await advanceTimers(5000);
657+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
657658
expect(replay.sendReplayRequest).toHaveBeenCalledTimes(2);
658659

659660
await advanceTimers(10000);
@@ -865,11 +866,11 @@ describe('Replay', () => {
865866
const TEST_EVENT = { data: {}, timestamp: BASE_TIMESTAMP, type: 2 };
866867
mockRecord._emitter(TEST_EVENT);
867868

868-
await advanceTimers(5000);
869+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
869870
expect(replay.flush).toHaveBeenCalledTimes(1);
870871

871872
// Make sure there's nothing queued up after
872-
await advanceTimers(5000);
873+
await advanceTimers(DEFAULT_FLUSH_MIN_DELAY);
873874
expect(replay.flush).toHaveBeenCalledTimes(1);
874875
});
875876
});

0 commit comments

Comments
 (0)