|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import { addVercelAiProcessors } from '../../../src/tracing/vercel-ai'; |
| 3 | +import type { SpanJSON } from '../../../src/types-hoist/span'; |
| 4 | +import { getDefaultTestClientOptions, TestClient } from '../../mocks/client'; |
| 5 | + |
| 6 | +describe('vercel-ai parent span token attributes', () => { |
| 7 | + it('should map ai.usage.inputTokens to gen_ai.usage.input_tokens', () => { |
| 8 | + const options = getDefaultTestClientOptions({ tracesSampleRate: 1.0 }); |
| 9 | + const client = new TestClient(options); |
| 10 | + client.init(); |
| 11 | + addVercelAiProcessors(client); |
| 12 | + |
| 13 | + const mockSpan: SpanJSON = { |
| 14 | + description: 'ai.streamText', |
| 15 | + span_id: 'test-span-id', |
| 16 | + trace_id: 'test-trace-id', |
| 17 | + start_timestamp: 1000, |
| 18 | + timestamp: 2000, |
| 19 | + origin: 'auto.vercelai.otel', |
| 20 | + data: { |
| 21 | + 'ai.usage.inputTokens': 100, |
| 22 | + 'ai.usage.outputTokens': 50, |
| 23 | + }, |
| 24 | + }; |
| 25 | + |
| 26 | + const event = { |
| 27 | + type: 'transaction' as const, |
| 28 | + spans: [mockSpan], |
| 29 | + }; |
| 30 | + |
| 31 | + const eventProcessor = client['_eventProcessors'].find(processor => processor.id === 'VercelAiEventProcessor'); |
| 32 | + expect(eventProcessor).toBeDefined(); |
| 33 | + |
| 34 | + const processedEvent = eventProcessor!(event, {}); |
| 35 | + |
| 36 | + expect(processedEvent?.spans?.[0]?.data?.['gen_ai.usage.input_tokens']).toBe(100); |
| 37 | + expect(processedEvent?.spans?.[0]?.data?.['gen_ai.usage.output_tokens']).toBe(50); |
| 38 | + // Original attributes should be renamed to vercel.ai.* namespace |
| 39 | + expect(processedEvent?.spans?.[0]?.data?.['ai.usage.inputTokens']).toBeUndefined(); |
| 40 | + expect(processedEvent?.spans?.[0]?.data?.['ai.usage.outputTokens']).toBeUndefined(); |
| 41 | + }); |
| 42 | + |
| 43 | + it('should map ai.response.avgOutputTokensPerSecond to ai.response.avgCompletionTokensPerSecond', () => { |
| 44 | + const options = getDefaultTestClientOptions({ tracesSampleRate: 1.0 }); |
| 45 | + const client = new TestClient(options); |
| 46 | + client.init(); |
| 47 | + addVercelAiProcessors(client); |
| 48 | + |
| 49 | + const mockSpan: SpanJSON = { |
| 50 | + description: 'ai.streamText.doStream', |
| 51 | + span_id: 'test-span-id', |
| 52 | + trace_id: 'test-trace-id', |
| 53 | + start_timestamp: 1000, |
| 54 | + timestamp: 2000, |
| 55 | + origin: 'auto.vercelai.otel', |
| 56 | + data: { |
| 57 | + 'ai.response.avgOutputTokensPerSecond': 25.5, |
| 58 | + }, |
| 59 | + }; |
| 60 | + |
| 61 | + const event = { |
| 62 | + type: 'transaction' as const, |
| 63 | + spans: [mockSpan], |
| 64 | + }; |
| 65 | + |
| 66 | + const eventProcessor = client['_eventProcessors'].find(processor => processor.id === 'VercelAiEventProcessor'); |
| 67 | + expect(eventProcessor).toBeDefined(); |
| 68 | + |
| 69 | + const processedEvent = eventProcessor!(event, {}); |
| 70 | + |
| 71 | + // Should be renamed to match the expected attribute name |
| 72 | + expect(processedEvent?.spans?.[0]?.data?.['vercel.ai.response.avgCompletionTokensPerSecond']).toBe(25.5); |
| 73 | + expect(processedEvent?.spans?.[0]?.data?.['ai.response.avgOutputTokensPerSecond']).toBeUndefined(); |
| 74 | + }); |
| 75 | +}); |
0 commit comments