-
Notifications
You must be signed in to change notification settings - Fork 518
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
fix(ioredis): fix instrumentation of ESM-imported ioredis #1694
fix(ioredis): fix instrumentation of ESM-imported ioredis #1694
Conversation
- With an ESM import the top-level object is a Module Namespace Object (https://tc39.es/ecma262/#sec-module-namespace-objects) that has no prototype. For compat, Node.js assigns the usual CommonJS module.exports to `<module>.default`, so use that when this is an ESM module. - Also, TypeScript translates class properties to assignments in the constructor after the super() call. Because super() can call init() and enable() synchronously, it calls back into 'IORedisInstrumentation' before 'traceSendCommand' was defined. Defining it as a method fixes that issue. Fixes: open-telemetry#1692
Note: I still have this in draft, because: 1. unit testsI haven't added any unit tests for this. Adding a unit test for ESM is a little bit of work. I'd appreciate thoughts/suggestions from others. Options:
|
I've resolved lint... by manually pointing to the eslint installed at the top level:
FWIW, just running
|
regarding the old eslint - this seems to be known issue since a while see here. |
Codecov Report
@@ Coverage Diff @@
## main #1694 +/- ##
=======================================
Coverage 91.69% 91.70%
=======================================
Files 139 139
Lines 7139 7134 -5
Branches 1436 1437 +1
=======================================
- Hits 6546 6542 -4
+ Misses 593 592 -1
|
Fixed. [email protected] is published. Now if I:
then |
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.
overall LGTM.
I never worked with ESM before, so would appreciate another review from someone with more knowledge (maybe @pichlermarc ?)
Is module[Symbol.toStringTag] === 'Module'
the canonical way to test for ESM vs CJS?
plugins/node/opentelemetry-instrumentation-ioredis/src/instrumentation.ts
Show resolved
Hide resolved
I'm not sure about it being canonical. I touched briefly on this in an "Aside" section of this long comment: #1692 (comment) By ECMAScript spec (https://tc39.es/ecma262/#sec-module-namespace-objects) the Module Namespace Object returned from an ESM import has a well-known symbol
That
It is spoofable:
I'm not sure if we need to worry about that kind of spoofing -- accidental or intentional. Another way to differentiate would be to pass in a (I'm not positive of the longevity of doing this. If I'm understanding recent changes in Node.js core (https://nodejs.org/en/blog/release/v20.6.0#new-nodemodule-api-register-for-module-customization-hooks-new-initialize-hook) we may eventually just have a single hook on a single Node.js core loader that handles loading both CommonJS and ESM.) |
The decision from the SIG meeting: we have come to the conclusion that this being spoofed does not need to be considered at this point in time. We also came to the conclusion that this seems like the best way to test for ESM vs CJS. |
🙂
Do you mean something like
I think this could work as a start.
That would also work, yes. I think the tests in @opentelemetry/instrumentation are completely separate ones so they don't test all the same things the CJS tests test do. But I think a simple test like this or the one from 2. would work to get this fix out, then we can think of how to do this in a broader scope in #1731 |
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.
…ests Running scripts out-of-process for testing allows running tests with different node options and with isolation. The former is useful for ESM testing. This change includes adding an ESM test for ioredis. This depends on open-telemetry#1694 to pass. Closes: open-telemetry#1731
The README example says the Hook callback `exported` arg is "effectively `import * as exported from ${url}`". https://tc39.es/ecma262/#sec-module-namespace-objects specs that a Module Namespace Object has a `@@toStringTag` property with value "Module" and no constructor. Fixes: #57 Obsoletes: #64 * * * This behaviour changed with the changes in #43 when the `register(...)`'d namespace changed from using an actual imported module object to using a plain object with module properties copied over to it: https://github.com/DataDog/import-in-the-middle/pull/43/files#diff-e69a24a4c3746fa1ee96a78e12bb12d2dd4eb6e4cacbced2bf1f4084952681d9L130-R208 I suspect that was an unintentional change. The main question here is **whether you would like import-in-the-middle to promise this**: that the `exported` namespace returned to the `Hook` callback mimics this aspect of `import * as exported from '...'`. As links to #57 show, this would help the OpenTelemetry JS project. I [started](open-telemetry/opentelemetry-js-contrib#1694 (comment)) using `exported[Symbol.toStringTag]` in OpenTelemetry instrumentations a while back as a way to handle differences in instrumentating a module based on whether it was being used from ESM code vs CommonJS code. This is convenient because **OpenTelemetry core instrumentation code uses the same hook function for require-in-the-middle and import-in-the-middle hooks**. It also seemed reasonable given the `Module Namespace Object` spec entry. However, I grant that the `exported` arg need not be a Module Namespace Object. * * * Assume you are willing to accept this, a note on my implementation: I chose to explicitly add the `@@toStringTag` property because: - it is more explicit - the `for (const k of Object.getOwnPropertySymbols(primary)) { ... }` alternative (proposed in #57 and #64) will only ever include the `@@toStringTag`. Assuming my read of the https://tc39.es/ecma262/#sec-module-namespace-objects and https://tc39.es/ecma262/#sec-module-namespace-exotic-objects sections is correct, the module object will only ever have *string* keys (for the "export"s), plus the one `@@toStringTag` property. - the `@@toStringTag` property should not be enumerable (i.e. `Object.getOwnPropertyDescriptor(exported, Symbol.toStringTag).enumerable === false`). The other implementation does not persist that descriptor value.
<module>.default
, so use that when this is an ESM module.Fixes: #1692
Checklist