diff --git a/dev-packages/browser-integration-tests/suites/public-api/beforeSendTransaction/init.js b/dev-packages/browser-integration-tests/suites/public-api/beforeSendTransaction/init.js new file mode 100644 index 000000000000..e94e2bd42ab2 --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/public-api/beforeSendTransaction/init.js @@ -0,0 +1,23 @@ +import * as Sentry from '@sentry/browser'; + +window.Sentry = Sentry; + +Sentry.init({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', + integrations: [Sentry.browserTracingIntegration()], + beforeSendTransaction: transactionEvent => { + const op = transactionEvent.contexts.trace.op; + if (op === 'pageload' || op === 'navigation') { + // use whatever logic you want to set the name + transactionEvent.transaction = 'customName'; + + transactionEvent.transaction_info.source = 'route'; + transactionEvent.contexts.trace.data = { + ...transactionEvent.contexts.trace.data, + [Sentry.SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]: 'route', + }; + } + return transactionEvent; + }, + tracesSampleRate: 1, +}); diff --git a/dev-packages/browser-integration-tests/suites/public-api/beforeSendTransaction/test.ts b/dev-packages/browser-integration-tests/suites/public-api/beforeSendTransaction/test.ts new file mode 100644 index 000000000000..14531498e90b --- /dev/null +++ b/dev-packages/browser-integration-tests/suites/public-api/beforeSendTransaction/test.ts @@ -0,0 +1,32 @@ +import { expect } from '@playwright/test'; +import type { Event } from '@sentry/types'; + +import { SEMANTIC_ATTRIBUTE_SENTRY_SOURCE } from '@sentry/browser'; +import { sentryTest } from '../../../utils/fixtures'; +import { getFirstSentryEnvelopeRequest, shouldSkipTracingTest } from '../../../utils/helpers'; + +sentryTest( + 'allows modification of the transaction name and source but overwrites source to custom', + async ({ getLocalTestUrl, page }) => { + if (shouldSkipTracingTest()) { + sentryTest.skip(); + } + + const url = await getLocalTestUrl({ testDir: __dirname }); + + const eventData = await getFirstSentryEnvelopeRequest(page, url); + + expect(eventData.type).toBe('transaction'); + + // user-changed name + expect(eventData.transaction).toBe('customName'); + + // Despite the user setting the source to 'route', the SDK detects that the txn name was changed + // and therefore sets the transaction_info.source to 'custom'. This is not ideal but also not easily changeable. + // Given that Relay doesn't differentiate between 'source' and 'route', we'll keep this as-is for now. + expect(eventData.transaction_info?.source).toBe('custom'); + + // This stays the same but it has no effect on Relay. + expect(eventData.contexts?.trace?.data?.[SEMANTIC_ATTRIBUTE_SENTRY_SOURCE]).toBe('route'); + }, +);