Skip to content

Commit 3cdb13f

Browse files
committed
fix(replay): Ensure we do not set replayId on dsc if replay is disabled
1 parent 15d9102 commit 3cdb13f

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-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: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { expect } from '@playwright/test';
22
import type { EventEnvelopeHeaders } from '@sentry/types';
3+
import type * as Sentry from '@sentry/browser';
34

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

13+
declare global {
14+
interface Window {
15+
Sentry: typeof Sentry;
16+
Replay: Sentry.Replay;
17+
}
18+
}
19+
1220
sentryTest('should add replay_id to dsc of transactions', async ({ getLocalTestPath, page, browserName }) => {
1321
// This is flaky on webkit, so skipping there...
1422
if (shouldSkipReplayTest() || shouldSkipTracingTest() || browserName === 'webkit') {
@@ -18,6 +26,13 @@ sentryTest('should add replay_id to dsc of transactions', async ({ getLocalTestP
1826
const url = await getLocalTestPath({ testDir: __dirname });
1927
await page.goto(url);
2028

29+
await page.evaluate(() => {
30+
window.Sentry.configureScope(scope => {
31+
scope.setUser({ id: 'user123', segment: 'segmentB' });
32+
scope.setTransactionName('testTransactionDSC');
33+
});
34+
});
35+
2136
const envHeader = await getFirstSentryEnvelopeRequest<EventEnvelopeHeaders>(page, url, envelopeHeaderRequestParser);
2237

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

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)