-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtest.ts
More file actions
176 lines (130 loc) · 7.55 KB
/
test.ts
File metadata and controls
176 lines (130 loc) · 7.55 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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { expect } from '@playwright/test';
import {
extractTraceparentData,
parseBaggageHeader,
SEMANTIC_ATTRIBUTE_SENTRY_PREVIOUS_TRACE_SAMPLE_RATE,
SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE,
} from '@sentry/core';
import { sentryTest } from '../../../../../../utils/fixtures';
import {
eventAndTraceHeaderRequestParser,
shouldSkipTracingTest,
waitForTracingHeadersOnUrl,
waitForTransactionRequest,
} from '../../../../../../utils/helpers';
const metaTagSampleRand = 0.051121;
const metaTagSampleRate = 0.2;
sentryTest.describe('When `consistentTraceSampling` is `true` and page contains <meta> tags', () => {
sentryTest('Continues sampling decision across all traces from meta tag', async ({ getLocalTestUrl, page }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}
const url = await getLocalTestUrl({ testDir: __dirname });
const pageloadTraceContext = await sentryTest.step('Initial pageload', async () => {
const pageloadRequestPromise = waitForTransactionRequest(page, evt => evt.contexts?.trace?.op === 'pageload');
await page.goto(url);
const [pageloadEvent, pageloadTraceHeader] = eventAndTraceHeaderRequestParser(await pageloadRequestPromise);
const pageloadTraceContext = pageloadEvent.contexts?.trace;
expect(Number(pageloadTraceHeader?.sample_rand)).toBe(metaTagSampleRand);
expect(Number(pageloadTraceHeader?.sample_rate)).toBe(metaTagSampleRate);
// since the local sample rate was not applied, the sample rate attribute shouldn't be set
expect(pageloadTraceContext?.data?.[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]).toBeUndefined();
expect(pageloadTraceContext?.data?.[SEMANTIC_ATTRIBUTE_SENTRY_PREVIOUS_TRACE_SAMPLE_RATE]).toBeUndefined();
return pageloadTraceContext;
});
const customTraceContext = await sentryTest.step('Custom trace', async () => {
const customTrace1RequestPromise = waitForTransactionRequest(page, evt => evt.contexts?.trace?.op === 'custom');
await page.locator('#btn1').click();
const [customTrace1Event, customTraceTraceHeader] = eventAndTraceHeaderRequestParser(
await customTrace1RequestPromise,
);
const customTraceContext = customTrace1Event.contexts?.trace;
expect(customTraceContext?.trace_id).not.toEqual(pageloadTraceContext?.trace_id);
expect(customTraceContext?.parent_span_id).toBeUndefined();
expect(Number(customTraceTraceHeader?.sample_rand)).toBe(metaTagSampleRand);
expect(Number(customTraceTraceHeader?.sample_rate)).toBe(metaTagSampleRate);
expect(Boolean(customTraceTraceHeader?.sampled)).toBe(true);
// since the local sample rate was not applied, the sample rate attribute shouldn't be set
expect(customTraceContext?.data?.[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]).toBeUndefined();
// but we need to set this attribute to still be able to correctly add the sample rate to the DSC (checked above in trace header)
expect(customTraceContext?.data?.[SEMANTIC_ATTRIBUTE_SENTRY_PREVIOUS_TRACE_SAMPLE_RATE]).toBe(metaTagSampleRate);
return customTraceContext;
});
await sentryTest.step('Navigation', async () => {
const navigation1RequestPromise = waitForTransactionRequest(
page,
evt => evt.contexts?.trace?.op === 'navigation',
);
await page.goto(`${url}#foo`);
const [navigationEvent, navigationTraceHeader] = eventAndTraceHeaderRequestParser(
await navigation1RequestPromise,
);
const navigationTraceContext = navigationEvent.contexts?.trace;
expect(navigationTraceContext?.trace_id).not.toEqual(pageloadTraceContext?.trace_id);
expect(navigationTraceContext?.trace_id).not.toEqual(customTraceContext?.trace_id);
expect(navigationTraceContext?.parent_span_id).toBeUndefined();
expect(Number(navigationTraceHeader?.sample_rand)).toEqual(metaTagSampleRand);
expect(Number(navigationTraceHeader?.sample_rate)).toEqual(metaTagSampleRate);
expect(Boolean(navigationTraceHeader?.sampled)).toEqual(true);
// since the local sample rate was not applied, the sample rate attribute shouldn't be set
expect(navigationTraceContext?.data?.[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]).toBeUndefined();
// but we need to set this attribute to still be able to correctly add the sample rate to the DSC (checked above in trace header)
expect(navigationTraceContext?.data?.[SEMANTIC_ATTRIBUTE_SENTRY_PREVIOUS_TRACE_SAMPLE_RATE]).toBe(
metaTagSampleRate,
);
});
});
sentryTest(
'Propagates continued <meta> tag sampling decision to outgoing requests',
async ({ page, getLocalTestUrl }) => {
if (shouldSkipTracingTest()) {
sentryTest.skip();
}
const url = await getLocalTestUrl({ testDir: __dirname });
const pageloadTraceContext = await sentryTest.step('Initial pageload', async () => {
const pageloadRequestPromise = waitForTransactionRequest(page, evt => evt.contexts?.trace?.op === 'pageload');
await page.goto(url);
const [pageloadEvent, pageloadTraceHeader] = eventAndTraceHeaderRequestParser(await pageloadRequestPromise);
const pageloadTraceContext = pageloadEvent.contexts?.trace;
expect(Number(pageloadTraceHeader?.sample_rand)).toBe(metaTagSampleRand);
expect(Number(pageloadTraceHeader?.sample_rate)).toBe(metaTagSampleRate);
// since the local sample rate was not applied, the sample rate attribute shouldn't be set
expect(pageloadTraceContext?.data?.[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]).toBeUndefined();
expect(pageloadTraceContext?.data?.[SEMANTIC_ATTRIBUTE_SENTRY_PREVIOUS_TRACE_SAMPLE_RATE]).toBeUndefined();
return pageloadTraceContext;
});
await sentryTest.step('Make fetch request', async () => {
const fetchTracePromise = waitForTransactionRequest(page, evt => evt.contexts?.trace?.op === 'custom');
const tracingHeadersPromise = waitForTracingHeadersOnUrl(page, 'http://sentry-test-external.io');
await page.locator('#btn2').click();
const { baggage, sentryTrace } = await tracingHeadersPromise;
const [fetchTraceEvent, fetchTraceTraceHeader] = eventAndTraceHeaderRequestParser(await fetchTracePromise);
const fetchTraceSampleRand = Number(fetchTraceTraceHeader?.sample_rand);
const fetchTraceTraceContext = fetchTraceEvent.contexts?.trace;
const httpClientSpan = fetchTraceEvent.spans?.find(span => span.op === 'http.client');
expect(fetchTraceSampleRand).toEqual(metaTagSampleRand);
expect(fetchTraceTraceContext?.data?.[SEMANTIC_ATTRIBUTE_SENTRY_SAMPLE_RATE]).toBeUndefined();
expect(fetchTraceTraceContext?.data?.[SEMANTIC_ATTRIBUTE_SENTRY_PREVIOUS_TRACE_SAMPLE_RATE]).toBe(
metaTagSampleRate,
);
expect(fetchTraceTraceContext?.trace_id).not.toEqual(pageloadTraceContext?.trace_id);
expect(sentryTrace).toBeDefined();
expect(baggage).toBeDefined();
expect(extractTraceparentData(sentryTrace)).toEqual({
traceId: fetchTraceTraceContext?.trace_id,
parentSpanId: httpClientSpan?.span_id,
parentSampled: true,
});
expect(parseBaggageHeader(baggage)).toEqual({
'sentry-environment': 'production',
'sentry-public_key': 'public',
'sentry-sample_rand': `${metaTagSampleRand}`,
'sentry-sample_rate': `${metaTagSampleRate}`,
'sentry-sampled': 'true',
'sentry-trace_id': fetchTraceTraceContext?.trace_id,
'sentry-transaction': 'custom root span 2',
});
});
},
);
});