diff --git a/dev-packages/deno-integration-tests/suites/orchestrion-vercel-ai/test.ts b/dev-packages/deno-integration-tests/suites/orchestrion-vercel-ai/test.ts new file mode 100644 index 000000000000..a452661027c7 --- /dev/null +++ b/dev-packages/deno-integration-tests/suites/orchestrion-vercel-ai/test.ts @@ -0,0 +1,107 @@ +// + +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('vercel-ai 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('VercelAI'), `VercelAI should be in defaults, got ${names.join(', ')}`); +}); + +Deno.test('vercel-ai instrumentation: orchestrion:ai:generateText channel produces a nested invoke_agent span', async () => { + resetGlobals(); + const sink = transactionSink(); + init({ + dsn: 'https://username@domain/123', + tracesSampleRate: 1, + beforeSendTransaction: sink.beforeSendTransaction, + }); + + const channel = tracingChannel('orchestrion:ai:generateText'); + + // `arguments[0]` is the options object passed to `generateText(options)`. + const callOptions = { model: { provider: 'openai', modelId: 'gpt-4o' }, prompt: 'hi' }; + const ctx: Record = { arguments: [callOptions] }; + + startSpan({ name: 'parent', op: 'test' }, () => { + channel.start.runStores(ctx, () => undefined); + channel.end.publish(ctx); + ctx.result = { + usage: { inputTokens: 10, outputTokens: 5 }, + response: { modelId: 'gpt-4o' }, + }; + 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.invoke_agent'); + assertExists( + aiSpan, + `expected a gen_ai.invoke_agent child span, got ops: ${parent.spans?.map(s => s.op).join(', ')}`, + ); + assertEquals(aiSpan!.description, 'invoke_agent'); + assertEquals(aiSpan!.data?.['gen_ai.system'], 'openai'); + assertEquals(aiSpan!.data?.['gen_ai.operation.name'], 'invoke_agent'); + assertEquals(aiSpan!.data?.['gen_ai.request.model'], 'gpt-4o'); + assertEquals(aiSpan!.data?.['vercel.ai.operationId'], 'ai.generateText'); + assertEquals(aiSpan!.data?.['gen_ai.usage.total_tokens'], 15); + assertEquals(aiSpan!.data?.['sentry.origin'], 'auto.vercelai.channel'); +}); diff --git a/packages/deno/src/index.ts b/packages/deno/src/index.ts index 858bb24adc4c..2382a4c85532 100644 --- a/packages/deno/src/index.ts +++ b/packages/deno/src/index.ts @@ -138,6 +138,7 @@ export { postgresChannelIntegration, postgresJsChannelIntegration, tediousChannelIntegration, + vercelAiChannelIntegration, } from '@sentry/server-utils/orchestrion'; // Deprecated aliases kept for back-compat. Each forwards to the shared // integration above, so its name is the shared name (e.g. `Mysql`), not the old diff --git a/packages/deno/src/sdk.ts b/packages/deno/src/sdk.ts index daea3ebb1166..7d8453af8c96 100644 --- a/packages/deno/src/sdk.ts +++ b/packages/deno/src/sdk.ts @@ -32,6 +32,7 @@ import { postgresChannelIntegration, postgresJsChannelIntegration, tediousChannelIntegration, + vercelAiChannelIntegration, } from '@sentry/server-utils/orchestrion'; import { DenoClient } from './client'; import { breadcrumbsIntegration } from './integrations/breadcrumbs'; @@ -81,6 +82,12 @@ export function getDefaultIntegrations(_options: Options): Integration[] { // channels, which need only tracingChannel. The orchestrion implementation // (graphql v14–16) stays inert until the runtime hook injects those channels. ...(TRACING_CHANNEL_SUPPORTED ? [graphqlDiagnosticsChannelIntegration()] : []), + // vercel-ai is gated on tracingChannel rather than the module hook, like + // graphql: the composed integration also subscribes to the `ai` SDK v7's + // native `ai:telemetry` channel, which needs only tracingChannel. The + // orchestrion implementation (ai v4–6) stays inert until the runtime hook + // injects those channels. + ...(TRACING_CHANNEL_SUPPORTED ? [vercelAiChannelIntegration()] : []), // orchestrion-based instrumentations. We add a deliberate list here rather // than every channel integration: each one needs a Deno test proving it // records spans. diff --git a/packages/deno/test/__snapshots__/mod.test.ts.snap b/packages/deno/test/__snapshots__/mod.test.ts.snap index 8265f60a6e48..3d0f31f63f5e 100644 --- a/packages/deno/test/__snapshots__/mod.test.ts.snap +++ b/packages/deno/test/__snapshots__/mod.test.ts.snap @@ -116,6 +116,7 @@ snapshot[`captureException 1`] = ` "DenoHttp", "DenoRedis", "Graphql", + "VercelAI", "Amqplib", "Anthropic_AI", "Aws", @@ -211,6 +212,7 @@ snapshot[`captureMessage 1`] = ` "DenoHttp", "DenoRedis", "Graphql", + "VercelAI", "Amqplib", "Anthropic_AI", "Aws", @@ -313,6 +315,7 @@ snapshot[`captureMessage twice 1`] = ` "DenoHttp", "DenoRedis", "Graphql", + "VercelAI", "Amqplib", "Anthropic_AI", "Aws", @@ -422,6 +425,7 @@ snapshot[`captureMessage twice 2`] = ` "DenoHttp", "DenoRedis", "Graphql", + "VercelAI", "Amqplib", "Anthropic_AI", "Aws",