Skip to content

Commit eeb7812

Browse files
committed
Inline span conversion
1 parent 257c9fb commit eeb7812

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

packages/core/src/tracing/spans/extractGenAiSpans.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function extractGenAiSpansFromEvent(event: Event, client: Client): SpanCo
2929

3030
for (const span of event.spans) {
3131
if (span.op?.startsWith('gen_ai.')) {
32-
genAiSpans.push(span);
32+
genAiSpans.push(spanJsonToSerializedStreamedSpan(span));
3333
} else {
3434
remainingSpans.push(span);
3535
}
@@ -39,13 +39,10 @@ export function extractGenAiSpansFromEvent(event: Event, client: Client): SpanCo
3939
return undefined;
4040
}
4141

42-
const serializedSpans = genAiSpans.map(span => spanJsonToSerializedStreamedSpan(span));
43-
44-
// Remove gen_ai spans from the legacy transaction
4542
event.spans = remainingSpans;
4643

4744
return [
48-
{ type: 'span', item_count: serializedSpans.length, content_type: 'application/vnd.sentry.items.span.v2+json' },
49-
{ items: serializedSpans },
45+
{ type: 'span', item_count: genAiSpans.length, content_type: 'application/vnd.sentry.items.span.v2+json' },
46+
{ items: genAiSpans },
5047
];
5148
}

packages/core/test/lib/tracing/spans/extractGenAiSpans.test.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function makeSpanJSON(overrides: Partial<SpanJSON> = {}): SpanJSON {
1414
};
1515
}
1616

17-
function makeTransactionEvent(spans: SpanJSON[], hasGenAiSpans = false): Event {
17+
function makeTransactionEvent(spans: SpanJSON[]): Event {
1818
return {
1919
type: 'transaction',
2020
transaction: 'GET /api/chat',
@@ -27,7 +27,7 @@ function makeTransactionEvent(spans: SpanJSON[], hasGenAiSpans = false): Event {
2727
},
2828
},
2929
sdkProcessingMetadata: {
30-
...(hasGenAiSpans && { hasGenAiSpans: true }),
30+
hasGenAiSpans: true,
3131
},
3232
spans,
3333
};
@@ -92,14 +92,18 @@ describe('extractGenAiSpansFromEvent', () => {
9292
});
9393

9494
it('returns undefined when hasGenAiSpans flag is not set', () => {
95-
const event = makeTransactionEvent([makeSpanJSON({ op: 'gen_ai.chat' })], false);
95+
const event: Event = {
96+
type: 'transaction',
97+
spans: [makeSpanJSON({ op: 'gen_ai.chat' })],
98+
sdkProcessingMetadata: {},
99+
};
96100

97101
expect(extractGenAiSpansFromEvent(event, makeClient())).toBeUndefined();
98102
expect(event.spans).toHaveLength(1);
99103
});
100104

101105
it('returns undefined when there are no gen_ai spans', () => {
102-
const event = makeTransactionEvent([makeSpanJSON({ op: 'http.client' }), makeSpanJSON({ op: 'db.query' })], true);
106+
const event = makeTransactionEvent([makeSpanJSON({ op: 'http.client' }), makeSpanJSON({ op: 'db.query' })]);
103107

104108
expect(extractGenAiSpansFromEvent(event, makeClient())).toBeUndefined();
105109
expect(event.spans).toHaveLength(2);
@@ -116,7 +120,7 @@ describe('extractGenAiSpansFromEvent', () => {
116120
});
117121

118122
it('returns undefined when span streaming is enabled', () => {
119-
const event = makeTransactionEvent([makeSpanJSON({ op: 'gen_ai.chat' })], true);
123+
const event = makeTransactionEvent([makeSpanJSON({ op: 'gen_ai.chat' })]);
120124
const client = makeClient({ traceLifecycle: 'stream' });
121125

122126
expect(extractGenAiSpansFromEvent(event, client)).toBeUndefined();
@@ -134,7 +138,7 @@ describe('extractGenAiSpansFromEvent', () => {
134138
op: 'http.client',
135139
});
136140

137-
const event = makeTransactionEvent([httpSpan, genAiSpan], true);
141+
const event = makeTransactionEvent([httpSpan, genAiSpan]);
138142
const result = extractGenAiSpansFromEvent(event, makeClient());
139143

140144
expect(result![1].items[0]!.parent_span_id).toBe('http001');

0 commit comments

Comments
 (0)