Skip to content

test(browser-integration-tests): Test trace lifetime for outgoing fetch requests #11614

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ window.Sentry = Sentry;
Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
integrations: [Sentry.browserTracingIntegration()],
tracePropagationTargets: ['http://example.com'],
tracesSampleRate: 1,
});
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ sentryTest('error after navigation has navigation traceId', async ({ getLocalTes
const navigationTraceId = navigationEvent.contexts?.trace?.trace_id;
expect(navigationTraceId).toMatch(/^[0-9a-f]{32}$/);

const [, errorEvent] = await Promise.all([
page.locator('#errorBtn').click(),
getFirstSentryEnvelopeRequest<Event>(page),
]);
const errorEventPromise = getFirstSentryEnvelopeRequest<Event>(page);
await page.locator('#errorBtn').click();
const errorEvent = await errorEventPromise;

const errorTraceId = errorEvent.contexts?.trace?.trace_id;
expect(errorTraceId).toBe(navigationTraceId);
Expand Down Expand Up @@ -80,3 +79,67 @@ sentryTest('error during navigation has new navigation traceId', async ({ getLoc
const errorTraceId = errorEvent?.contexts?.trace?.trace_id;
expect(errorTraceId).toBe(navigationTraceId);
});

sentryTest(
'outgoing fetch request after navigation has navigation traceId in headers',
async ({ getLocalTestPath, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}

const url = await getLocalTestPath({ testDir: __dirname });

// ensure navigation transaction is finished
await getFirstSentryEnvelopeRequest<Event>(page, url);

const navigationEvent = await getFirstSentryEnvelopeRequest<Event>(page, `${url}#foo`);
expect(navigationEvent.contexts?.trace?.op).toBe('navigation');

const navigationTraceId = navigationEvent.contexts?.trace?.trace_id;
expect(navigationTraceId).toMatch(/^[0-9a-f]{32}$/);

const requestPromise = page.waitForRequest('http://example.com/*');
await page.locator('#fetchBtn').click();
const request = await requestPromise;
const headers = request.headers();

// sampling decision is deferred b/c of no active span at the time of request
expect(headers['sentry-trace']).toMatch(new RegExp(`^${navigationTraceId}-[0-9a-f]{16}$`));
expect(headers['baggage']).toEqual(
`sentry-environment=production,sentry-public_key=public,sentry-trace_id=${navigationTraceId}`,
);
},
);

sentryTest(
'outgoing fetch request during navigation has navigation traceId in headers',
async ({ getLocalTestPath, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}

const url = await getLocalTestPath({ testDir: __dirname });

// ensure navigation transaction is finished
await getFirstSentryEnvelopeRequest<Event>(page, url);

const navigationEventPromise = getFirstSentryEnvelopeRequest<Event>(page);
const requestPromise = page.waitForRequest('http://example.com/*');
await page.goto(`${url}#foo`);
await page.locator('#fetchBtn').click();
const [navigationEvent, request] = await Promise.all([navigationEventPromise, requestPromise]);

expect(navigationEvent.contexts?.trace?.op).toBe('navigation');

const navigationTraceId = navigationEvent.contexts?.trace?.trace_id;
expect(navigationTraceId).toMatch(/^[0-9a-f]{32}$/);

const headers = request.headers();

// sampling decision is propagated from active span sampling decision
expect(headers['sentry-trace']).toMatch(new RegExp(`^${navigationTraceId}-[0-9a-f]{16}-1$`));
expect(headers['baggage']).toEqual(
`sentry-environment=production,sentry-public_key=public,sentry-trace_id=${navigationTraceId},sentry-sample_rate=1,sentry-sampled=true`,
);
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,61 @@ sentryTest('error during pageload has pageload traceId', async ({ getLocalTestPa
const errorTraceId = errorEvent?.contexts?.trace?.trace_id;
expect(errorTraceId).toBe(pageloadTraceId);
});

sentryTest(
'outgoing fetch request after pageload has pageload traceId in headers',
async ({ getLocalTestPath, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}

const url = await getLocalTestPath({ testDir: __dirname });

const pageloadEvent = await getFirstSentryEnvelopeRequest<Event>(page, url);
expect(pageloadEvent.contexts?.trace?.op).toBe('pageload');

const pageloadTraceId = pageloadEvent.contexts?.trace?.trace_id;
expect(pageloadTraceId).toMatch(/^[0-9a-f]{32}$/);

const requestPromise = page.waitForRequest('http://example.com/*');
await page.locator('#fetchBtn').click();
const request = await requestPromise;
const headers = request.headers();

// sampling decision is deferred b/c of no active span at the time of request
expect(headers['sentry-trace']).toMatch(new RegExp(`^${pageloadTraceId}-[0-9a-f]{16}$`));
expect(headers['baggage']).toEqual(
`sentry-environment=production,sentry-public_key=public,sentry-trace_id=${pageloadTraceId}`,
);
},
);

sentryTest(
'outgoing fetch request during pageload has pageload traceId in headers',
async ({ getLocalTestPath, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}

const url = await getLocalTestPath({ testDir: __dirname });

const pageloadEventPromise = getFirstSentryEnvelopeRequest<Event>(page);
const requestPromise = page.waitForRequest('http://example.com/*');
await page.goto(url);
await page.locator('#fetchBtn').click();
const [pageloadEvent, request] = await Promise.all([pageloadEventPromise, requestPromise]);

expect(pageloadEvent.contexts?.trace?.op).toBe('pageload');

const navigationTraceId = pageloadEvent.contexts?.trace?.trace_id;
expect(navigationTraceId).toMatch(/^[0-9a-f]{32}$/);

const headers = request.headers();

// sampling decision is propagated from active span sampling decision
expect(headers['sentry-trace']).toMatch(new RegExp(`^${navigationTraceId}-[0-9a-f]{16}-1$`));
expect(headers['baggage']).toEqual(
`sentry-environment=production,sentry-public_key=public,sentry-trace_id=${navigationTraceId},sentry-sample_rate=1,sentry-sampled=true`,
);
},
);
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ const errorBtn = document.getElementById('errorBtn');
errorBtn.addEventListener('click', () => {
throw new Error('Sentry Test Error');
});

const fetchBtn = document.getElementById('fetchBtn');
fetchBtn.addEventListener('click', async () => {
await fetch('http://example.com');
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
</head>
<body>
<button id="errorBtn">Throw Error</button>
<button id="fetchBtn">Fetch Request</button>
</body>
</html>