Skip to content

Commit 04c9dd6

Browse files
authored
fix(ai-sdk): transform only specific vercel spans (#610)
1 parent 32e7a22 commit 04c9dd6

File tree

1 file changed

+16
-9
lines changed

1 file changed

+16
-9
lines changed

packages/traceloop-sdk/src/lib/tracing/ai-sdk-transformations.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ import { SpanAttributes } from "@traceloop/ai-semantic-conventions";
33

44
const AI_GENERATE_TEXT_DO_GENERATE = "ai.generateText.doGenerate";
55
const AI_STREAM_TEXT_DO_STREAM = "ai.streamText.doStream";
6+
const HANDLED_SPAN_NAMES: Record<string, string> = {
7+
[AI_GENERATE_TEXT_DO_GENERATE]: "ai.generateText.generate",
8+
[AI_STREAM_TEXT_DO_STREAM]: "ai.streamText.stream",
9+
};
10+
611
const AI_RESPONSE_TEXT = "ai.response.text";
712
const AI_PROMPT_MESSAGES = "ai.prompt.messages";
813
const AI_USAGE_PROMPT_TOKENS = "ai.usage.promptTokens";
914
const AI_USAGE_COMPLETION_TOKENS = "ai.usage.completionTokens";
1015
const AI_MODEL_PROVIDER = "ai.model.provider";
1116

1217
export const transformAiSdkSpanName = (span: ReadableSpan): void => {
13-
const nameMap: Record<string, string> = {
14-
[AI_GENERATE_TEXT_DO_GENERATE]: "ai.generateText.generate",
15-
[AI_STREAM_TEXT_DO_STREAM]: "ai.streamText.stream",
16-
};
17-
18-
if (span.name in nameMap) {
19-
// Unfortunately, the span name is not writable as this is not the intended behavior
20-
// but it is a workaround to set the correct span name
21-
(span as any).name = nameMap[span.name];
18+
// Unfortunately, the span name is not writable as this is not the intended behavior
19+
// but it is a workaround to set the correct span name
20+
if (span.name in HANDLED_SPAN_NAMES) {
21+
(span as any).name = HANDLED_SPAN_NAMES[span.name];
2222
}
2323
};
2424

@@ -107,7 +107,14 @@ export const transformAiSdkAttributes = (
107107
transformVendor(attributes);
108108
};
109109

110+
export const shouldHandleSpan = (span: ReadableSpan): boolean => {
111+
return span.name in HANDLED_SPAN_NAMES;
112+
};
113+
110114
export const transformAiSdkSpan = (span: ReadableSpan): void => {
115+
if (!shouldHandleSpan(span)) {
116+
return;
117+
}
111118
transformAiSdkSpanName(span);
112119
transformAiSdkAttributes(span.attributes);
113120
};

0 commit comments

Comments
 (0)