Skip to content

Commit 6c8aeff

Browse files
authored
test(browser-integration-tests): Add trace lifetime tests in TwP scenario (#11636) (#11659)
For some reason, this commit (#11564) caused a conflict in our master->develop sync. Let's fix this by cherry picking it onto master.
1 parent 5f3e51b commit 6c8aeff

File tree

5 files changed

+131
-1
lines changed

5 files changed

+131
-1
lines changed

dev-packages/browser-integration-tests/suites/tracing/trace-lifetime/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ connected to a trace. This suite distinguishes the following cases:
55
2. `pageload-meta` - Traces started on the initial pageload as a continuation of the trace on the server (via `<meta>`
66
tags)
77
3. `navigation` - Traces started during navigations on a page
8+
4. `tracing-without-performance` - Traces originating from an app configured for "Tracing without Performance".
89

910
Tests scenarios should be fairly similar for all three cases but it's important we test all of them.

dev-packages/browser-integration-tests/suites/tracing/trace-lifetime/subject.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const errorBtn = document.getElementById('errorBtn');
22
errorBtn.addEventListener('click', () => {
3-
throw new Error('Sentry Test Error');
3+
throw new Error(`Sentry Test Error ${Math.random()}`);
44
});
55

66
const fetchBtn = document.getElementById('fetchBtn');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
// in browser TwP means not setting tracesSampleRate but adding browserTracingIntegration,
7+
dsn: 'https://[email protected]/1337',
8+
integrations: [Sentry.browserTracingIntegration()],
9+
tracePropagationTargets: ['http://example.com'],
10+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<!-- Purposefully emitting the `sampled` flag in the sentry-trace tag -->
6+
<meta name="sentry-trace" content="12345678901234567890123456789012-1234567890123456" />
7+
<meta name="baggage"
8+
content="sentry-trace_id=12345678901234567890123456789012,sentry-public_key=public,sentry-release=1.0.0,sentry-environment=prod"/>
9+
</head>
10+
<body>
11+
<button id="errorBtn">Throw Error</button>
12+
<button id="fetchBtn">Fetch Request</button>
13+
<button id="xhrBtn">XHR Request</button>
14+
</body>
15+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import { expect } from '@playwright/test';
2+
import type { Event } from '@sentry/types';
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
import { getFirstSentryEnvelopeRequest, shouldSkipTracingTest } from '../../../../utils/helpers';
5+
6+
const META_TAG_TRACE_ID = '12345678901234567890123456789012';
7+
const META_TAG_PARENT_SPAN_ID = '1234567890123456';
8+
const META_TAG_BAGGAGE =
9+
'sentry-trace_id=12345678901234567890123456789012,sentry-public_key=public,sentry-release=1.0.0,sentry-environment=prod';
10+
11+
sentryTest('error has new traceId after navigation', async ({ getLocalTestPath, page }) => {
12+
if (shouldSkipTracingTest()) {
13+
sentryTest.skip();
14+
}
15+
16+
const url = await getLocalTestPath({ testDir: __dirname });
17+
await page.goto(url);
18+
19+
const errorEventPromise = getFirstSentryEnvelopeRequest<Event>(page);
20+
await page.locator('#errorBtn').click();
21+
const errorEvent = await errorEventPromise;
22+
23+
expect(errorEvent.contexts?.trace).toEqual({
24+
trace_id: META_TAG_TRACE_ID,
25+
parent_span_id: META_TAG_PARENT_SPAN_ID,
26+
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
27+
});
28+
29+
const errorEventPromise2 = getFirstSentryEnvelopeRequest<Event>(page, `${url}#navigation`);
30+
await page.locator('#errorBtn').click();
31+
const errorEvent2 = await errorEventPromise2;
32+
33+
expect(errorEvent2.contexts?.trace).toEqual({
34+
trace_id: expect.stringMatching(/^[0-9a-f]{32}$/),
35+
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
36+
});
37+
expect(errorEvent2.contexts?.trace?.trace_id).not.toBe(META_TAG_TRACE_ID);
38+
});
39+
40+
sentryTest('outgoing fetch requests have new traceId after navigation', async ({ getLocalTestPath, page }) => {
41+
if (shouldSkipTracingTest()) {
42+
sentryTest.skip();
43+
}
44+
45+
const url = await getLocalTestPath({ testDir: __dirname });
46+
await page.goto(url);
47+
48+
const requestPromise = page.waitForRequest('http://example.com/*');
49+
await page.locator('#fetchBtn').click();
50+
const request = await requestPromise;
51+
const headers = request.headers();
52+
53+
// sampling decision is deferred because TwP means we didn't sample any span
54+
expect(headers['sentry-trace']).toMatch(new RegExp(`^${META_TAG_TRACE_ID}-[0-9a-f]{16}$`));
55+
expect(headers['baggage']).toBe(META_TAG_BAGGAGE);
56+
57+
await page.goto(`${url}#navigation`);
58+
59+
const requestPromise2 = page.waitForRequest('http://example.com/*');
60+
await page.locator('#fetchBtn').click();
61+
const request2 = await requestPromise2;
62+
const headers2 = request2.headers();
63+
64+
// sampling decision is deferred because TwP means we didn't sample any span
65+
expect(headers2['sentry-trace']).toMatch(/^[0-9a-f]{32}-[0-9a-f]{16}$/);
66+
expect(headers2['baggage']).not.toBe(`${META_TAG_TRACE_ID}-${META_TAG_PARENT_SPAN_ID}`);
67+
expect(headers2['baggage']).toMatch(
68+
/sentry-environment=production,sentry-public_key=public,sentry-trace_id=[0-9a-f]{32}/,
69+
);
70+
expect(headers2['baggage']).not.toContain(`sentry-trace_id=${META_TAG_TRACE_ID}`);
71+
});
72+
73+
sentryTest('outgoing XHR requests have new traceId after navigation', async ({ getLocalTestPath, page }) => {
74+
if (shouldSkipTracingTest()) {
75+
sentryTest.skip();
76+
}
77+
78+
const url = await getLocalTestPath({ testDir: __dirname });
79+
await page.goto(url);
80+
81+
const requestPromise = page.waitForRequest('http://example.com/*');
82+
await page.locator('#xhrBtn').click();
83+
const request = await requestPromise;
84+
const headers = request.headers();
85+
86+
// sampling decision is deferred because TwP means we didn't sample any span
87+
expect(headers['sentry-trace']).toMatch(new RegExp(`^${META_TAG_TRACE_ID}-[0-9a-f]{16}$`));
88+
expect(headers['baggage']).toBe(META_TAG_BAGGAGE);
89+
90+
await page.goto(`${url}#navigation`);
91+
92+
const requestPromise2 = page.waitForRequest('http://example.com/*');
93+
await page.locator('#xhrBtn').click();
94+
const request2 = await requestPromise2;
95+
const headers2 = request2.headers();
96+
97+
// sampling decision is deferred because TwP means we didn't sample any span
98+
expect(headers2['sentry-trace']).toMatch(/^[0-9a-f]{32}-[0-9a-f]{16}$/);
99+
expect(headers2['baggage']).not.toBe(`${META_TAG_TRACE_ID}-${META_TAG_PARENT_SPAN_ID}`);
100+
expect(headers2['baggage']).toMatch(
101+
/sentry-environment=production,sentry-public_key=public,sentry-trace_id=[0-9a-f]{32}/,
102+
);
103+
expect(headers2['baggage']).not.toContain(`sentry-trace_id=${META_TAG_TRACE_ID}`);
104+
});

0 commit comments

Comments
 (0)