-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathtest.ts
More file actions
187 lines (155 loc) · 7.2 KB
/
test.ts
File metadata and controls
187 lines (155 loc) · 7.2 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
177
178
179
180
181
182
183
184
185
186
187
import { expect } from '@playwright/test';
import type { Event, Profile, ProfileChunkEnvelope } from '@sentry/core';
import { sentryTest } from '../../../utils/fixtures';
import {
getMultipleSentryEnvelopeRequests,
properEnvelopeRequestParser,
properFullEnvelopeRequestParser,
shouldSkipTracingTest,
waitForTransactionRequestOnUrl,
} from '../../../utils/helpers';
sentryTest(
'does not send profile envelope when document-policy is not set',
async ({ page, getLocalTestUrl, browserName }) => {
if (shouldSkipTracingTest() || browserName !== 'chromium') {
// Profiling only works when tracing is enabled
sentryTest.skip();
}
const url = await getLocalTestUrl({ testDir: __dirname });
const req = await waitForTransactionRequestOnUrl(page, url);
const transactionEvent = properEnvelopeRequestParser<Event>(req, 0);
const profileEvent = properEnvelopeRequestParser<Profile>(req, 1);
expect(transactionEvent).toBeDefined();
expect(profileEvent).toBeUndefined();
},
);
sentryTest(
'sends profile envelope in trace mode (single chunk for overlapping spans)',
async ({ page, getLocalTestUrl, browserName }) => {
if (shouldSkipTracingTest() || browserName !== 'chromium') {
// Profiling only works when tracing is enabled
sentryTest.skip();
}
const url = await getLocalTestUrl({ testDir: __dirname, responseHeaders: { 'Document-Policy': 'js-profiling' } });
await page.goto(url);
const profileChunkEnvelopePromise = getMultipleSentryEnvelopeRequests<ProfileChunkEnvelope>(
page,
1,
{ envelopeType: 'profile_chunk' },
properFullEnvelopeRequestParser,
);
const profileChunkEnvelopeItem = (await profileChunkEnvelopePromise)[0][1][0];
const envelopeItemHeader = profileChunkEnvelopeItem[0];
const envelopeItemPayload = profileChunkEnvelopeItem[1];
expect(envelopeItemHeader).toHaveProperty('type', 'profile_chunk');
expect(envelopeItemPayload.profile).toBeDefined();
expect(envelopeItemPayload.version).toBe('2');
expect(envelopeItemPayload.platform).toBe('javascript');
// Required profile metadata (Sample Format V2)
// https://develop.sentry.dev/sdk/telemetry/profiles/sample-format-v2/
expect(typeof envelopeItemPayload.profiler_id).toBe('string');
expect(envelopeItemPayload.profiler_id).toMatch(/^[a-f\d]{32}$/);
expect(typeof envelopeItemPayload.chunk_id).toBe('string');
expect(envelopeItemPayload.chunk_id).toMatch(/^[a-f\d]{32}$/);
expect(envelopeItemPayload.client_sdk).toBeDefined();
expect(typeof envelopeItemPayload.client_sdk.name).toBe('string');
expect(typeof envelopeItemPayload.client_sdk.version).toBe('string');
expect(typeof envelopeItemPayload.release).toBe('string');
expect(envelopeItemPayload.debug_meta).toBeDefined();
expect(Array.isArray(envelopeItemPayload?.debug_meta?.images)).toBe(true);
const profile = envelopeItemPayload.profile;
expect(profile.samples).toBeDefined();
expect(profile.stacks).toBeDefined();
expect(profile.frames).toBeDefined();
expect(profile.thread_metadata).toBeDefined();
// Samples
expect(profile.samples.length).toBeGreaterThanOrEqual(2);
let previousTimestamp = Number.NEGATIVE_INFINITY;
for (const sample of profile.samples) {
expect(typeof sample.stack_id).toBe('number');
expect(sample.stack_id).toBeGreaterThanOrEqual(0);
expect(sample.stack_id).toBeLessThan(profile.stacks.length);
// In trace lifecycle mode, samples carry a numeric timestamp (ms since epoch or similar clock)
expect(typeof sample.timestamp).toBe('number');
const ts = sample.timestamp;
expect(Number.isFinite(ts)).toBe(true);
expect(ts).toBeGreaterThan(0);
// Monotonic non-decreasing timestamps
expect(ts).toBeGreaterThanOrEqual(previousTimestamp);
previousTimestamp = ts;
expect(sample.thread_id).toBe('0'); // Should be main thread
}
// Stacks
expect(profile.stacks.length).toBeGreaterThan(0);
for (const stack of profile.stacks) {
expect(Array.isArray(stack)).toBe(true);
for (const frameIndex of stack) {
expect(typeof frameIndex).toBe('number');
expect(frameIndex).toBeGreaterThanOrEqual(0);
expect(frameIndex).toBeLessThan(profile.frames.length);
}
}
// Frames
expect(profile.frames.length).toBeGreaterThan(0);
for (const frame of profile.frames) {
expect(frame).toHaveProperty('function');
expect(typeof frame.function).toBe('string');
if (frame.function !== 'fetch' && frame.function !== 'setTimeout') {
expect(frame).toHaveProperty('abs_path');
expect(frame).toHaveProperty('lineno');
expect(frame).toHaveProperty('colno');
expect(typeof frame.abs_path).toBe('string');
expect(typeof frame.lineno).toBe('number');
expect(typeof frame.colno).toBe('number');
}
}
const functionNames = profile.frames.map(frame => frame.function).filter(name => name !== '');
if ((process.env.PW_BUNDLE || '').endsWith('min')) {
// In bundled mode, function names are minified
expect(functionNames.length).toBeGreaterThan(0);
expect((functionNames as string[]).every(name => name?.length > 0)).toBe(true); // Just make sure they're not empty strings
} else {
expect(functionNames).toEqual(
expect.arrayContaining([
'_startRootSpan',
'withScope',
'createChildOrRootSpan',
'startSpanManual',
'startJSSelfProfile',
// both functions are captured
'fibonacci',
'largeSum',
]),
);
}
expect(profile.thread_metadata).toHaveProperty('0');
expect(profile.thread_metadata['0']).toHaveProperty('name');
expect(profile.thread_metadata['0'].name).toBe('main');
// Test that profile duration makes sense (should be > 20ms based on test setup)
const startTimeSec = (profile.samples[0] as any).timestamp as number;
const endTimeSec = (profile.samples[profile.samples.length - 1] as any).timestamp as number;
const durationSec = endTimeSec - startTimeSec;
// Should be at least 20ms based on our setTimeout(21) in the test
expect(durationSec).toBeGreaterThan(0.2);
},
);
sentryTest('attaches thread data to child spans (trace mode)', async ({ page, getLocalTestUrl, browserName }) => {
if (shouldSkipTracingTest() || browserName !== 'chromium') {
sentryTest.skip();
}
const url = await getLocalTestUrl({ testDir: __dirname, responseHeaders: { 'Document-Policy': 'js-profiling' } });
const req = await waitForTransactionRequestOnUrl(page, url);
const rootSpan = properEnvelopeRequestParser<Event>(req, 0) as any;
expect(rootSpan?.type).toBe('transaction');
expect(rootSpan.transaction).toBe('root-fibonacci-2');
const profilerId = rootSpan?.contexts?.profile?.profiler_id as string | undefined;
expect(typeof profilerId).toBe('string');
expect(profilerId).toMatch(/^[a-f\d]{32}$/);
const spans = (rootSpan?.spans ?? []) as Array<{ data?: Record<string, unknown> }>;
expect(spans.length).toBeGreaterThan(0);
for (const span of spans) {
expect(span.data).toBeDefined();
expect(span.data?.['thread.id']).toBe('0');
expect(span.data?.['thread.name']).toBe('main');
}
});