Skip to content

Commit afcdaee

Browse files
committed
Add units to web vitals measurements
1 parent 14c62ff commit afcdaee

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

packages/tracing/src/browser/metrics.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/* eslint-disable max-lines */
22
/* eslint-disable @typescript-eslint/no-explicit-any */
3-
import { SpanContext } from '@sentry/types';
3+
import { Measurements, SpanContext } from '@sentry/types';
44
import { browserPerformanceTimeOrigin, getGlobalObject, htmlTreeAsString, isNodeEnv, logger } from '@sentry/utils';
55

66
import { IS_DEBUG_BUILD } from '../flags';
@@ -17,7 +17,7 @@ const global = getGlobalObject<Window>();
1717

1818
/** Class tracking metrics */
1919
export class MetricsInstrumentation {
20-
private _measurements: Record<string, { value: number }> = {};
20+
private _measurements: Measurements = {};
2121

2222
private _performanceCursor: number = 0;
2323
private _lcpEntry: LargestContentfulPaint | undefined;
@@ -79,14 +79,14 @@ export class MetricsInstrumentation {
7979

8080
if (entry.name === 'first-paint' && shouldRecord) {
8181
IS_DEBUG_BUILD && logger.log('[Measurements] Adding FP');
82-
this._measurements['fp'] = { value: entry.startTime };
83-
this._measurements['mark.fp'] = { value: startTimestamp };
82+
this._measurements['fp'] = { value: entry.startTime, unit: 'millisecond' };
83+
this._measurements['mark.fp'] = { value: startTimestamp, unit: 'second' };
8484
}
8585

8686
if (entry.name === 'first-contentful-paint' && shouldRecord) {
8787
IS_DEBUG_BUILD && logger.log('[Measurements] Adding FCP');
88-
this._measurements['fcp'] = { value: entry.startTime };
89-
this._measurements['mark.fcp'] = { value: startTimestamp };
88+
this._measurements['fcp'] = { value: entry.startTime, unit: 'millisecond' };
89+
this._measurements['mark.fcp'] = { value: startTimestamp, unit: 'second' };
9090
}
9191

9292
break;
@@ -115,12 +115,18 @@ export class MetricsInstrumentation {
115115
// start of the response in milliseconds
116116
if (typeof responseStartTimestamp === 'number') {
117117
IS_DEBUG_BUILD && logger.log('[Measurements] Adding TTFB');
118-
this._measurements['ttfb'] = { value: (responseStartTimestamp - transaction.startTimestamp) * 1000 };
118+
this._measurements['ttfb'] = {
119+
value: (responseStartTimestamp - transaction.startTimestamp) * 1000,
120+
unit: 'millisecond',
121+
};
119122

120123
if (typeof requestStartTimestamp === 'number' && requestStartTimestamp <= responseStartTimestamp) {
121124
// Capture the time spent making the request and receiving the first byte of the response.
122125
// This is the time between the start of the request and the start of the response in milliseconds.
123-
this._measurements['ttfb.requestTime'] = { value: (responseStartTimestamp - requestStartTimestamp) * 1000 };
126+
this._measurements['ttfb.requestTime'] = {
127+
value: (responseStartTimestamp - requestStartTimestamp) * 1000,
128+
unit: 'second',
129+
};
124130
}
125131
}
126132

@@ -163,7 +169,11 @@ export class MetricsInstrumentation {
163169
}
164170

165171
Object.keys(this._measurements).forEach(measurementName => {
166-
transaction.setMeasurement(measurementName, this._measurements[measurementName].value);
172+
transaction.setMeasurement(
173+
measurementName,
174+
this._measurements[measurementName].value,
175+
this._measurements[measurementName].unit,
176+
);
167177
});
168178

169179
tagMetricInfo(transaction, this._lcpEntry, this._clsEntry);
@@ -192,11 +202,11 @@ export class MetricsInstrumentation {
192202
}
193203

194204
if (isMeasurementValue(connection.rtt)) {
195-
this._measurements['connection.rtt'] = { value: connection.rtt };
205+
this._measurements['connection.rtt'] = { value: connection.rtt, unit: 'millisecond' };
196206
}
197207

198208
if (isMeasurementValue(connection.downlink)) {
199-
this._measurements['connection.downlink'] = { value: connection.downlink };
209+
this._measurements['connection.downlink'] = { value: connection.downlink, unit: 'megabit' };
200210
}
201211
}
202212

@@ -221,7 +231,7 @@ export class MetricsInstrumentation {
221231
}
222232

223233
IS_DEBUG_BUILD && logger.log('[Measurements] Adding CLS');
224-
this._measurements['cls'] = { value: metric.value };
234+
this._measurements['cls'] = { value: metric.value, unit: 'millisecond' };
225235
this._clsEntry = entry as LayoutShift;
226236
});
227237
}
@@ -237,8 +247,8 @@ export class MetricsInstrumentation {
237247
const timeOrigin = msToSec(browserPerformanceTimeOrigin as number);
238248
const startTime = msToSec(entry.startTime);
239249
IS_DEBUG_BUILD && logger.log('[Measurements] Adding LCP');
240-
this._measurements['lcp'] = { value: metric.value };
241-
this._measurements['mark.lcp'] = { value: timeOrigin + startTime };
250+
this._measurements['lcp'] = { value: metric.value, unit: 'millisecond' };
251+
this._measurements['mark.lcp'] = { value: timeOrigin + startTime, unit: 'second' };
242252
this._lcpEntry = entry as LargestContentfulPaint;
243253
}, this._reportAllChanges);
244254
}
@@ -254,8 +264,8 @@ export class MetricsInstrumentation {
254264
const timeOrigin = msToSec(browserPerformanceTimeOrigin as number);
255265
const startTime = msToSec(entry.startTime);
256266
IS_DEBUG_BUILD && logger.log('[Measurements] Adding FID');
257-
this._measurements['fid'] = { value: metric.value };
258-
this._measurements['mark.fid'] = { value: timeOrigin + startTime };
267+
this._measurements['fid'] = { value: metric.value, unit: 'millisecond' };
268+
this._measurements['mark.fid'] = { value: timeOrigin + startTime, unit: 'second' };
259269
});
260270
}
261271
}

0 commit comments

Comments
 (0)