Skip to content

Commit 275793b

Browse files
committed
clean up code
1 parent 67e7d68 commit 275793b

3 files changed

Lines changed: 30 additions & 47 deletions

File tree

dev-packages/node-integration-tests/suites/tracing/vercelai/scenario-late-model-id.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ import * as Sentry from '@sentry/node';
22
import { generateText } from 'ai';
33

44
// Custom mock model that doesn't set modelId initially (simulates late model ID setting)
5-
// This tests that processEndedVercelAiSpan correctly sets the op even when
6-
// processGenerateSpan didn't run due to missing model ID at span start
5+
// This tests that the op is correctly set even when model ID is not available at span start.
6+
// The span name update (e.g., 'generate_text gpt-4') is skipped when model ID is missing.t
77
class LateModelIdMock {
88
specificationVersion = 'v1';
99
provider = 'late-model-provider';

dev-packages/node-integration-tests/suites/tracing/vercelai/test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -706,9 +706,8 @@ describe('Vercel AI integration', () => {
706706
transaction: 'main',
707707
spans: expect.arrayContaining([
708708
// The generateText span should have the correct op even though model ID was not available at span start
709-
// Since processGenerateSpan didn't run, the span description remains 'ai.generateText'
710709
expect.objectContaining({
711-
description: 'ai.generateText',
710+
description: 'generateText',
712711
op: 'gen_ai.invoke_agent',
713712
origin: 'auto.vercelai.otel',
714713
status: 'ok',
@@ -718,9 +717,9 @@ describe('Vercel AI integration', () => {
718717
'gen_ai.operation.name': 'ai.generateText',
719718
}),
720719
}),
721-
// The doGenerate span should also have the correct op
720+
// The doGenerate span - name stays as 'generateText.doGenerate' since model ID is missing
722721
expect.objectContaining({
723-
description: 'ai.generateText.doGenerate',
722+
description: 'generateText.doGenerate',
724723
op: 'gen_ai.generate_text',
725724
origin: 'auto.vercelai.otel',
726725
status: 'ok',

packages/core/src/tracing/vercel-ai/index.ts

Lines changed: 25 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -66,21 +66,10 @@ function onVercelAiSpanStart(span: Span): void {
6666
}
6767

6868
// Check if this is a Vercel AI span by name pattern.
69-
// We set origin even if model ID is missing, so processEndedVercelAiSpan
70-
// can still process the span when attributes are set late.
7169
if (!name.startsWith('ai.')) {
7270
return;
7371
}
7472

75-
addOriginToSpan(span, 'auto.vercelai.otel');
76-
77-
// The AI model ID must be defined for full generate span processing.
78-
// If it's not available at span start, processEndedVercelAiSpan will set the op.
79-
const aiModelId = attributes[AI_MODEL_ID_ATTRIBUTE];
80-
if (typeof aiModelId !== 'string' || !aiModelId) {
81-
return;
82-
}
83-
8473
processGenerateSpan(span, name, attributes);
8574
}
8675

@@ -119,21 +108,12 @@ function vercelAiEventProcessor(event: Event): Event {
119108
* Post-process spans emitted by the Vercel AI SDK.
120109
*/
121110
function processEndedVercelAiSpan(span: SpanJSON): void {
122-
const { data: attributes, origin, description: name } = span;
111+
const { data: attributes, origin } = span;
123112

124113
if (origin !== 'auto.vercelai.otel') {
125114
return;
126115
}
127116

128-
// Set span.op if it wasn't already set during span start
129-
// This can happen when the model attribute is set too late
130-
// Check for both undefined (OTel spans without op) and 'default'
131-
if ((!span.op || span.op === 'default') && name) {
132-
const op = getSpanOpFromName(name);
133-
span.op = op;
134-
attributes['sentry.op'] = op;
135-
}
136-
137117
renameAttributeKey(attributes, AI_USAGE_COMPLETION_TOKENS_ATTRIBUTE, GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE);
138118
renameAttributeKey(attributes, AI_USAGE_PROMPT_TOKENS_ATTRIBUTE, GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE);
139119
renameAttributeKey(attributes, AI_USAGE_CACHED_INPUT_TOKENS_ATTRIBUTE, GEN_AI_USAGE_INPUT_TOKENS_CACHED_ATTRIBUTE);
@@ -223,6 +203,8 @@ function processToolCallSpan(span: Span, attributes: SpanAttributes): void {
223203
}
224204

225205
function processGenerateSpan(span: Span, name: string, attributes: SpanAttributes): void {
206+
addOriginToSpan(span, 'auto.vercelai.otel');
207+
226208
const nameWthoutAi = name.replace('ai.', '');
227209
span.setAttribute('ai.pipeline.name', nameWthoutAi);
228210
span.updateName(nameWthoutAi);
@@ -248,27 +230,29 @@ function processGenerateSpan(span: Span, name: string, attributes: SpanAttribute
248230
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_OP, op);
249231
}
250232

251-
// Update span names for .do* spans to include the model ID
233+
// Update span names for .do* spans to include the model ID (only if model ID exists)
252234
const modelId = attributes[AI_MODEL_ID_ATTRIBUTE];
253-
switch (name) {
254-
case 'ai.generateText.doGenerate':
255-
span.updateName(`generate_text ${modelId}`);
256-
break;
257-
case 'ai.streamText.doStream':
258-
span.updateName(`stream_text ${modelId}`);
259-
break;
260-
case 'ai.generateObject.doGenerate':
261-
span.updateName(`generate_object ${modelId}`);
262-
break;
263-
case 'ai.streamObject.doStream':
264-
span.updateName(`stream_object ${modelId}`);
265-
break;
266-
case 'ai.embed.doEmbed':
267-
span.updateName(`embed ${modelId}`);
268-
break;
269-
case 'ai.embedMany.doEmbed':
270-
span.updateName(`embed_many ${modelId}`);
271-
break;
235+
if (modelId) {
236+
switch (name) {
237+
case 'ai.generateText.doGenerate':
238+
span.updateName(`generate_text ${modelId}`);
239+
break;
240+
case 'ai.streamText.doStream':
241+
span.updateName(`stream_text ${modelId}`);
242+
break;
243+
case 'ai.generateObject.doGenerate':
244+
span.updateName(`generate_object ${modelId}`);
245+
break;
246+
case 'ai.streamObject.doStream':
247+
span.updateName(`stream_object ${modelId}`);
248+
break;
249+
case 'ai.embed.doEmbed':
250+
span.updateName(`embed ${modelId}`);
251+
break;
252+
case 'ai.embedMany.doEmbed':
253+
span.updateName(`embed_many ${modelId}`);
254+
break;
255+
}
272256
}
273257
}
274258

0 commit comments

Comments
 (0)