Skip to content

ref(replay): Use Date.now() instead of new Date().getTime() #7492

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

Merged
merged 1 commit into from
Mar 17, 2023
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
2 changes: 1 addition & 1 deletion packages/browser/test/integration/polyfills/raf.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

if (!window.requestAnimationFrame)
window.requestAnimationFrame = function (callback, _element) {
var currTime = new Date().getTime();
var currTime = Date.now();
var timeToCall = Math.max(0, 16 - (currTime - lastTime));
var id = window.setTimeout(function () {
callback(currTime + timeToCall);
Expand Down
2 changes: 1 addition & 1 deletion packages/integrations/test/offline.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function prepopulateEvents(count: number = 1): void {
for (let i = 0; i < count; i++) {
events.push({
message: 'There was an error!',
timestamp: new Date().getTime(),
timestamp: Date.now(),
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/src/coreHandlers/handleHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ interface HistoryHandlerData {
function handleHistory(handlerData: HistoryHandlerData): ReplayPerformanceEntry {
const { from, to } = handlerData;

const now = new Date().getTime() / 1000;
const now = Date.now() / 1000;

return {
type: 'navigation.push',
Expand Down
12 changes: 6 additions & 6 deletions packages/replay/src/replay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class ReplayContainer implements ReplayContainerInterface {
/**
* Timestamp of the last user activity. This lives across sessions.
*/
private _lastActivity: number = new Date().getTime();
private _lastActivity: number = Date.now();

/**
* Is the integration currently active?
Expand Down Expand Up @@ -108,7 +108,7 @@ export class ReplayContainer implements ReplayContainerInterface {
traceIds: new Set(),
urls: [],
earliestEvent: null,
initialTimestamp: new Date().getTime(),
initialTimestamp: Date.now(),
initialUrl: '',
};

Expand Down Expand Up @@ -442,7 +442,7 @@ export class ReplayContainer implements ReplayContainerInterface {
this._clearContext();

this._context.initialUrl = url;
this._context.initialTimestamp = new Date().getTime();
this._context.initialTimestamp = Date.now();
this._context.urls.push(url);
}

Expand Down Expand Up @@ -634,14 +634,14 @@ export class ReplayContainer implements ReplayContainerInterface {
/**
* Update user activity (across session lifespans)
*/
private _updateUserActivity(_lastActivity: number = new Date().getTime()): void {
private _updateUserActivity(_lastActivity: number = Date.now()): void {
this._lastActivity = _lastActivity;
}

/**
* Updates the session's last activity timestamp
*/
private _updateSessionActivity(_lastActivity: number = new Date().getTime()): void {
private _updateSessionActivity(_lastActivity: number = Date.now()): void {
if (this.session) {
this.session.lastActivity = _lastActivity;
this._maybeSaveSession();
Expand Down Expand Up @@ -768,7 +768,7 @@ export class ReplayContainer implements ReplayContainerInterface {
eventContext,
session: this.session,
options: this.getOptions(),
timestamp: new Date().getTime(),
timestamp: Date.now(),
});
} catch (err) {
this._handleException(err);
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/src/session/Session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { isSampled } from '../util/isSampled';
* Get a session with defaults & applied sampling.
*/
export function makeSession(session: Partial<Session> & { sampled: Sampled }): Session {
const now = new Date().getTime();
const now = Date.now();
const id = session.id || uuid4();
// Note that this means we cannot set a started/lastActivity of `0`, but this should not be relevant outside of tests.
const started = session.started || now;
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/src/util/addEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export async function addEvent(
// page has been left open and idle for a long period of time and user
// comes back to trigger a new session. The performance entries rely on
// `performance.timeOrigin`, which is when the page first opened.
if (timestampInMs + replay.timeouts.sessionIdle < new Date().getTime()) {
if (timestampInMs + replay.timeouts.sessionIdle < Date.now()) {
return null;
}

Expand Down
2 changes: 1 addition & 1 deletion packages/replay/src/util/addMemoryEntry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function createMemoryEntry(memoryEntry: MemoryInfo): ReplayMemoryEntry {
const { jsHeapSizeLimit, totalJSHeapSize, usedJSHeapSize } = memoryEntry;
// we don't want to use `getAbsoluteTime` because it adds the event time to the
// time origin, so we get the current timestamp instead
const time = new Date().getTime() / 1000;
const time = Date.now() / 1000;
return {
type: 'memory',
name: 'memory',
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/src/util/createBreadcrumb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export function createBreadcrumb(
breadcrumb: Pick<Breadcrumb, RequiredProperties> & Partial<Omit<Breadcrumb, RequiredProperties>>,
): Breadcrumb {
return {
timestamp: new Date().getTime() / 1000,
timestamp: Date.now() / 1000,
type: 'default',
...breadcrumb,
};
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/test/fixtures/error.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Event } from '@sentry/types';

export function Error(obj?: Event): any {
const timestamp = new Date().getTime() / 1000;
const timestamp = Date.now() / 1000;

return {
exception: {
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/test/fixtures/transaction.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Event, SeverityLevel } from '@sentry/types';

export function Transaction(traceId?: string, obj?: Partial<Event>): any {
const timestamp = new Date().getTime() / 1000;
const timestamp = Date.now() / 1000;

return {
contexts: {
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/test/mocks/mockRrweb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type RecordMock = jest.MockedFunction<typeof rrwebRecord> & RecordAdditio
function createCheckoutPayload(isCheckout: boolean = true) {
return {
data: { isCheckout },
timestamp: new Date().getTime(),
timestamp: Date.now(),
type: isCheckout ? 2 : 3,
};
}
Expand Down
2 changes: 1 addition & 1 deletion packages/replay/test/unit/coreHandlers/handleScope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ describe('Unit | coreHandlers | handleScope', () => {
}

const testMsg = {
timestamp: new Date().getTime() / 1000,
timestamp: Date.now() / 1000,
message: 'testing',
category: 'console',
};
Expand Down
14 changes: 7 additions & 7 deletions packages/replay/test/unit/session/getSession.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const SAMPLE_RATES = {
errorSampleRate: 0,
};

function createMockSession(when: number = new Date().getTime()) {
function createMockSession(when: number = Date.now()) {
return makeSession({
id: 'test_session_id',
segmentId: 0,
Expand Down Expand Up @@ -66,7 +66,7 @@ describe('Unit | session | getSession', () => {
});

it('creates a non-sticky session, regardless of session existing in sessionStorage', function () {
saveSession(createMockSession(new Date().getTime() - 10000));
saveSession(createMockSession(Date.now() - 10000));

const { session } = getSession({
timeouts: {
Expand All @@ -93,8 +93,8 @@ describe('Unit | session | getSession', () => {
...SAMPLE_RATES,
currentSession: makeSession({
id: 'old_session_id',
lastActivity: new Date().getTime() - 1001,
started: new Date().getTime() - 1001,
lastActivity: Date.now() - 1001,
started: Date.now() - 1001,
segmentId: 0,
sampled: 'session',
}),
Expand Down Expand Up @@ -142,7 +142,7 @@ describe('Unit | session | getSession', () => {
});

it('fetches an existing sticky session', function () {
const now = new Date().getTime();
const now = Date.now();
saveSession(createMockSession(now));

const { session } = getSession({
Expand All @@ -168,8 +168,8 @@ describe('Unit | session | getSession', () => {
});

it('fetches an expired sticky session', function () {
const now = new Date().getTime();
saveSession(createMockSession(new Date().getTime() - 2000));
const now = Date.now();
saveSession(createMockSession(Date.now() - 2000));

const { session } = getSession({
timeouts: {
Expand Down
4 changes: 2 additions & 2 deletions packages/svelte/test/performance.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ describe('Sentry.trackComponent()', () => {
it('creates an update span, when the component is updated', async () => {
// Make the finish() function actually end the initSpan
testInitSpan.finish.mockImplementation(() => {
testInitSpan.endTimestamp = new Date().getTime();
testInitSpan.endTimestamp = Date.now();
});

// first we create the component
Expand Down Expand Up @@ -171,7 +171,7 @@ describe('Sentry.trackComponent()', () => {
it("doesn't record update spans, if there's no ongoing transaction at that time", async () => {
// Make the finish() function actually end the initSpan
testInitSpan.finish.mockImplementation(() => {
testInitSpan.endTimestamp = new Date().getTime();
testInitSpan.endTimestamp = Date.now();
});

// first we create the component
Expand Down