-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
Copy pathperformanceForSentry.tsx
391 lines (342 loc) · 12.5 KB
/
performanceForSentry.tsx
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
import {Fragment, Profiler, ReactNode, useEffect, useRef} from 'react';
import {captureException, captureMessage} from '@sentry/react';
import * as Sentry from '@sentry/react';
import {IdleTransaction} from '@sentry/tracing';
import {Transaction} from '@sentry/types';
import {browserPerformanceTimeOrigin, timestampWithMs} from '@sentry/utils';
import getCurrentSentryReactTransaction from './getCurrentSentryReactTransaction';
const MIN_UPDATE_SPAN_TIME = 16; // Frame boundary @ 60fps
const WAIT_POST_INTERACTION = 50; // Leave a small amount of time for observers and onRenderCallback to log since they come in after they occur and not during.
const INTERACTION_TIMEOUT = 2 * 60_000; // 2min. Wrap interactions up after this time since we don't want transactions sticking around forever.
/**
* It depends on where it is called but the way we fetch transactions can be empty despite an ongoing transaction existing.
* This will return an interaction-type transaction held onto by a class static if one exists.
*/
export function getPerformanceTransaction(): IdleTransaction | Transaction | undefined {
return PerformanceInteraction.getTransaction() ?? getCurrentSentryReactTransaction();
}
/**
* Callback for React Profiler https://reactjs.org/docs/profiler.html
*/
export function onRenderCallback(
id: string,
phase: 'mount' | 'update',
actualDuration: number
) {
try {
const transaction: Transaction | undefined = getPerformanceTransaction();
if (transaction && actualDuration > MIN_UPDATE_SPAN_TIME) {
const now = timestampWithMs();
transaction.startChild({
description: `<${id}>`,
op: `ui.react.${phase}`,
startTimestamp: now - actualDuration / 1000,
endTimestamp: now,
});
}
} catch (_) {
// Add defensive catch since this wraps all of App
}
}
export class PerformanceInteraction {
private static interactionTransaction: Transaction | null = null;
private static interactionTimeoutId: number | undefined = undefined;
static getTransaction() {
return PerformanceInteraction.interactionTransaction;
}
static startInteraction(name: string, timeout = INTERACTION_TIMEOUT, immediate = true) {
try {
const currentIdleTransaction = getCurrentSentryReactTransaction();
if (currentIdleTransaction) {
// If interaction is started while idle still exists.
currentIdleTransaction.setTag('finishReason', 'sentry.interactionStarted'); // Override finish reason so we can capture if this has effects on idle timeout.
currentIdleTransaction.finish();
}
PerformanceInteraction.finishInteraction(immediate);
const txn = Sentry?.startTransaction({
name: `ui.${name}`,
op: 'interaction',
});
PerformanceInteraction.interactionTransaction = txn;
// Auto interaction timeout
PerformanceInteraction.interactionTimeoutId = window.setTimeout(() => {
if (!PerformanceInteraction.interactionTransaction) {
return;
}
PerformanceInteraction.interactionTransaction.setTag(
'ui.interaction.finish',
'timeout'
);
PerformanceInteraction.finishInteraction(true);
}, timeout);
} catch (e) {
captureMessage(e);
}
}
static async finishInteraction(immediate = false) {
try {
if (!PerformanceInteraction.interactionTransaction) {
return;
}
clearTimeout(PerformanceInteraction.interactionTimeoutId);
if (immediate) {
PerformanceInteraction.interactionTransaction?.finish();
PerformanceInteraction.interactionTransaction = null;
return;
}
// Add a slight wait if this isn't called as the result of another transaction starting.
await new Promise(resolve => setTimeout(resolve, WAIT_POST_INTERACTION));
PerformanceInteraction.interactionTransaction?.finish();
PerformanceInteraction.interactionTransaction = null;
return;
} catch (e) {
captureMessage(e);
}
}
}
export class LongTaskObserver {
private static observer: PerformanceObserver;
private static longTaskCount = 0;
private static longTaskDuration = 0;
private static lastTransaction: IdleTransaction | Transaction | undefined;
static setLongTaskData(t: IdleTransaction | Transaction) {
const group =
[
1, 2, 5, 10, 25, 50, 100, 150, 200, 250, 300, 400, 500, 600, 700, 800, 900, 1001,
].find(n => LongTaskObserver.longTaskCount <= n) || -1;
t.setTag('ui.longTaskCount.grouped', group < 1001 ? `<=${group}` : `>1000`);
t.setMeasurement('longTaskCount', LongTaskObserver.longTaskCount, '');
t.setMeasurement('longTaskDuration', LongTaskObserver.longTaskDuration, '');
}
static startPerformanceObserver(): PerformanceObserver | null {
try {
if (LongTaskObserver.observer) {
LongTaskObserver.observer.disconnect();
try {
LongTaskObserver.observer.observe({entryTypes: ['longtask']});
} catch (_) {
// Safari doesn't support longtask, ignore this error.
}
return LongTaskObserver.observer;
}
if (!window.PerformanceObserver || !browserPerformanceTimeOrigin) {
return null;
}
const observer = new PerformanceObserver(function () {
try {
const transaction = getPerformanceTransaction();
if (!transaction) {
return;
}
if (transaction !== LongTaskObserver.lastTransaction) {
// If long tasks observer is active and is called while the transaction has changed.
if (LongTaskObserver.lastTransaction) {
LongTaskObserver.setLongTaskData(LongTaskObserver.lastTransaction);
}
LongTaskObserver.longTaskCount = 0;
LongTaskObserver.longTaskDuration = 0;
LongTaskObserver.lastTransaction = transaction;
}
LongTaskObserver.setLongTaskData(transaction);
} catch (_) {
// Defensive catch.
}
});
if (!observer || !observer.observe) {
return null;
}
LongTaskObserver.observer = observer;
try {
LongTaskObserver.observer.observe({entryTypes: ['longtask']});
} catch (_) {
// Safari doesn't support longtask, ignore this error.
}
return LongTaskObserver.observer;
} catch (e) {
captureException(e);
// Defensive try catch.
}
return null;
}
}
export const CustomerProfiler = ({id, children}: {children: ReactNode; id: string}) => {
return (
<Profiler id={id} onRender={onRenderCallback}>
{children}
</Profiler>
);
};
/**
* This component wraps the main component on a page with a measurement checking for visual completedness.
* It uses the data check to make sure endpoints have resolved and the component is meaningfully rendering
* which sets it apart from simply checking LCP, which makes it a good back up check the LCP heuristic performance.
*
* Since this component is guaranteed to be part of the -real- critical path, it also wraps the component with the custom profiler.
*/
export const VisuallyCompleteWithData = ({
id,
hasData,
children,
}: {
children: ReactNode;
hasData: boolean;
id: string;
}) => {
const isVisuallyCompleteSet = useRef(false);
const isDataCompleteSet = useRef(false);
const longTaskCount = useRef(0);
useEffect(() => {
let observer;
try {
if (!window.PerformanceObserver || !browserPerformanceTimeOrigin) {
return () => {};
}
observer = LongTaskObserver.startPerformanceObserver();
} catch (_) {
// Defensive since this is auxiliary code.
}
return () => {
if (observer && observer.disconnect) {
observer.disconnect();
}
};
}, []);
const num = useRef(1);
const isVCDSet = useRef(false);
if (isVCDSet && hasData && performance && performance.mark) {
performance.mark(`${id}-vcsd-start`);
isVCDSet.current = true;
}
useEffect(() => {
try {
const transaction: any = getCurrentSentryReactTransaction(); // Using any to override types for private api.
if (!transaction) {
return;
}
if (!isVisuallyCompleteSet.current) {
const time = performance.now();
transaction.registerBeforeFinishCallback((t: Transaction, _) => {
// Should be called after performance entries finish callback.
t.setMeasurement('visuallyComplete', time, 'ms');
});
isVisuallyCompleteSet.current = true;
}
if (!isDataCompleteSet.current && hasData) {
isDataCompleteSet.current = true;
performance.mark(`${id}-vcsd-end-pre-timeout`);
window.setTimeout(() => {
if (!browserPerformanceTimeOrigin) {
return;
}
performance.mark(`${id}-vcsd-end`);
const measureName = `VCD [${id}] #${num.current}`;
performance.measure(
`VCD [${id}] #${num.current}`,
`${id}-vcsd-start`,
`${id}-vcsd-end`
);
num.current = num.current++;
const [measureEntry] = performance.getEntriesByName(measureName);
if (!measureEntry) {
return;
}
transaction.registerBeforeFinishCallback((t: Transaction) => {
if (!browserPerformanceTimeOrigin) {
return;
}
// Should be called after performance entries finish callback.
const lcp = (t as any)._measurements.lcp?.value;
// Adjust to be relative to transaction.startTimestamp
const entryStartSeconds =
browserPerformanceTimeOrigin / 1000 + measureEntry.startTime / 1000;
const time = (entryStartSeconds - transaction.startTimestamp) * 1000;
if (lcp) {
t.setMeasurement('lcpDiffVCD', lcp - time, 'ms');
}
t.setTag('longTaskCount', longTaskCount.current);
t.setMeasurement('visuallyCompleteData', time, 'ms');
});
}, 0);
}
} catch (_) {
// Defensive catch since this code is auxiliary.
}
}, [hasData, id]);
return (
<Profiler id={id} onRender={onRenderCallback}>
<Fragment>{children}</Fragment>
</Profiler>
);
};
interface OpAssetMeasurementDefinition {
key: string;
}
const OP_ASSET_MEASUREMENT_MAP: Record<string, OpAssetMeasurementDefinition> = {
'resource.script': {
key: 'script',
},
'resource.css': {
key: 'css',
},
'resource.link': {
key: 'link',
},
'resource.img': {
key: 'img',
},
};
const ASSET_MEASUREMENT_ALL = 'allResources';
const measureAssetsOnTransaction = () => {
try {
const transaction: any = getCurrentSentryReactTransaction(); // Using any to override types for private api.
if (!transaction) {
return;
}
transaction.registerBeforeFinishCallback((t: Transaction) => {
const spans: any[] = (t as any).spanRecorder?.spans;
const measurements = (t as any)._measurements;
if (!spans) {
return;
}
if (measurements[ASSET_MEASUREMENT_ALL]) {
return;
}
let allTransfered = 0;
let allEncoded = 0;
let allCount = 0;
for (const [op, definition] of Object.entries(OP_ASSET_MEASUREMENT_MAP)) {
const filtered = spans.filter(s => s.op === op);
const count = filtered.length;
const transfered = filtered.reduce(
(acc, curr) => acc + (curr.data['Transfer Size'] ?? 0),
0
);
const encoded = filtered.reduce(
(acc, curr) => acc + (curr.data['Encoded Body Size'] ?? 0),
0
);
if (op === 'resource.script') {
t.setMeasurement(`assets.${definition.key}.encoded`, encoded, '');
t.setMeasurement(`assets.${definition.key}.transfer`, transfered, '');
t.setMeasurement(`assets.${definition.key}.count`, count, '');
}
allCount += count;
allTransfered += transfered;
allEncoded += encoded;
}
t.setMeasurement(`${ASSET_MEASUREMENT_ALL}.encoded`, allEncoded, '');
t.setMeasurement(`${ASSET_MEASUREMENT_ALL}.transfer`, allTransfered, '');
t.setMeasurement(`${ASSET_MEASUREMENT_ALL}.count`, allCount, '');
});
} catch (_) {
// Defensive catch since this code is auxiliary.
}
};
/**
* This will add asset-measurement code to the transaction after a timeout.
* Meant to be called from the sdk without pushing too many perf concerns into our initializeSdk code,
* it's fine if not every transaction gets recorded.
*/
export const initializeMeasureAssetsTimeout = () => {
setTimeout(measureAssetsOnTransaction, 1000);
};