Repository navigation
feat(instrumentations): rewrite ES modules loaded through require() - #11396
pabloerhard wants to merge 1 commit into
Conversation
…ith -r dd-trace/init
Overall package sizeSelf size: 9.56 MB Dependency sizes| name | version | self size | total size | |------|---------|-----------|------------| | import-in-the-middle | 3.5.3 | 131.25 kB | 531.9 kB | | opentracing | 0.14.7 | 194.81 kB | 194.81 kB | | dc-polyfill | 0.1.11 | 25.74 kB | 25.74 kB |🤖 This report was automatically generated by heaviest-objects-in-the-universe |
❌ ErrorsYour PR has failed checks. Please review the issues below and take necessary action before merging. 🚦 5 Pipeline jobs failed
ℹ️ InfoNo other issues found (see more)🧪 All tests passed 🎯 Code Coverage (details) Useful? React with 👍 / 👎 This comment will be updated automatically if new data arrives.🔗 Commit SHA: fd24816 | Docs | View more details | Give us feedback! |
Codecov Report✅ All modified and coverable lines are covered by tests.
Additional details and impacted files@@ Coverage Diff @@
## master #11396 +/- ##
===========================================
- Coverage 99.15% 81.36% -17.80%
===========================================
Files 1136 1114 -22
Lines 188287 181888 -6399
Branches 86 81 -5
===========================================
- Hits 186698 147991 -38707
- Misses 1589 33897 +32308
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
BenchmarksBenchmark execution time: 2026-11-24 00:00:39 Comparing candidate commit fd24816 in PR branch Found 0 performance improvements and 1 performance regressions! Performance is the same for 2469 metrics, 17 unstable metrics.
|
What does this PR do?
CommonJS apps started with only
-r dd-trace/initnow rewrite ES modules loaded throughrequire(), including the files those modules import, whichModule._compilenever sees.init()registers a smallmodule.registerHooksload hook that rewrites ES modules only. CommonJS stays with theModule._compileshim.register.jsorinitialize.mjsregisters dd-trace's loader, the loader takes over and the tracer's hook is deregistered. With sync hooks, the loader's combined hook runs the rewriter outside import-in-the-middle; on the async fallback, the sync hook is removed first, so sync and async rewriting never run side by side. If the loader registered first,init()adds nothing.Module._compileno longer rewrites an ES module that a sync hook already rewrote. Before, it was instrumented twice under--import dd-trace/register.js.Motivation
Rewrite targets that ship as ESM and get
require()d, such as postgres 4, were only partially instrumented without a loader flag.Why CommonJS rewrites stay in
Module._compileinstead of moving to sync hooks: when an async (module.register) loader is also in the chain, whether dd-trace's fallback or another vendor's like tsx or OpenTelemetry, Node.js hands sync hooks imported CommonJS withsource: undefinedand compiles the file later. A sync hook can't rewrite it.Module._compilealways sees the final source, so it stays the CommonJS path, and the sync hook only handles ESM.Why there is no async rewriter hook on releases without working sync hooks: registering one from
-r dd-trace/initstarts a loader thread that also runs-rpreloads, so it needs a guard to keep a second tracer out of that thread. Next to dd-trace's own async loader, it also needs both hooks to coordinate so files aren't rewritten twice. That complexity isn't worth it for older release lines; there,--import dd-trace/register.jsremains the way to get these rewrites.Additional Notes
-rpreloads run before--import, so without it the tracer's hook sits inside import-in-the-middle's, and import-in-the-middle's read of a module's exports triggers a full rewrite that is thrown away.isSyncLoaderHookVersionSupported()moved topackages/dd-trace/src/sync-loader-hooks.jssoloader.jscan share it. Without the check, the hook crashes on 22.15 (context.conditionsisn't an array) and next to another async loader before module: handle null source from async loader hooks in sync hooks nodejs/node#59929.module-hooks.spec.jscovers each startup combination. A preload counts rewrites per file, so a rewrite whose output is thrown away fails the test. Passing on Node.js 22.12, 22.15, 22.19 and 24.10 (sync-hook cases skipped) and on 24.15 and 26.5.