Skip to content

Commit ac60ff4

Browse files
committed
feat(deno): add vercelai integration
1 parent d8bf614 commit ac60ff4

4 files changed

Lines changed: 119 additions & 0 deletions

File tree

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
});

packages/deno/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ export {
138138
postgresChannelIntegration,
139139
postgresJsChannelIntegration,
140140
tediousChannelIntegration,
141+
vercelAiChannelIntegration,
141142
} from '@sentry/server-utils/orchestrion';
142143
// Deprecated aliases kept for back-compat. Each forwards to the shared
143144
// integration above, so its name is the shared name (e.g. `Mysql`), not the old

packages/deno/src/sdk.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ import {
3232
postgresChannelIntegration,
3333
postgresJsChannelIntegration,
3434
tediousChannelIntegration,
35+
vercelAiChannelIntegration,
3536
} from '@sentry/server-utils/orchestrion';
3637
import { DenoClient } from './client';
3738
import { breadcrumbsIntegration } from './integrations/breadcrumbs';
@@ -81,6 +82,12 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
8182
// channels, which need only tracingChannel. The orchestrion implementation
8283
// (graphql v14–16) stays inert until the runtime hook injects those channels.
8384
...(TRACING_CHANNEL_SUPPORTED ? [graphqlDiagnosticsChannelIntegration()] : []),
85+
// vercel-ai is gated on tracingChannel rather than the module hook, like
86+
// graphql: the composed integration also subscribes to the `ai` SDK v7's
87+
// native `ai:telemetry` channel, which needs only tracingChannel. The
88+
// orchestrion implementation (ai v4–6) stays inert until the runtime hook
89+
// injects those channels.
90+
...(TRACING_CHANNEL_SUPPORTED ? [vercelAiChannelIntegration()] : []),
8491
// orchestrion-based instrumentations. We add a deliberate list here rather
8592
// than every channel integration: each one needs a Deno test proving it
8693
// records spans.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ snapshot[`captureException 1`] = `
116116
"DenoHttp",
117117
"DenoRedis",
118118
"Graphql",
119+
"VercelAI",
119120
"Amqplib",
120121
"Anthropic_AI",
121122
"Aws",
@@ -211,6 +212,7 @@ snapshot[`captureMessage 1`] = `
211212
"DenoHttp",
212213
"DenoRedis",
213214
"Graphql",
215+
"VercelAI",
214216
"Amqplib",
215217
"Anthropic_AI",
216218
"Aws",
@@ -313,6 +315,7 @@ snapshot[`captureMessage twice 1`] = `
313315
"DenoHttp",
314316
"DenoRedis",
315317
"Graphql",
318+
"VercelAI",
316319
"Amqplib",
317320
"Anthropic_AI",
318321
"Aws",
@@ -422,6 +425,7 @@ snapshot[`captureMessage twice 2`] = `
422425
"DenoHttp",
423426
"DenoRedis",
424427
"Graphql",
428+
"VercelAI",
425429
"Amqplib",
426430
"Anthropic_AI",
427431
"Aws",

0 commit comments

Comments
 (0)