|
| 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('vercel-ai 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('VercelAI'), `VercelAI should be in defaults, got ${names.join(', ')}`); |
| 62 | +}); |
| 63 | + |
| 64 | +Deno.test('vercel-ai instrumentation: orchestrion:ai:generateText channel produces a nested invoke_agent 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:ai:generateText'); |
| 74 | + |
| 75 | + // `arguments[0]` is the options object passed to `generateText(options)`. |
| 76 | + const callOptions = { model: { provider: 'openai', modelId: 'gpt-4o' }, prompt: 'hi' }; |
| 77 | + const ctx: Record<string, unknown> = { arguments: [callOptions] }; |
| 78 | + |
| 79 | + startSpan({ name: 'parent', op: 'test' }, () => { |
| 80 | + channel.start.runStores(ctx, () => undefined); |
| 81 | + channel.end.publish(ctx); |
| 82 | + ctx.result = { |
| 83 | + usage: { inputTokens: 10, outputTokens: 5 }, |
| 84 | + response: { modelId: 'gpt-4o' }, |
| 85 | + }; |
| 86 | + channel.asyncEnd.publish(ctx); |
| 87 | + }); |
| 88 | + |
| 89 | + const parent = await withTimeout( |
| 90 | + sink.waitFor(t => t.transaction === 'parent'), |
| 91 | + 5000, |
| 92 | + "'parent' transaction", |
| 93 | + ); |
| 94 | + |
| 95 | + const aiSpan = parent.spans?.find(s => s.op === 'gen_ai.invoke_agent'); |
| 96 | + assertExists( |
| 97 | + aiSpan, |
| 98 | + `expected a gen_ai.invoke_agent child span, got ops: ${parent.spans?.map(s => s.op).join(', ')}`, |
| 99 | + ); |
| 100 | + assertEquals(aiSpan!.description, 'invoke_agent'); |
| 101 | + assertEquals(aiSpan!.data?.['gen_ai.system'], 'openai'); |
| 102 | + assertEquals(aiSpan!.data?.['gen_ai.operation.name'], 'invoke_agent'); |
| 103 | + assertEquals(aiSpan!.data?.['gen_ai.request.model'], 'gpt-4o'); |
| 104 | + assertEquals(aiSpan!.data?.['vercel.ai.operationId'], 'ai.generateText'); |
| 105 | + assertEquals(aiSpan!.data?.['gen_ai.usage.total_tokens'], 15); |
| 106 | + assertEquals(aiSpan!.data?.['sentry.origin'], 'auto.vercelai.channel'); |
| 107 | +}); |
0 commit comments