Skip to content

Commit 72a09fe

Browse files
committed
ref(core): Avoid side-effect of & streamline timestampInSeconds method
1 parent 7a8840d commit 72a09fe

File tree

2 files changed

+11
-9
lines changed
  • dev-packages/browser-integration-tests/suites/tracing/browserTracingIntegration/linked-traces/consistent-sampling/meta-precedence
  • packages/core/src/utils

2 files changed

+11
-9
lines changed

dev-packages/browser-integration-tests/suites/tracing/browserTracingIntegration/linked-traces/consistent-sampling/meta-precedence/test.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,6 @@ sentryTest.describe('When `consistentTraceSampling` is `true` and page contains
2525
sentryTest.skip();
2626
}
2727

28-
29-
30-
3128
const url = await getLocalTestUrl({ testDir: __dirname });
3229

3330
const clientReportPromise = waitForClientReportRequest(page);

packages/core/src/utils/time.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@ export function dateTimestampInSeconds(): number {
3232
*/
3333
function createUnixTimestampInSecondsFunc(): () => number {
3434
const { performance } = GLOBAL_OBJ as typeof GLOBAL_OBJ & { performance?: Performance };
35-
if (!performance?.now) {
35+
// Some browser and environments don't have a performance or timeOrigin, so we fallback to
36+
// using Date.now() to compute the starting time.
37+
if (!performance?.now || !performance.timeOrigin) {
3638
return dateTimestampInSeconds;
3739
}
3840

39-
// Some browser and environments don't have a timeOrigin, so we fallback to
40-
// using Date.now() to compute the starting time.
41-
const approxStartingTimeOrigin = Date.now() - performance.now();
42-
const timeOrigin = performance.timeOrigin == undefined ? approxStartingTimeOrigin : performance.timeOrigin;
41+
const timeOrigin = performance.timeOrigin;
4342

4443
// performance.now() is a monotonic clock, which means it starts at 0 when the process begins. To get the current
4544
// wall clock time (actual UNIX timestamp), we need to add the starting time origin and the current time elapsed.
@@ -55,6 +54,8 @@ function createUnixTimestampInSecondsFunc(): () => number {
5554
};
5655
}
5756

57+
let _cachedTimestampInSeconds: (() => number) | undefined;
58+
5859
/**
5960
* Returns a timestamp in seconds since the UNIX epoch using either the Performance or Date APIs, depending on the
6061
* availability of the Performance API.
@@ -64,7 +65,11 @@ function createUnixTimestampInSecondsFunc(): () => number {
6465
* skew can grow to arbitrary amounts like days, weeks or months.
6566
* See https://github.com/getsentry/sentry-javascript/issues/2590.
6667
*/
67-
export const timestampInSeconds = createUnixTimestampInSecondsFunc();
68+
export function timestampInSeconds(): number {
69+
// We store this in a closure so that we don't have to create a new function every time this is called.
70+
const func = _cachedTimestampInSeconds ?? (_cachedTimestampInSeconds = createUnixTimestampInSecondsFunc());
71+
return func();
72+
}
6873

6974
/**
7075
* Cached result of getBrowserTimeOrigin.

0 commit comments

Comments
 (0)