/
metrics.ts
289 lines (252 loc) · 9.84 KB
/
metrics.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
import { SpanContext } from '@sentry/types';
import { getGlobalObject, logger } from '@sentry/utils';
import { Span } from '../span';
import { Transaction } from '../transaction';
import { msToSec } from './utils';
const global = getGlobalObject<Window>();
/** Class tracking metrics */
export class MetricsInstrumentation {
private _lcp: Record<string, any> = {};
private _performanceCursor: number = 0;
private _forceLCP = () => {
/* No-op, replaced later if LCP API is available. */
return;
};
/** Starts tracking the Largest Contentful Paint on the current page. */
private _trackLCP(): void {
// Based on reference implementation from https://web.dev/lcp/#measure-lcp-in-javascript.
// Use a try/catch instead of feature detecting `largest-contentful-paint`
// support, since some browsers throw when using the new `type` option.
// https://bugs.webkit.org/show_bug.cgi?id=209216
try {
// Keep track of whether (and when) the page was first hidden, see:
// https://github.com/w3c/page-visibility/issues/29
// NOTE: ideally this check would be performed in the document <head>
// to avoid cases where the visibility state changes before this code runs.
let firstHiddenTime = document.visibilityState === 'hidden' ? 0 : Infinity;
document.addEventListener(
'visibilitychange',
event => {
firstHiddenTime = Math.min(firstHiddenTime, event.timeStamp);
},
{ once: true },
);
const updateLCP = (entry: PerformanceEntry) => {
// Only include an LCP entry if the page wasn't hidden prior to
// the entry being dispatched. This typically happens when a page is
// loaded in a background tab.
if (entry.startTime < firstHiddenTime) {
// NOTE: the `startTime` value is a getter that returns the entry's
// `renderTime` value, if available, or its `loadTime` value otherwise.
// The `renderTime` value may not be available if the element is an image
// that's loaded cross-origin without the `Timing-Allow-Origin` header.
this._lcp = {
// @ts-ignore
...(entry.id && { elementId: entry.id }),
// @ts-ignore
...(entry.size && { elementSize: entry.size }),
value: entry.startTime,
};
}
};
// Create a PerformanceObserver that calls `updateLCP` for each entry.
const po = new PerformanceObserver(entryList => {
entryList.getEntries().forEach(updateLCP);
});
// Observe entries of type `largest-contentful-paint`, including buffered entries,
// i.e. entries that occurred before calling `observe()` below.
po.observe({
buffered: true,
// @ts-ignore
type: 'largest-contentful-paint',
});
this._forceLCP = () => {
po.takeRecords().forEach(updateLCP);
};
} catch (e) {
// Do nothing if the browser doesn't support this API.
}
}
public constructor() {
if (global && global.performance) {
if (global.performance.mark) {
global.performance.mark('sentry-tracing-init');
}
this._trackLCP();
}
}
/** Add performance related spans to a transaction */
public addPerformanceEntires(transaction: Transaction): void {
if (!global || !global.performance || !global.performance.getEntries) {
// Gatekeeper if performance API not available
return;
}
logger.log('[Tracing] Adding & adjusting spans using Performance API');
// TODO(fixme): depending on the 'op' directly is brittle.
if (transaction.op === 'pageload') {
// Force any pending records to be dispatched.
this._forceLCP();
if (this._lcp) {
// Set the last observed LCP score.
transaction.setData('_sentry_web_vitals', { LCP: this._lcp });
}
}
const timeOrigin = msToSec(performance.timeOrigin);
let entryScriptSrc: string | undefined;
if (global.document) {
// tslint:disable-next-line: prefer-for-of
for (let i = 0; i < document.scripts.length; i++) {
// We go through all scripts on the page and look for 'data-entry'
// We remember the name and measure the time between this script finished loading and
// our mark 'sentry-tracing-init'
if (document.scripts[i].dataset.entry === 'true') {
entryScriptSrc = document.scripts[i].src;
break;
}
}
}
let entryScriptStartTimestamp: number | undefined;
let tracingInitMarkStartTime: number | undefined;
global.performance
.getEntries()
.slice(this._performanceCursor)
.forEach((entry: Record<string, any>) => {
const startTime = msToSec(entry.startTime as number);
const duration = msToSec(entry.duration as number);
if (transaction.op === 'navigation' && timeOrigin + startTime < transaction.startTimestamp) {
return;
}
switch (entry.entryType) {
case 'navigation':
addNavigationSpans(transaction, entry, timeOrigin);
break;
case 'mark':
case 'paint':
case 'measure':
const startTimestamp = addMeasureSpans(transaction, entry, startTime, duration, timeOrigin);
if (tracingInitMarkStartTime === undefined && entry.name === 'sentry-tracing-init') {
tracingInitMarkStartTime = startTimestamp;
}
break;
case 'resource':
const resourceName = (entry.name as string).replace(window.location.origin, '');
const endTimestamp = addResourceSpans(transaction, entry, resourceName, startTime, duration, timeOrigin);
// We remember the entry script end time to calculate the difference to the first init mark
if (entryScriptStartTimestamp === undefined && (entryScriptSrc || '').indexOf(resourceName) > -1) {
entryScriptStartTimestamp = endTimestamp;
}
break;
default:
// Ignore other entry types.
}
});
if (entryScriptStartTimestamp !== undefined && tracingInitMarkStartTime !== undefined) {
_startChild(transaction, {
description: 'evaluation',
endTimestamp: tracingInitMarkStartTime,
op: 'script',
startTimestamp: entryScriptStartTimestamp,
});
}
this._performanceCursor = Math.max(performance.getEntries().length - 1, 0);
}
}
/** Instrument navigation entries */
function addNavigationSpans(transaction: Transaction, entry: Record<string, any>, timeOrigin: number): void {
addPerformanceNavigationTiming(transaction, entry, 'unloadEvent', timeOrigin);
addPerformanceNavigationTiming(transaction, entry, 'domContentLoadedEvent', timeOrigin);
addPerformanceNavigationTiming(transaction, entry, 'loadEvent', timeOrigin);
addPerformanceNavigationTiming(transaction, entry, 'connect', timeOrigin);
addPerformanceNavigationTiming(transaction, entry, 'domainLookup', timeOrigin);
addRequest(transaction, entry, timeOrigin);
}
/** Create measure related spans */
function addMeasureSpans(
transaction: Transaction,
entry: Record<string, any>,
startTime: number,
duration: number,
timeOrigin: number,
): number {
const measureStartTimestamp = timeOrigin + startTime;
const measureEndTimestamp = measureStartTimestamp + duration;
_startChild(transaction, {
description: entry.name as string,
endTimestamp: measureEndTimestamp,
op: entry.entryType as string,
startTimestamp: measureStartTimestamp,
});
return measureStartTimestamp;
}
/** Create resource related spans */
function addResourceSpans(
transaction: Transaction,
entry: Record<string, any>,
resourceName: string,
startTime: number,
duration: number,
timeOrigin: number,
): number | undefined {
// we already instrument based on fetch and xhr, so we don't need to
// duplicate spans here.
if (entry.initiatorType === 'xmlhttprequest' || entry.initiatorType === 'fetch') {
return undefined;
}
const startTimestamp = timeOrigin + startTime;
const endTimestamp = startTimestamp + duration;
_startChild(transaction, {
description: `${entry.initiatorType} ${resourceName}`,
endTimestamp,
op: 'resource',
startTimestamp,
});
return endTimestamp;
}
/** Create performance navigation related spans */
function addPerformanceNavigationTiming(
transaction: Transaction,
entry: Record<string, any>,
event: string,
timeOrigin: number,
): void {
const end = entry[`${event}End`] as number | undefined;
const start = entry[`${event}Start`] as number | undefined;
if (!start || !end) {
return;
}
_startChild(transaction, {
description: event,
endTimestamp: timeOrigin + msToSec(end),
op: 'browser',
startTimestamp: timeOrigin + msToSec(start),
});
}
/** Create request and response related spans */
function addRequest(transaction: Transaction, entry: Record<string, any>, timeOrigin: number): void {
_startChild(transaction, {
description: 'request',
endTimestamp: timeOrigin + msToSec(entry.responseEnd as number),
op: 'browser',
startTimestamp: timeOrigin + msToSec(entry.requestStart as number),
});
_startChild(transaction, {
description: 'response',
endTimestamp: timeOrigin + msToSec(entry.responseEnd as number),
op: 'browser',
startTimestamp: timeOrigin + msToSec(entry.responseStart as number),
});
}
/**
* Helper function to start child on transactions. This function will make sure that the transaction will
* use the start timestamp of the created child span if it is earlier than the transactions actual
* start timestamp.
*/
export function _startChild(transaction: Transaction, { startTimestamp, ...ctx }: SpanContext): Span {
if (startTimestamp && transaction.startTimestamp > startTimestamp) {
transaction.startTimestamp = startTimestamp;
}
return transaction.startChild({
startTimestamp,
...ctx,
});
}