Skip to content

Commit 5a3c29d

Browse files
authored
fix(replay): Ensure we do not set replayId on dsc if replay is disabled (#7939)
1 parent 24a235d commit 5a3c29d

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

packages/browser-integration-tests/suites/replay/dsc/init.js

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,3 @@ Sentry.init({
1616
replaysSessionSampleRate: 0.0,
1717
replaysOnErrorSampleRate: 1.0,
1818
});
19-
20-
Sentry.configureScope(scope => {
21-
scope.setUser({ id: 'user123', segment: 'segmentB' });
22-
scope.setTransactionName('testTransactionDSC');
23-
});

packages/browser-integration-tests/suites/replay/dsc/test.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect } from '@playwright/test';
2+
import type * as Sentry from '@sentry/browser';
23
import type { EventEnvelopeHeaders } from '@sentry/types';
34

45
import { sentryTest } from '../../../utils/fixtures';
@@ -9,6 +10,8 @@ import {
910
} from '../../../utils/helpers';
1011
import { getReplaySnapshot, shouldSkipReplayTest, waitForReplayRunning } from '../../../utils/replayHelpers';
1112

13+
type TestWindow = Window & { Sentry: typeof Sentry; Replay: Sentry.Replay };
14+
1215
sentryTest('should add replay_id to dsc of transactions', async ({ getLocalTestPath, page, browserName }) => {
1316
// This is flaky on webkit, so skipping there...
1417
if (shouldSkipReplayTest() || shouldSkipTracingTest() || browserName === 'webkit') {
@@ -18,6 +21,13 @@ sentryTest('should add replay_id to dsc of transactions', async ({ getLocalTestP
1821
const url = await getLocalTestPath({ testDir: __dirname });
1922
await page.goto(url);
2023

24+
await page.evaluate(() => {
25+
(window as unknown as TestWindow).Sentry.configureScope(scope => {
26+
scope.setUser({ id: 'user123', segment: 'segmentB' });
27+
scope.setTransactionName('testTransactionDSC');
28+
});
29+
});
30+
2131
const envHeader = await getFirstSentryEnvelopeRequest<EventEnvelopeHeaders>(page, url, envelopeHeaderRequestParser);
2232

2333
await waitForReplayRunning(page);
@@ -35,3 +45,41 @@ sentryTest('should add replay_id to dsc of transactions', async ({ getLocalTestP
3545
replay_id: replay.session?.id,
3646
});
3747
});
48+
49+
sentryTest(
50+
'should not add replay_id to dsc of transactions if replay is not enabled',
51+
async ({ getLocalTestPath, page, browserName }) => {
52+
// This is flaky on webkit, so skipping there...
53+
if (shouldSkipReplayTest() || shouldSkipTracingTest() || browserName === 'webkit') {
54+
sentryTest.skip();
55+
}
56+
57+
const url = await getLocalTestPath({ testDir: __dirname });
58+
await page.goto(url);
59+
60+
await page.evaluate(() => {
61+
(window as unknown as TestWindow).Replay.stop();
62+
63+
(window as unknown as TestWindow).Sentry.configureScope(scope => {
64+
scope.setUser({ id: 'user123', segment: 'segmentB' });
65+
scope.setTransactionName('testTransactionDSC');
66+
});
67+
});
68+
69+
const envHeader = await getFirstSentryEnvelopeRequest<EventEnvelopeHeaders>(page, url, envelopeHeaderRequestParser);
70+
71+
await waitForReplayRunning(page);
72+
const replay = await getReplaySnapshot(page);
73+
74+
expect(replay.session?.id).toBeDefined();
75+
76+
expect(envHeader.trace).toBeDefined();
77+
expect(envHeader.trace).toEqual({
78+
environment: 'production',
79+
user_segment: 'segmentB',
80+
sample_rate: '1',
81+
trace_id: expect.any(String),
82+
public_key: 'public',
83+
});
84+
},
85+
);

packages/replay/src/util/addGlobalListeners.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ export function addGlobalListeners(replay: ReplayContainer): void {
3535
client.on('afterSendEvent', handleAfterSendEvent(replay));
3636
client.on('createDsc', (dsc: DynamicSamplingContext) => {
3737
const replayId = replay.getSessionId();
38-
if (replayId) {
38+
if (replayId && replay.isEnabled()) {
3939
dsc.replay_id = replayId;
4040
}
4141
});

0 commit comments

Comments
 (0)