Skip to content

Commit

Permalink
πŸ› [RUMF-201] use timing.navigationStart to compute fake timings (#217)
Browse files Browse the repository at this point in the history
* πŸ› [RUMF-201] use timing.navigationStart to compute fake timings

Using `performance.timing.navigationStart` instead of
`performance.timeOrigin` has several advantages:

* it is defined on more browsers ;
* it produces timings closer to the real PerformanceNavigationTiming
event ;
* performance.timeOrigin may be far in the past for some reason, causing
abnormal timings.

* Revert "πŸ”ŠπŸ› [RUMF-201] add internal logs for abnormal timeOrigin (#219)"

This reverts commit 41e8bce.

* πŸ‘ŒπŸ“ add a link to the related Firefox issue
  • Loading branch information
BenoitZugmeyer authored Jan 6, 2020
1 parent c50f8aa commit 9f59b8e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
11 changes: 8 additions & 3 deletions packages/core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,13 @@ export function isPercentage(value: unknown) {
return typeof value === 'number' && value >= 0 && value <= 100
}

/**
* Get the time since the navigation was started.
*
* Note: this does not use `performance.timeOrigin` because it doesn't seem to reflect the actual
* time on which the navigation has started: it may be much farther in the past, at least in Firefox 71.
* Related issue in Firefox: https://bugzilla.mozilla.org/show_bug.cgi?id=1429926
*/
export function getRelativeTime(timestamp: number) {
// performance.timeOrigin is undefined in WebKit, see https://bugs.webkit.org/show_bug.cgi?id=174862
const timeOrigin = performance.timeOrigin !== undefined ? performance.timeOrigin : performance.timing.navigationStart
return timestamp - timeOrigin
return timestamp - performance.timing.navigationStart
}
18 changes: 1 addition & 17 deletions packages/rum/src/performanceCollection.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { addMonitoringMessage, getRelativeTime, monitor } from '@datadog/browser-core'
import { getRelativeTime, monitor } from '@datadog/browser-core'

import { LifeCycle, LifeCycleEventType } from './lifeCycle'
import { RumSession } from './rumSession'
import { viewContext } from './viewTracker'

interface BrowserWindow extends Window {
PerformanceObserver?: PerformanceObserver
Expand Down Expand Up @@ -52,23 +51,8 @@ interface FakePerformanceNavigationTiming {
loadEventEnd: number
}

function reportAbnormalTimeOrigin() {
if (getRelativeTime(performance.timing.loadEventEnd) > 86400e3 /* 1 day in ms */) {
addMonitoringMessage(
`Got an abnormal loadEventEnd timing
Session Id: ${viewContext.sessionId}
View Id: ${viewContext.id}
timeOrigin: ${performance.timeOrigin}
navigationStart: ${performance.timing.navigationStart}
loadEventEnd: ${performance.timing.loadEventEnd}
timing: ${getRelativeTime(performance.timing.loadEventEnd)}`
)
}
}

function retrieveNavigationTimingWhenLoaded(callback: (timing: PerformanceNavigationTiming) => void) {
function sendFakeTiming() {
reportAbnormalTimeOrigin()
const timing: FakePerformanceNavigationTiming = {
domComplete: getRelativeTime(performance.timing.domComplete),
domContentLoadedEventEnd: getRelativeTime(performance.timing.domContentLoadedEventEnd),
Expand Down

0 comments on commit 9f59b8e

Please sign in to comment.