diff --git a/dev-packages/deno-integration-tests/suites/orchestrion-langchain/test.ts b/dev-packages/deno-integration-tests/suites/orchestrion-langchain/test.ts new file mode 100644 index 000000000000..2e64c059ac6c --- /dev/null +++ b/dev-packages/deno-integration-tests/suites/orchestrion-langchain/test.ts @@ -0,0 +1,100 @@ +// + +import { tracingChannel } from 'node:diagnostics_channel'; +import type { TransactionEvent } from '@sentry/core'; +import type { DenoClient } from '@sentry/deno'; +import { getCurrentScope, getGlobalScope, getIsolationScope, init, startSpan } from '@sentry/deno'; +import { assert } from 'https://deno.land/std@0.212.0/assert/assert.ts'; +import { assertExists } from 'https://deno.land/std@0.212.0/assert/assert_exists.ts'; +import { assertEquals } from 'https://deno.land/std@0.212.0/assert/assert_equals.ts'; + +function resetGlobals(): void { + getCurrentScope().clear(); + getCurrentScope().setClient(undefined); + getIsolationScope().clear(); + getGlobalScope().clear(); +} + +/** See deno-redis.test.ts — same sink shape, deduped for clarity. */ +function transactionSink(): { + beforeSendTransaction: (event: TransactionEvent) => null; + waitFor: (predicate: (event: TransactionEvent) => boolean) => Promise; +} { + const transactions: TransactionEvent[] = []; + const waiters: { predicate: (e: TransactionEvent) => boolean; resolve: (e: TransactionEvent) => void }[] = []; + return { + beforeSendTransaction(event) { + transactions.push(event); + for (let i = waiters.length - 1; i >= 0; i--) { + const w = waiters[i]!; + if (w.predicate(event)) { + waiters.splice(i, 1); + w.resolve(event); + } + } + return null; + }, + waitFor(predicate) { + const already = transactions.find(predicate); + if (already) return Promise.resolve(already); + return new Promise(resolve => { + waiters.push({ predicate, resolve }); + }); + }, + }; +} + +function withTimeout(p: Promise, ms: number, what: string): Promise { + let timer: ReturnType | undefined; + const timeout = new Promise((_, reject) => { + timer = setTimeout(() => reject(new Error(`Timed out waiting for ${what} after ${ms}ms`)), ms); + }); + return Promise.race([p, timeout]).finally(() => { + if (timer !== undefined) clearTimeout(timer); + }); +} + +Deno.test('langchain instrumentation: included in default integrations (Deno 2.8.0+)', () => { + resetGlobals(); + const client = init({ dsn: 'https://username@domain/123' }) as DenoClient; + const names = client.getOptions().integrations.map(i => i.name); + assert(names.includes('LangChain'), `LangChain should be in defaults, got ${names.join(', ')}`); +}); + +Deno.test('langchain instrumentation: orchestrion @langchain/openai:embedQuery channel produces a nested embeddings span', async () => { + resetGlobals(); + const sink = transactionSink(); + init({ + dsn: 'https://username@domain/123', + tracesSampleRate: 1, + beforeSendTransaction: sink.beforeSendTransaction, + }); + + const channel = tracingChannel('orchestrion:@langchain/openai:embedQuery'); + + // `self` is the embeddings instance: its constructor name infers the provider + // system and `model` names the span. `arguments[0]` is the text to embed. + const self = { constructor: { name: 'OpenAIEmbeddings' }, model: 'text-embedding-3-small' }; + const ctx: Record = { self, arguments: ['hello world'] }; + + startSpan({ name: 'parent', op: 'test' }, () => { + channel.start.runStores(ctx, () => undefined); + channel.end.publish(ctx); + ctx.result = [0.1, 0.2, 0.3]; + channel.asyncEnd.publish(ctx); + }); + + const parent = await withTimeout( + sink.waitFor(t => t.transaction === 'parent'), + 5000, + "'parent' transaction", + ); + + const aiSpan = parent.spans?.find(s => s.op === 'gen_ai.embeddings'); + assertExists(aiSpan, `expected a gen_ai.embeddings child span, got ops: ${parent.spans?.map(s => s.op).join(', ')}`); + assertEquals(aiSpan!.description, 'embeddings text-embedding-3-small'); + assertEquals(aiSpan!.data?.['gen_ai.system'], 'openai'); + assertEquals(aiSpan!.data?.['gen_ai.operation.name'], 'embeddings'); + assertEquals(aiSpan!.data?.['gen_ai.request.model'], 'text-embedding-3-small'); + assertEquals(aiSpan!.data?.['sentry.origin'], 'auto.ai.langchain'); +}); diff --git a/packages/deno/src/index.ts b/packages/deno/src/index.ts index 2382a4c85532..73035f7155b3 100644 --- a/packages/deno/src/index.ts +++ b/packages/deno/src/index.ts @@ -129,6 +129,7 @@ export { kafkajsChannelIntegration, knexChannelIntegration, koaChannelIntegration, + langChainChannelIntegration, lruMemoizerChannelIntegration, mongodbChannelIntegration, mongooseChannelIntegration, diff --git a/packages/deno/src/sdk.ts b/packages/deno/src/sdk.ts index 7d8453af8c96..95acaa34e30b 100644 --- a/packages/deno/src/sdk.ts +++ b/packages/deno/src/sdk.ts @@ -23,6 +23,7 @@ import { hapiChannelIntegration, kafkajsChannelIntegration, koaChannelIntegration, + langChainChannelIntegration, lruMemoizerChannelIntegration, mongodbChannelIntegration, mongooseChannelIntegration, @@ -107,6 +108,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] { hapiChannelIntegration(), kafkajsChannelIntegration(), koaChannelIntegration(), + langChainChannelIntegration(), lruMemoizerChannelIntegration(), mongodbChannelIntegration(), mongooseChannelIntegration(), diff --git a/packages/deno/test/__snapshots__/mod.test.ts.snap b/packages/deno/test/__snapshots__/mod.test.ts.snap index 3d0f31f63f5e..9ad6fe00f65a 100644 --- a/packages/deno/test/__snapshots__/mod.test.ts.snap +++ b/packages/deno/test/__snapshots__/mod.test.ts.snap @@ -127,6 +127,7 @@ snapshot[`captureException 1`] = ` "Hapi", "Kafka", "Koa", + "LangChain", "LruMemoizer", "Mongo", "Mongoose", @@ -223,6 +224,7 @@ snapshot[`captureMessage 1`] = ` "Hapi", "Kafka", "Koa", + "LangChain", "LruMemoizer", "Mongo", "Mongoose", @@ -326,6 +328,7 @@ snapshot[`captureMessage twice 1`] = ` "Hapi", "Kafka", "Koa", + "LangChain", "LruMemoizer", "Mongo", "Mongoose", @@ -436,6 +439,7 @@ snapshot[`captureMessage twice 2`] = ` "Hapi", "Kafka", "Koa", + "LangChain", "LruMemoizer", "Mongo", "Mongoose",