From da5b2e09acd6f10243b528cc2e99d2fb70494161 Mon Sep 17 00:00:00 2001 From: Charly Gomez Date: Tue, 19 Nov 2024 09:40:22 +0100 Subject: [PATCH] fix(replay): Remove replay id from DSC on expired sessions (#14342) --- .../src/coreHandlers/handleGlobalEvent.ts | 3 +++ .../coreHandlers/handleGlobalEvent.test.ts | 18 ++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/packages/replay-internal/src/coreHandlers/handleGlobalEvent.ts b/packages/replay-internal/src/coreHandlers/handleGlobalEvent.ts index d0ea607e1c06..6ba64244fddf 100644 --- a/packages/replay-internal/src/coreHandlers/handleGlobalEvent.ts +++ b/packages/replay-internal/src/coreHandlers/handleGlobalEvent.ts @@ -5,6 +5,7 @@ import type { ReplayContainer } from '../types'; import { isErrorEvent, isFeedbackEvent, isReplayEvent, isTransactionEvent } from '../util/eventUtils'; import { isRrwebError } from '../util/isRrwebError'; import { logger } from '../util/logger'; +import { resetReplayIdOnDynamicSamplingContext } from '../util/resetReplayIdOnDynamicSamplingContext'; import { addFeedbackBreadcrumb } from './util/addFeedbackBreadcrumb'; import { shouldSampleForBufferEvent } from './util/shouldSampleForBufferEvent'; @@ -34,6 +35,8 @@ export function handleGlobalEventListener(replay: ReplayContainer): (event: Even // Ensure we do not add replay_id if the session is expired const isSessionActive = replay.checkAndHandleExpiredSession(); if (!isSessionActive) { + // prevent exceeding replay durations by removing the expired replayId from the DSC + resetReplayIdOnDynamicSamplingContext(); return event; } diff --git a/packages/replay-internal/test/integration/coreHandlers/handleGlobalEvent.test.ts b/packages/replay-internal/test/integration/coreHandlers/handleGlobalEvent.test.ts index 9e888568d04d..a3ad967e1586 100644 --- a/packages/replay-internal/test/integration/coreHandlers/handleGlobalEvent.test.ts +++ b/packages/replay-internal/test/integration/coreHandlers/handleGlobalEvent.test.ts @@ -11,6 +11,7 @@ import { REPLAY_EVENT_NAME, SESSION_IDLE_EXPIRE_DURATION } from '../../../src/co import { handleGlobalEventListener } from '../../../src/coreHandlers/handleGlobalEvent'; import type { ReplayContainer } from '../../../src/replay'; import { makeSession } from '../../../src/session/Session'; +import * as resetReplayIdOnDynamicSamplingContextModule from '../../../src/util/resetReplayIdOnDynamicSamplingContext'; import { Error } from '../../fixtures/error'; import { Transaction } from '../../fixtures/transaction'; import { resetSdkMock } from '../../mocks/resetSdkMock'; @@ -416,4 +417,21 @@ describe('Integration | coreHandlers | handleGlobalEvent', () => { }), ); }); + + it('resets replayId on DSC when session expires', () => { + const errorEvent = Error(); + const txEvent = Transaction(); + + vi.spyOn(replay, 'checkAndHandleExpiredSession').mockReturnValue(false); + + const resetReplayIdSpy = vi.spyOn( + resetReplayIdOnDynamicSamplingContextModule, + 'resetReplayIdOnDynamicSamplingContext', + ); + + handleGlobalEventListener(replay)(errorEvent, {}); + handleGlobalEventListener(replay)(txEvent, {}); + + expect(resetReplayIdSpy).toHaveBeenCalledTimes(2); + }); });