@@ -24,21 +24,13 @@ import {
2424 GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE ,
2525 GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE ,
2626} from '../ai/gen-ai-attributes' ;
27- import {
28- DO_SPAN_NAME_PREFIX ,
29- EMBEDDINGS_OPS ,
30- GENERATE_CONTENT_OPS ,
31- INVOKE_AGENT_OPS ,
32- RERANK_OPS ,
33- toolCallSpanContextMap ,
34- } from './constants' ;
27+ import { SPAN_TO_OPERATION_NAME , toolCallSpanContextMap } from './constants' ;
3528import type { TokenSummary } from './types' ;
3629import {
3730 accumulateTokensForParent ,
3831 applyAccumulatedTokens ,
3932 applyToolDescriptionsAndTokens ,
4033 convertAvailableToolsToJsonString ,
41- getSpanOpFromName ,
4234 requestMessagesFromPrompt ,
4335} from './utils' ;
4436import type { OpenAiProviderMetadata , ProviderMetadata } from './vercel-ai-attributes' ;
@@ -66,32 +58,6 @@ import {
6658 OPERATION_NAME_ATTRIBUTE ,
6759} from './vercel-ai-attributes' ;
6860
69- /**
70- * Maps Vercel AI SDK operation names to OpenTelemetry semantic convention values
71- * @see https://opentelemetry.io/docs/specs/semconv/gen-ai/gen-ai-spans/#llm-request-spans
72- */
73- function mapVercelAiOperationName ( operationName : string ) : string {
74- // Top-level pipeline operations map to invoke_agent
75- if ( INVOKE_AGENT_OPS . has ( operationName ) ) {
76- return 'invoke_agent' ;
77- }
78- // .do* operations are the actual LLM calls
79- if ( GENERATE_CONTENT_OPS . has ( operationName ) ) {
80- return 'generate_content' ;
81- }
82- if ( EMBEDDINGS_OPS . has ( operationName ) ) {
83- return 'embeddings' ;
84- }
85- if ( RERANK_OPS . has ( operationName ) ) {
86- return 'rerank' ;
87- }
88- if ( operationName === 'ai.toolCall' ) {
89- return 'execute_tool' ;
90- }
91- // Return the original value for unknown operations
92- return operationName ;
93- }
94-
9561/**
9662 * Post-process spans emitted by the Vercel AI SDK.
9763 * This is supposed to be used in `client.on('spanStart', ...)
@@ -328,7 +294,7 @@ function processEndedVercelAiSpan(span: SpanJSON): void {
328294 const rawOperationName = attributes [ AI_OPERATION_ID_ATTRIBUTE ]
329295 ? ( attributes [ AI_OPERATION_ID_ATTRIBUTE ] as string )
330296 : ( attributes [ OPERATION_NAME_ATTRIBUTE ] as string ) ;
331- const operationName = mapVercelAiOperationName ( rawOperationName ) ;
297+ const operationName = SPAN_TO_OPERATION_NAME . get ( rawOperationName ) ?? rawOperationName ;
332298 attributes [ GEN_AI_OPERATION_NAME_ATTRIBUTE ] = operationName ;
333299 // eslint-disable-next-line @typescript-eslint/no-dynamic-delete
334300 delete attributes [ OPERATION_NAME_ATTRIBUTE ] ;
@@ -429,15 +395,17 @@ function processGenerateSpan(span: Span, name: string, attributes: SpanAttribute
429395 }
430396 span . setAttribute ( 'ai.streaming' , name . includes ( 'stream' ) ) ;
431397
432- // Set the op based on the span name
433- const op = getSpanOpFromName ( name ) ;
434- if ( op ) {
435- span . setAttribute ( SEMANTIC_ATTRIBUTE_SENTRY_OP , op ) ;
398+ // Set the op based on the operation name registry
399+ const operationName = SPAN_TO_OPERATION_NAME . get ( name ) ;
400+ if ( operationName ) {
401+ span . setAttribute ( SEMANTIC_ATTRIBUTE_SENTRY_OP , `gen_ai.${ operationName } ` ) ;
402+ } else if ( name . startsWith ( 'ai.stream' ) ) {
403+ span . setAttribute ( SEMANTIC_ATTRIBUTE_SENTRY_OP , 'ai.run' ) ;
436404 }
437405
438406 // For invoke_agent pipeline spans, use 'invoke_agent' as the description
439407 // to be consistent with other AI integrations (e.g. LangGraph)
440- if ( INVOKE_AGENT_OPS . has ( name ) ) {
408+ if ( operationName === 'invoke_agent' ) {
441409 if ( functionId && typeof functionId === 'string' ) {
442410 span . updateName ( `invoke_agent ${ functionId } ` ) ;
443411 } else {
@@ -447,11 +415,8 @@ function processGenerateSpan(span: Span, name: string, attributes: SpanAttribute
447415 }
448416
449417 const modelId = attributes [ AI_MODEL_ID_ATTRIBUTE ] ;
450- if ( modelId ) {
451- const doSpanPrefix = GENERATE_CONTENT_OPS . has ( name ) ? 'generate_content' : DO_SPAN_NAME_PREFIX [ name ] ;
452- if ( doSpanPrefix ) {
453- span . updateName ( `${ doSpanPrefix } ${ modelId } ` ) ;
454- }
418+ if ( modelId && operationName ) {
419+ span . updateName ( `${ operationName } ${ modelId } ` ) ;
455420 }
456421}
457422
0 commit comments