Skip to content

Conversation

@trentm
Copy link
Contributor

@trentm trentm commented Jan 28, 2026

This is an alternative to #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: #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 via import) 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 the internals: true option.

This enables internals: true for the import-in-the-middle (IITM) usage in the @opentelemetry/instrumentation package.

Let's use the @langchain/langgraph as 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/instrumentation below):

/* eslint-disable */
import { register } from 'node:module';
import {
  InstrumentationBase,
  InstrumentationNodeModuleDefinition,
  InstrumentationNodeModuleFile
} from '../build/src/index.js'; // @opentelemetry/instrumentation

register('../hook.mjs', import.meta.url); // @opentelemetry/instrumentation/hook.mjs

class TestInstrumentation extends InstrumentationBase {
  constructor(config = {}) {
    super('play', '1.2.3', config);
  }
  init() {
    return [
      new InstrumentationNodeModuleDefinition(
        '@langchain/langgraph',
        ['*'],
        function (mod, modVer) {
          // Hook the `@langchain/langgraph` main file, for either CJS or ESM.
          console.log('HIT: %s@%s', this.name, modVer);
          return mod;
        },
        null,
        [
          // This handles when `@langchain/langgraph` is loaded from CJS code.
          new InstrumentationNodeModuleFile(
            '@langchain/langgraph/dist/prebuilt/react_agent_executor.cjs',
            ['*'],
            function (mod, modVer) {
              console.log('HIT: %s@%s', this.name, modVer);
              return mod;
            },
            mod => mod,
          ),
          // This handles when `@langchain/langgraph` is loaded from ESM code.
          new InstrumentationNodeModuleFile(
            '@langchain/langgraph/dist/prebuilt/react_agent_executor.js',
            ['*'],
            function (mod, modVer) {
              console.log('HIT: %s@%s', this.name, modVer);
              return mod;
            },
            mod => mod,
          ),
        ]
      )
    ]
  }
}

const instr = new TestInstrumentation();

// ... the usual SDK setup stuff. However, because instrumentations currently
// (and crazily) are enabled when they are *constructed*, the code above is
// enough to show that the hooking is working.

foo.mjs

import '@langchain/langgraph/prebuilt';

Running that:

% node --import ./foo-telemetry.mjs foo.mjs
HIT: @langchain/langgraph/dist/prebuilt/[email protected]

…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 trentm self-assigned this Jan 28, 2026
@trentm trentm requested a review from a team as a code owner January 28, 2026 00:41
"dependencies": {
"@opentelemetry/api-logs": "0.211.0",
"import-in-the-middle": "^2.0.0",
"import-in-the-middle": "^2.0.6",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This change requires the fix in [email protected] (nodejs/import-in-the-middle#241).

@codecov
Copy link

codecov bot commented Jan 28, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.59%. Comparing base (2926172) to head (6089b07).
⚠️ Report is 8 commits behind head on main.

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:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@isaacs
Copy link

isaacs commented Jan 29, 2026

Verified, this works for our @langchain/langgraph/prebuilt instrumentation.

@trentm trentm added this pull request to the merge queue Jan 31, 2026
Merged via the queue into open-telemetry:main with commit f15aa7e Jan 31, 2026
27 checks passed
@trentm trentm deleted the trentm-itm-play branch January 31, 2026 01:09
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.

4 participants