Skip to content

Commit 959bffd

Browse files
committed
feat(replay): Do not capture replays < 5 seconds
Promotes the feature from #7949 to GA. Do not immediately flush on snapshot checkouts, instead delay by minimum flush delay (5 seconds). This means that we will not collect replays < 5 seconds. e.g. User opens site and immediately closes the tab. Closes getsentry/team-replay#65
1 parent b877c10 commit 959bffd

File tree

6 files changed

+160
-970
lines changed

6 files changed

+160
-970
lines changed

packages/replay/src/replay.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,13 @@ export class ReplayContainer implements ReplayContainerInterface {
524524
return this.flushImmediate();
525525
}
526526

527+
/**
528+
* Flush using debounce flush
529+
*/
530+
public flush(): Promise<void> {
531+
return this._debouncedFlush() as Promise<void>;
532+
}
533+
527534
/**
528535
* Always flush via `_debouncedFlush` so that we do not have flushes triggered
529536
* from calling both `flush` and `_debouncedFlush`. Otherwise, there could be

packages/replay/src/types/replay.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ export interface ReplayPluginOptions extends ReplayNetworkOptions {
193193
_experiments: Partial<{
194194
captureExceptions: boolean;
195195
traceInternals: boolean;
196-
delayFlushOnCheckout: number;
197196
}>;
198197
}
199198

@@ -430,6 +429,7 @@ export interface ReplayContainer {
430429
stopRecording(): boolean;
431430
sendBufferedReplayOrFlush(options?: SendBufferedReplayOptions): Promise<void>;
432431
conditionalFlush(): Promise<void>;
432+
flush(): Promise<void>;
433433
flushImmediate(): Promise<void>;
434434
cancelFlush(): void;
435435
triggerUserActivity(): void;

packages/replay/src/util/handleRecordingEmit.ts

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -80,41 +80,12 @@ export function getHandleRecordingEmit(replay: ReplayContainer): RecordingEmitCa
8080
}
8181
}
8282

83-
const options = replay.getOptions();
84-
85-
// TODO: We want this as an experiment so that we can test
86-
// internally and create metrics before making this the default
87-
if (options._experiments.delayFlushOnCheckout) {
83+
if (replay.recordingMode === 'session') {
8884
// If the full snapshot is due to an initial load, we will not have
8985
// a previous session ID. In this case, we want to buffer events
9086
// for a set amount of time before flushing. This can help avoid
9187
// capturing replays of users that immediately close the window.
92-
// TODO: We should check `recordingMode` here and do nothing if it's
93-
// buffer, instead of checking inside of timeout, this will make our
94-
// tests a bit cleaner as we will need to wait on the delay in order to
95-
// do nothing.
96-
setTimeout(() => replay.conditionalFlush(), options._experiments.delayFlushOnCheckout);
97-
98-
// Cancel any previously debounced flushes to ensure there are no [near]
99-
// simultaneous flushes happening. The latter request should be
100-
// insignificant in this case, so wait for additional user interaction to
101-
// trigger a new flush.
102-
//
103-
// This can happen because there's no guarantee that a recording event
104-
// happens first. e.g. a mouse click can happen and trigger a debounced
105-
// flush before the checkout.
106-
replay.cancelFlush();
107-
108-
return true;
109-
}
110-
111-
// Flush immediately so that we do not miss the first segment, otherwise
112-
// it can prevent loading on the UI. This will cause an increase in short
113-
// replays (e.g. opening and closing a tab quickly), but these can be
114-
// filtered on the UI.
115-
if (replay.recordingMode === 'session') {
116-
// We want to ensure the worker is ready, as otherwise we'd always send the first event uncompressed
117-
void replay.flushImmediate();
88+
void replay.flush();
11889
}
11990

12091
return true;

packages/replay/test/integration/coreHandlers/handleAfterSendEvent.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,13 @@ describe('Integration | coreHandlers | handleAfterSendEvent', () => {
148148

149149
jest.runAllTimers();
150150
await new Promise(process.nextTick);
151-
152151
// Send twice, one for the error & one right after for the session conversion
152+
expect(mockSend).toHaveBeenCalledTimes(1);
153+
154+
jest.runAllTimers();
155+
await new Promise(process.nextTick);
153156
expect(mockSend).toHaveBeenCalledTimes(2);
157+
154158
// This is removed now, because it has been converted to a "session" session
155159
expect(Array.from(replay.getContext().errorIds)).toEqual([]);
156160
expect(replay.isEnabled()).toBe(true);

0 commit comments

Comments
 (0)