Skip to content

Commit 436deb1

Browse files
committed
stop using <meta> tag data for navigation transactions
1 parent d9fe8c6 commit 436deb1

File tree

2 files changed

+64
-1
lines changed

2 files changed

+64
-1
lines changed

packages/tracing/src/browser/browsertracing.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,9 +192,11 @@ export class BrowserTracing implements Integration {
192192
// eslint-disable-next-line @typescript-eslint/unbound-method
193193
const { beforeNavigate, idleTimeout, maxTransactionDuration } = this.options;
194194

195+
const parentContextFromHeader = context.op === 'pageload' ? getHeaderContext() : undefined;
196+
195197
const expandedContext = {
196198
...context,
197-
...getHeaderContext(),
199+
...parentContextFromHeader,
198200
trimEnd: true,
199201
};
200202
const modifiedContext = typeof beforeNavigate === 'function' ? beforeNavigate(expandedContext) : expandedContext;

packages/tracing/test/browser/browsertracing.test.ts

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
BrowserTracing,
88
BrowserTracingOptions,
99
DEFAULT_MAX_TRANSACTION_DURATION_SECONDS,
10+
getHeaderContext,
1011
getMetaContent,
1112
} from '../../src/browser/browsertracing';
1213
import { defaultRequestInstrumentionOptions } from '../../src/browser/request';
@@ -379,5 +380,65 @@ describe('BrowserTracing', () => {
379380
expect(metaTagValue).toBe(content);
380381
});
381382
});
383+
384+
describe('getHeaderContext', () => {
385+
it('correctly parses a valid sentry-trace meta header', () => {
386+
document.head.innerHTML = `<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0">`;
387+
388+
const headerContext = getHeaderContext();
389+
390+
expect(headerContext).toBeDefined();
391+
expect(headerContext!.traceId).toEqual('12312012123120121231201212312012');
392+
expect(headerContext!.parentSpanId).toEqual('1121201211212012');
393+
expect(headerContext!.parentSampled).toEqual(false);
394+
});
395+
396+
it('returns undefined if the header is malformed', () => {
397+
document.head.innerHTML = `<meta name="sentry-trace" content="12312012-112120121-0">`;
398+
399+
const headerContext = getHeaderContext();
400+
401+
expect(headerContext).toBeUndefined();
402+
});
403+
404+
it("returns undefined if the header isn't there", () => {
405+
document.head.innerHTML = `<meta name="dogs" content="12312012123120121231201212312012-1121201211212012-0">`;
406+
407+
const headerContext = getHeaderContext();
408+
409+
expect(headerContext).toBeUndefined();
410+
});
411+
});
412+
413+
describe('using the data', () => {
414+
it('uses the data for pageload transactions', () => {
415+
document.head.innerHTML = `<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0">`;
416+
417+
// pageload transactions are created as part of the BrowserTracing integration's initialization
418+
createBrowserTracing(true);
419+
const transaction = getActiveTransaction(hub) as IdleTransaction;
420+
421+
expect(transaction).toBeDefined();
422+
expect(transaction.op).toBe('pageload');
423+
expect(transaction.traceId).toEqual('12312012123120121231201212312012');
424+
expect(transaction.parentSpanId).toEqual('1121201211212012');
425+
expect(transaction.sampled).toBe(false);
426+
});
427+
428+
it('ignores the data for navigation transactions', () => {
429+
mockChangeHistory = () => undefined;
430+
document.head.innerHTML = `<meta name="sentry-trace" content="12312012123120121231201212312012-1121201211212012-0">`;
431+
432+
createBrowserTracing(true);
433+
434+
mockChangeHistory({ to: 'here', from: 'there' });
435+
const transaction = getActiveTransaction(hub) as IdleTransaction;
436+
437+
expect(transaction).toBeDefined();
438+
expect(transaction.op).toBe('navigation');
439+
expect(transaction.traceId).not.toEqual('12312012123120121231201212312012');
440+
expect(transaction.parentSpanId).toBeUndefined();
441+
});
442+
});
382443
});
383444
});

0 commit comments

Comments
 (0)