feat(instrumentation): use the internals: true option with import-in-the-middle hook, allowing instrumentations to hook internal files in ES modules#6344
Merged
trentm merged 3 commits intoopen-telemetry:mainfrom Jan 31, 2026
Conversation
…n-the-middle hook, allowing instrumentations to hook internal files in ES modules This is an alternative to open-telemetry#6246 It allows ESM sub-file hooking to work, but with the same `InstrumentationNodeModuleFile` usage as instrumentation currently uses for CommonJS sub-file hooking. Obsoletes: open-telemetry#6246
trentm
commented
Jan 28, 2026
| "dependencies": { | ||
| "@opentelemetry/api-logs": "0.211.0", | ||
| "import-in-the-middle": "^2.0.0", | ||
| "import-in-the-middle": "^2.0.6", |
Contributor
Author
There was a problem hiding this comment.
This change requires the fix in import-in-the-middle@2.0.6 (nodejs/import-in-the-middle#241).
7 tasks
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #6344 +/- ##
=======================================
Coverage 95.59% 95.59%
=======================================
Files 314 314
Lines 9596 9596
Branches 2221 2221
=======================================
Hits 9173 9173
Misses 423 423 🚀 New features to boost your workflow:
|
|
Verified, this works for our |
pichlermarc
approved these changes
Jan 30, 2026
david-luna
approved these changes
Jan 30, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
This is an alternative to #6246
It allows ESM sub-file hooking to work, but with the same
InstrumentationNodeModuleFileusage as instrumentation currently usesfor CommonJS sub-file hooking.
Obsoletes: #6246
Details
Current OTel JS instrumentations monkey-patch modules on the fly using the require-in-the-middle (for code loaded via
require()) and import-in-the-middle (for code loaded viaimport) dependencies. One way (the blessed, and IMO most reliable way) to use those deps to match module internal files (i.e. files in the module other than the "main" file) is using theinternals: trueoption.This enables
internals: truefor the import-in-the-middle (IITM) usage in the@opentelemetry/instrumentationpackage.Let's use the
@langchain/langgraphas an example. It should now be possible to create an instrumentation like the following that works to hook both CJS and ESM internal files using:foo-telemetry.mjs (you may have to adjust the two relative paths for
@opentelemetry/instrumentationbelow):foo.mjs
Running that: