Skip to content

Commit

Permalink
fix(replay): Ensure replay_id is not captured when session is expir…
Browse files Browse the repository at this point in the history
…ed (#9109)

Closes #9106
  • Loading branch information
mydea authored Sep 25, 2023
1 parent c326e15 commit 618e992
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
6 changes: 6 additions & 0 deletions packages/replay/src/coreHandlers/handleGlobalEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export function handleGlobalEventListener(
return event;
}

// Ensure we do not add replay_id if the session is expired
const isSessionActive = replay.checkAndHandleExpiredSession();
if (!isSessionActive) {
return event;
}

// Unless `captureExceptions` is enabled, we want to ignore errors coming from rrweb
// As there can be a bunch of stuff going wrong in internals there, that we don't want to bubble up to users
if (isRrwebError(event, hint) && !replay.getOptions()._experiments.captureExceptions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { Event } from '@sentry/types';

import type { Replay as ReplayIntegration } from '../../../src';
import { REPLAY_EVENT_NAME } from '../../../src/constants';
import { REPLAY_EVENT_NAME, SESSION_IDLE_EXPIRE_DURATION } from '../../../src/constants';
import { handleGlobalEventListener } from '../../../src/coreHandlers/handleGlobalEvent';
import type { ReplayContainer } from '../../../src/replay';
import { makeSession } from '../../../src/session/Session';
import { Error } from '../../fixtures/error';
import { Transaction } from '../../fixtures/transaction';
import { resetSdkMock } from '../../mocks/resetSdkMock';
Expand Down Expand Up @@ -102,6 +103,32 @@ describe('Integration | coreHandlers | handleGlobalEvent', () => {
);
});

it('does not add replayId if replay session is expired', async () => {
const transaction = Transaction();
const error = Error();

const now = Date.now();

replay.session = makeSession({
id: 'test-session-id',
segmentId: 0,
lastActivity: now - SESSION_IDLE_EXPIRE_DURATION - 1,
started: now - SESSION_IDLE_EXPIRE_DURATION - 1,
sampled: 'session',
});

expect(handleGlobalEventListener(replay)(transaction, {})).toEqual(
expect.objectContaining({
tags: expect.not.objectContaining({ replayId: expect.anything() }),
}),
);
expect(handleGlobalEventListener(replay)(error, {})).toEqual(
expect.objectContaining({
tags: expect.not.objectContaining({ replayId: expect.anything() }),
}),
);
});

it('tags errors and transactions with replay id for session samples', async () => {
let integration: ReplayIntegration;
({ replay, integration } = await resetSdkMock({}));
Expand Down

0 comments on commit 618e992

Please sign in to comment.