|
| 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 | +}); |
0 commit comments