fix(otlp-exporter-base): use dynamic import instead of require in esm/esnext build#5220
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #5220 +/- ##
==========================================
- Coverage 94.60% 94.59% -0.02%
==========================================
Files 315 315
Lines 8011 8011
Branches 1617 1617
==========================================
- Hits 7579 7578 -1
- Misses 432 433 +1
|
raphael-theriault-swi
left a comment
There was a problem hiding this comment.
In my experience importing a core module before hooking doesn't actually cause any issues and it gets instrumented fine, we use fs before anything else to load a config file and it's never been a problem.
|
Closing this as there's no way I can make this work with the current tests.
@raphael-theriault-swi - I like that thought. Getting rid of this lazy loading would be amazing. It'd be interesting to have a sample app that shows that it works without lazy-loading and If we'd have such an example that shows that it does not impact the instrumentation at all, I'd gladly throw this code. Having to lazy load happens to be a major pain to everyone involved, and also blocks features such as an HTTP agent override (where importing from the HTTP module is required), |
Which problem is this PR solving?
When bundling with
rollup, the compiled files from the ESM build are used. When the bundle is run,requireis not defined because it's ESM - also the file it tries to require is not there since it's a bundle now.This is the case because we have a workaround in the
HttpExporterTransportto lazy load a file that requireshttpuntil we actually export. We need this workaround to ensure that the@opentelemetry/instrumentation-httppackage can properly wrap everything - I'm not sure exactly how much of that is actually needed so I'll follow up with some more investigation after we get this fix in.This PR changes the
require()to anawait import(), which compiles torequire()wrapped in aPromise.resolve()for CJSimport()for esm/esnextThese types of problems are actually prime candidates to test when we start working on
While this PR does not add any additional tests, it will unblock bundling with
rollupfor now. We should, however, get started on #4744 eventually to have this tested in an automated manner.Type of change
How Has This Been Tested?