fix(instrumentations): instrument loaded hooked built-ins when dd-trace is bundled #10915
Overall package size
Self size: 9.71 MB
Deduped: 10.45 MB
No deduping: 10.45 MB
<details><summary>Dependency sizes</summary>
| name | version | self size | total size |
|------|---------|-----------|------------|
| import-in-the-middle | 3.5.1 | 127.66 kB | 528.28 kB |
| opentracing | 0.14.7 | 194.81 kB | 194.81 kB |
| dc-polyfill | 0.1.11 | 25.74 kB | 25.74 kB |
</details>
<sub>🤖 This report was automatically generated by heaviest-objects-in-the-universe<sub>
<a target='_blank' href='https://app.datadoghq.com/ci/pipeline-executions?query=ci_level%3Ajob+%40git.commit.sha%3A96c414c0484f34a60dd4a484b5ab727214602eab&referrer=pr-comment&start=1793114878000'><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.datadoghq.com/static/images/pr_comments/software_delivery/badges_header/pipelines-ok.svg"/><img src="https://static.datadoghq.com/static/images/pr_comments/software_delivery/badges_header/pipelines-ok-light.svg" alt="Pipelines" align="center" /></picture></a> <a target='_blank' href='https://app.datadoghq.com/ci/test/runs?query=test_level%3Atest+%40git.commit.sha%3A93672729d9352820f63113595cc90dfac4d2ae5c&referrer=pr-comment&start=1793114878000'><picture><source media="(prefers-color-scheme: dark)" srcset="https://static.datadoghq.com/static/images/pr_comments/software_delivery/badges_header/tests-ok.svg"/><img src="https://static.datadoghq.com/static/images/pr_comments/software_delivery/badges_header/tests-ok-light.svg" alt="Tests" align="center" /></picture></a>
<details><summary><b>🎉 All green!</b></summary>
🧪 All tests passed
❄️ No new flaky tests detected
</details>
🔄 Datadog retried 3 tests - 3 passed on retry <a href="https://app.datadoghq.com/ci/test/runs?query=test_level%3Atest+%40git.commit.sha%3A93672729d9352820f63113595cc90dfac4d2ae5c+%40test.is_retry%3Atrue+AND+%28%40test.retry_reason%3Aauto_test_retry+OR+%28-%40test.retry_reason%3A%2A+AND+-%40test.is_new%3Atrue%29%29&referrer=pr-comment" title="View in Datadog"><img src="https://static.datadoghq.com/static/images/pr_comments/software_delivery/datadog_icon.svg" alt="View in Datadog" align="center" /></a>
🎯 Code Coverage (<a target='_blank' href='https://app.datadoghq.com/ci/code-coverage/github.com%2Fdatadog%2Fdd-trace-js/pull-requests/10915'>details</a>)<br>• <strong><span title="Percentage of modified executable lines covered by tests in this PR">Patch Coverage</span></strong>: 100.00%<br>• <strong><span title="Percentage of all executable lines covered by tests in the entire codebase">Overall Coverage</span></strong>: 98.37% <span title="Change in overall coverage compared to the base branch">(-0.05%)</span><br>
<sub>
This comment will be updated automatically if new data arrives. <br />
🔗 Commit SHA: 96c414c | <a href="https://docs.datadoghq.com/integrations/guide/source-code-integration/#pr-comments">Docs</a> | <a href="https://app.datadoghq.com/ci/pull-request/Z2l0aHViLmNvbS9kYXRhZG9nL2RkLXRyYWNlLWpzLzEwOTE1?referrer=pr-comment">View more details</a> | Give us <a href="https://dd.enterprise.slack.com/archives/C07E3U2QRMM">feedback</a>!
</sub>
Codecov Report
:white_check_mark: All modified and coverable lines are covered by tests.
:white_check_mark: Project coverage is 98.96%. Comparing base (
9e0c95f) to head (96c414c).:warning: Report is 25 commits behind head on master.
<details><summary>Additional details and impacted files</summary>
@@ Coverage Diff @@
## master #10915 +/- ##
========================================
Coverage 98.96% 98.96%
========================================
Files 1069 1069
Lines 175303 175667 +364
Branches 72 72
========================================
+ Hits 173485 173852 +367
+ Misses 1817 1814 -3
Partials 1 1
| Flag | Coverage Δ | |
|---|---|---|
| ai-guard | 63.62% <47.22%> (-0.05%) | :arrow_down: |
| apm-capabilities | 54.76% <47.22%> (-0.01%) | :arrow_down: |
| apm-integrations | 80.89% <47.22%> (-0.02%) | :arrow_down: |
| appsec | 57.27% <47.22%> (-0.01%) | :arrow_down: |
| debugger | 70.85% <47.22%> (-0.04%) | :arrow_down: |
| instrumentation | 55.44% <100.00%> (+0.03%) | :arrow_up: |
| llmobs | 81.35% <47.22%> (-0.06%) | :arrow_down: |
| master-coverage | 98.96% <100.00%> (?) | |
| openfeature | 67.34% <47.22%> (-0.03%) | :arrow_down: |
| platform | 68.39% <47.22%> (-0.01%) | :arrow_down: |
| profiling | 65.86% <47.22%> (-0.04%) | :arrow_down: |
| serverless | 64.99% <47.22%> (+0.07%) | :arrow_up: |
| test-optimization | 82.01% <47.22%> (-0.02%) | :arrow_down: |
Flags with carried forward coverage won't be shown. Click here to find out more.
</details>
:umbrella: View full report in Codecov by Harness.
:loudspeaker: Have feedback on the report? Share it here.
<details><summary> :rocket: New features to boost your workflow: </summary>
</details>
Benchmarks
Benchmark execution time: 2026-10-27 16:17:19
Comparing candidate commit 96c414c0484f34a60dd4a484b5ab727214602eab in PR branch pabloerhard/instrument-loaded-hooked-built-ins-when--8344dc2f with baseline commit 9e0c95fd448f7a6ede4a88185b655ce3fbccaf46 in branch master.
Found 0 performance improvements and 0 performance regressions! Performance is the same for 2451 metrics, 6 unstable metrics.
<details>
<summary><h3>Explanation</h3></summary>
This is an A/B test comparing a candidate commit's performance against that of a baseline commit. Performance changes are noted in the tables below as:
🟩 = significantly better candidate vs. baseline
🟥 = significantly worse candidate vs. baseline
We compute a confidence interval (CI) over the relative difference of means between metrics from the candidate and baseline commits, considering the baseline as the reference.
If the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD), the change is considered significant.
Feel free to reach out to #apm-benchmarking-platform on Slack if you have any questions.
<details>
<summary><h4>More details about the CI and significant changes</h4></summary>
You can imagine this CI as a range of values that is likely to contain the true difference of means between the candidate and baseline commits.
CIs of the difference of means are often centered around 0%, because often changes are not that big:
---------------------------------(------|---^--------)-------------------------------->
-0.6% 0% 0.3% +1.2%
| | |
lower bound of the CI --' | |
sample mean (center of the CI) -------------' |
upper bound of the CI ----------------------'
As described above, a change is considered significant if the CI is entirely outside the configured SIGNIFICANT_IMPACT_THRESHOLD (or the deprecated UNCONFIDENCE_THRESHOLD).
For instance, for an execution time metric, this confidence interval indicates a significantly worse performance:
----------------------------------------|---------|---(---------^---------)---------->
0% 1% 1.3% 2.2% 3.1%
| | | |
significant impact threshold --------------' | | |
lower bound of CI --------------' | |
sample mean (center of the CI) --------------------------' |
upper bound of CI ----------------------------------'
</details>
</details>
<details>
<summary><h3>Unstable benchmarks</h3></summary>
These benchmarks have a confidence interval too wide to call a change; treat them as noise rather than signal.
scenario:appsec-appsec-enabled-20
max_rss_usage [-38.288MB; +42.056MB] or [-13.363%; +14.678%]scenario:appsec-iast-no-vulnerability-iast-enabled-default-config-26
max_rss_usage [-36.896MB; +13.464MB] or [-13.281%; +4.846%]scenario:appsec-iast-with-vulnerability-iast-enabled-default-config-20
max_rss_usage [-32.606MB; +39.256MB] or [-9.087%; +10.941%]scenario:appsec-iast-with-vulnerability-iast-enabled-default-config-24
max_rss_usage [-18.619MB; +21.964MB] or [-5.152%; +6.078%]scenario:openfeature-scale-full-24
max_rss_usage [-31.191MB; +24.956MB] or [-10.048%; +8.039%]scenario:openfeature-scale-full-26
max_rss_usage [-40.066MB; +20.955MB] or [-16.349%; +8.551%]</details>
<!-- codex-pull-request-review-summary -->
<!-- codex-review-state:v1 {"code_review":{"commit_sha":"96c414c0484f34a60dd4a484b5ab727214602eab","has_any_findings":false,"has_unresolved_findings":false,"status":"completed"},"security_review":{"commit_sha":"96c414c0484f34a60dd4a484b5ab727214602eab","has_any_findings":false,"has_unresolved_findings":false,"status":"completed"}} -->
<!-- codex-security-review:v1 {"blockingSeverityThreshold":"P0","headSha":"96c414c0484f34a60dd4a484b5ab727214602eab","mergeGateEnabled":false,"pullRequestNumber":10915,"repository":"DataDog/dd-trace-js","status":"completed"} -->
Codex Review Summary
This comment shows the latest Codex review activity on this pull request.
| Review | Status | Commit | Review trigger |
| --- | --- | --- | --- |
| 📝 Code Review | ✅ Completed <relative-time datetime="2026-10-27T16:01:56.409296Z">2026-10-27T16:01:56.409296Z</relative-time> | 96c414c | New commits |
| 🔒 Security Review | ✅ Completed <relative-time datetime="2026-10-27T16:01:58.968152Z">2026-10-27T16:01:58.968152Z</relative-time> | 96c414c | New commits |
<details> <summary>ℹ️ About Codex in GitHub</summary>
<br/>
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.
</details>
<details>
<summary>More details</summary>
A standalone Node.js smoke check supported correct built-in re-requires and ESM synchronization. The completed static review identified no reportable defects.
</details>
Was this helpful? React 👍 or 👎
<sub>Open Bits AI session</sub>
<sub>🤖 Bits Code Review · Commit e86f267</sub>
<!-- autotest-meta model=gpt-6.1-sol engine=codex reasoning_effort=high verdict=PASS ddrs_bucket=unknown session_id=24de658f-7710-4a71-856f-68e4a4162843 -->
<!-- datadog-code-review reviewer-id=bad90057-5c12-4101-ad47-d91c9dff0f90 -->
<!-- datadog-autotest:v1:prod -->
<!-- datadog-autotest-delivery:v1:prod:91b06f5f5c1d754f64351ec213f810ba2084290da86f365c244a96cece07c1a0:bundle -->
How does this work when the export is a function and its reference is exported directly?
Or is that not something that ever happens with built-ins?
Any way to centralize this logic somewhere? Why does it have to be handled where it currently is handled plus here?
It's does not happen with the built-ins we currently instrument.
I guess we could indeed reuse some of the logic that register.js is doing. That said, we would still need to add some logic to register.js and additionally add an export to expose what we need to share with bundler-register.js
Approved these changes.
LGTM
rochdev
pabloerhard
What does this PR do?
When dd-trace is bundled inside the application (
bundler-register.jsdetects a bundled context via!(module instanceof Module)), it now re-requires every enabled hooked Node.js built-in that is already loaded (perprocess.moduleLoadList; all hooked built-ins if that list is unavailable), instead of onlyhttp/https. Disabled instrumentations and serverless exclusions are skipped,Module.syncBuiltinESMExports()runs afterwards so ESM named imports see the patched functions, and failures are logged instead of thrown.Motivation
In a bundle, built-ins loaded before dd-trace installs its require hooks are cached by the bundler and never pass through ritm. Only
http/httpswere re-required, so built-ins such asdnsorfsstayed uninstrumented.Additional Notes
moduleLoadListfallback, disabled/serverless filtering, sync running after every re-require, no sync when nothing was patched, and logged require/sync failures. The bundled cases run the real file under its own filename so coverage is attributed to it.bundler-register.spec.js(21 passing) and eslint on the changed files.