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

ref(utils): Clean up timestamp calculation code #10069

Merged
merged 3 commits into from
Jan 5, 2024

Conversation

AbhiPrasad
Copy link
Member

ref #10046

This PR removes the usage of dynamicRequire from packages/utils/src/time.ts and cleans up our timestamp code to be simpler and reduce bundle size. Removing dynamicRequire means that we no longer rely on perf_hooks for the performance API, but instead try to grab it from globalThis.performance. performance was added to the global in Node 16, which means we'll fallback to using Date.now() in Node 8, 10, 12, 14 (and in v8 just Node 14). I think that is an acceptable tradeoff, we just reduce accuracy for those versions.

This does not refactor browserPerformanceTimeOrigin code at the bottom of the file, I want to come back and touch that in a follow up PR to reduce the amount of changes made here.

I would appreciate reviews/opinions on this, I'm not 100% confident in the changes.

@AbhiPrasad AbhiPrasad requested review from timfish, anonrig, a team and stephanie-anderson and removed request for a team January 4, 2024 22:14
@@ -148,7 +90,7 @@ export const browserPerformanceTimeOrigin = ((): number | undefined => {
// performance.timing.navigationStart, which results in poor results in performance data. We only treat time origin
// data as reliable if they are within a reasonable threshold of the current time.

const { performance } = WINDOW;
const { performance } = GLOBAL_OBJ as typeof GLOBAL_OBJ & Window;
if (!performance || !performance.now) {
_browserPerformanceTimeOriginMode = 'none';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally what we do with browserPerformanceTimeOrigin is to make this timeOrigin calculation more generic for browser/node, I generally think it uses a good heuristic to determine timeOrigin reliability.

Copy link
Contributor

github-actions bot commented Jan 4, 2024

size-limit report 📦

Path Size
@sentry/browser (incl. Tracing, Replay, Feedback) - Webpack (gzipped) 76.15 KB (-0.25% 🔽)
@sentry/browser (incl. Tracing, Replay) - Webpack (gzipped) 67.54 KB (-0.29% 🔽)
@sentry/browser (incl. Tracing, Replay) - Webpack with treeshaking flags (gzipped) 61.15 KB (-0.31% 🔽)
@sentry/browser (incl. Tracing) - Webpack (gzipped) 31.79 KB (-1.82% 🔽)
@sentry/browser (incl. Feedback) - Webpack (gzipped) 30.5 KB (-0.67% 🔽)
@sentry/browser - Webpack (gzipped) 21.87 KB (-2.57% 🔽)
@sentry/browser (incl. Tracing, Replay, Feedback) - ES6 CDN Bundle (gzipped) 73.77 KB (-0.07% 🔽)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (gzipped) 65.42 KB (-0.05% 🔽)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (gzipped) 31.6 KB (-0.08% 🔽)
@sentry/browser - ES6 CDN Bundle (gzipped) 23.52 KB (-0.23% 🔽)
@sentry/browser (incl. Tracing, Replay) - ES6 CDN Bundle (minified & uncompressed) 205.13 KB (-0.04% 🔽)
@sentry/browser (incl. Tracing) - ES6 CDN Bundle (minified & uncompressed) 95.02 KB (-0.08% 🔽)
@sentry/browser - ES6 CDN Bundle (minified & uncompressed) 69.9 KB (-0.11% 🔽)
@sentry/browser (incl. Tracing) - ES5 CDN Bundle (gzipped) 34.53 KB (-0.1% 🔽)
@sentry/react (incl. Tracing, Replay) - Webpack (gzipped) 67.91 KB (-0.32% 🔽)
@sentry/react - Webpack (gzipped) 21.91 KB (-2.64% 🔽)
@sentry/nextjs Client (incl. Tracing, Replay) - Webpack (gzipped) 84.64 KB (-0.25% 🔽)
@sentry/nextjs Client - Webpack (gzipped) 48.97 KB (-0.97% 🔽)
@sentry-internal/feedback - Webpack (gzipped) 16.53 KB (-1.17% 🔽)

@AbhiPrasad AbhiPrasad self-assigned this Jan 4, 2024
packages/utils/src/time.ts Outdated Show resolved Hide resolved
@@ -148,7 +90,7 @@ export const browserPerformanceTimeOrigin = ((): number | undefined => {
// performance.timing.navigationStart, which results in poor results in performance data. We only treat time origin
// data as reliable if they are within a reasonable threshold of the current time.

const { performance } = WINDOW;
const { performance } = GLOBAL_OBJ as typeof GLOBAL_OBJ & Window;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we make this a new type instead of this type cast?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine to keep esp because it's only being used here.

packages/utils/src/time.ts Outdated Show resolved Hide resolved
Copy link
Member

@mydea mydea left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@AbhiPrasad AbhiPrasad merged commit 3fc7916 into develop Jan 5, 2024
95 checks passed
@AbhiPrasad AbhiPrasad deleted the abhi-refactor-time-utils branch January 5, 2024 23:12
c298lee pushed a commit that referenced this pull request Jan 9, 2024
This PR removes the usage of `dynamicRequire` from
`packages/utils/src/time.ts` and cleans up our timestamp code to be
simpler and reduce bundle size. Removing `dynamicRequire` means that we
no longer rely on `perf_hooks` for the `performance` API, but instead
try to grab it from `globalThis.performance`. `performance` was added to
the global in Node 16, which means we'll fallback to using `Date.now()`
in Node 8, 10, 12, 14 (and in v8 just Node 14). I think that is an
acceptable tradeoff, we just reduce accuracy for those versions.

This does not refactor `browserPerformanceTimeOrigin` code at the bottom
of the file, I want to come back and touch that in a follow up PR to
reduce the amount of changes made here.

I would appreciate reviews/opinions on this, I'm not 100% confident in
the changes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants