Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(browser): Use consistent timestamps #12063

Merged
merged 1 commit into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions packages/browser-utils/src/instrument/xhr.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { HandlerDataXhr, SentryWrappedXMLHttpRequest, WrappedFunction } from '@sentry/types';

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

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

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

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

const handlerData: HandlerDataXhr = {
endTimestamp: Date.now(),
endTimestamp: timestampInSeconds() * 1000,
startTimestamp,
xhr: this,
};
Expand Down Expand Up @@ -124,7 +124,7 @@ export function instrumentXHR(): void {
}

const handlerData: HandlerDataXhr = {
startTimestamp: Date.now(),
startTimestamp: timestampInSeconds() * 1000,
xhr: this,
};
triggerHandlers('xhr', handlerData);
Expand Down
13 changes: 10 additions & 3 deletions packages/browser/src/profiling/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@ import type {
StackParser,
ThreadCpuProfile,
} from '@sentry/types';
import { GLOBAL_OBJ, browserPerformanceTimeOrigin, forEachEnvelopeItem, logger, uuid4 } from '@sentry/utils';
import {
GLOBAL_OBJ,
browserPerformanceTimeOrigin,
forEachEnvelopeItem,
logger,
timestampInSeconds,
uuid4,
} from '@sentry/utils';

import { DEBUG_BUILD } from '../debug-build';
import { WINDOW } from '../helpers';
Expand Down Expand Up @@ -152,8 +159,8 @@ export function createProfilePayload(
? start_timestamp
: typeof event.start_timestamp === 'number'
? event.start_timestamp * 1000
: Date.now();
const transactionEndMs = typeof event.timestamp === 'number' ? event.timestamp * 1000 : Date.now();
: timestampInSeconds() * 1000;
const transactionEndMs = typeof event.timestamp === 'number' ? event.timestamp * 1000 : timestampInSeconds() * 1000;

const profile: Profile = {
event_id: profile_id,
Expand Down
5 changes: 3 additions & 2 deletions packages/core/src/integrations/sessiontiming.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import type { IntegrationFn } from '@sentry/types';
import { timestampInSeconds } from '@sentry/utils';
import { defineIntegration } from '../integration';

const INTEGRATION_NAME = 'SessionTiming';

const _sessionTimingIntegration = (() => {
const startTime = Date.now();
const startTime = timestampInSeconds() * 1000;

return {
name: INTEGRATION_NAME,
processEvent(event) {
const now = Date.now();
const now = timestampInSeconds() * 1000;

return {
...event,
Expand Down
7 changes: 4 additions & 3 deletions packages/utils/src/instrument/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type { HandlerDataFetch } from '@sentry/types';

import { fill } from '../object';
import { supportsNativeFetch } from '../supports';
import { timestampInSeconds } from '../time';
import { GLOBAL_OBJ } from '../worldwide';
import { addHandler, maybeInstrument, triggerHandlers } from './handlers';

Expand Down Expand Up @@ -37,7 +38,7 @@ function instrumentFetch(): void {
method,
url,
},
startTimestamp: Date.now(),
startTimestamp: timestampInSeconds() * 1000,
};

triggerHandlers('fetch', {
Expand All @@ -49,7 +50,7 @@ function instrumentFetch(): void {
(response: Response) => {
const finishedHandlerData: HandlerDataFetch = {
...handlerData,
endTimestamp: Date.now(),
endTimestamp: timestampInSeconds() * 1000,
response,
};

Expand All @@ -59,7 +60,7 @@ function instrumentFetch(): void {
(error: Error) => {
const erroredHandlerData: HandlerDataFetch = {
...handlerData,
endTimestamp: Date.now(),
endTimestamp: timestampInSeconds() * 1000,
error,
};

Expand Down
Loading