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

feat(node): Register ESM patching hooks in init for supported Node.js versions #11933

Merged
merged 9 commits into from
May 8, 2024

Conversation

lforst
Copy link
Member

@lforst lforst commented May 7, 2024

To avoid needing to have two --require flags when running a Node process (one for the ESM loader hooks, one for Sentry.init()), we can use Sentry.init() itself to register the ESM patching hooks.

We

  • Register the necessary ESM patching hooks with register when we are on a compatible Node.js version
  • Log a warning to use the loader flag on older versions if the the hook wasn't registered yet

// Register hook was added in v20.6.0 and v18.19.0
if (nodeMajor > 22 || (nodeMajor === 20 && nodeMinor >= 6) || (nodeMajor === 18 && nodeMinor >= 19)) {
// @ts-expect-error register is available in these versions & import.meta is allowed
moduleModule.register('@opentelemetry/instrumentation/hook.mjs', import.meta.url);
Copy link
Member

Choose a reason for hiding this comment

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

just to be sure, does it matter if this is registered twice?

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, I tested this and registering twice is a problem. We get around this for ourselves by only registering once and tracking with a global whether we have already registered.

I can see this becoming a problem if people have their own hooks registered already. I currently don't see a straight forward way of detecting this so for now we will just have to document this behaviour.

We should keep an eye open though for automatic detection.

Copy link
Member

@Lms24 Lms24 left a comment

Choose a reason for hiding this comment

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

Interesting, LGTM!

Copy link
Contributor

github-actions bot commented May 8, 2024

size-limit report 📦

Path Size
@sentry/browser 21.65 KB (0%)
@sentry/browser (incl. Tracing) 32.68 KB (0%)
@sentry/browser (incl. Tracing, Replay) 68.02 KB (0%)
@sentry/browser (incl. Tracing, Replay) - with treeshaking flags 61.42 KB (0%)
@sentry/browser (incl. Tracing, Replay with Canvas) 72.06 KB (0%)
@sentry/browser (incl. Tracing, Replay, Feedback) 84.15 KB (0%)
@sentry/browser (incl. Feedback) 37.66 KB (0%)
@sentry/browser (incl. sendFeedback) 26.21 KB (0%)
@sentry/browser (incl. FeedbackAsync) 30.84 KB (0%)
@sentry/react 24.33 KB (0%)
@sentry/react (incl. Tracing) 35.64 KB (0%)
@sentry/vue 25.48 KB (0%)
@sentry/vue (incl. Tracing) 34.47 KB (0%)
@sentry/svelte 21.78 KB (0%)
CDN Bundle 24.13 KB (0%)
CDN Bundle (incl. Tracing) 34.05 KB (0%)
CDN Bundle (incl. Tracing, Replay) 67.72 KB (0%)
CDN Bundle (incl. Tracing, Replay, Feedback) 72.93 KB (0%)
CDN Bundle - uncompressed 70.99 KB (0%)
CDN Bundle (incl. Tracing) - uncompressed 101 KB (0%)
CDN Bundle (incl. Tracing, Replay) - uncompressed 210.61 KB (0%)
CDN Bundle (incl. Tracing, Replay, Feedback) - uncompressed 223.89 KB (0%)
@sentry/nextjs (client) 34.87 KB (0%)
@sentry/sveltekit (client) 33.24 KB (0%)
@sentry/node 147.35 KB (+0.15% 🔺)

@lforst lforst enabled auto-merge (squash) May 8, 2024 12:59
@lforst lforst merged commit c3c2bd3 into develop May 8, 2024
98 checks passed
@lforst lforst deleted the lforst-esm-patching-in-init branch May 8, 2024 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants