feat(instrumentation): use the internals: true option with import-in-the-middle hook, allowing instrumentations to hook internal files in ES modules
#6344
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: