Skip to content

Commit acceafb

Browse files
committed
fix: bad reverts
1 parent ff5d63f commit acceafb

File tree

4 files changed

+33
-38
lines changed

4 files changed

+33
-38
lines changed

packages/core/src/constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export const DEFAULT_ENVIRONMENT = 'production';
2+
export const DEV_ENVIRONMENT = 'development';

packages/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ export {
101101
headersToDict,
102102
httpHeadersToSpanAttributes,
103103
} from './utils/request';
104-
export { DEFAULT_ENVIRONMENT } from './constants';
104+
export { DEFAULT_ENVIRONMENT, DEV_ENVIRONMENT } from './constants';
105105
export { addBreadcrumb } from './breadcrumbs';
106106
export { functionToStringIntegration } from './integrations/functiontostring';
107107
// eslint-disable-next-line deprecation/deprecation

packages/core/src/utils/time.ts

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,14 @@ let cachedTimeOrigin: number | null | undefined = null;
7878

7979
/**
8080
* Gets the time origin and the mode used to determine it.
81+
*
82+
* Unfortunately browsers may report an inaccurate time origin data, through either performance.timeOrigin or
83+
* performance.timing.navigationStart, which results in poor results in performance data. We only treat time origin
84+
* data as reliable if they are within a reasonable threshold of the current time.
85+
*
8186
* TODO: move to `@sentry/browser-utils` package.
8287
*/
8388
function getBrowserTimeOrigin(): number | undefined {
84-
// Unfortunately browsers may report an inaccurate time origin data, through either performance.timeOrigin or
85-
// performance.timing.navigationStart, which results in poor results in performance data. We only treat time origin
86-
// data as reliable if they are within a reasonable threshold of the current time.
8789
const { performance } = GLOBAL_OBJ as typeof GLOBAL_OBJ & Window;
8890
if (!performance?.now) {
8991
return undefined;
@@ -93,11 +95,13 @@ function getBrowserTimeOrigin(): number | undefined {
9395
const performanceNow = performance.now();
9496
const dateNow = Date.now();
9597

96-
// if timeOrigin isn't available set delta to threshold so it isn't used
97-
const timeOriginDelta = performance.timeOrigin
98-
? Math.abs(performance.timeOrigin + performanceNow - dateNow)
99-
: threshold;
100-
const timeOriginIsReliable = timeOriginDelta < threshold;
98+
const timeOrigin = performance.timeOrigin;
99+
if (typeof timeOrigin === 'number') {
100+
const timeOriginDelta = Math.abs(timeOrigin + performanceNow - dateNow);
101+
if (timeOriginDelta < threshold) {
102+
return timeOrigin;
103+
}
104+
}
101105

102106
// TODO: Remove all code related to `performance.timing.navigationStart` once we drop support for Safari 14.
103107
// `performance.timeSince` is available in Safari 15.
@@ -110,23 +114,16 @@ function getBrowserTimeOrigin(): number | undefined {
110114
// Date API.
111115
// eslint-disable-next-line deprecation/deprecation
112116
const navigationStart = performance.timing?.navigationStart;
113-
const hasNavigationStart = typeof navigationStart === 'number';
114-
// if navigationStart isn't available set delta to threshold so it isn't used
115-
const navigationStartDelta = hasNavigationStart ? Math.abs(navigationStart + performanceNow - dateNow) : threshold;
116-
const navigationStartIsReliable = navigationStartDelta < threshold;
117-
118-
// TODO: Since timeOrigin explicitly replaces navigationStart, we should probably remove the navigationStartIsReliable check.
119-
if (timeOriginIsReliable && timeOriginDelta <= navigationStartDelta) {
120-
return performance.timeOrigin;
121-
}
122-
123-
if (navigationStartIsReliable) {
124-
return navigationStart;
117+
if (typeof navigationStart === 'number') {
118+
const navigationStartDelta = Math.abs(navigationStart + performanceNow - dateNow);
119+
if (navigationStartDelta < threshold) {
120+
return navigationStart;
121+
}
125122
}
126123

127-
// TODO: We should probably fall back to Date.now() - performance.now(), since this is still more accurate than just Date.now() (?)
128-
// Either both timeOrigin and navigationStart are skewed or neither is available, fallback to Date.
129-
return dateNow;
124+
// Either both timeOrigin and navigationStart are skewed or neither is available, fallback to subtracting
125+
// `performance.now()` from `Date.now()`.
126+
return dateNow - performanceNow;
130127
}
131128

132129
/**

packages/core/test/lib/utils/time.test.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ describe('browserPerformanceTimeOrigin', () => {
2727
vi.unstubAllGlobals();
2828
});
2929

30-
it('returns `Date.now()` if `performance.timeOrigin` is not reliable', async () => {
30+
it('returns `Date.now() - performance.now()` if `performance.timeOrigin` is not reliable', async () => {
3131
const currentTimeMs = 1767778040866;
3232

3333
const unreliableTime = currentTimeMs - RELIABLE_THRESHOLD_MS - 2_000;
3434

35-
const timeSincePageloadMs = 1_234.789;
35+
const timeSincePageloadMs = 1_234.56789;
3636

3737
vi.useFakeTimers();
3838
vi.setSystemTime(new Date(currentTimeMs));
@@ -46,39 +46,36 @@ describe('browserPerformanceTimeOrigin', () => {
4646
});
4747

4848
const timeOrigin = await getFreshPerformanceTimeOrigin();
49-
expect(timeOrigin).toBe(1767778040866);
49+
expect(timeOrigin).toBe(currentTimeMs - timeSincePageloadMs);
5050

5151
vi.useRealTimers();
5252
vi.unstubAllGlobals();
5353
});
5454

55-
it('returns `performance.timing.navigationStart` if `performance.timeOrigin` is not available', async () => {
56-
const currentTimeMs = 1767778040870;
57-
58-
const navigationStartMs = currentTimeMs - 2_000;
55+
it('returns `Date.now() - performance.now()` if neither `performance.timeOrigin` nor `performance.timing.navigationStart` are available', async () => {
56+
const currentTimeMs = 1767778040866;
5957

60-
const timeSincePageloadMs = 1_234.789;
58+
const timeSincePageloadMs = 1_234.56789;
6159

6260
vi.useFakeTimers();
6361
vi.setSystemTime(new Date(currentTimeMs));
64-
6562
vi.stubGlobal('performance', {
6663
timeOrigin: undefined,
6764
timing: {
68-
navigationStart: navigationStartMs,
65+
navigationStart: undefined,
6966
},
7067
now: () => timeSincePageloadMs,
7168
});
7269

7370
const timeOrigin = await getFreshPerformanceTimeOrigin();
74-
expect(timeOrigin).toBe(navigationStartMs);
71+
expect(timeOrigin).toBe(currentTimeMs - timeSincePageloadMs);
7572

7673
vi.useRealTimers();
7774
vi.unstubAllGlobals();
7875
});
7976

80-
it('returns `performance.timing.navigationStart` if `performance.timeOrigin` is less reliable', async () => {
81-
const currentTimeMs = 1767778040874;
77+
it('returns `performance.timing.navigationStart` if `performance.timeOrigin` is not available', async () => {
78+
const currentTimeMs = 1767778040870;
8279

8380
const navigationStartMs = currentTimeMs - 2_000;
8481

@@ -88,7 +85,7 @@ describe('browserPerformanceTimeOrigin', () => {
8885
vi.setSystemTime(new Date(currentTimeMs));
8986

9087
vi.stubGlobal('performance', {
91-
timeOrigin: navigationStartMs - 1,
88+
timeOrigin: undefined,
9289
timing: {
9390
navigationStart: navigationStartMs,
9491
},

0 commit comments

Comments
 (0)