-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(deno): add google-genai integration #22469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| // <reference lib="deno.ns" /> | ||
|
|
||
| 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<TransactionEvent>; | ||
| } { | ||
| 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<TransactionEvent>(resolve => { | ||
| waiters.push({ predicate, resolve }); | ||
| }); | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function withTimeout<T>(p: Promise<T>, ms: number, what: string): Promise<T> { | ||
| let timer: ReturnType<typeof setTimeout> | undefined; | ||
| const timeout = new Promise<T>((_, 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('google-genai 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('Google_GenAI'), `Google_GenAI should be in defaults, got ${names.join(', ')}`); | ||
| }); | ||
|
|
||
| Deno.test('google-genai instrumentation: orchestrion @google/genai:generate-content channel produces a nested gen_ai span', async () => { | ||
| resetGlobals(); | ||
| const sink = transactionSink(); | ||
| init({ | ||
|
Comment on lines
+64
to
+67
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: A module-level Suggested FixThe Prompt for AI AgentAlso affects:
Did we get this right? 👍 / 👎 to inform future reviews. |
||
| dsn: 'https://username@domain/123', | ||
| tracesSampleRate: 1, | ||
| beforeSendTransaction: sink.beforeSendTransaction, | ||
| }); | ||
|
|
||
|
sentry[bot] marked this conversation as resolved.
Comment on lines
+64
to
+72
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: The module-level Suggested FixExport a reset function from the Prompt for AI Agent |
||
| const channel = tracingChannel('orchestrion:@google/genai:generate-content'); | ||
|
|
||
| // `arguments[0]` is the request params passed to `generateContent(params)`. | ||
| const params = { model: 'gemini-1.5-flash', contents: 'hi' }; | ||
| const ctx: Record<string, unknown> = { arguments: [params] }; | ||
|
|
||
| startSpan({ name: 'parent', op: 'test' }, () => { | ||
| channel.start.runStores(ctx, () => undefined); | ||
| channel.end.publish(ctx); | ||
| ctx.result = { | ||
| modelVersion: 'gemini-1.5-flash-002', | ||
| usageMetadata: { promptTokenCount: 10, candidatesTokenCount: 5, totalTokenCount: 15 }, | ||
| }; | ||
| 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.generate_content'); | ||
| assertExists( | ||
| aiSpan, | ||
| `expected a gen_ai.generate_content child span, got ops: ${parent.spans?.map(s => s.op).join(', ')}`, | ||
| ); | ||
| assertEquals(aiSpan!.description, 'generate_content gemini-1.5-flash'); | ||
| assertEquals(aiSpan!.data?.['gen_ai.system'], 'google_genai'); | ||
| assertEquals(aiSpan!.data?.['gen_ai.operation.name'], 'generate_content'); | ||
| assertEquals(aiSpan!.data?.['gen_ai.request.model'], 'gemini-1.5-flash'); | ||
| assertEquals(aiSpan!.data?.['gen_ai.response.model'], 'gemini-1.5-flash-002'); | ||
| assertEquals(aiSpan!.data?.['gen_ai.usage.total_tokens'], 15); | ||
| assertEquals(aiSpan!.data?.['sentry.origin'], 'auto.ai.orchestrion.google_genai'); | ||
| }); | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: The test unconditionally asserts
Google_GenAIis a default integration, but it's only included on Deno 2.8.0+. This will cause test failures on older Deno versions.Severity: MEDIUM
Suggested Fix
Conditionally skip the test assertion if the Deno version is less than 2.8.0. This can be achieved by checking the
MODULE_REGISTER_HOOKS_SUPPORTEDflag within the test file and only running the assertion if it's true. This will align the test's logic with the SDK's conditional inclusion logic.Prompt for AI Agent
Also affects:
packages/deno/src/sdk.ts:96~102