|
| 1 | +import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core'; |
| 2 | +import { |
| 3 | + GEN_AI_EMBEDDINGS_INPUT, |
| 4 | + GEN_AI_INPUT_MESSAGES, |
| 5 | + GEN_AI_OPERATION_NAME, |
| 6 | + GEN_AI_PROVIDER_NAME, |
| 7 | + GEN_AI_REQUEST_MODEL, |
| 8 | + GEN_AI_REQUEST_TEMPERATURE, |
| 9 | + GEN_AI_RESPONSE_FINISH_REASONS, |
| 10 | + GEN_AI_RESPONSE_ID, |
| 11 | + GEN_AI_RESPONSE_MODEL, |
| 12 | + GEN_AI_RESPONSE_STREAMING, |
| 13 | + GEN_AI_RESPONSE_TEXT, |
| 14 | + GEN_AI_SYSTEM_INSTRUCTIONS, |
| 15 | + GEN_AI_USAGE_INPUT_TOKENS, |
| 16 | + GEN_AI_USAGE_OUTPUT_TOKENS, |
| 17 | + GEN_AI_USAGE_TOTAL_TOKENS, |
| 18 | +} from '@sentry/conventions/attributes'; |
| 19 | +import { afterAll, describe, expect } from 'vitest'; |
| 20 | +import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner'; |
| 21 | + |
| 22 | +const PROVIDER = 'groq'; |
| 23 | +const ORIGIN = 'auto.ai.groq'; |
| 24 | + |
| 25 | +describe('Groq integration', () => { |
| 26 | + afterAll(() => { |
| 27 | + cleanupChildProcesses(); |
| 28 | + }); |
| 29 | + |
| 30 | + createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument.mjs', (createRunner, test) => { |
| 31 | + test('creates chat and embeddings spans with genAI recording disabled', async () => { |
| 32 | + await createRunner() |
| 33 | + .expect({ |
| 34 | + span: container => { |
| 35 | + const chatSpan = container.items.find(s => s.attributes[GEN_AI_RESPONSE_ID]?.value === 'chatcmpl-mock123'); |
| 36 | + expect(chatSpan).toBeDefined(); |
| 37 | + expect(chatSpan!.name).toBe('chat llama-3.3-70b-versatile'); |
| 38 | + expect(chatSpan!.status).toBe('ok'); |
| 39 | + expect(chatSpan!.attributes[GEN_AI_OPERATION_NAME]?.value).toBe('chat'); |
| 40 | + expect(chatSpan!.attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP]?.value).toBe('gen_ai.chat'); |
| 41 | + expect(chatSpan!.attributes[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]?.value).toBe(ORIGIN); |
| 42 | + expect(chatSpan!.attributes[GEN_AI_PROVIDER_NAME]?.value).toBe(PROVIDER); |
| 43 | + expect(chatSpan!.attributes[GEN_AI_REQUEST_MODEL]?.value).toBe('llama-3.3-70b-versatile'); |
| 44 | + expect(chatSpan!.attributes[GEN_AI_REQUEST_TEMPERATURE]?.value).toBe(0.7); |
| 45 | + expect(chatSpan!.attributes[GEN_AI_RESPONSE_MODEL]?.value).toBe('llama-3.3-70b-versatile'); |
| 46 | + expect(chatSpan!.attributes[GEN_AI_RESPONSE_FINISH_REASONS]?.value).toBe('["stop"]'); |
| 47 | + expect(chatSpan!.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(10); |
| 48 | + expect(chatSpan!.attributes[GEN_AI_USAGE_OUTPUT_TOKENS]?.value).toBe(15); |
| 49 | + expect(chatSpan!.attributes[GEN_AI_USAGE_TOTAL_TOKENS]?.value).toBe(25); |
| 50 | + // recording disabled → no prompt/response content |
| 51 | + expect(chatSpan!.attributes[GEN_AI_INPUT_MESSAGES]).toBeUndefined(); |
| 52 | + expect(chatSpan!.attributes[GEN_AI_RESPONSE_TEXT]).toBeUndefined(); |
| 53 | + |
| 54 | + const streamSpan = container.items.find( |
| 55 | + s => s.attributes[GEN_AI_RESPONSE_ID]?.value === 'chatcmpl-stream-123', |
| 56 | + ); |
| 57 | + expect(streamSpan).toBeDefined(); |
| 58 | + expect(streamSpan!.name).toBe('chat llama-3.1-8b-instant'); |
| 59 | + expect(streamSpan!.attributes[GEN_AI_OPERATION_NAME]?.value).toBe('chat'); |
| 60 | + expect(streamSpan!.attributes[GEN_AI_RESPONSE_STREAMING]?.value).toBe(true); |
| 61 | + expect(streamSpan!.attributes[GEN_AI_USAGE_TOTAL_TOKENS]?.value).toBe(30); |
| 62 | + |
| 63 | + const errorSpan = container.items.find(s => s.attributes[GEN_AI_REQUEST_MODEL]?.value === 'error-model'); |
| 64 | + expect(errorSpan).toBeDefined(); |
| 65 | + expect(errorSpan!.status).toBe('error'); |
| 66 | + |
| 67 | + const embeddingsSpan = container.items.find( |
| 68 | + s => s.attributes[GEN_AI_RESPONSE_ID]?.value === 'embd-mock123', |
| 69 | + ); |
| 70 | + expect(embeddingsSpan).toBeDefined(); |
| 71 | + expect(embeddingsSpan!.name).toBe('embeddings nomic-embed-text-v1_5'); |
| 72 | + expect(embeddingsSpan!.attributes[GEN_AI_OPERATION_NAME]?.value).toBe('embeddings'); |
| 73 | + expect(embeddingsSpan!.attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP]?.value).toBe('gen_ai.embeddings'); |
| 74 | + expect(embeddingsSpan!.attributes[GEN_AI_PROVIDER_NAME]?.value).toBe(PROVIDER); |
| 75 | + expect(embeddingsSpan!.attributes[GEN_AI_USAGE_INPUT_TOKENS]?.value).toBe(8); |
| 76 | + expect(embeddingsSpan!.attributes[GEN_AI_EMBEDDINGS_INPUT]).toBeUndefined(); |
| 77 | + }, |
| 78 | + }) |
| 79 | + .start() |
| 80 | + .completed(); |
| 81 | + }); |
| 82 | + }); |
| 83 | + |
| 84 | + createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument-with-pii.mjs', (createRunner, test) => { |
| 85 | + test('records chat and embeddings inputs/outputs with PII enabled', async () => { |
| 86 | + await createRunner() |
| 87 | + .expect({ |
| 88 | + span: container => { |
| 89 | + const chatSpan = container.items.find(s => s.attributes[GEN_AI_RESPONSE_ID]?.value === 'chatcmpl-mock123'); |
| 90 | + expect(chatSpan).toBeDefined(); |
| 91 | + // The system message is split out into gen_ai.system_instructions. |
| 92 | + expect(chatSpan!.attributes[GEN_AI_SYSTEM_INSTRUCTIONS]?.value).toContain('You are a helpful assistant.'); |
| 93 | + expect(chatSpan!.attributes[GEN_AI_INPUT_MESSAGES]?.value).toBe( |
| 94 | + '[{"role":"user","content":"What is the capital of France?"}]', |
| 95 | + ); |
| 96 | + expect(chatSpan!.attributes[GEN_AI_RESPONSE_TEXT]?.value).toBe('["Hello from Groq mock!"]'); |
| 97 | + |
| 98 | + const embeddingsSpan = container.items.find( |
| 99 | + s => s.attributes[GEN_AI_RESPONSE_ID]?.value === 'embd-mock123', |
| 100 | + ); |
| 101 | + expect(embeddingsSpan).toBeDefined(); |
| 102 | + expect(embeddingsSpan!.attributes[GEN_AI_EMBEDDINGS_INPUT]?.value).toContain('Embedding test!'); |
| 103 | + }, |
| 104 | + }) |
| 105 | + .start() |
| 106 | + .completed(); |
| 107 | + }); |
| 108 | + }); |
| 109 | +}); |
0 commit comments