-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(core): Instrument langgraph createReactAgent #20344
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
101833b
feat(core): Instrument langgraph createReactAgent
andreiborza eaceda5
Use `GEN_AI_TOOL_NAME_ATTRIBUTE` in tests
andreiborza 2142238
Don't set agent name on pipeline attr
andreiborza e96e6ca
Add stricter testing, matching the entire span structure
andreiborza 627298e
Guard langgraph instrumentations against double-wrapping
andreiborza e81c704
Auto-inject langchain cb handler for StateGraph too
andreiborza 0da2278
Ensure `model` is taken into account when `modelName` is missing
andreiborza 565b49f
Rename getAgentAttributesFromMetadata to getAgentNameFromMetadata
andreiborza File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
dev-packages/node-integration-tests/suites/tracing/langgraph/agent-scenario.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import { ChatAnthropic } from '@langchain/anthropic'; | ||
| import { HumanMessage, SystemMessage } from '@langchain/core/messages'; | ||
| import { createReactAgent } from '@langchain/langgraph/prebuilt'; | ||
| import * as Sentry from '@sentry/node'; | ||
| import express from 'express'; | ||
|
|
||
| function startMockAnthropicServer() { | ||
| const app = express(); | ||
| app.use(express.json()); | ||
|
|
||
| app.post('/v1/messages', (req, res) => { | ||
| const model = req.body.model; | ||
|
|
||
| res.json({ | ||
| id: 'msg_react_agent_123', | ||
| type: 'message', | ||
| role: 'assistant', | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: 'Paris is the capital of France.', | ||
| }, | ||
| ], | ||
| model: model, | ||
| stop_reason: 'end_turn', | ||
| stop_sequence: null, | ||
| usage: { | ||
| input_tokens: 20, | ||
| output_tokens: 10, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| return new Promise(resolve => { | ||
| const server = app.listen(0, () => { | ||
| resolve(server); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
| async function run() { | ||
| const server = await startMockAnthropicServer(); | ||
| const baseUrl = `http://localhost:${server.address().port}`; | ||
|
|
||
| await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { | ||
| const llm = new ChatAnthropic({ | ||
| model: 'claude-3-5-sonnet-20241022', | ||
| apiKey: 'mock-api-key', | ||
| clientOptions: { | ||
| baseURL: baseUrl, | ||
| }, | ||
| }); | ||
|
|
||
| const agent = createReactAgent({ llm, tools: [], name: 'helpful_assistant' }); | ||
|
|
||
| await agent.invoke({ | ||
| messages: [new SystemMessage('You are a helpful assistant.'), new HumanMessage('What is the capital of France?')], | ||
| }); | ||
| }); | ||
|
|
||
| await Sentry.flush(2000); | ||
| server.close(); | ||
| } | ||
|
|
||
| run(); |
122 changes: 122 additions & 0 deletions
122
dev-packages/node-integration-tests/suites/tracing/langgraph/agent-tools-scenario.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| import { tool } from '@langchain/core/tools'; | ||
| import { ChatAnthropic } from '@langchain/anthropic'; | ||
| import { createReactAgent } from '@langchain/langgraph/prebuilt'; | ||
| import { HumanMessage } from '@langchain/core/messages'; | ||
| import * as Sentry from '@sentry/node'; | ||
| import express from 'express'; | ||
| import { z } from 'zod'; | ||
|
|
||
| let callCount = 0; | ||
|
|
||
| function startMockAnthropicServer() { | ||
| const app = express(); | ||
| app.use(express.json()); | ||
|
|
||
| app.post('/v1/messages', (req, res) => { | ||
| callCount++; | ||
| const model = req.body.model; | ||
|
|
||
| if (callCount === 1) { | ||
| // First call: model decides to call the "add" tool | ||
| res.json({ | ||
| id: 'msg_1', | ||
| type: 'message', | ||
| role: 'assistant', | ||
| content: [ | ||
| { | ||
| type: 'tool_use', | ||
| id: 'toolu_add_1', | ||
| name: 'add', | ||
| input: { a: 3, b: 5 }, | ||
| }, | ||
| ], | ||
| model: model, | ||
| stop_reason: 'tool_use', | ||
| usage: { input_tokens: 20, output_tokens: 10 }, | ||
| }); | ||
| } else if (callCount === 2) { | ||
| // Second call: model sees add result=8, calls "multiply" | ||
| res.json({ | ||
| id: 'msg_2', | ||
| type: 'message', | ||
| role: 'assistant', | ||
| content: [ | ||
| { | ||
| type: 'tool_use', | ||
| id: 'toolu_mul_1', | ||
| name: 'multiply', | ||
| input: { a: 8, b: 4 }, | ||
| }, | ||
| ], | ||
| model: model, | ||
| stop_reason: 'tool_use', | ||
| usage: { input_tokens: 30, output_tokens: 10 }, | ||
| }); | ||
| } else { | ||
| // Third call: model returns final answer | ||
| res.json({ | ||
| id: 'msg_3', | ||
| type: 'message', | ||
| role: 'assistant', | ||
| content: [{ type: 'text', text: 'The result is 32.' }], | ||
| model: model, | ||
| stop_reason: 'end_turn', | ||
| usage: { input_tokens: 40, output_tokens: 10 }, | ||
| }); | ||
| } | ||
| }); | ||
|
|
||
| return new Promise(resolve => { | ||
| const server = app.listen(0, () => resolve(server)); | ||
| }); | ||
| } | ||
|
|
||
| async function run() { | ||
| const server = await startMockAnthropicServer(); | ||
| const baseUrl = `http://localhost:${server.address().port}`; | ||
|
|
||
| await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { | ||
| const llm = new ChatAnthropic({ | ||
| model: 'claude-3-5-sonnet-20241022', | ||
| apiKey: 'mock-api-key', | ||
| clientOptions: { baseURL: baseUrl }, | ||
| }); | ||
|
|
||
| const addTool = tool( | ||
| async ({ a, b }) => { | ||
| return String(a + b); | ||
| }, | ||
| { | ||
| name: 'add', | ||
| description: 'Add two numbers', | ||
| schema: z.object({ a: z.number(), b: z.number() }), | ||
| }, | ||
| ); | ||
|
|
||
| const multiplyTool = tool( | ||
| async ({ a, b }) => { | ||
| return String(a * b); | ||
| }, | ||
| { | ||
| name: 'multiply', | ||
| description: 'Multiply two numbers', | ||
| schema: z.object({ a: z.number(), b: z.number() }), | ||
| }, | ||
| ); | ||
|
|
||
| const agent = createReactAgent({ | ||
| llm, | ||
| tools: [addTool, multiplyTool], | ||
| name: 'math_assistant', | ||
| }); | ||
|
|
||
| await agent.invoke({ | ||
| messages: [new HumanMessage('Calculate (3 + 5) * 4')], | ||
| }); | ||
| }); | ||
|
|
||
| await Sentry.flush(2000); | ||
| server.close(); | ||
| } | ||
|
|
||
| run(); |
17 changes: 17 additions & 0 deletions
17
dev-packages/node-integration-tests/suites/tracing/langgraph/instrument-agent.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { loggingTransport } from '@sentry-internal/node-integration-tests'; | ||
|
|
||
| Sentry.init({ | ||
| dsn: 'https://public@dsn.ingest.sentry.io/1337', | ||
| release: '1.0', | ||
| tracesSampleRate: 1.0, | ||
| sendDefaultPii: true, | ||
| transport: loggingTransport, | ||
| beforeSendTransaction: event => { | ||
| // Filter out mock express server transactions | ||
| if (event.transaction && event.transaction.includes('/v1/messages')) { | ||
| return null; | ||
| } | ||
| return event; | ||
| }, | ||
| }); |
56 changes: 56 additions & 0 deletions
56
dev-packages/node-integration-tests/suites/tracing/langgraph/scenario-stategraph-chat.mjs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| import { ChatAnthropic } from '@langchain/anthropic'; | ||
| import { END, MessagesAnnotation, START, StateGraph } from '@langchain/langgraph'; | ||
| import * as Sentry from '@sentry/node'; | ||
| import express from 'express'; | ||
|
|
||
| function startMockAnthropicServer() { | ||
| const app = express(); | ||
| app.use(express.json()); | ||
|
|
||
| app.post('/v1/messages', (req, res) => { | ||
| res.json({ | ||
| id: 'msg_stategraph_chat_1', | ||
| type: 'message', | ||
| role: 'assistant', | ||
| content: [{ type: 'text', text: 'Hello from mock.' }], | ||
| model: req.body.model, | ||
| stop_reason: 'end_turn', | ||
| usage: { input_tokens: 5, output_tokens: 3 }, | ||
| }); | ||
| }); | ||
|
|
||
| return new Promise(resolve => { | ||
| const server = app.listen(0, () => resolve(server)); | ||
| }); | ||
| } | ||
|
|
||
| async function run() { | ||
| const server = await startMockAnthropicServer(); | ||
| const baseUrl = `http://localhost:${server.address().port}`; | ||
|
|
||
| await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { | ||
| const llm = new ChatAnthropic({ | ||
| model: 'claude-3-5-sonnet-20241022', | ||
| apiKey: 'mock-api-key', | ||
| clientOptions: { baseURL: baseUrl }, | ||
| }); | ||
|
|
||
| const callLlm = async state => { | ||
| const response = await llm.invoke(state.messages); | ||
| return { messages: [response] }; | ||
| }; | ||
|
|
||
| const graph = new StateGraph(MessagesAnnotation) | ||
| .addNode('agent', callLlm) | ||
| .addEdge(START, 'agent') | ||
| .addEdge('agent', END) | ||
| .compile({ name: 'plain_assistant' }); | ||
|
|
||
| await graph.invoke({ messages: [{ role: 'user', content: 'Hi.' }] }); | ||
| }); | ||
|
|
||
| await Sentry.flush(2000); | ||
| server.close(); | ||
| } | ||
|
|
||
| run(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.