|
1 | 1 | import { getGlobalObject } from './misc'; |
2 | 2 | import { dynamicRequire, isNodeEnv } from './node'; |
3 | 3 |
|
4 | | -const INITIAL_TIME = Date.now(); |
| 4 | +/** |
| 5 | + * An object that can return the current timestamp in seconds since the UNIX epoch. |
| 6 | + */ |
| 7 | +interface TimestampSource { |
| 8 | + nowSeconds(): number; |
| 9 | +} |
5 | 10 |
|
6 | 11 | /** |
7 | | - * Cross platform compatible partial performance implementation |
| 12 | + * A TimestampSource implementation for environments that do not support the Performance Web API natively. |
| 13 | + * |
| 14 | + * Note that this TimestampSource does not use a monotonic clock. A call to `nowSeconds` may return a timestamp earlier |
| 15 | + * than a previously returned value. We do not try to emulate a monotonic behavior in order to facilitate debugging. It |
| 16 | + * is more obvious to explain "why does my span have negative duration" than "why my spans have zero duration". |
8 | 17 | */ |
9 | | -interface CrossPlatformPerformance { |
10 | | - timeOrigin: number; |
| 18 | +const dateTimestampSource = ((): TimestampSource => ({ |
| 19 | + nowSeconds: () => Date.now() / 1000, |
| 20 | +}))(); |
| 21 | + |
| 22 | +/** |
| 23 | + * A partial definition of the [Performance Web API]{@link https://developer.mozilla.org/en-US/docs/Web/API/Performance} |
| 24 | + * for accessing a high resolution monotonic clock. |
| 25 | + */ |
| 26 | +interface Performance { |
11 | 27 | /** |
12 | | - * Returns the current timestamp in ms |
| 28 | + * Returns the current millisecond timestamp, where 0 represents the start of measurement. |
13 | 29 | */ |
14 | 30 | now(): number; |
| 31 | + /** |
| 32 | + * The millisecond timestamp at which measurement began, measured in Unix time. |
| 33 | + */ |
| 34 | + timeOrigin: number; |
15 | 35 | } |
16 | 36 |
|
17 | | -let prevNow = 0; |
| 37 | +/** |
| 38 | + * Returns a wrapper around the native Performance API browser implementation, or undefined for browsers that do not |
| 39 | + * support the API. |
| 40 | + * |
| 41 | + * Wrapping the native API works around differences in behavior from different browsers. |
| 42 | + */ |
| 43 | +function getBrowserPerformance(): Performance | undefined { |
| 44 | + const { performance } = getGlobalObject<Window>(); |
| 45 | + if (!performance || !performance.now) { |
| 46 | + return undefined; |
| 47 | + } |
| 48 | + |
| 49 | + // Replace performance.timeOrigin with our own timeOrigin based on Date.now(). |
| 50 | + // |
| 51 | + // This is a partial workaround for browsers reporting performance.timeOrigin such that performance.timeOrigin + |
| 52 | + // performance.now() gives a date arbitrarily in the past. |
| 53 | + // |
| 54 | + // Additionally, computing timeOrigin in this way fills the gap for browsers where performance.timeOrigin is |
| 55 | + // undefined. |
| 56 | + // |
| 57 | + // The assumption that performance.timeOrigin + performance.now() ~= Date.now() is flawed, but we depend on it to |
| 58 | + // interact with data coming out of performance entries. |
| 59 | + // |
| 60 | + // Note that despite recommendations against it in the spec, browsers implement the Performance API with a clock that |
| 61 | + // might stop when the computer is asleep (and perhaps under other circumstances). Such behavior causes |
| 62 | + // performance.timeOrigin + performance.now() to have an arbitrary skew over Date.now(). In laptop computers, we have |
| 63 | + // observed skews that can be as long as days, weeks or months. |
| 64 | + // |
| 65 | + // See https://github.com/getsentry/sentry-javascript/issues/2590. |
| 66 | + // |
| 67 | + // BUG: despite our best intentions, this workaround has its limitations. It mostly addresses timings of pageload |
| 68 | + // transactions, but ignores the skew built up over time that can aversely affect timestamps of navigation |
| 69 | + // transactions of long-lived web pages. |
| 70 | + const timeOrigin = Date.now() - performance.now(); |
18 | 71 |
|
19 | | -const performanceFallback: CrossPlatformPerformance = { |
20 | | - now(): number { |
21 | | - let now = Date.now() - INITIAL_TIME; |
22 | | - if (now < prevNow) { |
23 | | - now = prevNow; |
24 | | - } |
25 | | - prevNow = now; |
26 | | - return now; |
27 | | - }, |
28 | | - timeOrigin: INITIAL_TIME, |
29 | | -}; |
| 72 | + return { |
| 73 | + now: () => performance.now(), |
| 74 | + timeOrigin, |
| 75 | + }; |
| 76 | +} |
30 | 77 |
|
31 | | -const crossPlatformPerformance: CrossPlatformPerformance = ((): CrossPlatformPerformance => { |
32 | | - // React Native's performance.now() starts with a gigantic offset, so we need to wrap it. |
33 | | - if (isReactNative()) { |
34 | | - return getReactNativePerformanceWrapper(); |
| 78 | +/** |
| 79 | + * Returns the native Performance API implementation from Node.js. Returns undefined in old Node.js versions that don't |
| 80 | + * implement the API. |
| 81 | + */ |
| 82 | +function getNodePerformance(): Performance | undefined { |
| 83 | + try { |
| 84 | + const perfHooks = dynamicRequire(module, 'perf_hooks') as { performance: Performance }; |
| 85 | + return perfHooks.performance; |
| 86 | + } catch (_) { |
| 87 | + return undefined; |
35 | 88 | } |
| 89 | +} |
36 | 90 |
|
| 91 | +/** |
| 92 | + * The Performance API implementation for the current platform, if available. |
| 93 | + */ |
| 94 | +const platformPerformance = ((): Performance | undefined => { |
37 | 95 | if (isNodeEnv()) { |
38 | | - try { |
39 | | - const perfHooks = dynamicRequire(module, 'perf_hooks') as { performance: CrossPlatformPerformance }; |
40 | | - return perfHooks.performance; |
41 | | - } catch (_) { |
42 | | - return performanceFallback; |
43 | | - } |
| 96 | + return getNodePerformance(); |
44 | 97 | } |
| 98 | + return getBrowserPerformance(); |
| 99 | +})(); |
45 | 100 |
|
46 | | - const { performance } = getGlobalObject<Window>(); |
| 101 | +let timestampSource = dateTimestampSource; |
47 | 102 |
|
48 | | - if (!performance || !performance.now) { |
49 | | - return performanceFallback; |
50 | | - } |
51 | | - |
52 | | - // Polyfill for performance.timeOrigin. |
53 | | - // |
54 | | - // While performance.timing.navigationStart is deprecated in favor of performance.timeOrigin, performance.timeOrigin |
55 | | - // is not as widely supported. Namely, performance.timeOrigin is undefined in Safari as of writing. |
56 | | - if (performance.timeOrigin === undefined) { |
57 | | - // As of writing, performance.timing is not available in Web Workers in mainstream browsers, so it is not always a |
58 | | - // valid fallback. In the absence of a initial time provided by the browser, fallback to INITIAL_TIME. |
59 | | - // @ts-ignore ignored because timeOrigin is a readonly property but we want to override |
60 | | - // eslint-disable-next-line deprecation/deprecation |
61 | | - performance.timeOrigin = (performance.timing && performance.timing.navigationStart) || INITIAL_TIME; |
62 | | - } |
| 103 | +if (platformPerformance !== undefined) { |
| 104 | + timestampSource = { |
| 105 | + nowSeconds: () => (platformPerformance.timeOrigin + platformPerformance.now()) / 1000, |
| 106 | + }; |
| 107 | +} |
63 | 108 |
|
64 | | - return performance; |
65 | | -})(); |
| 109 | +/** |
| 110 | + * Returns a timestamp in seconds since the UNIX epoch using the Date API. |
| 111 | + */ |
| 112 | +export const { nowSeconds: dateTimestampInSeconds } = dateTimestampSource; |
66 | 113 |
|
67 | 114 | /** |
68 | | - * Returns a timestamp in seconds with milliseconds precision since the UNIX epoch calculated with the monotonic clock. |
| 115 | + * Returns a timestamp in seconds since the UNIX epoch using either the Performance or Date APIs, depending on the |
| 116 | + * availability of the Performance API. |
| 117 | + * |
| 118 | + * See `usingPerformanceAPI` to test whether the Performance API is used. |
| 119 | + * |
| 120 | + * BUG: Note that because of how browsers implement the Performance API, the clock might stop when the computer is |
| 121 | + * asleep. This creates a skew between `dateTimestampInSeconds` and `timestampInSeconds`. The |
| 122 | + * skew can grow to arbitrary amounts like days, weeks or months. |
| 123 | + * See https://github.com/getsentry/sentry-javascript/issues/2590. |
69 | 124 | */ |
70 | | -export function timestampWithMs(): number { |
71 | | - return (crossPlatformPerformance.timeOrigin + crossPlatformPerformance.now()) / 1000; |
72 | | -} |
| 125 | +export const { nowSeconds: timestampInSeconds } = timestampSource; |
| 126 | + |
| 127 | +// Re-exported with an old name for backwards-compatibility. |
| 128 | +export const { nowSeconds: timestampWithMs } = timestampSource; |
73 | 129 |
|
74 | 130 | /** |
75 | | - * Determines if running in react native |
| 131 | + * A boolean that is true when timestampInSeconds uses the Performance API to produce monotonic timestamps. |
76 | 132 | */ |
77 | | -function isReactNative(): boolean { |
78 | | - return getGlobalObject<Window>().navigator?.product === 'ReactNative'; |
79 | | -} |
| 133 | +export const usingPerformanceAPI = platformPerformance !== undefined; |
80 | 134 |
|
81 | 135 | /** |
82 | | - * Performance wrapper for react native as performance.now() has been found to start off with an unusual offset. |
| 136 | + * The number of milliseconds since the UNIX epoch. This value is only usable in a browser, and only when the |
| 137 | + * performance API is available. |
83 | 138 | */ |
84 | | -function getReactNativePerformanceWrapper(): CrossPlatformPerformance { |
85 | | - // Performance only available >= RN 0.63 |
| 139 | +export const browserPerformanceTimeOrigin = ((): number | undefined => { |
86 | 140 | const { performance } = getGlobalObject<Window>(); |
87 | | - if (performance && typeof performance.now === 'function') { |
88 | | - const INITIAL_OFFSET = performance.now(); |
89 | | - |
90 | | - return { |
91 | | - now(): number { |
92 | | - return performance.now() - INITIAL_OFFSET; |
93 | | - }, |
94 | | - timeOrigin: INITIAL_TIME, |
95 | | - }; |
| 141 | + if (!performance) { |
| 142 | + return undefined; |
96 | 143 | } |
97 | | - return performanceFallback; |
98 | | -} |
| 144 | + if (performance.timeOrigin) { |
| 145 | + return performance.timeOrigin; |
| 146 | + } |
| 147 | + // While performance.timing.navigationStart is deprecated in favor of performance.timeOrigin, performance.timeOrigin |
| 148 | + // is not as widely supported. Namely, performance.timeOrigin is undefined in Safari as of writing. |
| 149 | + // Also as of writing, performance.timing is not available in Web Workers in mainstream browsers, so it is not always |
| 150 | + // a valid fallback. In the absence of an initial time provided by the browser, fallback to the current time from the |
| 151 | + // Date API. |
| 152 | + // eslint-disable-next-line deprecation/deprecation |
| 153 | + return (performance.timing && performance.timing.navigationStart) || Date.now(); |
| 154 | +})(); |
0 commit comments