Skip to content

Commit fde4dd2

Browse files
committed
feat(deno): add google-genai integration
1 parent ed7e0b4 commit fde4dd2

4 files changed

Lines changed: 114 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('google-genai 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('Google_GenAI'), `Google_GenAI should be in defaults, got ${names.join(', ')}`);
62+
});
63+
64+
Deno.test('google-genai instrumentation: orchestrion @google/genai:generate-content channel produces a nested gen_ai 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:@google/genai:generate-content');
74+
75+
// `arguments[0]` is the request params passed to `generateContent(params)`.
76+
const params = { model: 'gemini-1.5-flash', contents: 'hi' };
77+
const ctx: Record<string, unknown> = { arguments: [params] };
78+
79+
startSpan({ name: 'parent', op: 'test' }, () => {
80+
channel.start.runStores(ctx, () => undefined);
81+
channel.end.publish(ctx);
82+
ctx.result = {
83+
modelVersion: 'gemini-1.5-flash-002',
84+
usageMetadata: { promptTokenCount: 10, candidatesTokenCount: 5, totalTokenCount: 15 },
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.generate_content');
96+
assertExists(
97+
aiSpan,
98+
`expected a gen_ai.generate_content child span, got ops: ${parent.spans?.map(s => s.op).join(', ')}`,
99+
);
100+
assertEquals(aiSpan!.description, 'generate_content gemini-1.5-flash');
101+
assertEquals(aiSpan!.data?.['gen_ai.system'], 'google_genai');
102+
assertEquals(aiSpan!.data?.['gen_ai.operation.name'], 'generate_content');
103+
assertEquals(aiSpan!.data?.['gen_ai.request.model'], 'gemini-1.5-flash');
104+
assertEquals(aiSpan!.data?.['gen_ai.response.model'], 'gemini-1.5-flash-002');
105+
assertEquals(aiSpan!.data?.['gen_ai.usage.total_tokens'], 15);
106+
assertEquals(aiSpan!.data?.['sentry.origin'], 'auto.ai.orchestrion.google_genai');
107+
});

packages/deno/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ export {
123123
expressChannelIntegration,
124124
firebaseChannelIntegration,
125125
genericPoolChannelIntegration,
126+
googleGenAIChannelIntegration,
126127
graphqlDiagnosticsChannelIntegration,
127128
hapiChannelIntegration,
128129
kafkajsChannelIntegration,

packages/deno/src/sdk.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
expressChannelIntegration,
1919
firebaseChannelIntegration,
2020
genericPoolChannelIntegration,
21+
googleGenAIChannelIntegration,
2122
graphqlDiagnosticsChannelIntegration,
2223
hapiChannelIntegration,
2324
kafkajsChannelIntegration,
@@ -95,6 +96,7 @@ export function getDefaultIntegrations(_options: Options): Integration[] {
9596
expressChannelIntegration(),
9697
firebaseChannelIntegration(),
9798
genericPoolChannelIntegration(),
99+
googleGenAIChannelIntegration(),
98100
hapiChannelIntegration(),
99101
kafkajsChannelIntegration(),
100102
koaChannelIntegration(),

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ snapshot[`captureException 1`] = `
122122
"Express",
123123
"Firebase",
124124
"GenericPool",
125+
"Google_GenAI",
125126
"Hapi",
126127
"Kafka",
127128
"Koa",
@@ -216,6 +217,7 @@ snapshot[`captureMessage 1`] = `
216217
"Express",
217218
"Firebase",
218219
"GenericPool",
220+
"Google_GenAI",
219221
"Hapi",
220222
"Kafka",
221223
"Koa",
@@ -317,6 +319,7 @@ snapshot[`captureMessage twice 1`] = `
317319
"Express",
318320
"Firebase",
319321
"GenericPool",
322+
"Google_GenAI",
320323
"Hapi",
321324
"Kafka",
322325
"Koa",
@@ -425,6 +428,7 @@ snapshot[`captureMessage twice 2`] = `
425428
"Express",
426429
"Firebase",
427430
"GenericPool",
431+
"Google_GenAI",
428432
"Hapi",
429433
"Kafka",
430434
"Koa",

0 commit comments

Comments
 (0)