From 9db877a1d2509d4b36e71ec687308807ad9a85e7 Mon Sep 17 00:00:00 2001 From: Francesco Novy Date: Mon, 24 Apr 2023 08:57:54 +0200 Subject: [PATCH] fix(replay): Ensure we do not set replayId on dsc if replay is disabled --- .../suites/replay/dsc/init.js | 5 -- .../suites/replay/dsc/test.ts | 48 +++++++++++++++++++ .../replay/src/util/addGlobalListeners.ts | 2 +- 3 files changed, 49 insertions(+), 6 deletions(-) diff --git a/packages/browser-integration-tests/suites/replay/dsc/init.js b/packages/browser-integration-tests/suites/replay/dsc/init.js index c43f001779eb..da0177961ea2 100644 --- a/packages/browser-integration-tests/suites/replay/dsc/init.js +++ b/packages/browser-integration-tests/suites/replay/dsc/init.js @@ -16,8 +16,3 @@ Sentry.init({ replaysSessionSampleRate: 0.0, replaysOnErrorSampleRate: 1.0, }); - -Sentry.configureScope(scope => { - scope.setUser({ id: 'user123', segment: 'segmentB' }); - scope.setTransactionName('testTransactionDSC'); -}); diff --git a/packages/browser-integration-tests/suites/replay/dsc/test.ts b/packages/browser-integration-tests/suites/replay/dsc/test.ts index f4cca11e2339..83e95d84b9d5 100644 --- a/packages/browser-integration-tests/suites/replay/dsc/test.ts +++ b/packages/browser-integration-tests/suites/replay/dsc/test.ts @@ -1,4 +1,5 @@ import { expect } from '@playwright/test'; +import type * as Sentry from '@sentry/browser'; import type { EventEnvelopeHeaders } from '@sentry/types'; import { sentryTest } from '../../../utils/fixtures'; @@ -9,6 +10,8 @@ import { } from '../../../utils/helpers'; import { getReplaySnapshot, shouldSkipReplayTest, waitForReplayRunning } from '../../../utils/replayHelpers'; +type TestWindow = Window & { Sentry: typeof Sentry; Replay: Sentry.Replay }; + sentryTest('should add replay_id to dsc of transactions', async ({ getLocalTestPath, page, browserName }) => { // This is flaky on webkit, so skipping there... if (shouldSkipReplayTest() || shouldSkipTracingTest() || browserName === 'webkit') { @@ -18,6 +21,13 @@ sentryTest('should add replay_id to dsc of transactions', async ({ getLocalTestP const url = await getLocalTestPath({ testDir: __dirname }); await page.goto(url); + await page.evaluate(() => { + (window as unknown as TestWindow).Sentry.configureScope(scope => { + scope.setUser({ id: 'user123', segment: 'segmentB' }); + scope.setTransactionName('testTransactionDSC'); + }); + }); + const envHeader = await getFirstSentryEnvelopeRequest(page, url, envelopeHeaderRequestParser); await waitForReplayRunning(page); @@ -35,3 +45,41 @@ sentryTest('should add replay_id to dsc of transactions', async ({ getLocalTestP replay_id: replay.session?.id, }); }); + +sentryTest( + 'should not add replay_id to dsc of transactions if replay is not enabled', + async ({ getLocalTestPath, page, browserName }) => { + // This is flaky on webkit, so skipping there... + if (shouldSkipReplayTest() || shouldSkipTracingTest() || browserName === 'webkit') { + sentryTest.skip(); + } + + const url = await getLocalTestPath({ testDir: __dirname }); + await page.goto(url); + + await page.evaluate(() => { + (window as unknown as TestWindow).Replay.stop(); + + (window as unknown as TestWindow).Sentry.configureScope(scope => { + scope.setUser({ id: 'user123', segment: 'segmentB' }); + scope.setTransactionName('testTransactionDSC'); + }); + }); + + const envHeader = await getFirstSentryEnvelopeRequest(page, url, envelopeHeaderRequestParser); + + await waitForReplayRunning(page); + const replay = await getReplaySnapshot(page); + + expect(replay.session?.id).toBeDefined(); + + expect(envHeader.trace).toBeDefined(); + expect(envHeader.trace).toEqual({ + environment: 'production', + user_segment: 'segmentB', + sample_rate: '1', + trace_id: expect.any(String), + public_key: 'public', + }); + }, +); diff --git a/packages/replay/src/util/addGlobalListeners.ts b/packages/replay/src/util/addGlobalListeners.ts index 46ba18bb9ed3..189867c721bb 100644 --- a/packages/replay/src/util/addGlobalListeners.ts +++ b/packages/replay/src/util/addGlobalListeners.ts @@ -35,7 +35,7 @@ export function addGlobalListeners(replay: ReplayContainer): void { client.on('afterSendEvent', handleAfterSendEvent(replay)); client.on('createDsc', (dsc: DynamicSamplingContext) => { const replayId = replay.getSessionId(); - if (replayId) { + if (replayId && replay.isEnabled()) { dsc.replay_id = replayId; } });