-
Notifications
You must be signed in to change notification settings - Fork 996
fix: instrument ESM package export submodule #6246
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
|
|
This patches the `@langchain/langgraph/prebuilt` submodule export, when loading in ESM mode. Requires an update to `@opentelemetry/instrumentation`, in order to not block the patching of ESM submodule exports. depends-on: open-telemetry/opentelemetry-js#6246
|
ping @open-telemetry/javascript-approvers Any guidance would be much appreciated, if this isn't the best way to get this done, but it's blocking the instrumenting of certain package submodule exports in ESM mode. |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #6246 +/- ##
=======================================
Coverage 95.39% 95.39%
=======================================
Files 316 316
Lines 9574 9574
Branches 2216 2216
=======================================
Hits 9133 9133
Misses 441 441 🚀 New features to boost your workflow:
|
|
Sorry for the radio silence on this, we are still reviewing and will post an update soon. |
|
Assigning myself for now. Thar be dragons... or at least I'm getting stuck on somethings. Note to self: see getsentry/sentry-javascript#18403 for motivating example. |
Add support for patching an ESM internal module that is exported with a submodule path name in the `package.json` exports object.
3f351ea to
2d95e95
Compare
|
Sorry for taking so long here. I would like to move the IITM hook in new InstrumentationNodeModuleDefinition(
// This works to hook `@langchain/langgraph` for both CJS and ESM.
'@langchain/langgraph',
['*'],
function (mod, modVer) {
console.log('HIT: %s@%s', this.name, modVer);
return mod;
},
null,
[
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,
),
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,
),
]
),However, there is a hiccup: IITM with I could probably also change the Hook handling in |
|
The reason I don't want start using sub-module paths / entry-points in Hooks for OTel instrumentation, e.g.: new InstrumentationNodeModuleDefinition(
'@langchain/langgraph/prebuilt',
supportedVersions,
this._patch.bind(this),
exports => exports,
[
new InstrumentationNodeModuleFile(
/**
* ESM builds use dist/prebuilt/index.js (without .cjs extension)
* This catches ESM imports that resolve through the main package,
* using the package.json submodule export
*/
'@langchain/langgraph/prebuilt',
supportedVersions,
this._patch.bind(this),
exports => exports,
),is because it doesn't always work to hook the target implementation file. It doesn't work if the target file (e.g. That can't happen in this particular case -- because AFAICT nothing else in In that case one of the motivating examples was Hook(['@langchain/core/vectorstores'], ...)it will be matched when a user does: import ... from '@langchain/core/vectorstores';
// OR
require('@langchain/core/vectorstores')However, the underlying functionality (in |
…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
|
I will check it out, thanks! |
Add support for patching an ESM internal module that is exported with a submodule path name in the
package.jsonexports object.Which problem is this PR solving?
It is not currently possible to patch an ESM submodule export.
Fixes # (issue)
Short description of the changes
When determining whether to apply a patch, the
file.nameis compared against thenormalizedPath. However, this makes it impossible to get import-in-the-middle to consider the module a valid hook target.Allow passing in the submodule specifier as the file name, treating it as the effective "path" to be hooked.
This is the continuation (one level higher in the stack) of nodejs/import-in-the-middle#215
Type of change
Please delete options that are not relevant.
I believe it could be argued that this is a new feature, and perhaps even not the most elegant approach. It does feel a bit clunky, tbh, since the "filename" being passed in is actually a virtual path defined in
package.json, not an actual path on disk, since those are fully abstracted away by the ESM module loading semantics. But, I can't see how it would break any existing use cases.How Has This Been Tested?
Test included in the patch, at
experimental/packages/opentelemetry-instrumentation/test/node/EsmSubmoduleInstrumentation.test.mjsChecklist: