66 InstrumentationNodeModuleFile ,
77} from '@opentelemetry/instrumentation' ;
88import type { CompiledGraph , LangGraphOptions } from '@sentry/core' ;
9- import { getClient , instrumentStateGraphCompile , instrumentCreateReactAgent , SDK_VERSION } from '@sentry/core' ;
9+ import { getClient , instrumentCreateReactAgent , instrumentStateGraphCompile , SDK_VERSION } from '@sentry/core' ;
1010
1111const supportedVersions = [ '>=0.0.0 <2.0.0' ] ;
1212
@@ -31,57 +31,57 @@ export class SentryLangGraphInstrumentation extends InstrumentationBase<LangGrap
3131 /**
3232 * Initializes the instrumentation by defining the modules to be patched.
3333 */
34- public init ( ) : InstrumentationModuleDefinition [ ] {
35- const mainModule = new InstrumentationNodeModuleDefinition (
34+ public init ( ) : InstrumentationModuleDefinition {
35+ const module = new InstrumentationNodeModuleDefinition (
3636 '@langchain/langgraph' ,
3737 supportedVersions ,
38- this . _patchMainModule . bind ( this ) ,
38+ this . _patch . 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- * _patchMainModule runs again on the concrete implementation
47- */
46+ * _patch runs again on the concrete implementation
47+ */
4848 '@langchain/langgraph/dist/index.cjs' ,
4949 supportedVersions ,
50- this . _patchMainModule . bind ( this ) ,
50+ this . _patch . bind ( this ) ,
5151 exports => exports ,
5252 ) ,
53- ] ,
54- ) ;
55-
56- const prebuiltModule = new InstrumentationNodeModuleDefinition (
57- '@langchain/langgraph/prebuilt' ,
58- supportedVersions ,
59- this . _patchPrebuiltModule . bind ( this ) ,
60- exports => exports ,
61- [
6253 new InstrumentationNodeModuleFile (
6354 /**
6455 * In CJS, LangGraph packages re-export from dist/prebuilt/index.cjs files.
6556 * Patching only the root module sometimes misses the real implementation or
6657 * gets overwritten when that file is loaded. We add a file-level patch so that
67- * _patchPrebuiltModule runs again on the concrete implementation
68- */
58+ * _patch runs again on the concrete implementation
59+ */
6960 '@langchain/langgraph/dist/prebuilt/index.cjs' ,
7061 supportedVersions ,
71- this . _patchPrebuiltModule . bind ( this ) ,
62+ this . _patch . bind ( this ) ,
7263 exports => exports ,
7364 ) ,
7465 ] ,
7566 ) ;
76-
77- return [ mainModule , prebuiltModule ] ;
67+ return module ;
7868 }
7969
8070 /**
81- * Patch logic applying instrumentation to the LangGraph main module.
71+ * Core patch logic applying instrumentation to the LangGraph module.
8272 */
83- private _patchMainModule ( exports : PatchedModuleExports ) : PatchedModuleExports | void {
84- const options = this . _getOptions ( ) ;
73+ private _patch ( exports : PatchedModuleExports ) : PatchedModuleExports | void {
74+ const client = getClient ( ) ;
75+ const defaultPii = Boolean ( client ?. getOptions ( ) . sendDefaultPii ) ;
76+
77+ const config = this . getConfig ( ) ;
78+ const recordInputs = config . recordInputs ?? defaultPii ;
79+ const recordOutputs = config . recordOutputs ?? defaultPii ;
80+
81+ const options : LangGraphOptions = {
82+ recordInputs,
83+ recordOutputs,
84+ } ;
8585
8686 // Patch StateGraph.compile to instrument both compile() and invoke()
8787 if ( exports . StateGraph && typeof exports . StateGraph === 'function' ) {
@@ -95,37 +95,18 @@ export class SentryLangGraphInstrumentation extends InstrumentationBase<LangGrap
9595 ) ;
9696 }
9797
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-
10798 // Patch createReactAgent to instrument the agent creation and invocation
108- if ( exports . createReactAgent && typeof exports . createReactAgent === 'function' ) {
109- exports . createReactAgent = instrumentCreateReactAgent (
110- exports . createReactAgent as ( ...args : unknown [ ] ) => CompiledGraph ,
99+ const originalCreateReactAgent = exports . createReactAgent ;
100+ Object . defineProperty ( exports , 'createReactAgent' , {
101+ value : instrumentCreateReactAgent (
102+ originalCreateReactAgent as ( ...args : unknown [ ] ) => CompiledGraph ,
111103 options ,
112- ) ;
113- }
104+ ) ,
105+ writable : true ,
106+ enumerable : true ,
107+ configurable : true ,
108+ } ) ;
114109
115110 return exports ;
116111 }
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- }
131112}
0 commit comments