-
Notifications
You must be signed in to change notification settings - Fork 832
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(instrumentation): support ESM in node via import-in-the-middle #2763
Conversation
Codecov Report
@@ Coverage Diff @@
## main #2763 +/- ##
==========================================
+ Coverage 93.25% 93.40% +0.15%
==========================================
Files 152 164 +12
Lines 4904 5580 +676
Branches 1039 1176 +137
==========================================
+ Hits 4573 5212 +639
- Misses 331 368 +37
|
Oh, I see the problem, Typescript expects:
but the javascript defines it as
so TS complains if it's called as Oh, I see, but it's... sometimes a constructor! and sometimes an object, depending upon how it's called? hilarious. functionality is copied from https://github.com/elastic/require-in-the-middle/pull/12/files but I have no idea why they did it that way. |
I appreciate all of the work done to get to this point. Is this something that will be supported in the near term? |
It seems the ideal solution is to enable esModuleInterop in tsconfig. This would require some changes throughout the codebase for many imports, e.g. no longer doing It looks like the esModuleInterop setting was shot down with a concern about it breaking downstream customers unless they also enable this, though it seems the only customers that would be affected are those that export or reference types that come from non-esm friendly packages with d.ts files, that are not already using this setting. Either way, it seems likely that this setting will be needed at some point. It's a default setting when initializing tsconfig, and included as a recommended base config for most use cases. As it's set up today though, without enabling esModuleInterop, it looks like there is still a workaround. You can do something like PR 2846 that is aiming to achieve the same thing: // current import:
import ImportInTheMiddle, { HookFn } from 'import-in-the-middle';
// proposed change:
import { HookFn } from 'import-in-the-middle';
import * as ImportInTheMiddle from 'import-in-the-middle';
...
// current usage:
new ImportInTheMiddle([module.name], { internals: true }, <HookFn>hookFn);
// proposed change:
new (ImportInTheMiddle as unknown as typeof ImportInTheMiddle.default) ([module.name], { internals: true }, <HookFn>hookFn); Build output in // current, doesn't work
new import_in_the_middle_1.default([module.name], { internals: true }, hookFn);
// new, after change
new ImportInTheMiddle([module.name], { internals: true }, hookFn); |
experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts
Outdated
Show resolved
Hide resolved
experimental/packages/opentelemetry-instrumentation/src/platform/node/instrumentation.ts
Outdated
Show resolved
Hide resolved
Co-authored-by: Jamie Danielson <jamieedanielson@gmail.com>
Thanks for the help, @JamieDanielson. @vmarchaud, given tests are now passing over here, shall we proceed with officially doing review over here? |
I don't know about other but i think we need to have a test in place to be sure that works (thats something i've looked into for my PR over the last couple weekends) |
Yeah this is exactly what happened.
Very interesting, I hadn't realized that. In any case, ESM support is something we will definitely need at some point. I agree with @vmarchaud that we need some testing to ensure that we're useable both by ESM and by require users. I'm not an ESM export and @vmarchaud has been doing much more work than I have on that front so I'll let him continue to lead on this topic. |
This PR is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 14 days. |
Completed in #3698, huge thanks to everyone involved 🙂 |
Revised version of #2640 for review now that types/tests pass. Originally authored by @bengl.
Which problem is this PR solving?
Node.js ESM support.
Short description of the changes
Adds import-in-the-middle alongside require-in-the-middle. Also avoids hanging on to the hooks, because they're unnecessary (not used in disable).
Type of change
How Has This Been Tested?
The existing tests pass. The part of code that uses require-in-the-middle does not appear to be tested by the test files in the package.
Checklist: