|
| 1 | +import { describe, expect, it } from 'vitest'; |
| 2 | +import type { Event } from '../../../../src/types-hoist/event'; |
| 3 | +import type { SpanJSON } from '../../../../src/types-hoist/span'; |
| 4 | +import { extractGenAiSpansFromEvent } from '../../../../src/tracing/spans/extractGenAiSpans'; |
| 5 | +import { getDefaultTestClientOptions, TestClient } from '../../../mocks/client'; |
| 6 | + |
| 7 | +function makeSpanJSON(overrides: Partial<SpanJSON> = {}): SpanJSON { |
| 8 | + return { |
| 9 | + span_id: 'abc123def456789a', |
| 10 | + trace_id: '00112233445566778899aabbccddeeff', |
| 11 | + start_timestamp: 1000, |
| 12 | + data: {}, |
| 13 | + ...overrides, |
| 14 | + }; |
| 15 | +} |
| 16 | + |
| 17 | +function makeTransactionEvent(spans: SpanJSON[]): Event { |
| 18 | + return { |
| 19 | + type: 'transaction', |
| 20 | + transaction: 'GET /api/chat', |
| 21 | + release: '1.0.0', |
| 22 | + environment: 'production', |
| 23 | + contexts: { |
| 24 | + trace: { |
| 25 | + span_id: 'root0000deadbeef', |
| 26 | + trace_id: '00112233445566778899aabbccddeeff', |
| 27 | + }, |
| 28 | + }, |
| 29 | + spans, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +function makeClient(options: Partial<Parameters<typeof getDefaultTestClientOptions>[0]> = {}): TestClient { |
| 34 | + return new TestClient( |
| 35 | + getDefaultTestClientOptions({ |
| 36 | + dsn: 'https://dsn@ingest.f00.f00/1', |
| 37 | + ...options, |
| 38 | + }), |
| 39 | + ); |
| 40 | +} |
| 41 | + |
| 42 | +describe('extractGenAiSpansFromEvent', () => { |
| 43 | + it('extracts gen_ai spans and removes them from the event', () => { |
| 44 | + const genAiSpan = makeSpanJSON({ |
| 45 | + span_id: 'genai001', |
| 46 | + op: 'gen_ai.chat', |
| 47 | + description: 'chat gpt-4', |
| 48 | + timestamp: 1005, |
| 49 | + }); |
| 50 | + const httpSpan = makeSpanJSON({ |
| 51 | + span_id: 'http001', |
| 52 | + op: 'http.client', |
| 53 | + description: 'GET /api', |
| 54 | + timestamp: 1002, |
| 55 | + }); |
| 56 | + |
| 57 | + const event = makeTransactionEvent([genAiSpan, httpSpan]); |
| 58 | + const client = makeClient(); |
| 59 | + |
| 60 | + const result = extractGenAiSpansFromEvent(event, client); |
| 61 | + |
| 62 | + // gen_ai spans should be in the container item |
| 63 | + expect(result).toBeDefined(); |
| 64 | + const [headers, payload] = result!; |
| 65 | + expect(headers.type).toBe('span'); |
| 66 | + expect(headers.item_count).toBe(1); |
| 67 | + expect(headers.content_type).toBe('application/vnd.sentry.items.span.v2+json'); |
| 68 | + expect(payload.items).toHaveLength(1); |
| 69 | + expect(payload.items[0]!.span_id).toBe('genai001'); |
| 70 | + expect(payload.items[0]!.name).toBe('chat gpt-4'); |
| 71 | + |
| 72 | + // gen_ai spans should be removed from the event |
| 73 | + expect(event.spans).toHaveLength(1); |
| 74 | + expect(event.spans![0]!.span_id).toBe('http001'); |
| 75 | + }); |
| 76 | + |
| 77 | + it('extracts multiple gen_ai spans', () => { |
| 78 | + const chatSpan = makeSpanJSON({ span_id: 'chat001', op: 'gen_ai.chat', description: 'chat' }); |
| 79 | + const embeddingsSpan = makeSpanJSON({ span_id: 'embed001', op: 'gen_ai.embeddings', description: 'embed' }); |
| 80 | + const agentSpan = makeSpanJSON({ span_id: 'agent001', op: 'gen_ai.invoke_agent', description: 'agent' }); |
| 81 | + const dbSpan = makeSpanJSON({ span_id: 'db001', op: 'db.query', description: 'SELECT *' }); |
| 82 | + |
| 83 | + const event = makeTransactionEvent([chatSpan, embeddingsSpan, dbSpan, agentSpan]); |
| 84 | + const client = makeClient(); |
| 85 | + |
| 86 | + const result = extractGenAiSpansFromEvent(event, client); |
| 87 | + |
| 88 | + expect(result).toBeDefined(); |
| 89 | + expect(result![0].item_count).toBe(3); |
| 90 | + expect(result![1].items).toHaveLength(3); |
| 91 | + expect(result![1].items.map(s => s.span_id)).toEqual(['chat001', 'embed001', 'agent001']); |
| 92 | + |
| 93 | + // Only the db span should remain |
| 94 | + expect(event.spans).toHaveLength(1); |
| 95 | + expect(event.spans![0]!.span_id).toBe('db001'); |
| 96 | + }); |
| 97 | + |
| 98 | + it('returns undefined when there are no gen_ai spans', () => { |
| 99 | + const httpSpan = makeSpanJSON({ op: 'http.client' }); |
| 100 | + const dbSpan = makeSpanJSON({ op: 'db.query' }); |
| 101 | + |
| 102 | + const event = makeTransactionEvent([httpSpan, dbSpan]); |
| 103 | + const client = makeClient(); |
| 104 | + |
| 105 | + const result = extractGenAiSpansFromEvent(event, client); |
| 106 | + |
| 107 | + expect(result).toBeUndefined(); |
| 108 | + expect(event.spans).toHaveLength(2); |
| 109 | + }); |
| 110 | + |
| 111 | + it('returns undefined when event has no spans', () => { |
| 112 | + const event = makeTransactionEvent([]); |
| 113 | + const client = makeClient(); |
| 114 | + |
| 115 | + expect(extractGenAiSpansFromEvent(event, client)).toBeUndefined(); |
| 116 | + }); |
| 117 | + |
| 118 | + it('returns undefined when event is not a transaction', () => { |
| 119 | + const event: Event = { type: undefined, spans: [makeSpanJSON({ op: 'gen_ai.chat' })] }; |
| 120 | + const client = makeClient(); |
| 121 | + |
| 122 | + expect(extractGenAiSpansFromEvent(event, client)).toBeUndefined(); |
| 123 | + }); |
| 124 | + |
| 125 | + it('returns undefined when span streaming is enabled', () => { |
| 126 | + const event = makeTransactionEvent([makeSpanJSON({ op: 'gen_ai.chat' })]); |
| 127 | + const client = makeClient({ traceLifecycle: 'stream' }); |
| 128 | + |
| 129 | + expect(extractGenAiSpansFromEvent(event, client)).toBeUndefined(); |
| 130 | + // Spans should not be modified |
| 131 | + expect(event.spans).toHaveLength(1); |
| 132 | + }); |
| 133 | + |
| 134 | + it('preserves parent_span_id pointing to v1 spans', () => { |
| 135 | + const genAiSpan = makeSpanJSON({ |
| 136 | + span_id: 'genai001', |
| 137 | + parent_span_id: 'http001', |
| 138 | + op: 'gen_ai.chat', |
| 139 | + }); |
| 140 | + const httpSpan = makeSpanJSON({ |
| 141 | + span_id: 'http001', |
| 142 | + op: 'http.client', |
| 143 | + }); |
| 144 | + |
| 145 | + const event = makeTransactionEvent([httpSpan, genAiSpan]); |
| 146 | + const client = makeClient(); |
| 147 | + |
| 148 | + const result = extractGenAiSpansFromEvent(event, client); |
| 149 | + |
| 150 | + // The v2 span should still reference the v1 parent |
| 151 | + expect(result![1].items[0]!.parent_span_id).toBe('http001'); |
| 152 | + // The v1 parent should remain in the transaction |
| 153 | + expect(event.spans).toHaveLength(1); |
| 154 | + expect(event.spans![0]!.span_id).toBe('http001'); |
| 155 | + }); |
| 156 | +}); |
0 commit comments