GeezerVison is showing github.com — fix(esm): await dd-trace/register.mjs before loading handlers by pabloerhard · Pull Request #872 · DataDog/datadog-lambda-js
Conversation
What does this PR do?
- When the tracer ships
dd-trace/register.mjs (DataDog/dd-trace-js#11296), the programmatic bootstrap loads it with native import() and awaits it.
handler.mjs awaits initTracer(), so the hooks are installed before the custom trace extractor and the customer handler are loaded.
- Tracers without
register.mjs keep the existing path unchanged: register.js when module.register() exists, falling back to loader-hook.mjs for tracers without register.js.
- Registration errors are still caught and logged at debug level, so they never break the customer's function.
Motivation
AWS Lambda disables require(esm). Requiring register.js therefore cannot install the synchronous loader hooks (Module.registerHooks) and falls back to the asynchronous module.register() loader, which Node.js 26 deprecates and nodejs/node#67090 removes. Without that loader, ES modules load without instrumentation. The bootstrap also skipped registration entirely when module.register() was absent.
register.mjs picks the synchronous hooks or the asynchronous fallback itself and does not register twice, so the bootstrap only needs to detect it and await it.
Testing Guidelines
yarn test: 55 suites, 734 tests pass. yarn lint and yarn check-formatting pass.
- New
src/handler-esm-registration.spec.ts runs the built dist/handler.mjs against a fake tracer with --no-experimental-require-module and module.register removed:
register.mjs loads before the extractor and the handler;
- if
register.mjs fails, the handler still loads and returns its result.
src/handler.spec.ts (durable handler, real tracer) now runs with --no-experimental-require-module and counts module.register / module.registerHooks calls instead of checking that register.js loaded. Without this, it would fail as soon as the dev dependency moves to a tracer with register.mjs (checked by swapping in dd-trace 6.27.0).
src/runtime/module-importer.spec.ts keeps covering tracers without register.mjs.
- Affected specs also pass on Node 18.20.8, 20.19.5 and 22.19.0 (the durable tests are skipped below Node 22, as before).
- Manual check with real tracers on Node 26 and
--no-experimental-require-module, verifying that an ES module imported by the customer handler is intercepted:
- dd-trace 6.27.0: instrumented through synchronous hooks, including with
module.register removed (base: not instrumented when module.register is removed);
- dd-trace 6.24.0: instrumented through
module.register(), as today;
- dd-trace 5.138.0 on Node 18.20.8: instrumented through
module.register().
Additional Notes
Types of Changes
- Bug fix
- New feature
- Breaking change
- Misc (docs, refactoring, dependency upgrade, etc.)
Check all that apply
- This PR's description is comprehensive
- This PR contains breaking changes that are documented in the description
- This PR introduces new APIs or parameters that are documented and unlikely to change in the foreseeable future
- This PR impacts documentation, and it has been updated (or a ticket has been logged)
- This PR's changes are covered by the automated tests
- This PR collects user input/sensitive content into Datadog
- This PR passes the integration tests (ask a Datadog member to run the tests)
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
datadog-datadog-prod-us1[bot] reacted with thumbs up emoji
Lambda disables require(esm), so requiring dd-trace/register.js cannot
install the synchronous loader hooks. It falls back to module.register(),
which Node.js 26 deprecates and nodejs/node#67090 removes, leaving ES
modules uninstrumented.
When the tracer ships register.mjs (DataDog/dd-trace-js#11296), import it
natively and await it before the custom extractor and customer handler are
loaded. Tracers without register.mjs keep the existing register.js path.
|

✨ Unblock PR with BitsAI
❌ Errors
Your PR has failed checks. Please review the issues below and take necessary action before merging.
🚦 1 Pipeline job failed
Useful? React with 👍 / 👎
This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 514ea93 | Docs | View more details | Give us feedback!
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.

More details
Loader registration completes before extractor and handler imports, while registration failures still allow handler startup. The static review identified no actionable regressions.
Was this helpful? React 👍 or 👎
Open Bits AI session
🤖 Bits Code Review · Commit 514ea93
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This branch has not been deployed
No deployments
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
|