Skip to content

feat(replay): Expose recordingMode in public API #14085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
});
});
Loading