Skip to content

Commit cc6e9cc

Browse files
committed
feat(deno): add langgraph integration
1 parent 5cf5d89 commit cc6e9cc

4 files changed

Lines changed: 115 additions & 0 deletions

File tree

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
// <reference lib="deno.ns" />
2+
3+
import { tracingChannel } from 'node:diagnostics_channel';
4+
import type { TransactionEvent } from '@sentry/core';
5+
import type { DenoClient } from '@sentry/deno';
6+
import { getCurrentScope, getGlobalScope, getIsolationScope, init, startSpan } from '@sentry/deno';
7+
import { assert } from 'https://deno.land/std@0.212.0/assert/assert.ts';
8+
import { assertExists } from 'https://deno.land/std@0.212.0/assert/assert_exists.ts';
9+
import { assertEquals } from 'https://deno.land/std@0.212.0/assert/assert_equals.ts';
10+
11+
function resetGlobals(): void {
12+
getCurrentScope().clear();
13+
getCurrentScope().setClient(undefined);
14+
getIsolationScope().clear();
15+
getGlobalScope().clear();
16+
}
17+
18+
/** See deno-redis.test.ts — same sink shape, deduped for clarity. */
19+
function transactionSink(): {
20+
beforeSendTransaction: (event: TransactionEvent) => null;
21+
waitFor: (predicate: (event: TransactionEvent) => boolean) => Promise<TransactionEvent>;
22+
} {
23+
const transactions: TransactionEvent[] = [];
24+
const waiters: { predicate: (e: TransactionEvent) => boolean; resolve: (e: TransactionEvent) => void }[] = [];
25+
return {
26+
beforeSendTransaction(event) {
27+
transactions.push(event);
28+
for (let i = waiters.length - 1; i >= 0; i--) {
29+
const w = waiters[i]!;
30+
if (w.predicate(event)) {
31+
waiters.splice(i, 1);
32+
w.resolve(event);
33+
}
34+
}
35+
return null;
36+
},
37+
waitFor(predicate) {
38+
const already = transactions.find(predicate);
39+
if (already) return Promise.resolve(already);
40+
return new Promise<TransactionEvent>(resolve => {
41+
waiters.push({ predicate, resolve });
42+
});
43+
},
44+
};
45+
}
46+
47+
function withTimeout<T>(p: Promise<T>, ms: number, what: string): Promise<T> {
48+
let timer: ReturnType<typeof setTimeout> | undefined;
49+
const timeout = new Promise<T>((_, reject) => {
50+
timer = setTimeout(() => reject(new Error(`Timed out waiting for ${what} after ${ms}ms`)), ms);
51+
});
52+
return Promise.race([p, timeout]).finally(() => {
53+
if (timer !== undefined) clearTimeout(timer);
54+
});
55+
}
56+
57+
Deno.test('langgraph instrumentation: included in default integrations (Deno 2.8.0+)', () => {
58+
resetGlobals();
59+
const client = init({ dsn: 'https://username@domain/123' }) as DenoClient;
60+
const names = client.getOptions().integrations.map(i => i.name);
61+
assert(names.includes('LangGraph'), `LangGraph should be in defaults, got ${names.join(', ')}`);
62+
});
63+
64+
// Drives the `orchestrion:@langchain/langgraph:stateGraphCompile` channel — the same
65+
// events the orchestrion transform publishes around `StateGraph.compile` — so no live
66+
// LangGraph call is needed. The subscriber reads the agent name off the compile
67+
// options (`arguments[0]`) and opens a `gen_ai.create_agent` span. (The
68+
// `createReactAgent` channel only wraps tools/invoke and opens no span of its own, so
69+
// the compile channel is the span-bound path to drive.)
70+
Deno.test('langgraph instrumentation: orchestrion stateGraphCompile channel produces a nested create_agent span', async () => {
71+
resetGlobals();
72+
const sink = transactionSink();
73+
init({
74+
dsn: 'https://username@domain/123',
75+
tracesSampleRate: 1,
76+
beforeSendTransaction: sink.beforeSendTransaction,
77+
});
78+
79+
const channel = tracingChannel('orchestrion:@langchain/langgraph:stateGraphCompile');
80+
81+
// `arguments[0]` is the compile options; `name` names the agent span.
82+
const ctx: Record<string, unknown> = { arguments: [{ name: 'my-agent' }] };
83+
84+
startSpan({ name: 'parent', op: 'test' }, () => {
85+
channel.start.runStores(ctx, () => undefined);
86+
// The compiled graph result has no `invoke`, so the `beforeSpanEnd` wrapper is a
87+
// no-op; the span still ends normally.
88+
ctx.result = {};
89+
channel.end.publish(ctx);
90+
channel.asyncEnd.publish(ctx);
91+
});
92+
93+
const parent = await withTimeout(
94+
sink.waitFor(t => t.transaction === 'parent'),
95+
5000,
96+
"'parent' transaction",
97+
);
98+
99+
const aiSpan = parent.spans?.find(s => s.op === 'gen_ai.create_agent');
100+
assertExists(
101+
aiSpan,
102+
`expected a gen_ai.create_agent child span, got ops: ${parent.spans?.map(s => s.op).join(', ')}`,
103+
);
104+
assertEquals(aiSpan!.description, 'create_agent my-agent');
105+
assertEquals(aiSpan!.data?.['gen_ai.operation.name'], 'create_agent');
106+
assertEquals(aiSpan!.data?.['gen_ai.agent.name'], 'my-agent');
107+
assertEquals(aiSpan!.data?.['sentry.origin'], 'auto.ai.langgraph');
108+
});

