Skip to content

Commit bdf5a28

Browse files
committed
feat(deno): add langchain integration
1 parent 70147fc commit bdf5a28

4 files changed

Lines changed: 107 additions & 0 deletions

File tree

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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('langchain 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('LangChain'), `LangChain should be in defaults, got ${names.join(', ')}`);
62+
});
63+
64+
Deno.test('langchain instrumentation: orchestrion @langchain/openai:embedQuery channel produces a nested embeddings span', async () => {
65+
resetGlobals();
66+
const sink = transactionSink();
67+
init({
68+
dsn: 'https://username@domain/123',
69+
tracesSampleRate: 1,
70+
beforeSendTransaction: sink.beforeSendTransaction,
71+
});
72+
73+
const channel = tracingChannel('orchestrion:@langchain/openai:embedQuery');
74+
75+
// `self` is the embeddings instance: its constructor name infers the provider
76+
// system and `model` names the span. `arguments[0]` is the text to embed.
77+
const self = { constructor: { name: 'OpenAIEmbeddings' }, model: 'text-embedding-3-small' };
78+
const ctx: Record<string, unknown> = { self, arguments: ['hello world'] };
79+
80+
startSpan({ name: 'parent', op: 'test' }, () => {
81+
channel.start.runStores(ctx, () => undefined);
82+
channel.end.publish(ctx);
83+
ctx.result = [0.1, 0.2, 0.3];
84+
channel.asyncEnd.publish(ctx);
85+
});
86+
87+
const parent = await withTimeout(
88+
sink.waitFor(t => t.transaction === 'parent'),
89+
5000,
90+
"'parent' transaction",
91+
);
92+
93+
const aiSpan = parent.spans?.find(s => s.op === 'gen_ai.embeddings');
94+
assertExists(aiSpan, `expected a gen_ai.embeddings child span, got ops: ${parent.spans?.map(s => s.op).join(', ')}`);
95+
assertEquals(aiSpan!.description, 'embeddings text-embedding-3-small');
96+
assertEquals(aiSpan!.data?.['gen_ai.system'], 'openai');
97+
assertEquals(aiSpan!.data?.['gen_ai.operation.name'], 'embeddings');
98+
assertEquals(aiSpan!.data?.['gen_ai.request.model'], 'text-embedding-3-small');
99+
assertEquals(aiSpan!.data?.['sentry.origin'], 'auto.ai.langchain');
100+
});

packages/deno/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ export {
129129
kafkajsChannelIntegration,
130130
knexChannelIntegration,
131131
koaChannelIntegration,
132+
langChainChannelIntegration,
132133
lruMemoizerChannelIntegration,
133134
mongodbChannelIntegration,
134135
mongooseChannelIntegration,

packages/deno/src/sdk.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
hapiChannelIntegration,
2424
kafkajsChannelIntegration,
2525
koaChannelIntegration,
26+
langChainChannelIntegration,
2627
lruMemoizerChannelIntegration,
2728
mongodbChannelIntegration,
2829
mongooseChannelIntegration,
@@ -107,6 +108,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
107108
hapiChannelIntegration(),
108109
kafkajsChannelIntegration(),
109110
koaChannelIntegration(),
111+
langChainChannelIntegration(),
110112
lruMemoizerChannelIntegration(),
111113
mongodbChannelIntegration(),
112114
mongooseChannelIntegration(),

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ snapshot[`captureException 1`] = `
127127
"Hapi",
128128
"Kafka",
129129
"Koa",
130+
"LangChain",
130131
"LruMemoizer",
131132
"Mongo",
132133
"Mongoose",
@@ -223,6 +224,7 @@ snapshot[`captureMessage 1`] = `
223224
"Hapi",
224225
"Kafka",
225226
"Koa",
227+
"LangChain",
226228
"LruMemoizer",
227229
"Mongo",
228230
"Mongoose",
@@ -326,6 +328,7 @@ snapshot[`captureMessage twice 1`] = `
326328
"Hapi",
327329
"Kafka",
328330
"Koa",
331+
"LangChain",
329332
"LruMemoizer",
330333
"Mongo",
331334
"Mongoose",
@@ -436,6 +439,7 @@ snapshot[`captureMessage twice 2`] = `
436439
"Hapi",
437440
"Kafka",
438441
"Koa",
442+
"LangChain",
439443
"LruMemoizer",
440444
"Mongo",
441445
"Mongoose",

0 commit comments

Comments
 (0)