Skip to content

Commit 2930499

Browse files
mydeas1gr1d
andauthored
test(browser:) Add test about re-sampling new traces (#16730)
Adding a test for a support case, to ensure that new trace are correctly sampled. --------- Co-authored-by: Sigrid Huemer <32902192+s1gr1d@users.noreply.github.com>
1 parent 327594c commit 2930499

File tree

4 files changed

+130
-0
lines changed

4 files changed

+130
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import * as Sentry from '@sentry/browser';
2+
3+
window.Sentry = Sentry;
4+
5+
// Force this so that the initial sampleRand is consistent
6+
Math.random = () => 0.45;
7+
8+
Sentry.init({
9+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
10+
integrations: [Sentry.browserTracingIntegration()],
11+
tracePropagationTargets: ['http://sentry-test-site.example'],
12+
tracesSampler: ({ name }) => {
13+
if (name === 'new-trace') {
14+
return 0.9;
15+
}
16+
17+
return 0.5;
18+
},
19+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const newTraceBtn = document.getElementById('newTrace');
2+
newTraceBtn.addEventListener('click', async () => {
3+
Sentry.startNewTrace(() => {
4+
// We want to ensure the new trace is sampled, so we force the sample_rand to a value above 0.9
5+
Sentry.getCurrentScope().setPropagationContext({
6+
...Sentry.getCurrentScope().getPropagationContext(),
7+
sampleRand: 0.85,
8+
});
9+
Sentry.startSpan({ op: 'ui.interaction.click', name: 'new-trace' }, async () => {
10+
await fetch('http://sentry-test-site.example');
11+
});
12+
});
13+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8" />
5+
</head>
6+
<body>
7+
<button id="newTrace">new Trace</button>
8+
</body>
9+
</html>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { expect } from '@playwright/test';
2+
import { sentryTest } from '../../../../utils/fixtures';
3+
import type { EventAndTraceHeader } from '../../../../utils/helpers';
4+
import {
5+
eventAndTraceHeaderRequestParser,
6+
getFirstSentryEnvelopeRequest,
7+
shouldSkipTracingTest,
8+
waitForTransactionRequest,
9+
} from '../../../../utils/helpers';
10+
11+
sentryTest(
12+
'new trace started with `startNewTrace` is sampled according to the `tracesSampler`',
13+
async ({ getLocalTestUrl, page }) => {
14+
if (shouldSkipTracingTest()) {
15+
sentryTest.skip();
16+
}
17+
18+
const url = await getLocalTestUrl({ testDir: __dirname });
19+
20+
await page.route('http://sentry-test-site.example/**', route => {
21+
return route.fulfill({
22+
status: 200,
23+
contentType: 'application/json',
24+
body: JSON.stringify({}),
25+
});
26+
});
27+
28+
const [pageloadEvent, pageloadTraceHeaders] = await getFirstSentryEnvelopeRequest<EventAndTraceHeader>(
29+
page,
30+
url,
31+
eventAndTraceHeaderRequestParser,
32+
);
33+
34+
const pageloadTraceContext = pageloadEvent.contexts?.trace;
35+
36+
expect(pageloadEvent.type).toEqual('transaction');
37+
38+
expect(pageloadTraceContext).toMatchObject({
39+
op: 'pageload',
40+
trace_id: expect.stringMatching(/^[0-9a-f]{32}$/),
41+
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
42+
data: {
43+
'sentry.sample_rate': 0.5,
44+
},
45+
});
46+
expect(pageloadTraceContext).not.toHaveProperty('parent_span_id');
47+
48+
expect(pageloadTraceHeaders).toEqual({
49+
environment: 'production',
50+
public_key: 'public',
51+
sample_rate: '0.5',
52+
sampled: 'true',
53+
trace_id: pageloadTraceContext?.trace_id,
54+
sample_rand: '0.45',
55+
});
56+
57+
const transactionPromise = waitForTransactionRequest(page, event => {
58+
return event.transaction === 'new-trace';
59+
});
60+
61+
await page.locator('#newTrace').click();
62+
63+
const [newTraceTransactionEvent, newTraceTransactionTraceHeaders] = eventAndTraceHeaderRequestParser(
64+
await transactionPromise,
65+
);
66+
67+
const newTraceTransactionTraceContext = newTraceTransactionEvent.contexts?.trace;
68+
expect(newTraceTransactionTraceContext).toMatchObject({
69+
op: 'ui.interaction.click',
70+
trace_id: expect.stringMatching(/^[0-9a-f]{32}$/),
71+
span_id: expect.stringMatching(/^[0-9a-f]{16}$/),
72+
data: {
73+
'sentry.sample_rate': 0.9,
74+
},
75+
});
76+
77+
expect(newTraceTransactionTraceHeaders).toEqual({
78+
environment: 'production',
79+
public_key: 'public',
80+
sample_rate: '0.9',
81+
sampled: 'true',
82+
trace_id: newTraceTransactionTraceContext?.trace_id,
83+
transaction: 'new-trace',
84+
sample_rand: '0.85',
85+
});
86+
87+
expect(newTraceTransactionTraceContext?.trace_id).not.toEqual(pageloadTraceContext?.trace_id);
88+
},
89+
);

0 commit comments

Comments
 (0)