Skip to content

Commit e5c7ef9

Browse files
authored
fix(web-vitals): Generate TTFB (Time to first byte) from span data (#3515)
1 parent 117bc99 commit e5c7ef9

File tree

2 files changed

+20
-92
lines changed

2 files changed

+20
-92
lines changed

packages/tracing/src/browser/metrics.ts

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { msToSec } from '../utils';
99
import { getCLS } from './web-vitals/getCLS';
1010
import { getFID } from './web-vitals/getFID';
1111
import { getLCP, LargestContentfulPaint } from './web-vitals/getLCP';
12-
import { getTTFB } from './web-vitals/getTTFB';
1312
import { getFirstHidden } from './web-vitals/lib/getFirstHidden';
1413
import { NavigatorDeviceMemory, NavigatorNetworkInformation } from './web-vitals/types';
1514

@@ -31,7 +30,6 @@ export class MetricsInstrumentation {
3130
this._trackCLS();
3231
this._trackLCP();
3332
this._trackFID();
34-
this._trackTTFB();
3533
}
3634
}
3735

@@ -62,6 +60,8 @@ export class MetricsInstrumentation {
6260

6361
let entryScriptStartTimestamp: number | undefined;
6462
let tracingInitMarkStartTime: number | undefined;
63+
let responseStartTimestamp: number | undefined;
64+
let requestStartTimestamp: number | undefined;
6565

6666
global.performance
6767
.getEntries()
@@ -75,9 +75,12 @@ export class MetricsInstrumentation {
7575
}
7676

7777
switch (entry.entryType) {
78-
case 'navigation':
78+
case 'navigation': {
7979
addNavigationSpans(transaction, entry, timeOrigin);
80+
responseStartTimestamp = timeOrigin + msToSec(entry.responseStart as number);
81+
requestStartTimestamp = timeOrigin + msToSec(entry.requestStart as number);
8082
break;
83+
}
8184
case 'mark':
8285
case 'paint':
8386
case 'measure': {
@@ -139,7 +142,20 @@ export class MetricsInstrumentation {
139142

140143
const timeOrigin = msToSec(browserPerformanceTimeOrigin);
141144

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 => {
143159
if (!this._measurements[name] || timeOrigin >= transaction.startTimestamp) {
144160
return;
145161
}
@@ -282,24 +298,6 @@ export class MetricsInstrumentation {
282298
this._measurements['mark.fid'] = { value: timeOrigin + startTime };
283299
});
284300
}
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-
}
303301
}
304302

305303
/** Instrument navigation entries */

packages/tracing/src/browser/web-vitals/getTTFB.ts

Lines changed: 0 additions & 70 deletions
This file was deleted.

0 commit comments

Comments
 (0)