Skip to content

Commit 877b10e

Browse files
committed
Try to instrument createReactAgent (doesn't work yet)
1 parent cee1039 commit 877b10e

File tree

3 files changed

+77
-2
lines changed

3 files changed

+77
-2
lines changed

packages/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ export type { GoogleGenAIResponse } from './tracing/google-genai/types';
152152
export { createLangChainCallbackHandler } from './tracing/langchain';
153153
export { LANGCHAIN_INTEGRATION_NAME } from './tracing/langchain/constants';
154154
export type { LangChainOptions, LangChainIntegration } from './tracing/langchain/types';
155-
export { instrumentStateGraphCompile, instrumentLangGraph } from './tracing/langgraph';
155+
export { instrumentStateGraphCompile, instrumentCreateReactAgent, instrumentLangGraph } from './tracing/langgraph';
156156
export { LANGGRAPH_INTEGRATION_NAME } from './tracing/langgraph/constants';
157157
export type { LangGraphOptions, LangGraphIntegration, CompiledGraph } from './tracing/langgraph/types';
158158
export type { OpenAiClient, OpenAiOptions, InstrumentedMethod } from './tracing/openai/types';

packages/core/src/tracing/langgraph/index.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,51 @@ function instrumentCompiledGraphInvoke(
156156
}) as (...args: unknown[]) => Promise<unknown>;
157157
}
158158

159+
/**
160+
* Instruments createReactAgent to create spans for agent creation and invocation
161+
*
162+
* Creates a `gen_ai.create_agent` span when createReactAgent() is called
163+
*/
164+
export function instrumentCreateReactAgent(
165+
originalCreateReactAgent: (...args: unknown[]) => CompiledGraph,
166+
options: LangGraphOptions,
167+
): (...args: unknown[]) => CompiledGraph {
168+
return new Proxy(originalCreateReactAgent, {
169+
apply(target, thisArg, args: unknown[]): CompiledGraph {
170+
return startSpan(
171+
{
172+
op: 'gen_ai.create_agent',
173+
name: 'create_agent',
174+
attributes: {
175+
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: LANGGRAPH_ORIGIN,
176+
[SEMANTIC_ATTRIBUTE_SENTRY_OP]: 'gen_ai.create_agent',
177+
[GEN_AI_OPERATION_NAME_ATTRIBUTE]: 'create_agent',
178+
},
179+
},
180+
() => {
181+
try {
182+
const compiledGraph = Reflect.apply(target, thisArg, args);
183+
const compiledOptions = args.length > 0 ? (args[0] as Record<string, unknown>) : {};
184+
const originalInvoke = compiledGraph.invoke;
185+
if (originalInvoke && typeof originalInvoke === 'function') {
186+
compiledGraph.invoke = instrumentCompiledGraphInvoke(
187+
originalInvoke.bind(compiledGraph) as (...args: unknown[]) => Promise<unknown>,
188+
compiledGraph,
189+
compiledOptions,
190+
options,
191+
) as typeof originalInvoke;
192+
}
193+
194+
return compiledGraph;
195+
} catch (error) {
196+
throw error;
197+
}
198+
},
199+
);
200+
},
201+
}) as (...args: unknown[]) => CompiledGraph;
202+
}
203+
159204
/**
160205
* Directly instruments a StateGraph instance to add tracing spans
161206
*

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

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
InstrumentationNodeModuleFile,
77
} from '@opentelemetry/instrumentation';
88
import type { CompiledGraph, LangGraphOptions } from '@sentry/core';
9-
import { getClient, instrumentStateGraphCompile, SDK_VERSION } from '@sentry/core';
9+
import { getClient, instrumentStateGraphCompile, instrumentCreateReactAgent, SDK_VERSION } from '@sentry/core';
1010

1111
const supportedVersions = ['>=0.0.0 <2.0.0'];
1212

@@ -50,6 +50,16 @@ export class SentryLangGraphInstrumentation extends InstrumentationBase<LangGrap
5050
this._patch.bind(this),
5151
exports => exports,
5252
),
53+
new InstrumentationNodeModuleFile(
54+
/**
55+
* Patch the prebuilt subpath exports for CJS.
56+
* The @langchain/langgraph/prebuilt entry point re-exports from dist/prebuilt/index.cjs
57+
*/
58+
'@langchain/langgraph/dist/prebuilt/index.cjs',
59+
supportedVersions,
60+
this._patch.bind(this),
61+
exports => exports,
62+
),
5363
],
5464
);
5565
return module;
@@ -83,6 +93,26 @@ export class SentryLangGraphInstrumentation extends InstrumentationBase<LangGrap
8393
);
8494
}
8595

96+
// Patch createReactAgent to instrument the agent creation and invocation
97+
if (exports.createReactAgent && typeof exports.createReactAgent === 'function') {
98+
exports.createReactAgent = instrumentCreateReactAgent(
99+
exports.createReactAgent as (...args: unknown[]) => CompiledGraph,
100+
options,
101+
);
102+
/*
103+
const originalCreateReactAgent = exports.createReactAgent;
104+
Object.defineProperty(exports, 'createReactAgent', {
105+
value: instrumentCreateReactAgent(
106+
originalCreateReactAgent as (...args: unknown[]) => CompiledGraph,
107+
options,
108+
),
109+
writable: true,
110+
enumerable: true,
111+
configurable: true,
112+
});
113+
*/
114+
}
115+
86116
return exports;
87117
}
88118
}

0 commit comments

Comments
 (0)