Skip to content

Commit 39c8290

Browse files
authored
test(browser-integration-tests): Add trace lifetime tests for <meta> tag pageload transactions (#11622)
Add a couple of tests to our `trace-lifetime` integration test suite to test `<meta>` tag continued pageload traces. These differ from "normal" pageload traces in the sense that they should pick up and continue the trace from the service that added the meta tags to the HTML response. So in our suite: - `trace-lifetime/pageload` - pageload is head of trace - `trace-lifetime/pageload-meta` - pageload continues trace from server
1 parent 8cb70e3 commit 39c8290

File tree

3 files changed

+181
-0
lines changed

3 files changed

+181
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Tests in this suite are meant to test the lifetime of a trace in the browser SDK and how different events sent are
2+
connected to a trace. This suite distinguishes the following cases:
3+
4+
1. `pageload` - Traces started on the initial pageload as head of trace
5+
2. `pageload-meta` - Traces started on the initial pageload as a continuation of the trace on the server (via `<meta>`
6+
tags)
7+
3. `navigation` - Traces started during navigations on a page
8+
9+
Tests scenarios should be fairly similar for all three cases but it's important we test all of them.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
<meta name="sentry-trace" content="12345678901234567890123456789012-1234567890123456-1" />
6+
<meta name="baggage"
7+
content="sentry-trace_id=12345678901234567890123456789012,sentry-sample_rate=0.2,sentry-transaction=my-transaction,sentry-public_key=public,sentry-release=1.0.0,sentry-environment=prod"/>
8+
</head>
9+
<body>
10+
<button id="errorBtn">Throw Error</button>
11+
<button id="fetchBtn">Fetch Request</button>
12+
</body>
13+
</html>
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
import { expect } from '@playwright/test';
2+
import type { Event } from '@sentry/types';
3+
import { sentryTest } from '../../../../utils/fixtures';
4+
import {
5+
getFirstSentryEnvelopeRequest,
6+
getMultipleSentryEnvelopeRequests,
7+
shouldSkipTracingTest,
8+
} from '../../../../utils/helpers';
9+
10+
const META_TAG_TRACE_ID = '12345678901234567890123456789012';
11+
const META_TAG_PARENT_SPAN_ID = '1234567890123456';
12+
const META_TAG_BAGGAGE =
13+
'sentry-trace_id=12345678901234567890123456789012,sentry-sample_rate=0.2,sentry-transaction=my-transaction,sentry-public_key=public,sentry-release=1.0.0,sentry-environment=prod';
14+
15+
sentryTest(
16+
'create a new trace for a navigation after the <meta> tag pageload trace',
17+
async ({ getLocalTestPath, page }) => {
18+
if (shouldSkipTracingTest()) {
19+
sentryTest.skip();
20+
}
21+
22+
const url = await getLocalTestPath({ testDir: __dirname });
23+
24+
const pageloadEvent = await getFirstSentryEnvelopeRequest<Event>(page, url);
25+
const navigationEvent = await getFirstSentryEnvelopeRequest<Event>(page, `${url}#foo`);
26+
27+
const pageloadTraceContext = pageloadEvent.contexts?.trace;
28+
const navigationTraceContext = navigationEvent.contexts?.trace;
29+
30+
expect(pageloadTraceContext).toMatchObject({
31+
op: 'pageload',
32+
trace_id: META_TAG_TRACE_ID,
33+
parent_span_id: META_TAG_PARENT_SPAN_ID,
34+
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
35+
});
36+
expect(navigationTraceContext).toMatchObject({
37+
op: 'navigation',
38+
trace_id: expect.stringMatching(/^[0-9a-f]{32}$/),
39+
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
40+
});
41+
// navigation span is head of trace, so there's no parent span:
42+
expect(navigationTraceContext?.trace_id).not.toHaveProperty('parent_span_id');
43+
44+
expect(pageloadTraceContext?.trace_id).not.toEqual(navigationTraceContext?.trace_id);
45+
},
46+
);
47+
48+
sentryTest('error after <meta> tag pageload has pageload traceId', async ({ getLocalTestPath, page }) => {
49+
if (shouldSkipTracingTest()) {
50+
sentryTest.skip();
51+
}
52+
53+
const url = await getLocalTestPath({ testDir: __dirname });
54+
55+
const pageloadEvent = await getFirstSentryEnvelopeRequest<Event>(page, url);
56+
expect(pageloadEvent.contexts?.trace).toMatchObject({
57+
op: 'pageload',
58+
trace_id: META_TAG_TRACE_ID,
59+
parent_span_id: META_TAG_PARENT_SPAN_ID,
60+
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
61+
});
62+
63+
const errorEventPromise = getFirstSentryEnvelopeRequest<Event>(page);
64+
await page.locator('#errorBtn').click();
65+
const errorEvent = await errorEventPromise;
66+
67+
expect(errorEvent.contexts?.trace).toMatchObject({
68+
trace_id: META_TAG_TRACE_ID,
69+
parent_span_id: META_TAG_PARENT_SPAN_ID,
70+
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
71+
});
72+
});
73+
74+
sentryTest('error during <meta> tag pageload has pageload traceId', async ({ getLocalTestPath, page }) => {
75+
if (shouldSkipTracingTest()) {
76+
sentryTest.skip();
77+
}
78+
79+
const url = await getLocalTestPath({ testDir: __dirname });
80+
81+
const envelopeRequestsPromise = getMultipleSentryEnvelopeRequests<Event>(page, 2);
82+
await page.goto(url);
83+
await page.locator('#errorBtn').click();
84+
const events = await envelopeRequestsPromise;
85+
86+
const pageloadEvent = events.find(event => event.type === 'transaction');
87+
const errorEvent = events.find(event => !event.type);
88+
89+
expect(pageloadEvent?.contexts?.trace).toMatchObject({
90+
op: 'pageload',
91+
trace_id: META_TAG_TRACE_ID,
92+
parent_span_id: META_TAG_PARENT_SPAN_ID,
93+
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
94+
});
95+
96+
expect(errorEvent?.contexts?.trace).toMatchObject({
97+
trace_id: META_TAG_TRACE_ID,
98+
parent_span_id: META_TAG_PARENT_SPAN_ID,
99+
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
100+
});
101+
});
102+
103+
sentryTest(
104+
'outgoing fetch request after <meta> tag pageload has pageload traceId in headers',
105+
async ({ getLocalTestPath, page }) => {
106+
if (shouldSkipTracingTest()) {
107+
sentryTest.skip();
108+
}
109+
110+
const url = await getLocalTestPath({ testDir: __dirname });
111+
112+
const pageloadEvent = await getFirstSentryEnvelopeRequest<Event>(page, url);
113+
expect(pageloadEvent?.contexts?.trace).toMatchObject({
114+
op: 'pageload',
115+
trace_id: META_TAG_TRACE_ID,
116+
parent_span_id: META_TAG_PARENT_SPAN_ID,
117+
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
118+
});
119+
120+
const requestPromise = page.waitForRequest('http://example.com/*');
121+
await page.locator('#fetchBtn').click();
122+
const request = await requestPromise;
123+
const headers = request.headers();
124+
125+
// sampling decision is propagated from meta tag's sentry-trace sampled flag
126+
expect(headers['sentry-trace']).toMatch(new RegExp(`^${META_TAG_TRACE_ID}-[0-9a-f]{16}-1$`));
127+
expect(headers['baggage']).toBe(META_TAG_BAGGAGE);
128+
},
129+
);
130+
131+
sentryTest(
132+
'outgoing fetch request during <meta> tag pageload has pageload traceId in headers',
133+
async ({ getLocalTestPath, page }) => {
134+
if (shouldSkipTracingTest()) {
135+
sentryTest.skip();
136+
}
137+
138+
const url = await getLocalTestPath({ testDir: __dirname });
139+
140+
const pageloadEventPromise = getFirstSentryEnvelopeRequest<Event>(page);
141+
const requestPromise = page.waitForRequest('http://example.com/*');
142+
await page.goto(url);
143+
await page.locator('#fetchBtn').click();
144+
const [pageloadEvent, request] = await Promise.all([pageloadEventPromise, requestPromise]);
145+
146+
expect(pageloadEvent?.contexts?.trace).toMatchObject({
147+
op: 'pageload',
148+
trace_id: META_TAG_TRACE_ID,
149+
parent_span_id: META_TAG_PARENT_SPAN_ID,
150+
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
151+
});
152+
153+
const headers = request.headers();
154+
155+
// sampling decision is propagated from meta tag's sentry-trace sampled flag
156+
expect(headers['sentry-trace']).toMatch(new RegExp(`^${META_TAG_TRACE_ID}-[0-9a-f]{16}-1$`));
157+
expect(headers['baggage']).toBe(META_TAG_BAGGAGE);
158+
},
159+
);

0 commit comments

Comments
 (0)