From 5b88dc5ce71af4a41a441807ee5fba72fd450ad0 Mon Sep 17 00:00:00 2001 From: Charly Gomez Date: Mon, 18 Nov 2024 12:29:43 +0100 Subject: [PATCH 1/2] remove replay id from dsc on expired sessions --- .../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..4f9bb07a06b7 100644 --- a/packages/replay-internal/src/coreHandlers/handleGlobalEvent.ts +++ b/packages/replay-internal/src/coreHandlers/handleGlobalEvent.ts @@ -7,6 +7,7 @@ import { isRrwebError } from '../util/isRrwebError'; import { logger } from '../util/logger'; import { addFeedbackBreadcrumb } from './util/addFeedbackBreadcrumb'; import { shouldSampleForBufferEvent } from './util/shouldSampleForBufferEvent'; +import { resetReplayIdOnDynamicSamplingContext } from '../util/resetReplayIdOnDynamicSamplingContext'; /** * Returns a listener to be added to `addEventProcessor(listener)`. @@ -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..297fe65dbb8e 100644 --- a/packages/replay-internal/test/integration/coreHandlers/handleGlobalEvent.test.ts +++ b/packages/replay-internal/test/integration/coreHandlers/handleGlobalEvent.test.ts @@ -15,6 +15,7 @@ import { Error } from '../../fixtures/error'; import { Transaction } from '../../fixtures/transaction'; import { resetSdkMock } from '../../mocks/resetSdkMock'; import { useFakeTimers } from '../../utils/use-fake-timers'; +import * as resetReplayIdOnDynamicSamplingContextModule from '../../../src/util/resetReplayIdOnDynamicSamplingContext'; useFakeTimers(); let replay: ReplayContainer; @@ -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); + }); }); From 3c638bd33c12563540942eeafb0dd8046ddd6f91 Mon Sep 17 00:00:00 2001 From: Charly Gomez Date: Mon, 18 Nov 2024 15:39:01 +0100 Subject: [PATCH 2/2] organize imports --- packages/replay-internal/src/coreHandlers/handleGlobalEvent.ts | 2 +- .../test/integration/coreHandlers/handleGlobalEvent.test.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/replay-internal/src/coreHandlers/handleGlobalEvent.ts b/packages/replay-internal/src/coreHandlers/handleGlobalEvent.ts index 4f9bb07a06b7..6ba64244fddf 100644 --- a/packages/replay-internal/src/coreHandlers/handleGlobalEvent.ts +++ b/packages/replay-internal/src/coreHandlers/handleGlobalEvent.ts @@ -5,9 +5,9 @@ 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'; -import { resetReplayIdOnDynamicSamplingContext } from '../util/resetReplayIdOnDynamicSamplingContext'; /** * Returns a listener to be added to `addEventProcessor(listener)`. diff --git a/packages/replay-internal/test/integration/coreHandlers/handleGlobalEvent.test.ts b/packages/replay-internal/test/integration/coreHandlers/handleGlobalEvent.test.ts index 297fe65dbb8e..a3ad967e1586 100644 --- a/packages/replay-internal/test/integration/coreHandlers/handleGlobalEvent.test.ts +++ b/packages/replay-internal/test/integration/coreHandlers/handleGlobalEvent.test.ts @@ -11,11 +11,11 @@ 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'; import { useFakeTimers } from '../../utils/use-fake-timers'; -import * as resetReplayIdOnDynamicSamplingContextModule from '../../../src/util/resetReplayIdOnDynamicSamplingContext'; useFakeTimers(); let replay: ReplayContainer;