Skip to content

Commit

Permalink
feat(replay): Expose recordingMode in public API (#14085)
Browse files Browse the repository at this point in the history
  • Loading branch information
chargome authored Oct 29, 2024
1 parent 06ef628 commit a091bdd
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 deletions.
24 changes: 23 additions & 1 deletion packages/replay-internal/src/integration.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import { parseSampleRate } from '@sentry/core';
import type { BrowserClientReplayOptions, Client, Integration, IntegrationFn } from '@sentry/types';
import type {
BrowserClientReplayOptions,
Client,
Integration,
IntegrationFn,
ReplayRecordingMode,
} from '@sentry/types';
import { consoleSandbox, dropUndefinedKeys, isBrowser } from '@sentry/utils';

import {
Expand Down Expand Up @@ -297,6 +303,22 @@ export class Replay implements Integration {
return this._replay.getSessionId();
}

/**
* Get the current recording mode. This can be either `session` or `buffer`.
*
* `session`: Recording the whole session, sending it continuously
* `buffer`: Always keeping the last 60s of recording, requires:
* - having replaysOnErrorSampleRate > 0 to capture replay when an error occurs
* - or calling `flush()` to send the replay
*/
public getRecordingMode(): ReplayRecordingMode | undefined {
if (!this._replay || !this._replay.isEnabled()) {
return;
}

return this._replay.recordingMode;
}

/**
* Initializes replay.
*/
Expand Down
2 changes: 1 addition & 1 deletion packages/replay-internal/src/replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class ReplayContainer implements ReplayContainerInterface {
public clickDetector: ClickDetector | undefined;

/**
* Recording can happen in one of three modes:
* Recording can happen in one of two modes:
* - session: Record the whole session, sending it continuously
* - buffer: Always keep the last 60s of recording, requires:
* - having replaysOnErrorSampleRate > 0 to capture replay when an error occurs
Expand Down
83 changes: 83 additions & 0 deletions packages/replay-internal/test/integration/recordingMode.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* @vitest-environment jsdom
*/

import { describe, expect, test } from 'vitest';
import { resetSdkMock } from '../mocks/resetSdkMock';
import { useFakeTimers } from '../utils/use-fake-timers';

useFakeTimers();

describe('Integration | getRecordingMode()', () => {
test('returns "session" when session sampling is enabled', async () => {
const { integration } = await resetSdkMock({
replayOptions: {
stickySession: false,
},
sentryOptions: {
replaysSessionSampleRate: 1.0,
},
});
expect(integration.getRecordingMode()).toBe('session');
});

test('returns "buffer" when buffering is enabled', async () => {
const { integration, replay } = await resetSdkMock({
replayOptions: {
stickySession: false,
},
sentryOptions: {
replaysSessionSampleRate: 1.0,
},
});
replay.stop();
replay.startBuffering();
expect(integration.getRecordingMode()).toBe('buffer');
});

test('returns undefined when replay is stopped', async () => {
const { integration, replay } = await resetSdkMock({
replayOptions: {
stickySession: false,
},
sentryOptions: {
replaysSessionSampleRate: 1.0,
},
});
replay.stop();
expect(integration.getRecordingMode()).toBeUndefined();
});

test('returns undefined when session sampling is disabled', async () => {
const { integration } = await resetSdkMock({
replayOptions: { stickySession: false },
sentryOptions: {
replaysSessionSampleRate: 0.0,
replaysOnErrorSampleRate: 0.0,
},
});
expect(integration.getRecordingMode()).toBeUndefined();
});

test('returns "buffer" when session rate is 0 and onError rate is 1', async () => {
const { integration } = await resetSdkMock({
replayOptions: { stickySession: false },
sentryOptions: {
replaysSessionSampleRate: 0.0,
replaysOnErrorSampleRate: 1.0,
},
});
expect(integration.getRecordingMode()).toBe('buffer');
});

test('returns "session" when both sampling rates are 1', async () => {
const { integration } = await resetSdkMock({
replayOptions: { stickySession: false },
sentryOptions: {
replaysSessionSampleRate: 1.0,
replaysOnErrorSampleRate: 1.0,
},
});
expect(integration.getRecordingMode()).toBe('session');
});
});

0 comments on commit a091bdd

Please sign in to comment.