|
| 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 | +}); |
0 commit comments