Skip to content

fix(replay): Ensure replay_id is not captured when session is expired #9109

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 1 commit into from
Sep 25, 2023
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
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