Skip to content

Commit a091bdd

Browse files
authored
feat(replay): Expose recordingMode in public API (#14085)
1 parent 06ef628 commit a091bdd

File tree

3 files changed

+107
-2
lines changed

3 files changed

+107
-2
lines changed

packages/replay-internal/src/integration.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
import { parseSampleRate } from '@sentry/core';
2-
import type { BrowserClientReplayOptions, Client, Integration, IntegrationFn } from '@sentry/types';
2+
import type {
3+
BrowserClientReplayOptions,
4+
Client,
5+
Integration,
6+
IntegrationFn,
7+
ReplayRecordingMode,
8+
} from '@sentry/types';
39
import { consoleSandbox, dropUndefinedKeys, isBrowser } from '@sentry/utils';
410

511
import {
@@ -297,6 +303,22 @@ export class Replay implements Integration {
297303
return this._replay.getSessionId();
298304
}
299305

306+
/**
307+
* Get the current recording mode. This can be either `session` or `buffer`.
308+
*
309+
* `session`: Recording the whole session, sending it continuously
310+
* `buffer`: Always keeping the last 60s of recording, requires:
311+
* - having replaysOnErrorSampleRate > 0 to capture replay when an error occurs
312+
* - or calling `flush()` to send the replay
313+
*/
314+
public getRecordingMode(): ReplayRecordingMode | undefined {
315+
if (!this._replay || !this._replay.isEnabled()) {
316+
return;
317+
}
318+
319+
return this._replay.recordingMode;
320+
}
321+
300322
/**
301323
* Initializes replay.
302324
*/

packages/replay-internal/src/replay.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export class ReplayContainer implements ReplayContainerInterface {
7474
public clickDetector: ClickDetector | undefined;
7575

7676
/**
77-
* Recording can happen in one of three modes:
77+
* Recording can happen in one of two modes:
7878
* - session: Record the whole session, sending it continuously
7979
* - buffer: Always keep the last 60s of recording, requires:
8080
* - having replaysOnErrorSampleRate > 0 to capture replay when an error occurs
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/**
2+
* @vitest-environment jsdom
3+
*/
4+
5+
import { describe, expect, test } from 'vitest';
6+
import { resetSdkMock } from '../mocks/resetSdkMock';
7+
import { useFakeTimers } from '../utils/use-fake-timers';
8+
9+
useFakeTimers();
10+
11+
describe('Integration | getRecordingMode()', () => {
12+
test('returns "session" when session sampling is enabled', async () => {
13+
const { integration } = await resetSdkMock({
14+
replayOptions: {
15+
stickySession: false,
16+
},
17+
sentryOptions: {
18+
replaysSessionSampleRate: 1.0,
19+
},
20+
});
21+
expect(integration.getRecordingMode()).toBe('session');
22+
});
23+
24+
test('returns "buffer" when buffering is enabled', async () => {
25+
const { integration, replay } = await resetSdkMock({
26+
replayOptions: {
27+
stickySession: false,
28+
},
29+
sentryOptions: {
30+
replaysSessionSampleRate: 1.0,
31+
},
32+
});
33+
replay.stop();
34+
replay.startBuffering();
35+
expect(integration.getRecordingMode()).toBe('buffer');
36+
});
37+
38+
test('returns undefined when replay is stopped', async () => {
39+
const { integration, replay } = await resetSdkMock({
40+
replayOptions: {
41+
stickySession: false,
42+
},
43+
sentryOptions: {
44+
replaysSessionSampleRate: 1.0,
45+
},
46+
});
47+
replay.stop();
48+
expect(integration.getRecordingMode()).toBeUndefined();
49+
});
50+
51+
test('returns undefined when session sampling is disabled', async () => {
52+
const { integration } = await resetSdkMock({
53+
replayOptions: { stickySession: false },
54+
sentryOptions: {
55+
replaysSessionSampleRate: 0.0,
56+
replaysOnErrorSampleRate: 0.0,
57+
},
58+
});
59+
expect(integration.getRecordingMode()).toBeUndefined();
60+
});
61+
62+
test('returns "buffer" when session rate is 0 and onError rate is 1', async () => {
63+
const { integration } = await resetSdkMock({
64+
replayOptions: { stickySession: false },
65+
sentryOptions: {
66+
replaysSessionSampleRate: 0.0,
67+
replaysOnErrorSampleRate: 1.0,
68+
},
69+
});
70+
expect(integration.getRecordingMode()).toBe('buffer');
71+
});
72+
73+
test('returns "session" when both sampling rates are 1', async () => {
74+
const { integration } = await resetSdkMock({
75+
replayOptions: { stickySession: false },
76+
sentryOptions: {
77+
replaysSessionSampleRate: 1.0,
78+
replaysOnErrorSampleRate: 1.0,
79+
},
80+
});
81+
expect(integration.getRecordingMode()).toBe('session');
82+
});
83+
});

0 commit comments

Comments
 (0)