Skip to content

fix(web-vitals): Adjust some web vitals to be relative to fetchStart and some other improvements #3019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions packages/tracing/src/browser/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,43 @@ export class MetricsInstrumentation {

// Measurements are only available for pageload transactions
if (transaction.op === 'pageload') {
// normalize applicable web vital values to be relative to transaction.startTimestamp

const timeOrigin = msToSec(performance.timeOrigin);

['fcp', 'fp', 'lcp', 'ttfb'].forEach(name => {
if (!this._measurements[name] || timeOrigin >= transaction.startTimestamp) {
return;
}

// The web vitals, fcp, fp, lcp, and ttfb, all measure relative to timeOrigin.
// Unfortunately, timeOrigin is not captured within the transaction span data, so these web vitals will need
// to be adjusted to be relative to transaction.startTimestamp.

const oldValue = this._measurements[name].value;
const measurementTimestamp = timeOrigin + msToSec(oldValue);
// normalizedValue should be in milliseconds
const normalizedValue = (measurementTimestamp - transaction.startTimestamp) * 1000;

const delta = normalizedValue - oldValue;
logger.log(
`[Measurements] Normalized ${name} from ${this._measurements[name].value} to ${normalizedValue} (${delta})`,
);

this._measurements[name] = { value: normalizedValue };
});

if (this._measurements['mark.fid'] && this._measurements['fid']) {
// create span for FID

_startChild(transaction, {
description: 'first input delay',
endTimestamp: this._measurements['mark.fid'].value + msToSec(this._measurements['fid'].value),
op: 'web vitals',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be 'vitals' to match the more generic name we'll likely use in the future?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now we've not committed to using that term as a generic umbrella.

We've only used "web vitals" thus far.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the fact that the op is currently two words going to mean that when searching for it, you have to put it in quotes? (I would guess that the answer is yes.) If so, can we make this one word (even if it's webVitals or web.vitals or webvitals) so that users don't have to do that?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

web.vitals works 👍

startTimestamp: this._measurements['mark.fid'].value,
});
}

transaction.setMeasurements(this._measurements);
}
}
Expand Down Expand Up @@ -253,6 +290,7 @@ function addNavigationSpans(transaction: Transaction, entry: Record<string, any>
addPerformanceNavigationTiming(transaction, entry, 'loadEvent', timeOrigin);
addPerformanceNavigationTiming(transaction, entry, 'connect', timeOrigin);
addPerformanceNavigationTiming(transaction, entry, 'secureConnection', timeOrigin, 'connectEnd');
addPerformanceNavigationTiming(transaction, entry, 'fetch', timeOrigin, 'domainLookupStart');
addPerformanceNavigationTiming(transaction, entry, 'domainLookup', timeOrigin);
addRequest(transaction, entry, timeOrigin);
}
Expand Down
8 changes: 6 additions & 2 deletions packages/tracing/src/browser/web-vitals/getTTFB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
* limitations under the License.
*/

import { getGlobalObject } from '@sentry/utils';

import { initMetric } from './lib/initMetric';
import { ReportHandler } from './types';

const global = getGlobalObject<Window>();

interface NavigationEntryShim {
// From `PerformanceNavigationTimingEntry`.
entryType: string;
Expand Down Expand Up @@ -80,7 +84,7 @@ const getNavigationEntryFromPerformanceTiming = (): PerformanceNavigationTiming
// Note: browsers that do not support navigation entries will fall back to using performance.timing
// (with the timestamps converted from epoch time to DOMHighResTimeStamp).
// eslint-disable-next-line deprecation/deprecation
const timing = performance.timing;
const timing = global.performance.timing;

const navigationEntry: NavigationEntryShim = {
entryType: 'navigation',
Expand All @@ -105,7 +109,7 @@ export const getTTFB = (onReport: ReportHandler): void => {
try {
// Use the NavigationTiming L2 entry if available.
const navigationEntry =
performance.getEntriesByType('navigation')[0] || getNavigationEntryFromPerformanceTiming();
global.performance.getEntriesByType('navigation')[0] || getNavigationEntryFromPerformanceTiming();

metric.value = metric.delta = (navigationEntry as PerformanceNavigationTiming).responseStart;

Expand Down