@@ -9,7 +9,6 @@ import { msToSec } from '../utils';
9
9
import { getCLS } from './web-vitals/getCLS' ;
10
10
import { getFID } from './web-vitals/getFID' ;
11
11
import { getLCP , LargestContentfulPaint } from './web-vitals/getLCP' ;
12
- import { getTTFB } from './web-vitals/getTTFB' ;
13
12
import { getFirstHidden } from './web-vitals/lib/getFirstHidden' ;
14
13
import { NavigatorDeviceMemory , NavigatorNetworkInformation } from './web-vitals/types' ;
15
14
@@ -31,7 +30,6 @@ export class MetricsInstrumentation {
31
30
this . _trackCLS ( ) ;
32
31
this . _trackLCP ( ) ;
33
32
this . _trackFID ( ) ;
34
- this . _trackTTFB ( ) ;
35
33
}
36
34
}
37
35
@@ -62,6 +60,8 @@ export class MetricsInstrumentation {
62
60
63
61
let entryScriptStartTimestamp : number | undefined ;
64
62
let tracingInitMarkStartTime : number | undefined ;
63
+ let responseStartTimestamp : number | undefined ;
64
+ let requestStartTimestamp : number | undefined ;
65
65
66
66
global . performance
67
67
. getEntries ( )
@@ -75,9 +75,12 @@ export class MetricsInstrumentation {
75
75
}
76
76
77
77
switch ( entry . entryType ) {
78
- case 'navigation' :
78
+ case 'navigation' : {
79
79
addNavigationSpans ( transaction , entry , timeOrigin ) ;
80
+ responseStartTimestamp = timeOrigin + msToSec ( entry . responseStart as number ) ;
81
+ requestStartTimestamp = timeOrigin + msToSec ( entry . requestStart as number ) ;
80
82
break ;
83
+ }
81
84
case 'mark' :
82
85
case 'paint' :
83
86
case 'measure' : {
@@ -139,7 +142,20 @@ export class MetricsInstrumentation {
139
142
140
143
const timeOrigin = msToSec ( browserPerformanceTimeOrigin ) ;
141
144
142
- [ 'fcp' , 'fp' , 'lcp' , 'ttfb' ] . forEach ( name => {
145
+ // Generate TTFB (Time to First Byte), which measured as the time between the beginning of the transaction and the
146
+ // start of the response in milliseconds
147
+ if ( typeof responseStartTimestamp === 'number' ) {
148
+ logger . log ( '[Measurements] Adding TTFB' ) ;
149
+ this . _measurements [ 'ttfb' ] = { value : ( responseStartTimestamp - transaction . startTimestamp ) * 1000 } ;
150
+
151
+ if ( typeof requestStartTimestamp === 'number' && requestStartTimestamp <= responseStartTimestamp ) {
152
+ // Capture the time spent making the request and receiving the first byte of the response.
153
+ // This is the time between the start of the request and the start of the response in milliseconds.
154
+ this . _measurements [ 'ttfb.requestTime' ] = { value : ( responseStartTimestamp - requestStartTimestamp ) * 1000 } ;
155
+ }
156
+ }
157
+
158
+ [ 'fcp' , 'fp' , 'lcp' ] . forEach ( name => {
143
159
if ( ! this . _measurements [ name ] || timeOrigin >= transaction . startTimestamp ) {
144
160
return ;
145
161
}
@@ -282,24 +298,6 @@ export class MetricsInstrumentation {
282
298
this . _measurements [ 'mark.fid' ] = { value : timeOrigin + startTime } ;
283
299
} ) ;
284
300
}
285
-
286
- /** Starts tracking the Time to First Byte on the current page. */
287
- private _trackTTFB ( ) : void {
288
- getTTFB ( metric => {
289
- const entry = metric . entries . pop ( ) ;
290
-
291
- if ( ! entry ) {
292
- return ;
293
- }
294
-
295
- logger . log ( '[Measurements] Adding TTFB' ) ;
296
- this . _measurements [ 'ttfb' ] = { value : metric . value } ;
297
-
298
- // Capture the time spent making the request and receiving the first byte of the response
299
- const requestTime = metric . value - ( ( metric . entries [ 0 ] ?? entry ) as PerformanceNavigationTiming ) . requestStart ;
300
- this . _measurements [ 'ttfb.requestTime' ] = { value : requestTime } ;
301
- } ) ;
302
- }
303
301
}
304
302
305
303
/** Instrument navigation entries */
0 commit comments