-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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: Add loader file to node-based SDKs to support ESM monkeypatching #11338
Conversation
I am having a problem where this loader simply doesn't work with our SDK. I am doing the following but no spans are created for incoming requests (in fact otel doesn't seem to path the http module): // test.mjs
import * as Sentry from '@sentry/node';
Sentry.init({
dsn: 'https://7cea2b6e298f4fcc86bb28d22ceaeac4@o447951.ingest.sentry.io/4505391490007040',
tracesSampleRate: 1,
debug: true,
});
import('node:http').then(http => {
const PORT = 3000;
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.end('<html>response</html>');
});
server.listen(PORT, () => {
console.log(`Server is running on http://localhost:${PORT}`);
});
});
If I do
it crashes with
|
Bundle ReportChanges will decrease total bundle size by 381.72kB ⬇️
|
Talked with @timfish (🙏). So apparently, All of the above is outlined and tracked here: open-telemetry/opentelemetry-js#4547 (comment) |
dev-packages/e2e-tests/test-applications/esm-loader-node-express-app/event-proxy-server.ts
Dismissed
Show dismissed
Hide dismissed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
--loader
is deprecated and marked for removal at some point in the future so we should be targeting --import
.
https://nodejs.org/api/cli.html#--experimental-loadermodule
This flag is discouraged and may be removed in a future version of Node.js. Please use --import with register() instead.
The only difference is that the code in the register export should pass the loader to register
. Then it can be used in the same way but wth --import
:
import { register } from 'node:module';
register('../build/loader-hook.mjs', import.meta.url););
Docs are here:
https://nodejs.org/api/module.html#enabling
It looks like we need
I don't fully understand yet why, but I assume it doesn't know how to resolve relative imports without a base url. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
We might also want to export the loader hook too, to support the < Node v18 case?
Fair point, it would need to be in a separate hook because simply importing |
Yeah, there would need to be another export. Maybe |
I changed it so we have two files. Users would have to do the following:
|
An important todo for us here is gonna be documenting this properly when import-in-the-middle and |
Perhaps we could rename hook to loader and register to import? I forgot these names in the middle of writing this comment. 😅 Or is there a naming convention I'm not aware of? |
@wojtekmaj That's a very fair suggestion. Otel and import-in-the-middle call the hook "hook", so that's why we named it like that. It's like a semi-convention. But I see the appeal of naming them |
https://github.com/tapjs/tsimp uses what I proposed and I love I never need to dig through the docs to configure it properly :) |
Ok that looks actually pretty clean. Let me change this :D |
@wojtekmaj What do you think about |
Makes sense to me! I'd go for a more descriptive version if you think it's gonna be used as one-time thing you put in your |
Ref: #10046
Adds a loader script that can be used like
node --loader=@sentry/[sdk-of-your-choice] your-script.js
to allow for monkey-patching modules even in ESM.Note
Currently, this is broken for upstream reasons. See #11338 (comment)