Skip to content

feat(handler): register dd-trace ESM loader hook programmatically - #801

Merged
joeyzhao2018 merged 5 commits into
mainfrom
joey/register-esm-loader-hook
Jul 29, 2026
Merged

feat(handler): register dd-trace ESM loader hook programmatically#801
joeyzhao2018 merged 5 commits into
mainfrom
joey/register-esm-loader-hook

Conversation

@joeyzhao2018

@joeyzhao2018 joeyzhao2018 commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

https://datadoghq.atlassian.net/browse/APMSVLS-587

ESM instrumentation (orchestrion/rewriter-based, e.g. the @aws/durable-execution-sdk-js integration) depends on dd-trace's loader-hook.mjs being registered via --import dd-trace/initialize.mjs. Runtimes that ignore NODE_OPTIONS (e.g. the AWS durable functions runtime (not the regualr lambda runtime)) never register it, so ESM handlers get no rewritten instrumentation while CJS handlers work.

Mirror what dd-trace/initialize.mjs does: resolve the dd-trace package root and Module.register('./loader-hook.mjs') from handler.mjs before the user's ESM handler is imported.

Guards:

  • Skip when the loader hook is already active via NODE_OPTIONS/execArgv (module.register() does not dedupe; a second registration would run the rewriter twice on every module).
  • Skip when the tracer failed to initialize, matching initialize.mjs.
  • Use a namespace import for 'module' so runtimes without module.register (< 18.19 / < 20.6) don't fail at link time.

Testing Guidelines

  • Tested it working in an example durable function

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)

ESM instrumentation (orchestrion/rewriter-based, e.g. the
@aws/durable-execution-sdk-js integration) depends on dd-trace's
loader-hook.mjs being registered via --import dd-trace/initialize.mjs.
Runtimes that ignore NODE_OPTIONS (e.g. the AWS durable functions
runtime) never register it, so ESM handlers get no rewritten
instrumentation while CJS handlers work.

Mirror what dd-trace/initialize.mjs does: resolve the dd-trace package
root and Module.register('./loader-hook.mjs') from handler.mjs before
the user's ESM handler is imported.

Guards:
- Skip when the loader hook is already active via NODE_OPTIONS/execArgv
  (module.register() does not dedupe; a second registration would run
  the rewriter twice on every module).
- Skip when the tracer failed to initialize, matching initialize.mjs.
- Use a namespace import for 'module' so runtimes without
  module.register (< 18.19 / < 20.6) don't fail at link time.
@joeyzhao2018
joeyzhao2018 marked this pull request as ready for review July 27, 2026 14:36
@joeyzhao2018
joeyzhao2018 requested review from a team as code owners July 27, 2026 14:36
@joeyzhao2018
joeyzhao2018 requested a review from lym953 July 27, 2026 14:36

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 08f0e5af99

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/handler.mjs Outdated
@datadog-prod-us1-5

datadog-prod-us1-5 Bot commented Jul 27, 2026

Copy link
Copy Markdown

Pipelines

Unblock PR with BitsAI

⚠️ Warnings

🚦 2 Pipeline jobs failed

DataDog/datadog-lambda-js | integration test (node18)   View in Datadog   GitLab

DataDog/datadog-lambda-js | integration test (node24)   View in Datadog   GitLab

Useful? React with 👍 / 👎

This comment will be updated automatically if new data arrives.
🔗 Commit SHA: 2e49e48 | Docs | Datadog PR Page | Give us feedback!

--require dd-trace/register.js (and --import of it, which works on
modern Node via CJS interop) also registers loader-hook.mjs, but the
.mjs-only pattern missed it, causing a second registration and double
rewriting. Match register.js alongside .mjs preload entry points.
@joeyzhao2018
joeyzhao2018 merged commit 0d47ad7 into main Jul 29, 2026
40 of 42 checks passed
@joeyzhao2018
joeyzhao2018 deleted the joey/register-esm-loader-hook branch July 29, 2026 03:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants