1
1
/* eslint-disable max-lines */
2
2
/* eslint-disable @typescript-eslint/no-explicit-any */
3
- import { SpanContext } from '@sentry/types' ;
3
+ import { Measurements , SpanContext } from '@sentry/types' ;
4
4
import { browserPerformanceTimeOrigin , getGlobalObject , htmlTreeAsString , isNodeEnv , logger } from '@sentry/utils' ;
5
5
6
6
import { IS_DEBUG_BUILD } from '../flags' ;
@@ -17,7 +17,7 @@ const global = getGlobalObject<Window>();
17
17
18
18
/** Class tracking metrics */
19
19
export class MetricsInstrumentation {
20
- private _measurements : Record < string , { value : number } > = { } ;
20
+ private _measurements : Measurements = { } ;
21
21
22
22
private _performanceCursor : number = 0 ;
23
23
private _lcpEntry : LargestContentfulPaint | undefined ;
@@ -79,14 +79,14 @@ export class MetricsInstrumentation {
79
79
80
80
if ( entry . name === 'first-paint' && shouldRecord ) {
81
81
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' } ;
84
84
}
85
85
86
86
if ( entry . name === 'first-contentful-paint' && shouldRecord ) {
87
87
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' } ;
90
90
}
91
91
92
92
break ;
@@ -115,12 +115,18 @@ export class MetricsInstrumentation {
115
115
// start of the response in milliseconds
116
116
if ( typeof responseStartTimestamp === 'number' ) {
117
117
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
+ } ;
119
122
120
123
if ( typeof requestStartTimestamp === 'number' && requestStartTimestamp <= responseStartTimestamp ) {
121
124
// Capture the time spent making the request and receiving the first byte of the response.
122
125
// 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
+ } ;
124
130
}
125
131
}
126
132
@@ -163,7 +169,11 @@ export class MetricsInstrumentation {
163
169
}
164
170
165
171
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
+ ) ;
167
177
} ) ;
168
178
169
179
tagMetricInfo ( transaction , this . _lcpEntry , this . _clsEntry ) ;
@@ -192,11 +202,11 @@ export class MetricsInstrumentation {
192
202
}
193
203
194
204
if ( isMeasurementValue ( connection . rtt ) ) {
195
- this . _measurements [ 'connection.rtt' ] = { value : connection . rtt } ;
205
+ this . _measurements [ 'connection.rtt' ] = { value : connection . rtt , unit : 'millisecond' } ;
196
206
}
197
207
198
208
if ( isMeasurementValue ( connection . downlink ) ) {
199
- this . _measurements [ 'connection.downlink' ] = { value : connection . downlink } ;
209
+ this . _measurements [ 'connection.downlink' ] = { value : connection . downlink , unit : 'megabit' } ;
200
210
}
201
211
}
202
212
@@ -221,7 +231,7 @@ export class MetricsInstrumentation {
221
231
}
222
232
223
233
IS_DEBUG_BUILD && logger . log ( '[Measurements] Adding CLS' ) ;
224
- this . _measurements [ 'cls' ] = { value : metric . value } ;
234
+ this . _measurements [ 'cls' ] = { value : metric . value , unit : 'millisecond' } ;
225
235
this . _clsEntry = entry as LayoutShift ;
226
236
} ) ;
227
237
}
@@ -237,8 +247,8 @@ export class MetricsInstrumentation {
237
247
const timeOrigin = msToSec ( browserPerformanceTimeOrigin as number ) ;
238
248
const startTime = msToSec ( entry . startTime ) ;
239
249
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' } ;
242
252
this . _lcpEntry = entry as LargestContentfulPaint ;
243
253
} , this . _reportAllChanges ) ;
244
254
}
@@ -254,8 +264,8 @@ export class MetricsInstrumentation {
254
264
const timeOrigin = msToSec ( browserPerformanceTimeOrigin as number ) ;
255
265
const startTime = msToSec ( entry . startTime ) ;
256
266
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' } ;
259
269
} ) ;
260
270
}
261
271
}
0 commit comments