Skip to content

Commit 819cf39

Browse files
committed
Split patch for main and prebuilt langgraph modules
1 parent 40cde73 commit 819cf39

File tree

1 file changed

+37
-22
lines changed

1 file changed

+37
-22
lines changed

packages/node/src/integrations/tracing/langgraph/instrumentation.ts

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,19 +35,19 @@ export class SentryLangGraphInstrumentation extends InstrumentationBase<LangGrap
3535
const mainModule = new InstrumentationNodeModuleDefinition(
3636
'@langchain/langgraph',
3737
supportedVersions,
38-
this._patch.bind(this),
38+
this._patchMainModule.bind(this),
3939
exports => exports,
4040
[
4141
new InstrumentationNodeModuleFile(
4242
/**
4343
* In CJS, LangGraph packages re-export from dist/index.cjs files.
4444
* Patching only the root module sometimes misses the real implementation or
4545
* gets overwritten when that file is loaded. We add a file-level patch so that
46-
* _patch runs again on the concrete implementation
47-
*/
46+
* _patchMainModule runs again on the concrete implementation
47+
*/
4848
'@langchain/langgraph/dist/index.cjs',
4949
supportedVersions,
50-
this._patch.bind(this),
50+
this._patchMainModule.bind(this),
5151
exports => exports,
5252
),
5353
],
@@ -56,17 +56,19 @@ export class SentryLangGraphInstrumentation extends InstrumentationBase<LangGrap
5656
const prebuiltModule = new InstrumentationNodeModuleDefinition(
5757
'@langchain/langgraph/prebuilt',
5858
supportedVersions,
59-
this._patch.bind(this),
59+
this._patchPrebuiltModule.bind(this),
6060
exports => exports,
6161
[
6262
new InstrumentationNodeModuleFile(
6363
/**
64-
* Patch the prebuilt subpath exports for CJS.
65-
* The @langchain/langgraph/prebuilt entry point re-exports from dist/prebuilt/index.cjs
66-
*/
64+
* In CJS, LangGraph packages re-export from dist/prebuilt/index.cjs files.
65+
* Patching only the root module sometimes misses the real implementation or
66+
* gets overwritten when that file is loaded. We add a file-level patch so that
67+
* _patchPrebuiltModule runs again on the concrete implementation
68+
*/
6769
'@langchain/langgraph/dist/prebuilt/index.cjs',
6870
supportedVersions,
69-
this._patch.bind(this),
71+
this._patchPrebuiltModule.bind(this),
7072
exports => exports,
7173
),
7274
],
@@ -76,20 +78,10 @@ export class SentryLangGraphInstrumentation extends InstrumentationBase<LangGrap
7678
}
7779

7880
/**
79-
* Core patch logic applying instrumentation to the LangGraph module.
81+
* Patch logic applying instrumentation to the LangGraph main module.
8082
*/
81-
private _patch(exports: PatchedModuleExports): PatchedModuleExports | void {
82-
const client = getClient();
83-
const defaultPii = Boolean(client?.getOptions().sendDefaultPii);
84-
85-
const config = this.getConfig();
86-
const recordInputs = config.recordInputs ?? defaultPii;
87-
const recordOutputs = config.recordOutputs ?? defaultPii;
88-
89-
const options: LangGraphOptions = {
90-
recordInputs,
91-
recordOutputs,
92-
};
83+
private _patchMainModule(exports: PatchedModuleExports): PatchedModuleExports | void {
84+
const options = this._getOptions();
9385

9486
// Patch StateGraph.compile to instrument both compile() and invoke()
9587
if (exports.StateGraph && typeof exports.StateGraph === 'function') {
@@ -103,6 +95,15 @@ export class SentryLangGraphInstrumentation extends InstrumentationBase<LangGrap
10395
);
10496
}
10597

98+
return exports;
99+
}
100+
101+
/**
102+
* Patch logic applying instrumentation to the LangGraph prebuilt module.
103+
*/
104+
private _patchPrebuiltModule(exports: PatchedModuleExports): PatchedModuleExports | void {
105+
const options = this._getOptions();
106+
106107
// Patch createReactAgent to instrument the agent creation and invocation
107108
if (exports.createReactAgent && typeof exports.createReactAgent === 'function') {
108109
exports.createReactAgent = instrumentCreateReactAgent(
@@ -113,4 +114,18 @@ export class SentryLangGraphInstrumentation extends InstrumentationBase<LangGrap
113114

114115
return exports;
115116
}
117+
118+
/**
119+
* Helper to get instrumentation options
120+
*/
121+
private _getOptions(): LangGraphOptions {
122+
const client = getClient();
123+
const defaultPii = Boolean(client?.getOptions().sendDefaultPii);
124+
const config = this.getConfig();
125+
126+
return {
127+
recordInputs: config.recordInputs ?? defaultPii,
128+
recordOutputs: config.recordOutputs ?? defaultPii,
129+
};
130+
}
116131
}

0 commit comments

Comments
 (0)