Skip to content

Commit

Permalink
test(browser): Add integration test for changing transaction name in …
Browse files Browse the repository at this point in the history
…`beforeSendTransaction` (#14495)
  • Loading branch information
Lms24 authored Nov 27, 2024
1 parent 02be07d commit c3c3910
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import * as Sentry from '@sentry/browser';

window.Sentry = Sentry;

Sentry.init({
dsn: 'https://[email protected]/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,
});
Original file line number Diff line number Diff line change
@@ -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<Event>(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');
},
);

0 comments on commit c3c3910

Please sign in to comment.