Skip to content

Commit 05e5a43

Browse files
committed
add integration tests
1 parent bb6c1c8 commit 05e5a43

File tree

12 files changed

+600
-0
lines changed

12 files changed

+600
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
dsn: 'https://[email protected]/1337',
7+
integrations: [
8+
Sentry.browserTracingIntegration({
9+
linkPreviousTrace: 'in-memory',
10+
sampleLinkedTracesConsistently: true
11+
}),
12+
],
13+
tracePropagationTargets: ['someurl.com'],
14+
tracesSampleRate: 1,
15+
debug: true,
16+
sendClientReports: true
17+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const btn1 = document.getElementById('btn1');
2+
3+
const btn2 = document.getElementById('btn2');
4+
5+
btn1.addEventListener('click', () => {
6+
Sentry.startNewTrace(() => {
7+
Sentry.startSpan({name: 'custom root span 1', op: 'custom'}, () => {});
8+
});
9+
});
10+
11+
btn2.addEventListener('click', () => {
12+
Sentry.startNewTrace(() => {
13+
Sentry.startSpan({name: 'custom root span 2', op: 'custom'}, async () => {
14+
await fetch('https://someUrl.com');
15+
});
16+
});
17+
});
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-0" />
6+
<meta
7+
name="baggage"
8+
content="sentry-trace_id=12345678901234567890123456789012,sentry-sample_rate=0.2,sentry-sampled=false,sentry-transaction=my-transaction,sentry-public_key=public,sentry-release=1.0.0,sentry-environment=prod,sentry-sample_rand=0.9"
9+
/>
10+
</head>
11+
<button id="btn1">Custom Trace</button>
12+
<button id="btn2">fetch request</button>
13+
</html>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import { expect } from '@playwright/test';
2+
import type { ClientReport } from '@sentry/core';
3+
import { extractTraceparentData, parseBaggageHeader } from '@sentry/core';
4+
5+
import { sentryTest } from '../../../../../utils/fixtures';
6+
import {
7+
envelopeRequestParser,
8+
getMultipleSentryEnvelopeRequests,
9+
shouldSkipTracingTest,
10+
waitForClientReportRequest,
11+
} from '../../../../../utils/helpers';
12+
13+
const metaTagSampleRand = 0.9;
14+
const metaTagSampleRate = 0.2;
15+
const metaTagTraceId = '12345678901234567890123456789012';
16+
17+
sentryTest.describe('When `sampleLinkedTracesConsistently` is `true` and page contains <meta> tags', () => {
18+
sentryTest(
19+
'Continues negative sampling decision from meta tag across all traces and downstream propagations',
20+
async ({ getLocalTestUrl, page }) => {
21+
if (shouldSkipTracingTest()) {
22+
sentryTest.skip();
23+
}
24+
25+
const url = await getLocalTestUrl({ testDir: __dirname });
26+
27+
let txnsReceived = 0;
28+
// @ts-expect-error - no need to return something valid here
29+
getMultipleSentryEnvelopeRequests<Event>(page, 1, { envelopeType: 'transaction' }, () => {
30+
++txnsReceived;
31+
return {};
32+
});
33+
34+
const clientReportPromise = waitForClientReportRequest(page);
35+
36+
await sentryTest.step('Initial pageload', async () => {
37+
await page.goto(url);
38+
expect(txnsReceived).toEqual(0);
39+
});
40+
41+
await sentryTest.step('Custom instrumented button click', async () => {
42+
await page.locator('#btn1').click();
43+
expect(txnsReceived).toEqual(0);
44+
});
45+
46+
await sentryTest.step('Navigation', async () => {
47+
await page.goto(`${url}#foo`);
48+
expect(txnsReceived).toEqual(0);
49+
});
50+
51+
await sentryTest.step('Make fetch request', async () => {
52+
let sentryTrace = undefined;
53+
let baggage = undefined;
54+
55+
await page.route('https://someUrl.com', (route, req) => {
56+
baggage = req.headers()['baggage'];
57+
sentryTrace = req.headers()['sentry-trace'];
58+
return route.fulfill({ status: 200, body: 'ok' });
59+
});
60+
61+
await page.locator('#btn2').click();
62+
63+
expect(sentryTrace).toBeDefined();
64+
expect(baggage).toBeDefined();
65+
66+
expect(extractTraceparentData(sentryTrace)).toEqual({
67+
traceId: expect.not.stringContaining(metaTagTraceId),
68+
parentSpanId: expect.stringMatching(/^[0-9a-f]{16}$/),
69+
parentSampled: false,
70+
});
71+
72+
expect(parseBaggageHeader(baggage)).toEqual({
73+
'sentry-environment': 'production',
74+
'sentry-public_key': 'public',
75+
'sentry-sample_rand': `${metaTagSampleRand}`,
76+
'sentry-sample_rate': `${metaTagSampleRate}`,
77+
'sentry-sampled': 'false',
78+
'sentry-trace_id': expect.not.stringContaining(metaTagTraceId),
79+
'sentry-transaction': 'custom root span 2',
80+
});
81+
});
82+
83+
await sentryTest.step('Client report', async () => {
84+
await page.evaluate(() => {
85+
Object.defineProperty(document, 'visibilityState', {
86+
configurable: true,
87+
get: function () {
88+
return 'hidden';
89+
},
90+
});
91+
92+
// Dispatch the visibilitychange event to notify listeners
93+
document.dispatchEvent(new Event('visibilitychange'));
94+
});
95+
96+
const clientReport = envelopeRequestParser<ClientReport>(await clientReportPromise);
97+
expect(clientReport).toEqual({
98+
timestamp: expect.any(Number),
99+
discarded_events: [
100+
{
101+
category: 'transaction',
102+
quantity: 4,
103+
reason: 'sample_rate',
104+
},
105+
],
106+
});
107+
});
108+
109+
await sentryTest.step('Wait for transactions to be discarded', async () => {
110+
// give it a little longer just in case a txn is pending to be sent
111+
await page.waitForTimeout(1000);
112+
expect(txnsReceived).toEqual(0);
113+
});
114+
},
115+
);
116+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
Sentry.init({
6+
dsn: 'https://[email protected]/1337',
7+
integrations: [
8+
Sentry.browserTracingIntegration({
9+
linkPreviousTrace: 'in-memory',
10+
sampleLinkedTracesConsistently: true
11+
}),
12+
],
13+
tracePropagationTargets: ['someurl.com'],
14+
// only take into account sampling from meta tag; otherwise sample negatively
15+
tracesSampleRate: 0,
16+
debug: true,
17+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
const btn1 = document.getElementById('btn1');
2+
3+
const btn2 = document.getElementById('btn2');
4+
5+
btn1.addEventListener('click', () => {
6+
Sentry.startNewTrace(() => {
7+
Sentry.startSpan({name: 'custom root span 1', op: 'custom'}, () => {});
8+
});
9+
});
10+
11+
btn2.addEventListener('click', () => {
12+
Sentry.startNewTrace(() => {
13+
Sentry.startSpan({name: 'custom root span 2', op: 'custom'}, async () => {
14+
await fetch('https://someUrl.com');
15+
});
16+
});
17+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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-sampled=true,sentry-transaction=my-transaction,sentry-public_key=public,sentry-release=1.0.0,sentry-environment=prod,sentry-sample_rand=0.051121"/>
8+
9+
</head>
10+
<button id="btn1">
11+
</button>
12+
<button id="btn2">
13+
</button>
14+
</html>

0 commit comments

Comments
 (0)