Skip to content

Commit 677602f

Browse files
authored
fix(browser): Use consistent timestamps (#12063)
In some places we used `Date.now()`, instead of `timestampInSeconds()` which used performance.now(). There are still other places where we use `Date.now()`, esp. in replay and node, but I focused on regular browser stuff for now (because this is most likely to be affected by this).
1 parent d634385 commit 677602f

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

packages/browser-utils/src/instrument/xhr.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { HandlerDataXhr, SentryWrappedXMLHttpRequest, WrappedFunction } from '@sentry/types';
22

3-
import { addHandler, fill, isString, maybeInstrument, triggerHandlers } from '@sentry/utils';
3+
import { addHandler, fill, isString, maybeInstrument, timestampInSeconds, triggerHandlers } from '@sentry/utils';
44
import { WINDOW } from '../metrics/types';
55

66
export const SENTRY_XHR_DATA_KEY = '__sentry_xhr_v3__';
@@ -31,7 +31,7 @@ export function instrumentXHR(): void {
3131

3232
fill(xhrproto, 'open', function (originalOpen: () => void): () => void {
3333
return function (this: XMLHttpRequest & SentryWrappedXMLHttpRequest, ...args: unknown[]): void {
34-
const startTimestamp = Date.now();
34+
const startTimestamp = timestampInSeconds() * 1000;
3535

3636
// open() should always be called with two or more arguments
3737
// But to be on the safe side, we actually validate this and bail out if we don't have a method & url
@@ -71,7 +71,7 @@ export function instrumentXHR(): void {
7171
}
7272

7373
const handlerData: HandlerDataXhr = {
74-
endTimestamp: Date.now(),
74+
endTimestamp: timestampInSeconds() * 1000,
7575
startTimestamp,
7676
xhr: this,
7777
};
@@ -124,7 +124,7 @@ export function instrumentXHR(): void {
124124
}
125125

126126
const handlerData: HandlerDataXhr = {
127-
startTimestamp: Date.now(),
127+
startTimestamp: timestampInSeconds() * 1000,
128128
xhr: this,
129129
};
130130
triggerHandlers('xhr', handlerData);

packages/browser/src/profiling/utils.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,14 @@ import type {
1212
StackParser,
1313
ThreadCpuProfile,
1414
} from '@sentry/types';
15-
import { GLOBAL_OBJ, browserPerformanceTimeOrigin, forEachEnvelopeItem, logger, uuid4 } from '@sentry/utils';
15+
import {
16+
GLOBAL_OBJ,
17+
browserPerformanceTimeOrigin,
18+
forEachEnvelopeItem,
19+
logger,
20+
timestampInSeconds,
21+
uuid4,
22+
} from '@sentry/utils';
1623

1724
import { DEBUG_BUILD } from '../debug-build';
1825
import { WINDOW } from '../helpers';
@@ -152,8 +159,8 @@ export function createProfilePayload(
152159
? start_timestamp
153160
: typeof event.start_timestamp === 'number'
154161
? event.start_timestamp * 1000
155-
: Date.now();
156-
const transactionEndMs = typeof event.timestamp === 'number' ? event.timestamp * 1000 : Date.now();
162+
: timestampInSeconds() * 1000;
163+
const transactionEndMs = typeof event.timestamp === 'number' ? event.timestamp * 1000 : timestampInSeconds() * 1000;
157164

158165
const profile: Profile = {
159166
event_id: profile_id,

packages/core/src/integrations/sessiontiming.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
import type { IntegrationFn } from '@sentry/types';
2+
import { timestampInSeconds } from '@sentry/utils';
23
import { defineIntegration } from '../integration';
34

45
const INTEGRATION_NAME = 'SessionTiming';
56

67
const _sessionTimingIntegration = (() => {
7-
const startTime = Date.now();
8+
const startTime = timestampInSeconds() * 1000;
89

910
return {
1011
name: INTEGRATION_NAME,
1112
processEvent(event) {
12-
const now = Date.now();
13+
const now = timestampInSeconds() * 1000;
1314

1415
return {
1516
...event,

packages/utils/src/instrument/fetch.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import type { HandlerDataFetch } from '@sentry/types';
33

44
import { fill } from '../object';
55
import { supportsNativeFetch } from '../supports';
6+
import { timestampInSeconds } from '../time';
67
import { GLOBAL_OBJ } from '../worldwide';
78
import { addHandler, maybeInstrument, triggerHandlers } from './handlers';
89

@@ -37,7 +38,7 @@ function instrumentFetch(): void {
3738
method,
3839
url,
3940
},
40-
startTimestamp: Date.now(),
41+
startTimestamp: timestampInSeconds() * 1000,
4142
};
4243

4344
triggerHandlers('fetch', {
@@ -49,7 +50,7 @@ function instrumentFetch(): void {
4950
(response: Response) => {
5051
const finishedHandlerData: HandlerDataFetch = {
5152
...handlerData,
52-
endTimestamp: Date.now(),
53+
endTimestamp: timestampInSeconds() * 1000,
5354
response,
5455
};
5556

@@ -59,7 +60,7 @@ function instrumentFetch(): void {
5960
(error: Error) => {
6061
const erroredHandlerData: HandlerDataFetch = {
6162
...handlerData,
62-
endTimestamp: Date.now(),
63+
endTimestamp: timestampInSeconds() * 1000,
6364
error,
6465
};
6566

0 commit comments

Comments
 (0)