-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtest.ts
More file actions
118 lines (99 loc) · 4.7 KB
/
test.ts
File metadata and controls
118 lines (99 loc) · 4.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import { expect } from '@playwright/test';
import type { ClientReport } from '@sentry/core';
import { extractTraceparentData, parseBaggageHeader } from '@sentry/core';
import { sentryTest } from '../../../../../../utils/fixtures';
import {
envelopeRequestParser,
hidePage,
shouldSkipTracingTest,
testingCdnBundle,
waitForClientReportRequest,
waitForTracingHeadersOnUrl,
} from '../../../../../../utils/helpers';
import { getSpanOp, waitForStreamedSpanEnvelope } from '../../../../../../utils/spanUtils';
const metaTagSampleRand = 0.9;
const metaTagSampleRate = 0.2;
const metaTagTraceIdIndex = '12345678901234567890123456789012';
const metaTagTraceIdPage1 = 'a2345678901234567890123456789012';
sentryTest.describe('When `consistentTraceSampling` is `true` and page contains <meta> tags', () => {
sentryTest(
'meta tag decision has precedence over sampling decision from previous trace in session storage',
async ({ getLocalTestUrl, page }) => {
sentryTest.skip(shouldSkipTracingTest() || testingCdnBundle());
const url = await getLocalTestUrl({ testDir: __dirname });
const clientReportPromise = waitForClientReportRequest(page);
await sentryTest.step('Initial pageload', async () => {
// negative sampling decision -> no pageload span
await page.goto(url);
});
await sentryTest.step('Make fetch request', async () => {
// The fetch requests starts a new trace on purpose. So we only want the
// sampling decision and rand to be the same as from the meta tag but not the trace id or DSC
const tracingHeadersPromise = waitForTracingHeadersOnUrl(page, 'http://sentry-test-external.io');
await page.locator('#btn2').click();
const { baggage, sentryTrace } = await tracingHeadersPromise;
expect(sentryTrace).toBeDefined();
expect(baggage).toBeDefined();
expect(extractTraceparentData(sentryTrace)).toEqual({
traceId: expect.not.stringContaining(metaTagTraceIdIndex),
parentSpanId: expect.stringMatching(/^[\da-f]{16}$/),
parentSampled: false,
});
expect(parseBaggageHeader(baggage)).toEqual({
'sentry-environment': 'production',
'sentry-public_key': 'public',
'sentry-sample_rand': `${metaTagSampleRand}`,
'sentry-sample_rate': `${metaTagSampleRate}`,
'sentry-sampled': 'false',
'sentry-trace_id': expect.not.stringContaining(metaTagTraceIdIndex),
'sentry-transaction': 'custom root span 2',
});
});
await sentryTest.step('Client report', async () => {
await hidePage(page);
const clientReport = envelopeRequestParser<ClientReport>(await clientReportPromise);
expect(clientReport).toEqual({
timestamp: expect.any(Number),
discarded_events: [
{
category: 'span',
quantity: expect.any(Number),
reason: 'sample_rate',
},
],
});
// exact number depends on performance observer emissions
expect(clientReport.discarded_events[0].quantity).toBeGreaterThanOrEqual(3);
});
await sentryTest.step('Navigate to another page with meta tags', async () => {
const page1PageloadEnvelopePromise = waitForStreamedSpanEnvelope(
page,
env => !!env[1][0][1].items.find(s => getSpanOp(s) === 'pageload' && s.trace_id === metaTagTraceIdPage1),
);
await page.locator('a').click();
const envelope = await page1PageloadEnvelopePromise;
const pageloadSpan = envelope[1][0][1].items.find(s => getSpanOp(s) === 'pageload')!;
expect(Number(envelope[0].trace?.sample_rand)).toBe(0.12);
expect(Number(envelope[0].trace?.sample_rate)).toBe(0.2);
expect(pageloadSpan.trace_id).toEqual(metaTagTraceIdPage1);
});
await sentryTest.step('Navigate to another page without meta tags', async () => {
const page2PageloadEnvelopePromise = waitForStreamedSpanEnvelope(
page,
env =>
!!env[1][0][1].items.find(
s =>
getSpanOp(s) === 'pageload' && s.trace_id !== metaTagTraceIdPage1 && s.trace_id !== metaTagTraceIdIndex,
),
);
await page.locator('a').click();
const envelope = await page2PageloadEnvelopePromise;
const pageloadSpan = envelope[1][0][1].items.find(s => getSpanOp(s) === 'pageload')!;
expect(Number(envelope[0].trace?.sample_rand)).toBe(0.12);
expect(Number(envelope[0].trace?.sample_rate)).toBe(0.2);
expect(pageloadSpan.trace_id).not.toEqual(metaTagTraceIdPage1);
expect(pageloadSpan.trace_id).not.toEqual(metaTagTraceIdIndex);
});
},
);
});