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!: better ESM instrumentation; bump min-supported node to ^18.19.0 || >=20.6.0 #584

Merged
merged 6 commits into from
Feb 4, 2025

Conversation

trentm
Copy link
Member

@trentm trentm commented Feb 4, 2025

This uses the IITM feature to only hook ES modules that will be patched by
instrumentations. This means instr-openai can possibly work with ESM usage of
openai -- before this it would crash from IITM hook changes.

This drops Node.js 14 and 16 support, in favour of the new base versions
that support module.register(). This is the same base version for coming
upstream OTel JS SDK 2.0. This allows us to simplify the ESM story: both
for docs and implementation.

Using --experimental-loader to somewhat support ESM for older node.js
versions is no longer supported. The main recommendation is to use
node --import @elastic/opentelemetry-node ...
I.e. to use --import rather than --require. Only with --import will
EDOT Node.js register the loader hook for ESM instrumentation.
Using --require is still fine for CommonJS-only instr.

Closes: #288


@david-luna This is a re-run at #499.
I've not added anything for handling multiple installations of IITM. I will follow-up with an issue to discuss options for that... and possibly a PR for that, though I think it isn't necessary for GA.

…0 || >=20.6.0

This uses the IITM feature to only hook ES modules that will be patched by
instrumentations. This means instr-openai can possibly work with ESM usage of
openai -- before this it would crash from IITM hook changes.

This drops Node.js 14 and 16 support, in favour of the new base versions
that support module.register(). This is the same base version for coming
upstream OTel JS SDK 2.0. This allows us to simplify the ESM story: both
for docs and implementation.

Using --experimental-loader to somewhat support ESM for older node.js
versions is no longer supported. The main recommendation is to use
    node --import @elastic/opentelemetry-node ...
I.e. to use --import rather than --require. Only with --import will
EDOT Node.js register the loader hook for ESM instrumentation.
Using --require is still fine for CommonJS-only instr.
@trentm trentm requested a review from david-luna February 4, 2025 04:22
@trentm trentm self-assigned this Feb 4, 2025
@trentm
Copy link
Member Author

trentm commented Feb 4, 2025

If it helps, this is what attempting to instrument ESM code using openai looked like before this PR:

$ cd examples/openai
$ node --env-file .env --import @elastic/opentelemetry-node chat.mjs
file:///Users/trentm/el/elastic-otel-node3/examples/openai/node_modules/openai/core.mjs:219
        const httpAgent = options.httpAgent ?? this.httpAgent ?? getDefaultAgent(url);
                                                                 ^

TypeError: getDefaultAgent is not a function
    at OpenAI.buildRequest (file:///Users/trentm/el/elastic-otel-node3/examples/openai/node_modules/openai/core.mjs:219:66)
    at OpenAI.makeRequest (file:///Users/trentm/el/elastic-otel-node3/examples/openai/node_modules/openai/core.mjs:298:44)

Node.js v20.18.1

@@ -43,7 +43,7 @@ const testFixtures = [
// Ref: https://github.com/mongodb/node-mongodb-native/blob/a8370367f7470962a834ddf36f9a6c62621d6345/package.json#L118
node: '>=16.20.1',
},
// verbose: true,
verbose: true,
Copy link
Member Author

Choose a reason for hiding this comment

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

Reviewer note: This could be excluded. I added this as a first step to trying to observe flaky test failures from this test case. I've seen it fail in local runs twice now.

@trentm trentm merged commit 77b6656 into main Feb 4, 2025
8 checks passed
@trentm trentm deleted the trentm/better-esm-support-part1 branch February 4, 2025 20:55
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.

use IITM wrap-only-hooked support for ESM
2 participants