-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat(cloudflare/vercel-edge): Add manual instrumentation for LangGraph #18112
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
+196
−2
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
66 changes: 66 additions & 0 deletions
66
dev-packages/cloudflare-integration-tests/suites/tracing/langgraph/index.ts
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,66 @@ | ||
| import { END, MessagesAnnotation, START, StateGraph } from '@langchain/langgraph'; | ||
| import * as Sentry from '@sentry/cloudflare'; | ||
|
|
||
| interface Env { | ||
| SENTRY_DSN: string; | ||
| } | ||
|
|
||
| export default Sentry.withSentry( | ||
| (env: Env) => ({ | ||
| dsn: env.SENTRY_DSN, | ||
| tracesSampleRate: 1.0, | ||
| sendDefaultPii: true, | ||
| }), | ||
| { | ||
| async fetch(_request, _env, _ctx) { | ||
| // Define simple mock LLM function | ||
| const mockLlm = (): { | ||
| messages: { | ||
| role: string; | ||
| content: string; | ||
| response_metadata: { | ||
| model_name: string; | ||
| finish_reason: string; | ||
| tokenUsage: { promptTokens: number; completionTokens: number; totalTokens: number }; | ||
| }; | ||
| tool_calls: never[]; | ||
| }[]; | ||
| } => { | ||
| return { | ||
| messages: [ | ||
| { | ||
| role: 'assistant', | ||
| content: 'Mock response from LangGraph agent', | ||
| response_metadata: { | ||
| model_name: 'mock-model', | ||
| finish_reason: 'stop', | ||
| tokenUsage: { | ||
| promptTokens: 20, | ||
| completionTokens: 10, | ||
| totalTokens: 30, | ||
| }, | ||
| }, | ||
| tool_calls: [], | ||
| }, | ||
| ], | ||
| }; | ||
| }; | ||
|
|
||
| // Create and instrument the graph | ||
| const graph = new StateGraph(MessagesAnnotation) | ||
| .addNode('agent', mockLlm) | ||
| .addEdge(START, 'agent') | ||
| .addEdge('agent', END); | ||
|
|
||
| Sentry.instrumentLangGraph(graph, { recordInputs: true, recordOutputs: true }); | ||
|
|
||
| const compiled = graph.compile({ name: 'weather_assistant' }); | ||
|
|
||
| await compiled.invoke({ | ||
| messages: [{ role: 'user', content: 'What is the weather in SF?' }], | ||
| }); | ||
|
|
||
| return new Response(JSON.stringify({ success: true })); | ||
| }, | ||
| }, | ||
| ); |
59 changes: 59 additions & 0 deletions
59
dev-packages/cloudflare-integration-tests/suites/tracing/langgraph/test.ts
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,59 @@ | ||
| import { expect, it } from 'vitest'; | ||
| import { createRunner } from '../../../runner'; | ||
|
|
||
| // These tests are not exhaustive because the instrumentation is | ||
| // already tested in the node integration tests and we merely | ||
| // want to test that the instrumentation does not break in our | ||
| // cloudflare SDK. | ||
|
|
||
| it('traces langgraph compile and invoke operations', async ({ signal }) => { | ||
| const runner = createRunner(__dirname) | ||
| .ignore('event') | ||
| .expect(envelope => { | ||
| const transactionEvent = envelope[1]?.[0]?.[1] as any; | ||
|
|
||
| expect(transactionEvent.transaction).toBe('GET /'); | ||
|
|
||
| // Check create_agent span | ||
| const createAgentSpan = transactionEvent.spans.find((span: any) => span.op === 'gen_ai.create_agent'); | ||
| expect(createAgentSpan).toMatchObject({ | ||
| data: { | ||
| 'gen_ai.operation.name': 'create_agent', | ||
| 'sentry.op': 'gen_ai.create_agent', | ||
| 'sentry.origin': 'auto.ai.langgraph', | ||
| 'gen_ai.agent.name': 'weather_assistant', | ||
| }, | ||
| description: 'create_agent weather_assistant', | ||
| op: 'gen_ai.create_agent', | ||
| origin: 'auto.ai.langgraph', | ||
| }); | ||
|
|
||
| // Check invoke_agent span | ||
| const invokeAgentSpan = transactionEvent.spans.find((span: any) => span.op === 'gen_ai.invoke_agent'); | ||
| expect(invokeAgentSpan).toMatchObject({ | ||
| data: expect.objectContaining({ | ||
| 'gen_ai.operation.name': 'invoke_agent', | ||
| 'sentry.op': 'gen_ai.invoke_agent', | ||
| 'sentry.origin': 'auto.ai.langgraph', | ||
| 'gen_ai.agent.name': 'weather_assistant', | ||
| 'gen_ai.pipeline.name': 'weather_assistant', | ||
| 'gen_ai.request.messages': '[{"role":"user","content":"What is the weather in SF?"}]', | ||
| 'gen_ai.response.model': 'mock-model', | ||
| 'gen_ai.usage.input_tokens': 20, | ||
| 'gen_ai.usage.output_tokens': 10, | ||
| 'gen_ai.usage.total_tokens': 30, | ||
| }), | ||
| description: 'invoke_agent weather_assistant', | ||
| op: 'gen_ai.invoke_agent', | ||
| origin: 'auto.ai.langgraph', | ||
| }); | ||
|
|
||
| // Verify tools are captured | ||
| if (invokeAgentSpan.data['gen_ai.request.available_tools']) { | ||
| expect(invokeAgentSpan.data['gen_ai.request.available_tools']).toMatch(/get_weather/); | ||
| } | ||
| }) | ||
| .start(signal); | ||
| await runner.makeRequest('get', '/'); | ||
| await runner.completed(); | ||
| }); | ||
6 changes: 6 additions & 0 deletions
6
dev-packages/cloudflare-integration-tests/suites/tracing/langgraph/wrangler.jsonc
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,6 @@ | ||
| { | ||
| "name": "worker-name", | ||
| "compatibility_date": "2025-06-17", | ||
| "main": "index.ts", | ||
| "compatibility_flags": ["nodejs_compat"], | ||
| } |
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
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.
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.