open-telemetry / opentelemetry-js-contrib

OpenTelemetry instrumentation for JavaScript modules
https://opentelemetry.io
Apache License 2.0
670 stars 494 forks source link

coordinate first pass at ESM support for all instrumentations #1942

Open trentm opened 7 months ago

trentm commented 7 months ago

This is a meta issue to coordinate doing a first pass at ECMAScript module (ESM) support and a test for every node.js instrumentation. (This is about working with the import-in-the-middle-based hook so is not relevant to browser instrumentations.) I'm not necessarily volunteering to do all these :), but I'll certainly help.

Hopefully this can also be useful for support/help requests for why instrumentation might not be working for ESM-using code. If the module/instrumentation in question is not checked off here, then ESM instrumentation is not expected to work (it might be luck).

dev notes

Run the following to show which (contrib-repo) instrumentations have an ESM-related test. This shows usage of the required experimental-loader hook to support hooking ESM modules.

rg hook.mjs -g '*.test.*'

Guide on adding ESM support and an ESM test to an instrumentation

TODO(trentm)

trentm commented 7 months ago

notes on fastify ESM support

Fastify ESM support is slightly limited. The current import style in "opentelemetry-js-contrib/plugins/node/opentelemetry-instrumentation-fastify/test/fixtures/use-fastify.mjs" works, but this fails:

...
import * as mod from 'fastify';
console.log('mod: ', mod);
const app = mod.fastify();
...

Here mod.default is instrumented, but mod.fastify is not. This is probably an exceedingly rare way to import and use fastify. Still, this is why I haven't checked fastify above.

tobiasmuehl commented 7 months ago

According to this post, the following already work, based likely on luck:

Your checklist only supports binary display - either fully working or not working at all. How about adding emojis to the list to signal the current implementation status? States could be:

tobiasmuehl commented 7 months ago

🎉 I found a working solution here! My project is using esm and I switched my runtime from node to jiti and NOW EVERYTHING JUST WORKS!!!!!! I'm using http, express and pg instrumentations. Works on node18 and node20

If we are living in a simulation then this is a complete cheat code 🥷🏼

trentm commented 6 months ago

Your checklist only supports binary display ...

Heh. I don't object to more info being included in each bullet point. I'd like to keep the checkboxes. To me "checked" will mean "we've done a first pass on ESM instrumentation for this module". When everything is checked, we can close this issue. There could be separate follow-up issues for particular limitations.