-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathcreatePerformanceEntries.ts
270 lines (241 loc) · 7.36 KB
/
createPerformanceEntries.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
import { record } from '@sentry-internal/rrweb';
import { browserPerformanceTimeOrigin } from '@sentry/core';
import { WINDOW } from '../constants';
import type {
AllPerformanceEntry,
AllPerformanceEntryData,
ExperimentalPerformanceResourceTiming,
NavigationData,
PaintData,
ReplayContainer,
ReplayPerformanceEntry,
ResourceData,
WebVitalData,
} from '../types';
// Map entryType -> function to normalize data for event
const ENTRY_TYPES: Record<
string,
(entry: AllPerformanceEntry) => null | ReplayPerformanceEntry<AllPerformanceEntryData>
> = {
// @ts-expect-error TODO: entry type does not fit the create* functions entry type
resource: createResourceEntry,
paint: createPaintEntry,
// @ts-expect-error TODO: entry type does not fit the create* functions entry type
navigation: createNavigationEntry,
};
export interface Metric {
/**
* The current value of the metric.
*/
value: number;
/**
* The rating as to whether the metric value is within the "good",
* "needs improvement", or "poor" thresholds of the metric.
*/
rating: 'good' | 'needs-improvement' | 'poor';
/**
* Any performance entries relevant to the metric value calculation.
* The array may also be empty if the metric value was not based on any
* entries (e.g. a CLS value of 0 given no layout shifts).
*/
entries: PerformanceEntry[] | LayoutShift[];
}
interface LayoutShift extends PerformanceEntry {
value: number;
sources: LayoutShiftAttribution[];
hadRecentInput: boolean;
}
interface LayoutShiftAttribution {
node?: Node;
previousRect: DOMRectReadOnly;
currentRect: DOMRectReadOnly;
}
/**
* Handler creater for web vitals
*/
export function webVitalHandler(
getter: (metric: Metric) => ReplayPerformanceEntry<AllPerformanceEntryData>,
replay: ReplayContainer,
): (data: { metric: Metric }) => void {
return ({ metric }) => void replay.replayPerformanceEntries.push(getter(metric));
}
/**
* Create replay performance entries from the browser performance entries.
*/
export function createPerformanceEntries(
entries: AllPerformanceEntry[],
): ReplayPerformanceEntry<AllPerformanceEntryData>[] {
return entries.map(createPerformanceEntry).filter(Boolean) as ReplayPerformanceEntry<AllPerformanceEntryData>[];
}
function createPerformanceEntry(entry: AllPerformanceEntry): ReplayPerformanceEntry<AllPerformanceEntryData> | null {
const entryType = ENTRY_TYPES[entry.entryType];
if (!entryType) {
return null;
}
return entryType(entry);
}
function getAbsoluteTime(time: number): number {
// browserPerformanceTimeOrigin can be undefined if `performance` or
// `performance.now` doesn't exist, but this is already checked by this integration
return ((browserPerformanceTimeOrigin() || WINDOW.performance.timeOrigin) + time) / 1000;
}
function createPaintEntry(entry: PerformancePaintTiming): ReplayPerformanceEntry<PaintData> {
const { duration, entryType, name, startTime } = entry;
const start = getAbsoluteTime(startTime);
return {
type: entryType,
name,
start,
end: start + duration,
data: undefined,
};
}
function createNavigationEntry(entry: PerformanceNavigationTiming): ReplayPerformanceEntry<NavigationData> | null {
const {
entryType,
name,
decodedBodySize,
duration,
domComplete,
encodedBodySize,
domContentLoadedEventStart,
domContentLoadedEventEnd,
domInteractive,
loadEventStart,
loadEventEnd,
redirectCount,
startTime,
transferSize,
type,
} = entry;
// Ignore entries with no duration, they do not seem to be useful and cause dupes
if (duration === 0) {
return null;
}
return {
type: `${entryType}.${type}`,
start: getAbsoluteTime(startTime),
end: getAbsoluteTime(domComplete),
name,
data: {
size: transferSize,
decodedBodySize,
encodedBodySize,
duration,
domInteractive,
domContentLoadedEventStart,
domContentLoadedEventEnd,
loadEventStart,
loadEventEnd,
domComplete,
redirectCount,
},
};
}
function createResourceEntry(
entry: ExperimentalPerformanceResourceTiming,
): ReplayPerformanceEntry<ResourceData> | null {
const {
entryType,
initiatorType,
name,
responseEnd,
startTime,
decodedBodySize,
encodedBodySize,
responseStatus,
transferSize,
} = entry;
// Core SDK handles these
if (['fetch', 'xmlhttprequest'].includes(initiatorType)) {
return null;
}
return {
type: `${entryType}.${initiatorType}`,
start: getAbsoluteTime(startTime),
end: getAbsoluteTime(responseEnd),
name,
data: {
size: transferSize,
statusCode: responseStatus,
decodedBodySize,
encodedBodySize,
},
};
}
/**
* Add a LCP event to the replay based on a LCP metric.
*/
export function getLargestContentfulPaint(metric: Metric): ReplayPerformanceEntry<WebVitalData> {
const lastEntry = metric.entries[metric.entries.length - 1] as (PerformanceEntry & { element?: Node }) | undefined;
const node = lastEntry?.element ? [lastEntry.element] : undefined;
return getWebVital(metric, 'largest-contentful-paint', node);
}
function isLayoutShift(entry: PerformanceEntry): entry is LayoutShift {
return (entry as LayoutShift).sources !== undefined;
}
/**
* Add a CLS event to the replay based on a CLS metric.
*/
export function getCumulativeLayoutShift(metric: Metric): ReplayPerformanceEntry<WebVitalData> {
const layoutShifts: WebVitalData['attributions'] = [];
const nodes: Node[] = [];
for (const entry of metric.entries) {
if (isLayoutShift(entry)) {
const nodeIds = [];
for (const source of entry.sources) {
if (source.node) {
nodes.push(source.node);
const nodeId = record.mirror.getId(source.node);
if (nodeId) {
nodeIds.push(nodeId);
}
}
}
layoutShifts.push({ value: entry.value, nodeIds: nodeIds.length ? nodeIds : undefined });
}
}
return getWebVital(metric, 'cumulative-layout-shift', nodes, layoutShifts);
}
/**
* Add a FID event to the replay based on a FID metric.
*/
export function getFirstInputDelay(metric: Metric): ReplayPerformanceEntry<WebVitalData> {
const lastEntry = metric.entries[metric.entries.length - 1] as (PerformanceEntry & { target?: Node }) | undefined;
const node = lastEntry?.target ? [lastEntry.target] : undefined;
return getWebVital(metric, 'first-input-delay', node);
}
/**
* Add an INP event to the replay based on an INP metric.
*/
export function getInteractionToNextPaint(metric: Metric): ReplayPerformanceEntry<WebVitalData> {
const lastEntry = metric.entries[metric.entries.length - 1] as (PerformanceEntry & { target?: Node }) | undefined;
const node = lastEntry?.target ? [lastEntry.target] : undefined;
return getWebVital(metric, 'interaction-to-next-paint', node);
}
/**
* Add an web vital event to the replay based on the web vital metric.
*/
function getWebVital(
metric: Metric,
name: string,
nodes: Node[] | undefined,
attributions?: WebVitalData['attributions'],
): ReplayPerformanceEntry<WebVitalData> {
const value = metric.value;
const rating = metric.rating;
const end = getAbsoluteTime(value);
return {
type: 'web-vital',
name,
start: end,
end,
data: {
value,
size: value,
rating,
nodeIds: nodes ? nodes.map(node => record.mirror.getId(node)) : undefined,
attributions,
},
};
}