packages/deno/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ export {
130130
knexChannelIntegration,
131131
koaChannelIntegration,
132132
langChainChannelIntegration,
133+
langGraphChannelIntegration,
133134
lruMemoizerChannelIntegration,
134135
mongodbChannelIntegration,
135136
mongooseChannelIntegration,

packages/deno/src/sdk.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import {
2424
kafkajsChannelIntegration,
2525
koaChannelIntegration,
2626
langChainChannelIntegration,
27+
langGraphChannelIntegration,
2728
lruMemoizerChannelIntegration,
2829
mongodbChannelIntegration,
2930
mongooseChannelIntegration,
@@ -109,6 +110,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
109110
kafkajsChannelIntegration(),
110111
koaChannelIntegration(),
111112
langChainChannelIntegration(),
113+
langGraphChannelIntegration(),
112114
lruMemoizerChannelIntegration(),
113115
mongodbChannelIntegration(),
114116
mongooseChannelIntegration(),

packages/deno/test/__snapshots__/mod.test.ts.snap

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ snapshot[`captureException 1`] = `
128128
"Kafka",
129129
"Koa",
130130
"LangChain",
131+
"LangGraph",
131132
"LruMemoizer",
132133
"Mongo",
133134
"Mongoose",
@@ -225,6 +226,7 @@ snapshot[`captureMessage 1`] = `
225226
"Kafka",
226227
"Koa",
227228
"LangChain",
229+
"LangGraph",
228230
"LruMemoizer",
229231
"Mongo",
230232
"Mongoose",
@@ -329,6 +331,7 @@ snapshot[`captureMessage twice 1`] = `
329331
"Kafka",
330332
"Koa",
331333
"LangChain",
334+
"LangGraph",
332335
"LruMemoizer",
333336
"Mongo",
334337
"Mongoose",
@@ -440,6 +443,7 @@ snapshot[`captureMessage twice 2`] = `
440443
"Kafka",
441444
"Koa",
442445
"LangChain",
446+
"LangGraph",
443447
"LruMemoizer",
444448
"Mongo",
445449
"Mongoose",

0 commit comments

Comments
 (0)