Skip to content

Commit efb6037

Browse files
committed
feat(cloudflare/vercel-edge): Add manual instrumentation for LangGraph
1 parent f5e94f4 commit efb6037

File tree

9 files changed

+210
-4
lines changed

9 files changed

+210
-4
lines changed

dev-packages/cloudflare-integration-tests/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"test:watch": "yarn test --watch"
1414
},
1515
"dependencies": {
16+
"@langchain/langgraph": "^1.0.1",
1617
"@sentry/cloudflare": "10.25.0"
1718
},
1819
"devDependencies": {
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { END, MessagesAnnotation, START, StateGraph } from '@langchain/langgraph';
2+
import * as Sentry from '@sentry/cloudflare';
3+
4+
interface Env {
5+
SENTRY_DSN: string;
6+
}
7+
8+
export default Sentry.withSentry(
9+
(env: Env) => ({
10+
dsn: env.SENTRY_DSN,
11+
tracesSampleRate: 1.0,
12+
sendDefaultPii: true,
13+
}),
14+
{
15+
async fetch(_request, _env, _ctx) {
16+
// Define simple mock LLM function
17+
const mockLlm = (): {
18+
messages: {
19+
role: string;
20+
content: string;
21+
response_metadata: {
22+
model_name: string;
23+
finish_reason: string;
24+
tokenUsage: { promptTokens: number; completionTokens: number; totalTokens: number };
25+
};
26+
tool_calls: never[];
27+
}[];
28+
} => {
29+
return {
30+
messages: [
31+
{
32+
role: 'assistant',
33+
content: 'Mock response from LangGraph agent',
34+
response_metadata: {
35+
model_name: 'mock-model',
36+
finish_reason: 'stop',
37+
tokenUsage: {
38+
promptTokens: 20,
39+
completionTokens: 10,
40+
totalTokens: 30,
41+
},
42+
},
43+
tool_calls: [],
44+
},
45+
],
46+
};
47+
};
48+
49+
// Create and instrument the graph
50+
const graph = new StateGraph(MessagesAnnotation)
51+
.addNode('agent', mockLlm)
52+
.addEdge(START, 'agent')
53+
.addEdge('agent', END);
54+
55+
Sentry.instrumentLangGraph(graph, { recordInputs: true, recordOutputs: true });
56+
57+
const compiled = graph.compile({ name: 'weather_assistant' });
58+
59+
await compiled.invoke({
60+
messages: [{ role: 'user', content: 'What is the weather in SF?' }],
61+
});
62+
63+
return new Response(JSON.stringify({ success: true }));
64+
},
65+
},
66+
);
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { expect, it } from 'vitest';
2+
import { createRunner } from '../../../runner';
3+
4+
// These tests are not exhaustive because the instrumentation is
5+
// already tested in the node integration tests and we merely
6+
// want to test that the instrumentation does not break in our
7+
// cloudflare SDK.
8+
9+
it('traces langgraph compile and invoke operations', async ({ signal }) => {
10+
const runner = createRunner(__dirname)
11+
.ignore('event')
12+
.expect(envelope => {
13+
const transactionEvent = envelope[1]?.[0]?.[1] as any;
14+
15+
expect(transactionEvent.transaction).toBe('GET /');
16+
17+
// Check create_agent span
18+
const createAgentSpan = transactionEvent.spans.find((span: any) => span.op === 'gen_ai.create_agent');
19+
expect(createAgentSpan).toMatchObject({
20+
data: {
21+
'gen_ai.operation.name': 'create_agent',
22+
'sentry.op': 'gen_ai.create_agent',
23+
'sentry.origin': 'auto.ai.langgraph',
24+
'gen_ai.agent.name': 'weather_assistant',
25+
},
26+
description: 'create_agent weather_assistant',
27+
op: 'gen_ai.create_agent',
28+
origin: 'auto.ai.langgraph',
29+
});
30+
31+
// Check invoke_agent span
32+
const invokeAgentSpan = transactionEvent.spans.find((span: any) => span.op === 'gen_ai.invoke_agent');
33+
expect(invokeAgentSpan).toMatchObject({
34+
data: expect.objectContaining({
35+
'gen_ai.operation.name': 'invoke_agent',
36+
'sentry.op': 'gen_ai.invoke_agent',
37+
'sentry.origin': 'auto.ai.langgraph',
38+
'gen_ai.agent.name': 'weather_assistant',
39+
'gen_ai.pipeline.name': 'weather_assistant',
40+
'gen_ai.request.messages': '[{"role":"user","content":"What is the weather in SF?"}]',
41+
'gen_ai.response.model': 'mock-model',
42+
'gen_ai.usage.input_tokens': 20,
43+
'gen_ai.usage.output_tokens': 10,
44+
'gen_ai.usage.total_tokens': 30,
45+
}),
46+
description: 'invoke_agent weather_assistant',
47+
op: 'gen_ai.invoke_agent',
48+
origin: 'auto.ai.langgraph',
49+
});
50+
51+
// Verify tools are captured
52+
if (invokeAgentSpan.data['gen_ai.request.available_tools']) {
53+
expect(invokeAgentSpan.data['gen_ai.request.available_tools']).toMatch(/get_weather/);
54+
}
55+
})
56+
.start(signal);
57+
await runner.makeRequest('get', '/');
58+
await runner.completed();
59+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"name": "worker-name",
3+
"compatibility_date": "2025-06-17",
4+
"main": "index.ts",
5+
"compatibility_flags": ["nodejs_compat"]
6+
}
7+

packages/cloudflare/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export type {
2222
export type { CloudflareOptions } from './client';
2323

2424
export {
25+
instrumentLangGraph,
2526
addEventProcessor,
2627
addBreadcrumb,
2728
addIntegration,

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 } from './tracing/langgraph';
155+
export { instrumentStateGraphCompile, 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: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { getClient } from '../../currentScopes';
12
import { captureException } from '../../exports';
23
import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '../../semanticAttributes';
34
import { SPAN_STATUS_ERROR } from '../../tracing';
@@ -17,6 +18,51 @@ import { LANGGRAPH_ORIGIN } from './constants';
1718
import type { CompiledGraph, LangGraphOptions } from './types';
1819
import { extractToolsFromCompiledGraph, setResponseAttributes } from './utils';
1920

21+
/**
22+
* Directly instruments a StateGraph instance to add tracing spans
23+
*
24+
* This function can be used to manually instrument LangGraph StateGraph instances
25+
* in environments where automatic instrumentation is not available or desired.
26+
*
27+
* @param stateGraph - The StateGraph instance to instrument
28+
* @param options - Optional configuration for recording inputs/outputs
29+
*
30+
* @example
31+
* ```typescript
32+
* import { instrumentLangGraph } from '@sentry/cloudflare';
33+
* import { StateGraph } from '@langchain/langgraph';
34+
*
35+
* const graph = new StateGraph(MessagesAnnotation)
36+
* .addNode('agent', mockLlm)
37+
* .addEdge(START, 'agent')
38+
* .addEdge('agent', END);
39+
*
40+
* instrumentLangGraph(graph, { recordInputs: true, recordOutputs: true });
41+
* const compiled = graph.compile({ name: 'my_agent' });
42+
* ```
43+
*/
44+
export function instrumentLangGraph(
45+
stateGraph: { compile?: (...args: unknown[]) => CompiledGraph },
46+
options: LangGraphOptions = {},
47+
): void {
48+
const client = getClient();
49+
const clientOptions = client?.getOptions();
50+
const defaultPii = Boolean(clientOptions?.sendDefaultPii);
51+
52+
const recordInputs = options.recordInputs ?? defaultPii;
53+
const recordOutputs = options.recordOutputs ?? defaultPii;
54+
55+
const instrumentationOptions: LangGraphOptions = {
56+
recordInputs,
57+
recordOutputs,
58+
};
59+
60+
if (stateGraph.compile && typeof stateGraph.compile === 'function') {
61+
const originalCompile = stateGraph.compile;
62+
stateGraph.compile = instrumentStateGraphCompile(originalCompile, instrumentationOptions);
63+
}
64+
}
65+
2066
/**
2167
* Instruments StateGraph's compile method to create spans for agent creation and invocation
2268
*

packages/vercel-edge/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ export {
7171
// eslint-disable-next-line deprecation/deprecation
7272
inboundFiltersIntegration,
7373
instrumentOpenAiClient,
74+
instrumentLangGraph,
7475
instrumentGoogleGenAIClient,
7576
instrumentAnthropicAiClient,
7677
eventFiltersIntegration,

yarn.lock

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4931,6 +4931,13 @@
49314931
zod "^3.25.32"
49324932
zod-to-json-schema "^3.22.3"
49334933

4934+
"@langchain/langgraph-checkpoint@^1.0.0":
4935+
version "1.0.0"
4936+
resolved "https://registry.npmjs.org/@langchain/langgraph-checkpoint/-/langgraph-checkpoint-1.0.0.tgz#ece2ede439d0d0b0b532c4be7817fd5029afe4f8"
4937+
integrity sha512-xrclBGvNCXDmi0Nz28t3vjpxSH6UYx6w5XAXSiiB1WEdc2xD2iY/a913I3x3a31XpInUW/GGfXXfePfaghV54A==
4938+
dependencies:
4939+
uuid "^10.0.0"
4940+
49344941
"@langchain/langgraph-checkpoint@~0.0.17":
49354942
version "0.0.18"
49364943
resolved "https://registry.npmjs.org/@langchain/langgraph-checkpoint/-/langgraph-checkpoint-0.0.18.tgz#2f7a9cdeda948ccc8d312ba9463810709d71d0b8"
@@ -4948,6 +4955,15 @@
49484955
p-retry "4"
49494956
uuid "^9.0.0"
49504957

4958+
"@langchain/langgraph-sdk@~1.0.0":
4959+
version "1.0.0"
4960+
resolved "https://registry.npmjs.org/@langchain/langgraph-sdk/-/langgraph-sdk-1.0.0.tgz#16faca6cc426432dee9316428d0aecd94e5b7989"
4961+
integrity sha512-g25ti2W7Dl5wUPlNK+0uIGbeNFqf98imhHlbdVVKTTkDYLhi/pI1KTgsSSkzkeLuBIfvt2b0q6anQwCs7XBlbw==
4962+
dependencies:
4963+
p-queue "^6.6.2"
4964+
p-retry "4"
4965+
uuid "^9.0.0"
4966+
49514967
"@langchain/langgraph@^0.2.32":
49524968
version "0.2.74"
49534969
resolved "https://registry.npmjs.org/@langchain/langgraph/-/langgraph-0.2.74.tgz#37367a1e8bafda3548037a91449a69a84f285def"
@@ -4958,6 +4974,15 @@
49584974
uuid "^10.0.0"
49594975
zod "^3.23.8"
49604976

4977+
"@langchain/langgraph@^1.0.1":
4978+
version "1.0.1"
4979+
resolved "https://registry.npmjs.org/@langchain/langgraph/-/langgraph-1.0.1.tgz#d0be714653e8a27665f86ea795c5c34189455406"
4980+
integrity sha512-7y8OTDLrHrpJ55Y5x7c7zU2BbqNllXwxM106Xrd+NaQB5CpEb4hbUfIwe4XmhhscKPwvhXAq3tjeUxw9MCiurQ==
4981+
dependencies:
4982+
"@langchain/langgraph-checkpoint" "^1.0.0"
4983+
"@langchain/langgraph-sdk" "~1.0.0"
4984+
uuid "^10.0.0"
4985+
49614986
"@leichtgewicht/ip-codec@^2.0.1":
49624987
version "2.0.4"
49634988
resolved "https://registry.yarnpkg.com/@leichtgewicht/ip-codec/-/ip-codec-2.0.4.tgz#b2ac626d6cb9c8718ab459166d4bb405b8ffa78b"
@@ -28530,7 +28555,7 @@ string-template@~0.2.1:
2853028555

2853128556
[email protected], "string-width@^1.0.2 || 2 || 3 || 4", string-width@^4.1.0, string-width@^4.2.0, string-width@^4.2.3:
2853228557
version "4.2.3"
28533-
resolved "https://registry.yarnpkg.com/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
28558+
resolved "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz#269c7117d27b05ad2e536830a8ec895ef9c6d010"
2853428559
integrity sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==
2853528560
dependencies:
2853628561
emoji-regex "^8.0.0"
@@ -28640,7 +28665,7 @@ stringify-object@^3.2.1:
2864028665

2864128666
[email protected], strip-ansi@^6.0.0, strip-ansi@^6.0.1:
2864228667
version "6.0.1"
28643-
resolved "https://registry.yarnpkg.com/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
28668+
resolved "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz#9e26c63d30f53443e9489495b2105d37b67a85d9"
2864428669
integrity sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==
2864528670
dependencies:
2864628671
ansi-regex "^5.0.1"
@@ -31635,7 +31660,7 @@ [email protected]:
3163531660

3163631661
[email protected], wrap-ansi@^7.0.0:
3163731662
version "7.0.0"
31638-
resolved "https://registry.yarnpkg.com/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
31663+
resolved "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-7.0.0.tgz#67e145cff510a6a6984bdf1152911d69d2eb9e43"
3163931664
integrity sha512-YVGIj2kamLSTxw6NsZjoBxfSwsn0ycdesmc4p+Q21c5zPuZ1pl+NfxVdxPtdHvmNVOQ6XSYG4AUtyt/Fi7D16Q==
3164031665
dependencies:
3164131666
ansi-styles "^4.0.0"

0 commit comments

Comments
 (0)