-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathbrowserMetrics.test.ts
429 lines (363 loc) · 11.9 KB
/
browserMetrics.test.ts
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
import {
SEMANTIC_ATTRIBUTE_SENTRY_OP,
SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN,
SentrySpan,
getClient,
getCurrentScope,
getIsolationScope,
setCurrentClient,
spanToJSON,
} from '@sentry/core';
import type { Span } from '@sentry/core';
import { describe, beforeEach, it, expect, beforeAll, afterAll } from 'vitest';
import { _addMeasureSpans, _addResourceSpans } from '../../src/metrics/browserMetrics';
import { WINDOW } from '../../src/types';
import { TestClient, getDefaultClientOptions } from '../utils/TestClient';
const mockWindowLocation = {
ancestorOrigins: {},
href: 'https://example.com/path/to/something',
origin: 'https://example.com',
protocol: 'https',
host: 'example.com',
hostname: 'example.com',
port: '',
pathname: '/path/to/something',
search: '',
hash: '',
} as Window['location'];
const originalLocation = WINDOW.location;
const resourceEntryName = 'https://example.com/assets/to/css';
interface AdditionalPerformanceResourceTiming {
renderBlockingStatus?: 'non-blocking' | 'blocking' | '';
deliveryType?: 'cache' | 'navigational-prefetch' | '';
}
function mockPerformanceResourceTiming(
data: Partial<PerformanceResourceTiming> & AdditionalPerformanceResourceTiming,
): PerformanceResourceTiming & AdditionalPerformanceResourceTiming {
return data as PerformanceResourceTiming & AdditionalPerformanceResourceTiming;
}
describe('_addMeasureSpans', () => {
const span = new SentrySpan({ op: 'pageload', name: '/', sampled: true });
beforeEach(() => {
getCurrentScope().clear();
getIsolationScope().clear();
const client = new TestClient(
getDefaultClientOptions({
tracesSampleRate: 1,
}),
);
setCurrentClient(client);
client.init();
});
it('adds measure spans to a span', () => {
const spans: Span[] = [];
getClient()?.on('spanEnd', span => {
spans.push(span);
});
const entry = {
entryType: 'measure',
name: 'measure-1',
duration: 10,
startTime: 12,
} as PerformanceEntry;
const timeOrigin = 100;
const startTime = 23;
const duration = 356;
_addMeasureSpans(span, entry, startTime, duration, timeOrigin);
expect(spans).toHaveLength(1);
expect(spanToJSON(spans[0]!)).toEqual(
expect.objectContaining({
description: 'measure-1',
start_timestamp: timeOrigin + startTime,
timestamp: timeOrigin + startTime + duration,
op: 'measure',
origin: 'auto.resource.browser.metrics',
data: {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'measure',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.resource.browser.metrics',
},
}),
);
});
it('drops measurement spans with negative duration', () => {
const spans: Span[] = [];
getClient()?.on('spanEnd', span => {
spans.push(span);
});
const entry = {
entryType: 'measure',
name: 'measure-1',
duration: 10,
startTime: 12,
} as PerformanceEntry;
const timeOrigin = 100;
const startTime = 23;
const duration = -50;
_addMeasureSpans(span, entry, startTime, duration, timeOrigin);
expect(spans).toHaveLength(0);
});
});
describe('_addResourceSpans', () => {
const span = new SentrySpan({ op: 'pageload', name: '/', sampled: true });
beforeAll(() => {
setGlobalLocation(mockWindowLocation);
});
afterAll(() => {
resetGlobalLocation();
});
beforeEach(() => {
getCurrentScope().clear();
getIsolationScope().clear();
const client = new TestClient(
getDefaultClientOptions({
tracesSampleRate: 1,
}),
);
setCurrentClient(client);
client.init();
});
it('does not create spans for xmlhttprequest', () => {
const spans: Span[] = [];
getClient()?.on('spanEnd', span => {
spans.push(span);
});
const entry = mockPerformanceResourceTiming({
initiatorType: 'xmlhttprequest',
transferSize: 256,
encodedBodySize: 256,
decodedBodySize: 256,
renderBlockingStatus: 'non-blocking',
nextHopProtocol: 'http/1.1',
});
_addResourceSpans(span, entry, resourceEntryName, 123, 456, 100);
expect(spans).toHaveLength(0);
});
it('does not create spans for fetch', () => {
const spans: Span[] = [];
getClient()?.on('spanEnd', span => {
spans.push(span);
});
const entry = mockPerformanceResourceTiming({
initiatorType: 'fetch',
transferSize: 256,
encodedBodySize: 256,
decodedBodySize: 256,
renderBlockingStatus: 'non-blocking',
nextHopProtocol: 'http/1.1',
});
_addResourceSpans(span, entry, 'https://example.com/assets/to/me', 123, 456, 100);
expect(spans).toHaveLength(0);
});
it('creates spans for resource spans', () => {
const spans: Span[] = [];
getClient()?.on('spanEnd', span => {
spans.push(span);
});
const entry = mockPerformanceResourceTiming({
initiatorType: 'css',
transferSize: 256,
encodedBodySize: 456,
decodedBodySize: 593,
renderBlockingStatus: 'non-blocking',
nextHopProtocol: 'http/1.1',
});
const timeOrigin = 100;
const startTime = 23;
const duration = 356;
_addResourceSpans(span, entry, resourceEntryName, startTime, duration, timeOrigin);
expect(spans).toHaveLength(1);
expect(spanToJSON(spans[0]!)).toEqual(
expect.objectContaining({
description: '/assets/to/css',
start_timestamp: timeOrigin + startTime,
timestamp: timeOrigin + startTime + duration,
op: 'resource.css',
origin: 'auto.resource.browser.metrics',
data: {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'resource.css',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.resource.browser.metrics',
['http.decoded_response_content_length']: entry.decodedBodySize,
['http.response_content_length']: entry.encodedBodySize,
['http.response_transfer_size']: entry.transferSize,
['resource.render_blocking_status']: entry.renderBlockingStatus,
['url.scheme']: 'https',
['server.address']: 'example.com',
['url.same_origin']: true,
['network.protocol.name']: 'http',
['network.protocol.version']: '1.1',
},
}),
);
});
it('creates a variety of resource spans', () => {
const spans: Span[] = [];
getClient()?.on('spanEnd', span => {
spans.push(span);
});
const table = [
{
initiatorType: undefined,
op: 'resource.other',
},
{
initiatorType: '',
op: 'resource.other',
},
{
initiatorType: 'css',
op: 'resource.css',
},
{
initiatorType: 'image',
op: 'resource.image',
},
{
initiatorType: 'script',
op: 'resource.script',
},
];
for (let i = 0; i < table.length; i++) {
const { initiatorType, op } = table[i]!;
const entry = mockPerformanceResourceTiming({
initiatorType,
nextHopProtocol: 'http/1.1',
});
_addResourceSpans(span, entry, 'https://example.com/assets/to/me', 123, 234, 465);
expect(spans).toHaveLength(i + 1);
expect(spanToJSON(spans[i]!)).toEqual(expect.objectContaining({ op }));
}
});
it('allows for enter size of 0', () => {
const spans: Span[] = [];
getClient()?.on('spanEnd', span => {
spans.push(span);
});
const entry = mockPerformanceResourceTiming({
initiatorType: 'css',
transferSize: 0,
encodedBodySize: 0,
decodedBodySize: 0,
renderBlockingStatus: 'non-blocking',
nextHopProtocol: 'h2',
});
_addResourceSpans(span, entry, resourceEntryName, 100, 23, 345);
expect(spans).toHaveLength(1);
expect(spanToJSON(spans[0]!)).toEqual(
expect.objectContaining({
data: {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'resource.css',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.resource.browser.metrics',
['http.decoded_response_content_length']: entry.decodedBodySize,
['http.response_content_length']: entry.encodedBodySize,
['http.response_transfer_size']: entry.transferSize,
['resource.render_blocking_status']: entry.renderBlockingStatus,
['url.scheme']: 'https',
['server.address']: 'example.com',
['url.same_origin']: true,
['network.protocol.name']: 'http',
['network.protocol.version']: '2',
},
}),
);
});
it('does not attach resource sizes that exceed MAX_INT bytes', () => {
const spans: Span[] = [];
getClient()?.on('spanEnd', span => {
spans.push(span);
});
const entry = mockPerformanceResourceTiming({
initiatorType: 'css',
transferSize: 2147483647,
encodedBodySize: 2147483647,
decodedBodySize: 2147483647,
nextHopProtocol: 'h3',
});
_addResourceSpans(span, entry, resourceEntryName, 100, 23, 345);
expect(spans).toHaveLength(1);
expect(spanToJSON(spans[0]!)).toEqual(
expect.objectContaining({
data: {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'resource.css',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.resource.browser.metrics',
'server.address': 'example.com',
'url.same_origin': true,
'url.scheme': 'https',
['network.protocol.name']: 'http',
['network.protocol.version']: '3',
},
description: '/assets/to/css',
timestamp: 468,
op: 'resource.css',
origin: 'auto.resource.browser.metrics',
start_timestamp: 445,
}),
);
});
// resource sizes can be set as null on some browsers
// https://github.com/getsentry/sentry/pull/60601
it('does not attach null resource sizes', () => {
const spans: Span[] = [];
getClient()?.on('spanEnd', span => {
spans.push(span);
});
const entry = {
initiatorType: 'css',
transferSize: null,
encodedBodySize: null,
decodedBodySize: null,
nextHopProtocol: 'h3',
} as unknown as PerformanceResourceTiming;
_addResourceSpans(span, entry, resourceEntryName, 100, 23, 345);
expect(spans).toHaveLength(1);
expect(spanToJSON(spans[0]!)).toEqual(
expect.objectContaining({
data: {
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'resource.css',
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.resource.browser.metrics',
'server.address': 'example.com',
'url.same_origin': true,
'url.scheme': 'https',
['network.protocol.name']: 'http',
['network.protocol.version']: '3',
},
description: '/assets/to/css',
timestamp: 468,
op: 'resource.css',
origin: 'auto.resource.browser.metrics',
start_timestamp: 445,
}),
);
});
// resource delivery types: https://developer.mozilla.org/en-US/docs/Web/API/PerformanceResourceTiming/deliveryType
// i.e. better but not yet widely supported way to check for browser cache hit
it.each(['cache', 'navigational-prefetch', ''] as const)(
'attaches delivery type ("%s") to resource spans if available',
deliveryType => {
const spans: Span[] = [];
getClient()?.on('spanEnd', span => {
spans.push(span);
});
const entry = mockPerformanceResourceTiming({
initiatorType: 'css',
transferSize: 0,
encodedBodySize: 0,
decodedBodySize: 0,
deliveryType,
nextHopProtocol: 'h3',
});
_addResourceSpans(span, entry, resourceEntryName, 100, 23, 345);
expect(spans).toHaveLength(1);
expect(spanToJSON(spans[0]!).data).toMatchObject({ 'http.response_delivery_type': deliveryType });
},
);
});
const setGlobalLocation = (location: Location) => {
// @ts-expect-error need to delete this in order to set to new value
delete WINDOW.location;
WINDOW.location = location;
};
const resetGlobalLocation = () => {
// @ts-expect-error need to delete this in order to set to new value
delete WINDOW.location;
WINDOW.location = originalLocation;
};