|
| 1 | +import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '@sentry/core'; |
| 2 | +import { afterAll, describe, expect } from 'vitest'; |
| 3 | +import { |
| 4 | + GEN_AI_INPUT_MESSAGES_ATTRIBUTE, |
| 5 | + GEN_AI_OPERATION_NAME_ATTRIBUTE, |
| 6 | + GEN_AI_REQUEST_MODEL_ATTRIBUTE, |
| 7 | + GEN_AI_REQUEST_TEMPERATURE_ATTRIBUTE, |
| 8 | + GEN_AI_RESPONSE_FINISH_REASONS_ATTRIBUTE, |
| 9 | + GEN_AI_RESPONSE_ID_ATTRIBUTE, |
| 10 | + GEN_AI_RESPONSE_MODEL_ATTRIBUTE, |
| 11 | + GEN_AI_RESPONSE_TEXT_ATTRIBUTE, |
| 12 | + GEN_AI_SYSTEM_ATTRIBUTE, |
| 13 | + GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE, |
| 14 | + GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE, |
| 15 | + GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE, |
| 16 | +} from '../../../../../../packages/core/src/tracing/ai/gen-ai-attributes'; |
| 17 | +import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../../utils/runner'; |
| 18 | + |
| 19 | +// The origin distinguishes the orchestrion (diagnostics-channel) path from the |
| 20 | +// OTel/proxy one (`auto.ai.openai`). |
| 21 | +const ORCHESTRION_ORIGIN = 'auto.ai.orchestrion.openai'; |
| 22 | + |
| 23 | +describe('OpenAI integration (orchestrion)', () => { |
| 24 | + afterAll(() => { |
| 25 | + cleanupChildProcesses(); |
| 26 | + }); |
| 27 | + |
| 28 | + createEsmAndCjsTests( |
| 29 | + __dirname, |
| 30 | + 'scenario-orchestrion-chat.mjs', |
| 31 | + 'instrument-orchestrion.mjs', |
| 32 | + (createRunner, test) => { |
| 33 | + test('creates openai chat spans via the diagnostics-channel path', async () => { |
| 34 | + await createRunner() |
| 35 | + .ignore('event') |
| 36 | + .expect({ transaction: { transaction: 'main' } }) |
| 37 | + .expect({ |
| 38 | + span: container => { |
| 39 | + const chatSpan = container.items.find( |
| 40 | + span => span.attributes[GEN_AI_RESPONSE_ID_ATTRIBUTE]?.value === 'chatcmpl-mock123', |
| 41 | + ); |
| 42 | + expect(chatSpan).toBeDefined(); |
| 43 | + expect(chatSpan!.name).toBe('chat gpt-3.5-turbo'); |
| 44 | + expect(chatSpan!.status).toBe('ok'); |
| 45 | + expect(chatSpan!.attributes[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]).toEqual({ |
| 46 | + type: 'string', |
| 47 | + value: ORCHESTRION_ORIGIN, |
| 48 | + }); |
| 49 | + expect(chatSpan!.attributes[SEMANTIC_ATTRIBUTE_SENTRY_OP]).toEqual({ |
| 50 | + type: 'string', |
| 51 | + value: 'gen_ai.chat', |
| 52 | + }); |
| 53 | + expect(chatSpan!.attributes[GEN_AI_OPERATION_NAME_ATTRIBUTE]).toEqual({ type: 'string', value: 'chat' }); |
| 54 | + expect(chatSpan!.attributes[GEN_AI_SYSTEM_ATTRIBUTE]).toEqual({ type: 'string', value: 'openai' }); |
| 55 | + expect(chatSpan!.attributes[GEN_AI_REQUEST_MODEL_ATTRIBUTE]).toEqual({ |
| 56 | + type: 'string', |
| 57 | + value: 'gpt-3.5-turbo', |
| 58 | + }); |
| 59 | + expect(chatSpan!.attributes[GEN_AI_REQUEST_TEMPERATURE_ATTRIBUTE]).toEqual({ |
| 60 | + type: 'double', |
| 61 | + value: 0.7, |
| 62 | + }); |
| 63 | + expect(chatSpan!.attributes[GEN_AI_RESPONSE_MODEL_ATTRIBUTE]).toEqual({ |
| 64 | + type: 'string', |
| 65 | + value: 'gpt-3.5-turbo', |
| 66 | + }); |
| 67 | + expect(chatSpan!.attributes[GEN_AI_RESPONSE_FINISH_REASONS_ATTRIBUTE]).toEqual({ |
| 68 | + type: 'string', |
| 69 | + value: '["stop"]', |
| 70 | + }); |
| 71 | + expect(chatSpan!.attributes[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE]).toEqual({ type: 'integer', value: 10 }); |
| 72 | + expect(chatSpan!.attributes[GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE]).toEqual({ |
| 73 | + type: 'integer', |
| 74 | + value: 15, |
| 75 | + }); |
| 76 | + expect(chatSpan!.attributes[GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE]).toEqual({ type: 'integer', value: 25 }); |
| 77 | + // Recording disabled: no inputs/outputs captured. |
| 78 | + expect(chatSpan!.attributes[GEN_AI_INPUT_MESSAGES_ATTRIBUTE]).toBeUndefined(); |
| 79 | + expect(chatSpan!.attributes[GEN_AI_RESPONSE_TEXT_ATTRIBUTE]).toBeUndefined(); |
| 80 | + |
| 81 | + const errorSpan = container.items.find(span => span.name === 'chat error-model'); |
| 82 | + expect(errorSpan).toBeDefined(); |
| 83 | + // `bindTracingChannelToSpan` sets the error status message to the OpenAI error's message, |
| 84 | + // so the serialized status is that message rather than the literal 'error' — just assert |
| 85 | + // the span was marked as errored. |
| 86 | + expect(errorSpan!.status).toBeDefined(); |
| 87 | + expect(errorSpan!.status).not.toBe('ok'); |
| 88 | + expect(errorSpan!.attributes[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]).toEqual({ |
| 89 | + type: 'string', |
| 90 | + value: ORCHESTRION_ORIGIN, |
| 91 | + }); |
| 92 | + }, |
| 93 | + }) |
| 94 | + .start() |
| 95 | + .completed(); |
| 96 | + }); |
| 97 | + }, |
| 98 | + ); |
| 99 | + |
| 100 | + createEsmAndCjsTests( |
| 101 | + __dirname, |
| 102 | + 'scenario-orchestrion-chat.mjs', |
| 103 | + 'instrument-orchestrion-with-pii.mjs', |
| 104 | + (createRunner, test) => { |
| 105 | + test('records inputs and outputs when PII is enabled', async () => { |
| 106 | + await createRunner() |
| 107 | + .ignore('event') |
| 108 | + .expect({ transaction: { transaction: 'main' } }) |
| 109 | + .expect({ |
| 110 | + span: container => { |
| 111 | + const chatSpan = container.items.find( |
| 112 | + span => span.attributes[GEN_AI_RESPONSE_ID_ATTRIBUTE]?.value === 'chatcmpl-mock123', |
| 113 | + ); |
| 114 | + expect(chatSpan).toBeDefined(); |
| 115 | + expect(chatSpan!.attributes[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]).toEqual({ |
| 116 | + type: 'string', |
| 117 | + value: ORCHESTRION_ORIGIN, |
| 118 | + }); |
| 119 | + expect(chatSpan!.attributes[GEN_AI_INPUT_MESSAGES_ATTRIBUTE]?.value).toContain( |
| 120 | + 'What is the capital of France?', |
| 121 | + ); |
| 122 | + expect(chatSpan!.attributes[GEN_AI_RESPONSE_TEXT_ATTRIBUTE]?.value).toContain('Hello from OpenAI mock!'); |
| 123 | + }, |
| 124 | + }) |
| 125 | + .start() |
| 126 | + .completed(); |
| 127 | + }); |
| 128 | + }, |
| 129 | + ); |
| 130 | +}); |
0 commit comments