From 5edf92bd7ada803ceec8b1c5943b1e580c5e77da Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Wed, 12 Apr 2023 12:49:21 +0200 Subject: [PATCH 1/2] feat(replay): Add `getSessonId()` method --- packages/replay/README.md | 5 ++++ packages/replay/src/integration.ts | 11 ++++++++ .../test/integration/getSessionId.test.ts | 26 +++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 packages/replay/test/integration/getSessionId.test.ts diff --git a/packages/replay/README.md b/packages/replay/README.md index 80d568cff52e..e17680067a10 100644 --- a/packages/replay/README.md +++ b/packages/replay/README.md @@ -138,6 +138,11 @@ A session starts when the Session Replay SDK is first loaded and initialized. Th [^1]: An 'interaction' refers to either a mouse click or a browser navigation event. +### Accessing the Replay Session ID + +You can get the ID of the currently running session via `replay.getSessionId()`. +This will return `undefined` if no session is ongoing. + ### Replay Captures Only on Errors Alternatively, rather than recording an entire session, you can capture a replay only when an error occurs. In this case, the integration will buffer up to one minute worth of events prior to the error being thrown. It will continue to record the session following the rules above regarding session life and activity. Read the [sampling](#Sampling) section for configuration options. diff --git a/packages/replay/src/integration.ts b/packages/replay/src/integration.ts index c08eb0431cac..2c4601f6d73e 100644 --- a/packages/replay/src/integration.ts +++ b/packages/replay/src/integration.ts @@ -226,6 +226,17 @@ Sentry.init({ replaysOnErrorSampleRate: ${errorSampleRate} })`, return this._replay.flushImmediate(); } + /** + * Get the current session ID. + */ + public getSessionId(): string | undefined { + if (!this._replay || !this._replay.isEnabled()) { + return; + } + + return this._replay.getSessionId(); + } + /** Setup the integration. */ private _setup(): void { // Client is not available in constructor, so we need to wait until setupOnce diff --git a/packages/replay/test/integration/getSessionId.test.ts b/packages/replay/test/integration/getSessionId.test.ts new file mode 100644 index 000000000000..50cc7cf4a37a --- /dev/null +++ b/packages/replay/test/integration/getSessionId.test.ts @@ -0,0 +1,26 @@ +import { mockSdk } from '../mocks/mockSdk'; +import { useFakeTimers } from '../utils/use-fake-timers'; + +useFakeTimers(); + +describe('Integration | getSessionId', () => { + afterEach(() => { + jest.clearAllMocks(); + }); + + it('works', async () => { + const { integration, replay } = await mockSdk({ + replayOptions: { + stickySession: true, + }, + }); + + expect(integration.getSessionId()).toBeDefined(); + expect(integration.getSessionId()).toEqual(replay.session?.id); + + // When stopped, it is undefined + integration.stop(); + + expect(integration.getSessionId()).toBeUndefined(); + }); +}); From 2956dc07d958f5af03249f952f9c273a047d24e0 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Wed, 12 Apr 2023 16:02:22 +0200 Subject: [PATCH 2/2] rename to `getReplayId` --- packages/replay/README.md | 2 +- packages/replay/src/integration.ts | 2 +- .../{getSessionId.test.ts => getReplayId.test.ts} | 8 ++++---- 3 files changed, 6 insertions(+), 6 deletions(-) rename packages/replay/test/integration/{getSessionId.test.ts => getReplayId.test.ts} (63%) diff --git a/packages/replay/README.md b/packages/replay/README.md index e17680067a10..8c0ea81e5cba 100644 --- a/packages/replay/README.md +++ b/packages/replay/README.md @@ -140,7 +140,7 @@ A session starts when the Session Replay SDK is first loaded and initialized. Th ### Accessing the Replay Session ID -You can get the ID of the currently running session via `replay.getSessionId()`. +You can get the ID of the currently running session via `replay.getReplayId()`. This will return `undefined` if no session is ongoing. ### Replay Captures Only on Errors diff --git a/packages/replay/src/integration.ts b/packages/replay/src/integration.ts index 2c4601f6d73e..f426a9051f97 100644 --- a/packages/replay/src/integration.ts +++ b/packages/replay/src/integration.ts @@ -229,7 +229,7 @@ Sentry.init({ replaysOnErrorSampleRate: ${errorSampleRate} })`, /** * Get the current session ID. */ - public getSessionId(): string | undefined { + public getReplayId(): string | undefined { if (!this._replay || !this._replay.isEnabled()) { return; } diff --git a/packages/replay/test/integration/getSessionId.test.ts b/packages/replay/test/integration/getReplayId.test.ts similarity index 63% rename from packages/replay/test/integration/getSessionId.test.ts rename to packages/replay/test/integration/getReplayId.test.ts index 50cc7cf4a37a..1080186974fc 100644 --- a/packages/replay/test/integration/getSessionId.test.ts +++ b/packages/replay/test/integration/getReplayId.test.ts @@ -3,7 +3,7 @@ import { useFakeTimers } from '../utils/use-fake-timers'; useFakeTimers(); -describe('Integration | getSessionId', () => { +describe('Integration | getReplayId', () => { afterEach(() => { jest.clearAllMocks(); }); @@ -15,12 +15,12 @@ describe('Integration | getSessionId', () => { }, }); - expect(integration.getSessionId()).toBeDefined(); - expect(integration.getSessionId()).toEqual(replay.session?.id); + expect(integration.getReplayId()).toBeDefined(); + expect(integration.getReplayId()).toEqual(replay.session?.id); // When stopped, it is undefined integration.stop(); - expect(integration.getSessionId()).toBeUndefined(); + expect(integration.getReplayId()).toBeUndefined(); }); });