|
| 1 | +import type { Client } from '../client'; |
| 2 | +import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '../semanticAttributes'; |
| 3 | +import type { Event } from '../types-hoist/event'; |
| 4 | +import type { Span, SpanAttributes, SpanJSON, SpanOrigin } from '../types-hoist/span'; |
| 5 | +import { spanToJSON } from './spanUtils'; |
| 6 | +import { |
| 7 | + AI_MODEL_ID_ATTRIBUTE, |
| 8 | + AI_MODEL_PROVIDER_ATTRIBUTE, |
| 9 | + AI_PROMPT_ATTRIBUTE, |
| 10 | + AI_PROMPT_MESSAGES_ATTRIBUTE, |
| 11 | + AI_PROMPT_TOOLS_ATTRIBUTE, |
| 12 | + AI_RESPONSE_TEXT_ATTRIBUTE, |
| 13 | + AI_RESPONSE_TOOL_CALLS_ATTRIBUTE, |
| 14 | + AI_TELEMETRY_FUNCTION_ID_ATTRIBUTE, |
| 15 | + AI_TOOL_CALL_ID_ATTRIBUTE, |
| 16 | + AI_TOOL_CALL_NAME_ATTRIBUTE, |
| 17 | + AI_USAGE_COMPLETION_TOKENS_ATTRIBUTE, |
| 18 | + AI_USAGE_PROMPT_TOKENS_ATTRIBUTE, |
| 19 | + GEN_AI_RESPONSE_MODEL_ATTRIBUTE, |
| 20 | + GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE, |
| 21 | + GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE, |
| 22 | +} from './vercel-ai-attributes'; |
| 23 | + |
| 24 | +function addOriginToSpan(span: Span, origin: SpanOrigin): void { |
| 25 | + span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, origin); |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * Post-process spans emitted by the Vercel AI SDK. |
| 30 | + * This is supposed to be used in `client.on('spanStart', ...) |
| 31 | + */ |
| 32 | +function onVercelAiSpanStart(span: Span): void { |
| 33 | + const { data: attributes, description: name } = spanToJSON(span); |
| 34 | + |
| 35 | + if (!name) { |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + // Tool call spans |
| 40 | + // https://ai-sdk.dev/docs/ai-sdk-core/telemetry#tool-call-spans |
| 41 | + if (attributes[AI_TOOL_CALL_NAME_ATTRIBUTE] && attributes[AI_TOOL_CALL_ID_ATTRIBUTE] && name === 'ai.toolCall') { |
| 42 | + processToolCallSpan(span, attributes); |
| 43 | + return; |
| 44 | + } |
| 45 | + |
| 46 | + // The AI and Provider must be defined for generate, stream, and embed spans. |
| 47 | + // The id of the model |
| 48 | + const aiModelId = attributes[AI_MODEL_ID_ATTRIBUTE]; |
| 49 | + // the provider of the model |
| 50 | + const aiModelProvider = attributes[AI_MODEL_PROVIDER_ATTRIBUTE]; |
| 51 | + if (typeof aiModelId !== 'string' || typeof aiModelProvider !== 'string' || !aiModelId || !aiModelProvider) { |
| 52 | + return; |
| 53 | + } |
| 54 | + |
| 55 | + processGenerateSpan(span, name, attributes); |
| 56 | +} |
| 57 | + |
| 58 | +const vercelAiEventProcessor = Object.assign( |
| 59 | + (event: Event): Event => { |
| 60 | + if (event.type === 'transaction' && event.spans) { |
| 61 | + for (const span of event.spans) { |
| 62 | + // this mutates spans in-place |
| 63 | + processEndedVercelAiSpan(span); |
| 64 | + } |
| 65 | + } |
| 66 | + return event; |
| 67 | + }, |
| 68 | + { id: 'VercelAiEventProcessor' }, |
| 69 | +); |
| 70 | + |
| 71 | +/** |
| 72 | + * Post-process spans emitted by the Vercel AI SDK. |
| 73 | + */ |
| 74 | +function processEndedVercelAiSpan(span: SpanJSON): void { |
| 75 | + const { data: attributes, origin } = span; |
| 76 | + |
| 77 | + if (origin !== 'auto.vercelai.otel') { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + renameAttributeKey(attributes, AI_USAGE_COMPLETION_TOKENS_ATTRIBUTE, GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE); |
| 82 | + renameAttributeKey(attributes, AI_USAGE_PROMPT_TOKENS_ATTRIBUTE, GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE); |
| 83 | + |
| 84 | + if ( |
| 85 | + typeof attributes[GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE] === 'number' && |
| 86 | + typeof attributes[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE] === 'number' |
| 87 | + ) { |
| 88 | + attributes['gen_ai.usage.total_tokens'] = |
| 89 | + attributes[GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE] + attributes[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE]; |
| 90 | + } |
| 91 | + |
| 92 | + // Rename AI SDK attributes to standardized gen_ai attributes |
| 93 | + renameAttributeKey(attributes, AI_PROMPT_MESSAGES_ATTRIBUTE, 'gen_ai.request.messages'); |
| 94 | + renameAttributeKey(attributes, AI_RESPONSE_TEXT_ATTRIBUTE, 'gen_ai.response.text'); |
| 95 | + renameAttributeKey(attributes, AI_RESPONSE_TOOL_CALLS_ATTRIBUTE, 'gen_ai.response.tool_calls'); |
| 96 | + renameAttributeKey(attributes, AI_PROMPT_TOOLS_ATTRIBUTE, 'gen_ai.request.available_tools'); |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Renames an attribute key in the provided attributes object if the old key exists. |
| 101 | + * This function safely handles null and undefined values. |
| 102 | + */ |
| 103 | +function renameAttributeKey(attributes: Record<string, unknown>, oldKey: string, newKey: string): void { |
| 104 | + if (attributes[oldKey] != null) { |
| 105 | + attributes[newKey] = attributes[oldKey]; |
| 106 | + // eslint-disable-next-line @typescript-eslint/no-dynamic-delete |
| 107 | + delete attributes[oldKey]; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +function processToolCallSpan(span: Span, attributes: SpanAttributes): void { |
| 112 | + addOriginToSpan(span, 'auto.vercelai.otel'); |
| 113 | + span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'gen_ai.execute_tool'); |
| 114 | + span.setAttribute('gen_ai.tool.call.id', attributes[AI_TOOL_CALL_ID_ATTRIBUTE]); |
| 115 | + span.setAttribute('gen_ai.tool.name', attributes[AI_TOOL_CALL_NAME_ATTRIBUTE]); |
| 116 | + span.updateName(`execute_tool ${attributes[AI_TOOL_CALL_NAME_ATTRIBUTE]}`); |
| 117 | +} |
| 118 | + |
| 119 | +function processGenerateSpan(span: Span, name: string, attributes: SpanAttributes): void { |
| 120 | + addOriginToSpan(span, 'auto.vercelai.otel'); |
| 121 | + |
| 122 | + const nameWthoutAi = name.replace('ai.', ''); |
| 123 | + span.setAttribute('ai.pipeline.name', nameWthoutAi); |
| 124 | + span.updateName(nameWthoutAi); |
| 125 | + |
| 126 | + // If a Telemetry name is set and it is a pipeline span, use that as the operation name |
| 127 | + const functionId = attributes[AI_TELEMETRY_FUNCTION_ID_ATTRIBUTE]; |
| 128 | + if (functionId && typeof functionId === 'string' && name.split('.').length - 1 === 1) { |
| 129 | + span.updateName(`${nameWthoutAi} ${functionId}`); |
| 130 | + span.setAttribute('ai.pipeline.name', functionId); |
| 131 | + } |
| 132 | + |
| 133 | + if (attributes[AI_PROMPT_ATTRIBUTE]) { |
| 134 | + span.setAttribute('gen_ai.prompt', attributes[AI_PROMPT_ATTRIBUTE]); |
| 135 | + } |
| 136 | + if (attributes[AI_MODEL_ID_ATTRIBUTE] && !attributes[GEN_AI_RESPONSE_MODEL_ATTRIBUTE]) { |
| 137 | + span.setAttribute(GEN_AI_RESPONSE_MODEL_ATTRIBUTE, attributes[AI_MODEL_ID_ATTRIBUTE]); |
| 138 | + } |
| 139 | + span.setAttribute('ai.streaming', name.includes('stream')); |
| 140 | + |
| 141 | + // Generate Spans |
| 142 | + if (name === 'ai.generateText') { |
| 143 | + span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'gen_ai.invoke_agent'); |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + if (name === 'ai.generateText.doGenerate') { |
| 148 | + span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'gen_ai.generate_text'); |
| 149 | + span.updateName(`generate_text ${attributes[AI_MODEL_ID_ATTRIBUTE]}`); |
| 150 | + return; |
| 151 | + } |
| 152 | + |
| 153 | + if (name === 'ai.streamText') { |
| 154 | + span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'gen_ai.invoke_agent'); |
| 155 | + return; |
| 156 | + } |
| 157 | + |
| 158 | + if (name === 'ai.streamText.doStream') { |
| 159 | + span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'gen_ai.stream_text'); |
| 160 | + span.updateName(`stream_text ${attributes[AI_MODEL_ID_ATTRIBUTE]}`); |
| 161 | + return; |
| 162 | + } |
| 163 | + |
| 164 | + if (name === 'ai.generateObject') { |
| 165 | + span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'gen_ai.invoke_agent'); |
| 166 | + return; |
| 167 | + } |
| 168 | + |
| 169 | + if (name === 'ai.generateObject.doGenerate') { |
| 170 | + span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'gen_ai.generate_object'); |
| 171 | + span.updateName(`generate_object ${attributes[AI_MODEL_ID_ATTRIBUTE]}`); |
| 172 | + return; |
| 173 | + } |
| 174 | + |
| 175 | + if (name === 'ai.streamObject') { |
| 176 | + span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'gen_ai.invoke_agent'); |
| 177 | + return; |
| 178 | + } |
| 179 | + |
| 180 | + if (name === 'ai.streamObject.doStream') { |
| 181 | + span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'gen_ai.stream_object'); |
| 182 | + span.updateName(`stream_object ${attributes[AI_MODEL_ID_ATTRIBUTE]}`); |
| 183 | + return; |
| 184 | + } |
| 185 | + |
| 186 | + if (name === 'ai.embed') { |
| 187 | + span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'gen_ai.invoke_agent'); |
| 188 | + return; |
| 189 | + } |
| 190 | + |
| 191 | + if (name === 'ai.embed.doEmbed') { |
| 192 | + span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'gen_ai.embed'); |
| 193 | + span.updateName(`embed ${attributes[AI_MODEL_ID_ATTRIBUTE]}`); |
| 194 | + return; |
| 195 | + } |
| 196 | + |
| 197 | + if (name === 'ai.embedMany') { |
| 198 | + span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'gen_ai.invoke_agent'); |
| 199 | + return; |
| 200 | + } |
| 201 | + |
| 202 | + if (name === 'ai.embedMany.doEmbed') { |
| 203 | + span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'gen_ai.embed_many'); |
| 204 | + span.updateName(`embed_many ${attributes[AI_MODEL_ID_ATTRIBUTE]}`); |
| 205 | + return; |
| 206 | + } |
| 207 | + |
| 208 | + if (name.startsWith('ai.stream')) { |
| 209 | + span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, 'ai.run'); |
| 210 | + return; |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +/** |
| 215 | + * Add event processors to the given client to process Vercel AI spans. |
| 216 | + */ |
| 217 | +export function addVercelAiProcessors(client: Client): void { |
| 218 | + client.on('spanStart', onVercelAiSpanStart); |
| 219 | + // Note: We cannot do this on `spanEnd`, because the span cannot be mutated anymore at this point |
| 220 | + client.addEventProcessor(vercelAiEventProcessor); |
| 221 | +} |
0 commit comments