Skip to content

Commit af2805c

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

4 files changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
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+
// Drives the `orchestrion:@google/genai:generate-content` channel — the same events
65+
// the orchestrion transform publishes around the SDK's `Models.generateContent` — so
66+
// no live Google GenAI client is needed. The subscriber reads the request params off
67+
// `arguments[0]` and opens a `gen_ai.generate_content` span, then enriches it from
68+
// the settled `result`.
69+
Deno.test('google-genai instrumentation: orchestrion @google/genai:generate-content channel produces a nested gen_ai span', async () => {
70+
resetGlobals();
71+
const sink = transactionSink();
72+
init({
73+
dsn: 'https://username@domain/123',
74+
tracesSampleRate: 1,
75+
beforeSendTransaction: sink.beforeSendTransaction,
76+
});
77+
78+
const channel = tracingChannel('orchestrion:@google/genai:generate-content');
79+
80+
// `arguments[0]` is the request params passed to `generateContent(params)`.
81+
const params = { model: 'gemini-1.5-flash', contents: 'hi' };
82+
const ctx: Record<string, unknown> = { arguments: [params] };
83+
84+
startSpan({ name: 'parent', op: 'test' }, () => {
85+
channel.start.runStores(ctx, () => undefined);
86+
// A non-streaming (non-async-iterable) result ends the span via `beforeSpanEnd`,
87+
// which reads the response model version and token usage off it.
88+
ctx.result = {
89+
modelVersion: 'gemini-1.5-flash-002',
90+
usageMetadata: { promptTokenCount: 10, candidatesTokenCount: 5, totalTokenCount: 15 },
91+
};
92+
channel.end.publish(ctx);
93+
channel.asyncEnd.publish(ctx);
94+
});
95+
96+
const parent = await withTimeout(
97+
sink.waitFor(t => t.transaction === 'parent'),
98+
5000,
99+
"'parent' transaction",
100+
);
101+
102+
const aiSpan = parent.spans?.find(s => s.op === 'gen_ai.generate_content');
103+
assertExists(
104+
aiSpan,
105+
`expected a gen_ai.generate_content child span, got ops: ${parent.spans?.map(s => s.op).join(', ')}`,
106+
);
107+
assertEquals(aiSpan!.description, 'generate_content gemini-1.5-flash');
108+
assertEquals(aiSpan!.data?.['gen_ai.system'], 'google_genai');
109+
assertEquals(aiSpan!.data?.['gen_ai.operation.name'], 'generate_content');
110+
assertEquals(aiSpan!.data?.['gen_ai.request.model'], 'gemini-1.5-flash');
111+
assertEquals(aiSpan!.data?.['gen_ai.response.model'], 'gemini-1.5-flash-002');
112+
assertEquals(aiSpan!.data?.['gen_ai.usage.total_tokens'], 15);
113+
assertEquals(aiSpan!.data?.['sentry.origin'], 'auto.ai.orchestrion.google_genai');
114+
});

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)