Skip to content

fix(web-vitals): Fix TTFB capture in Safari #3106

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 3 commits into from
Dec 8, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
8 changes: 3 additions & 5 deletions packages/tracing/src/browser/metrics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export class MetricsInstrumentation {
if (transaction.op === 'pageload') {
// normalize applicable web vital values to be relative to transaction.startTimestamp

const timeOrigin = msToSec(performance.timeOrigin);
const timeOrigin = msToSec(browserPerformanceTimeOrigin);

['fcp', 'fp', 'lcp', 'ttfb'].forEach(name => {
if (!this._measurements[name] || timeOrigin >= transaction.startTimestamp) {
Expand All @@ -150,12 +150,10 @@ export class MetricsInstrumentation {
const oldValue = this._measurements[name].value;
const measurementTimestamp = timeOrigin + msToSec(oldValue);
// normalizedValue should be in milliseconds
const normalizedValue = (measurementTimestamp - transaction.startTimestamp) * 1000;
const normalizedValue = Math.abs((measurementTimestamp - transaction.startTimestamp) * 1000);

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

this._measurements[name].value = normalizedValue;
});
Expand Down
64 changes: 5 additions & 59 deletions packages/tracing/src/browser/web-vitals/getTTFB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,58 +17,10 @@
import { getGlobalObject } from '@sentry/utils';

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

const global = getGlobalObject<Window>();

interface NavigationEntryShim {
// From `PerformanceNavigationTimingEntry`.
entryType: string;
startTime: number;

// From `performance.timing`.
connectEnd?: number;
connectStart?: number;
domComplete?: number;
domContentLoadedEventEnd?: number;
domContentLoadedEventStart?: number;
domInteractive?: number;
domainLookupEnd?: number;
domainLookupStart?: number;
fetchStart?: number;
loadEventEnd?: number;
loadEventStart?: number;
redirectEnd?: number;
redirectStart?: number;
requestStart?: number;
responseEnd?: number;
responseStart?: number;
secureConnectionStart?: number;
unloadEventEnd?: number;
unloadEventStart?: number;
}

type PerformanceTimingKeys =
| 'connectEnd'
| 'connectStart'
| 'domComplete'
| 'domContentLoadedEventEnd'
| 'domContentLoadedEventStart'
| 'domInteractive'
| 'domainLookupEnd'
| 'domainLookupStart'
| 'fetchStart'
| 'loadEventEnd'
| 'loadEventStart'
| 'redirectEnd'
| 'redirectStart'
| 'requestStart'
| 'responseEnd'
| 'responseStart'
| 'secureConnectionStart'
| 'unloadEventEnd'
| 'unloadEventStart';

const afterLoad = (callback: () => void): void => {
if (document.readyState === 'complete') {
// Queue a task so the callback runs after `loadEventEnd`.
Expand All @@ -79,27 +31,22 @@ const afterLoad = (callback: () => void): void => {
}
};

const getNavigationEntryFromPerformanceTiming = (): PerformanceNavigationTiming => {
const getNavigationEntryFromPerformanceTiming = (): NavigationTimingPolyfillEntry => {
// Really annoying that TypeScript errors when using `PerformanceTiming`.
// 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 = global.performance.timing;

const navigationEntry: NavigationEntryShim = {
const navigationEntry: { [key: string]: number | string } = {
entryType: 'navigation',
startTime: 0,
};

for (const key in timing) {
if (key !== 'navigationStart' && key !== 'toJSON') {
navigationEntry[key as PerformanceTimingKeys] = Math.max(
timing[key as PerformanceTimingKeys] - timing.navigationStart,
0,
);
navigationEntry[key] = Math.max((timing[key as keyof PerformanceTiming] as number) - timing.navigationStart, 0);
}
}
return navigationEntry as PerformanceNavigationTiming;
return navigationEntry as NavigationTimingPolyfillEntry;
};

export const getTTFB = (onReport: ReportHandler): void => {
Expand All @@ -114,7 +61,6 @@ export const getTTFB = (onReport: ReportHandler): void => {
metric.value = metric.delta = (navigationEntry as PerformanceNavigationTiming).responseStart;

metric.entries = [navigationEntry];
metric.isFinal = true;

onReport(metric);
} catch (error) {
Expand Down
11 changes: 11 additions & 0 deletions packages/tracing/src/browser/web-vitals/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,14 @@ interface NetworkInformation extends EventTarget {
export interface NavigatorDeviceMemory {
readonly deviceMemory?: number;
}

export type NavigationTimingPolyfillEntry = Omit<
PerformanceNavigationTiming,
| 'initiatorType'
| 'nextHopProtocol'
| 'redirectCount'
| 'transferSize'
| 'encodedBodySize'
| 'decodedBodySize'
| 'toJSON'
>;