Skip to content

Commit 8b62c33

Browse files
committed
simplify
1 parent f234d1a commit 8b62c33

File tree

4 files changed

+101
-331
lines changed

4 files changed

+101
-331
lines changed

packages/core/src/tracing/openai/index.ts

Lines changed: 2 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
GEN_AI_OPERATION_NAME_ATTRIBUTE,
1313
GEN_AI_REQUEST_AVAILABLE_TOOLS_ATTRIBUTE,
1414
GEN_AI_REQUEST_MODEL_ATTRIBUTE,
15-
GEN_AI_RESPONSE_TEXT_ATTRIBUTE,
1615
GEN_AI_SYSTEM_ATTRIBUTE,
1716
GEN_AI_SYSTEM_INSTRUCTIONS_ATTRIBUTE,
1817
} from '../ai/gen-ai-attributes';
@@ -26,18 +25,8 @@ import {
2625
} from '../ai/utils';
2726
import { OPENAI_METHOD_REGISTRY } from './constants';
2827
import { instrumentStream } from './streaming';
29-
import type { ChatCompletionChunk, OpenAiOptions, OpenAiResponse, OpenAIStream, ResponseStreamingEvent } from './types';
30-
import {
31-
addChatCompletionAttributes,
32-
addConversationAttributes,
33-
addEmbeddingsAttributes,
34-
addResponsesApiAttributes,
35-
extractRequestParameters,
36-
isChatCompletionResponse,
37-
isConversationResponse,
38-
isEmbeddingsResponse,
39-
isResponsesApiResponse,
40-
} from './utils';
28+
import type { ChatCompletionChunk, OpenAiOptions, OpenAIStream, ResponseStreamingEvent } from './types';
29+
import { addResponseAttributes, extractRequestParameters } from './utils';
4130

4231
/**
4332
* Extract available tools from request parameters
@@ -88,33 +77,6 @@ function extractRequestAttributes(args: unknown[], operationName: string): Recor
8877
return attributes;
8978
}
9079

91-
/**
92-
* Add response attributes to spans
93-
* This supports Chat Completion, Responses API, Embeddings, and Conversations API responses
94-
*/
95-
function addResponseAttributes(span: Span, result: unknown, recordOutputs?: boolean): void {
96-
if (!result || typeof result !== 'object') return;
97-
98-
const response = result as OpenAiResponse;
99-
100-
if (isChatCompletionResponse(response)) {
101-
addChatCompletionAttributes(span, response, recordOutputs);
102-
if (recordOutputs && response.choices?.length) {
103-
const responseTexts = response.choices.map(choice => choice.message?.content || '');
104-
span.setAttributes({ [GEN_AI_RESPONSE_TEXT_ATTRIBUTE]: JSON.stringify(responseTexts) });
105-
}
106-
} else if (isResponsesApiResponse(response)) {
107-
addResponsesApiAttributes(span, response, recordOutputs);
108-
if (recordOutputs && response.output_text) {
109-
span.setAttributes({ [GEN_AI_RESPONSE_TEXT_ATTRIBUTE]: response.output_text });
110-
}
111-
} else if (isEmbeddingsResponse(response)) {
112-
addEmbeddingsAttributes(span, response);
113-
} else if (isConversationResponse(response)) {
114-
addConversationAttributes(span, response);
115-
}
116-
}
117-
11880
// Extract and record AI request inputs, if present. This is intentionally separate from response attributes.
11981
function addRequestAttributes(span: Span, params: Record<string, unknown>, operationName: string): void {
12082
// Store embeddings input on a separate attribute and do not truncate it

packages/core/src/tracing/openai/streaming.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@ import { SPAN_STATUS_ERROR } from '../../tracing';
33
import type { Span } from '../../types-hoist/span';
44
import {
55
GEN_AI_RESPONSE_FINISH_REASONS_ATTRIBUTE,
6+
GEN_AI_RESPONSE_ID_ATTRIBUTE,
7+
GEN_AI_RESPONSE_MODEL_ATTRIBUTE,
68
GEN_AI_RESPONSE_STREAMING_ATTRIBUTE,
79
GEN_AI_RESPONSE_TEXT_ATTRIBUTE,
810
GEN_AI_RESPONSE_TOOL_CALLS_ATTRIBUTE,
11+
GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE,
12+
GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE,
13+
GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE,
914
} from '../ai/gen-ai-attributes';
1015
import { RESPONSE_EVENT_TYPES } from './constants';
1116
import type {
@@ -15,12 +20,7 @@ import type {
1520
ResponseFunctionCall,
1621
ResponseStreamingEvent,
1722
} from './types';
18-
import {
19-
isChatCompletionChunk,
20-
isResponsesApiStreamEvent,
21-
setCommonResponseAttributes,
22-
setTokenUsageAttributes,
23-
} from './utils';
23+
import { isChatCompletionChunk, isResponsesApiStreamEvent } from './utils';
2424

2525
/**
2626
* State object used to accumulate information from a stream of OpenAI events/chunks.
@@ -240,35 +240,31 @@ export async function* instrumentStream<T>(
240240
yield event;
241241
}
242242
} finally {
243-
setCommonResponseAttributes(span, state.responseId, state.responseModel);
244-
setTokenUsageAttributes(span, state.promptTokens, state.completionTokens, state.totalTokens);
245-
246-
span.setAttributes({
243+
const attrs: Record<string, string | number | boolean> = {
244+
[GEN_AI_RESPONSE_ID_ATTRIBUTE]: state.responseId,
245+
[GEN_AI_RESPONSE_MODEL_ATTRIBUTE]: state.responseModel,
247246
[GEN_AI_RESPONSE_STREAMING_ATTRIBUTE]: true,
248-
});
247+
};
248+
249+
if (state.promptTokens !== undefined) attrs[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE] = state.promptTokens;
250+
if (state.completionTokens !== undefined) attrs[GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE] = state.completionTokens;
251+
if (state.totalTokens !== undefined) attrs[GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE] = state.totalTokens;
249252

250253
if (state.finishReasons.length) {
251-
span.setAttributes({
252-
[GEN_AI_RESPONSE_FINISH_REASONS_ATTRIBUTE]: JSON.stringify(state.finishReasons),
253-
});
254+
attrs[GEN_AI_RESPONSE_FINISH_REASONS_ATTRIBUTE] = JSON.stringify(state.finishReasons);
254255
}
255256

256257
if (recordOutputs && state.responseTexts.length) {
257-
span.setAttributes({
258-
[GEN_AI_RESPONSE_TEXT_ATTRIBUTE]: state.responseTexts.join(''),
259-
});
258+
attrs[GEN_AI_RESPONSE_TEXT_ATTRIBUTE] = state.responseTexts.join('');
260259
}
261260

262-
// Set tool calls attribute if any were accumulated
263261
const chatCompletionToolCallsArray = Object.values(state.chatCompletionToolCalls);
264262
const allToolCalls = [...chatCompletionToolCallsArray, ...state.responsesApiToolCalls];
265-
266263
if (allToolCalls.length > 0) {
267-
span.setAttributes({
268-
[GEN_AI_RESPONSE_TOOL_CALLS_ATTRIBUTE]: JSON.stringify(allToolCalls),
269-
});
264+
attrs[GEN_AI_RESPONSE_TOOL_CALLS_ATTRIBUTE] = JSON.stringify(allToolCalls);
270265
}
271266

267+
span.setAttributes(attrs);
272268
span.end();
273269
}
274270
}

0 commit comments

Comments
 (0)