Skip to content

feat!: better ESM instrumentation; bump min-supported node to ^18.19.0 || >=20.6.0#584

Merged
trentm merged 6 commits into
mainfrom
trentm/better-esm-support-part1
Feb 4, 2025
Merged

feat!: better ESM instrumentation; bump min-supported node to ^18.19.0 || >=20.6.0#584
trentm merged 6 commits into
mainfrom
trentm/better-esm-support-part1

Conversation

@trentm
Copy link
Copy Markdown
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
Copy Markdown
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

node: '>=16.20.1',
},
// verbose: true,
verbose: true,
Copy link
Copy Markdown
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.

Comment thread packages/opentelemetry-node/docs/supported-technologies.md
Comment thread packages/opentelemetry-node/docs/supported-technologies.md Outdated
@trentm trentm merged commit 77b6656 into main Feb 4, 2025
@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