|
| 1 | +import { afterAll, describe, expect } from 'vitest'; |
| 2 | +import { |
| 3 | + GEN_AI_OPERATION_NAME, |
| 4 | + GEN_AI_PROVIDER_NAME, |
| 5 | + GEN_AI_REQUEST_MAX_TOKENS, |
| 6 | + GEN_AI_REQUEST_MODEL, |
| 7 | + GEN_AI_REQUEST_TEMPERATURE, |
| 8 | + GEN_AI_REQUEST_TOP_P, |
| 9 | + GEN_AI_USAGE_INPUT_TOKENS, |
| 10 | + GEN_AI_USAGE_OUTPUT_TOKENS, |
| 11 | + GEN_AI_USAGE_TOTAL_TOKENS, |
| 12 | + SENTRY_OP, |
| 13 | + SENTRY_ORIGIN, |
| 14 | +} from '@sentry/conventions/attributes'; |
| 15 | +import { GEN_AI_CHAT, GEN_AI_EMBEDDINGS, GEN_AI_GENERATE_CONTENT } from '@sentry/conventions/op'; |
| 16 | +import { supports } from '../../../utils'; |
| 17 | +import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner'; |
| 18 | + |
| 19 | +const EXPECTED_ORIGIN = 'auto.ai.google_genai'; |
| 20 | + |
| 21 | +// `@google/genai` v3 needs Node 22 and calls the `v1` API instead of `v1beta`, so the scenarios here |
| 22 | +// differ from `../google-genai-v2` only in the mock server routes. |
| 23 | +describe.runIf(supports({ min: 22 }))('Google GenAI integration (v3)', () => { |
| 24 | + afterAll(() => { |
| 25 | + cleanupChildProcesses(); |
| 26 | + }); |
| 27 | + |
| 28 | + createEsmAndCjsTests( |
| 29 | + __dirname, |
| 30 | + 'scenario.mjs', |
| 31 | + 'instrument.mjs', |
| 32 | + (createRunner, test) => { |
| 33 | + test('auto-instruments chat and generateContent on @google/genai v3', async () => { |
| 34 | + const runner = createRunner().ignore('event'); |
| 35 | + const spansPromise = runner.collectStreamedSpansUntilSegment('main'); |
| 36 | + |
| 37 | + await runner.start().completed(); |
| 38 | + |
| 39 | + const spans = (await spansPromise).filter(span => span.attributes[SENTRY_ORIGIN]?.value === EXPECTED_ORIGIN); |
| 40 | + expect(spans).toHaveLength(3); |
| 41 | + expect(spans.map(span => span.name).sort()).toEqual([ |
| 42 | + 'chat gemini-1.5-pro', |
| 43 | + 'generate_content error-model', |
| 44 | + 'generate_content gemini-1.5-flash', |
| 45 | + ]); |
| 46 | + |
| 47 | + const chatSpan = spans.find(span => span.name === 'chat gemini-1.5-pro'); |
| 48 | + expect(chatSpan!.status).toBe('ok'); |
| 49 | + expect(chatSpan!.attributes[SENTRY_OP].value).toBe(GEN_AI_CHAT); |
| 50 | + expect(chatSpan!.attributes[GEN_AI_OPERATION_NAME].value).toBe('chat'); |
| 51 | + expect(chatSpan!.attributes[SENTRY_ORIGIN].value).toBe(EXPECTED_ORIGIN); |
| 52 | + expect(chatSpan!.attributes[GEN_AI_PROVIDER_NAME].value).toBe('google_genai'); |
| 53 | + expect(chatSpan!.attributes[GEN_AI_REQUEST_MODEL].value).toBe('gemini-1.5-pro'); |
| 54 | + expect(chatSpan!.attributes[GEN_AI_USAGE_INPUT_TOKENS].value).toBe(8); |
| 55 | + expect(chatSpan!.attributes[GEN_AI_USAGE_OUTPUT_TOKENS].value).toBe(12); |
| 56 | + expect(chatSpan!.attributes[GEN_AI_USAGE_TOTAL_TOKENS].value).toBe(20); |
| 57 | + |
| 58 | + const generateContentSpan = spans.find(span => span.name === 'generate_content gemini-1.5-flash'); |
| 59 | + expect(generateContentSpan!.status).toBe('ok'); |
| 60 | + expect(generateContentSpan!.attributes[SENTRY_OP].value).toBe(GEN_AI_GENERATE_CONTENT); |
| 61 | + expect(generateContentSpan!.attributes[GEN_AI_OPERATION_NAME].value).toBe('generate_content'); |
| 62 | + expect(generateContentSpan!.attributes[SENTRY_ORIGIN].value).toBe(EXPECTED_ORIGIN); |
| 63 | + expect(generateContentSpan!.attributes[GEN_AI_PROVIDER_NAME].value).toBe('google_genai'); |
| 64 | + expect(generateContentSpan!.attributes[GEN_AI_REQUEST_MODEL].value).toBe('gemini-1.5-flash'); |
| 65 | + expect(generateContentSpan!.attributes[GEN_AI_REQUEST_TEMPERATURE].value).toBe(0.7); |
| 66 | + expect(generateContentSpan!.attributes[GEN_AI_REQUEST_TOP_P].value).toBe(0.9); |
| 67 | + expect(generateContentSpan!.attributes[GEN_AI_REQUEST_MAX_TOKENS].value).toBe(100); |
| 68 | + expect(generateContentSpan!.attributes[GEN_AI_USAGE_INPUT_TOKENS].value).toBe(8); |
| 69 | + expect(generateContentSpan!.attributes[GEN_AI_USAGE_OUTPUT_TOKENS].value).toBe(12); |
| 70 | + expect(generateContentSpan!.attributes[GEN_AI_USAGE_TOTAL_TOKENS].value).toBe(20); |
| 71 | + |
| 72 | + const errorSpan = spans.find(span => span.name === 'generate_content error-model'); |
| 73 | + expect(errorSpan!.status).toBe('error'); |
| 74 | + expect(errorSpan!.attributes[SENTRY_OP].value).toBe(GEN_AI_GENERATE_CONTENT); |
| 75 | + expect(errorSpan!.attributes[GEN_AI_OPERATION_NAME].value).toBe('generate_content'); |
| 76 | + expect(errorSpan!.attributes[GEN_AI_REQUEST_MODEL].value).toBe('error-model'); |
| 77 | + }); |
| 78 | + }, |
| 79 | + { additionalDependencies: { '@google/genai': '^3' } }, |
| 80 | + ); |
| 81 | + |
| 82 | + createEsmAndCjsTests( |
| 83 | + __dirname, |
| 84 | + 'scenario-embeddings.mjs', |
| 85 | + 'instrument.mjs', |
| 86 | + (createRunner, test) => { |
| 87 | + test('auto-instruments embedContent on @google/genai v3', async () => { |
| 88 | + const runner = createRunner().ignore('event'); |
| 89 | + const spansPromise = runner.collectStreamedSpansUntilSegment('main'); |
| 90 | + |
| 91 | + await runner.start().completed(); |
| 92 | + |
| 93 | + const spans = (await spansPromise).filter(span => span.attributes[SENTRY_ORIGIN]?.value === EXPECTED_ORIGIN); |
| 94 | + expect(spans).toHaveLength(3); |
| 95 | + expect(spans.map(span => span.name).sort()).toEqual([ |
| 96 | + 'embeddings error-model', |
| 97 | + 'embeddings text-embedding-004', |
| 98 | + 'embeddings text-embedding-004', |
| 99 | + ]); |
| 100 | + |
| 101 | + const successfulSpans = spans.filter( |
| 102 | + span => span.name === 'embeddings text-embedding-004' && span.status === 'ok', |
| 103 | + ); |
| 104 | + expect(successfulSpans).toHaveLength(2); |
| 105 | + for (const span of successfulSpans) { |
| 106 | + expect(span.attributes[SENTRY_OP].value).toBe(GEN_AI_EMBEDDINGS); |
| 107 | + expect(span.attributes[GEN_AI_OPERATION_NAME].value).toBe('embeddings'); |
| 108 | + expect(span.attributes[SENTRY_ORIGIN].value).toBe(EXPECTED_ORIGIN); |
| 109 | + expect(span.attributes[GEN_AI_PROVIDER_NAME].value).toBe('google_genai'); |
| 110 | + expect(span.attributes[GEN_AI_REQUEST_MODEL].value).toBe('text-embedding-004'); |
| 111 | + } |
| 112 | + |
| 113 | + const errorSpan = spans.find(span => span.name === 'embeddings error-model'); |
| 114 | + expect(errorSpan!.status).toBe('error'); |
| 115 | + expect(errorSpan!.attributes[SENTRY_OP].value).toBe(GEN_AI_EMBEDDINGS); |
| 116 | + expect(errorSpan!.attributes[GEN_AI_OPERATION_NAME].value).toBe('embeddings'); |
| 117 | + }); |
| 118 | + }, |
| 119 | + { additionalDependencies: { '@google/genai': '^3' } }, |
| 120 | + ); |
| 121 | +}); |
0 commit comments