-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix(server-utils): Record Vercel AI experimental_telemetry.metadata on gen_ai spans
#24721
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import * as Sentry from '@sentry/node'; | ||
| import { embed, embedMany, generateText, streamText } from 'ai'; | ||
| import { MockEmbeddingModelV3, MockLanguageModelV3, simulateReadableStream } from 'ai/test'; | ||
|
|
||
| const usage = { | ||
| inputTokens: { total: 10, noCache: 10, cached: 0 }, | ||
| outputTokens: { total: 20, noCache: 20, cached: 0 }, | ||
| totalTokens: { total: 30, noCache: 30, cached: 0 }, | ||
| }; | ||
|
|
||
| async function run() { | ||
| await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { | ||
| await generateText({ | ||
| experimental_telemetry: { | ||
| isEnabled: true, | ||
| functionId: 'support-chat', | ||
| metadata: { requestId: 'req_generate', tenantId: 'acme' }, | ||
| }, | ||
| model: new MockLanguageModelV3({ | ||
| doGenerate: async () => ({ | ||
| finishReason: { unified: 'stop', raw: 'stop' }, | ||
| usage, | ||
| content: [{ type: 'text', text: 'Hello!' }], | ||
| warnings: [], | ||
| }), | ||
| }), | ||
| prompt: 'Hi', | ||
| }); | ||
|
|
||
| const result = streamText({ | ||
| experimental_telemetry: { | ||
| isEnabled: true, | ||
| functionId: 'support-stream', | ||
| metadata: { requestId: 'req_stream', tenantId: 'acme' }, | ||
| }, | ||
| model: new MockLanguageModelV3({ | ||
| doStream: async () => ({ | ||
| stream: simulateReadableStream({ | ||
| chunks: [ | ||
| { type: 'stream-start', warnings: [] }, | ||
| { type: 'text-start', id: '0' }, | ||
| { type: 'text-delta', id: '0', delta: 'Hello!' }, | ||
| { type: 'text-end', id: '0' }, | ||
| { type: 'finish', finishReason: { unified: 'stop', raw: 'stop' }, usage }, | ||
| ], | ||
| }), | ||
| }), | ||
| }), | ||
| prompt: 'Hi', | ||
| }); | ||
| for await (const _part of result.fullStream) { | ||
| void _part; | ||
| } | ||
|
|
||
| await embed({ | ||
| experimental_telemetry: { isEnabled: true, metadata: { requestId: 'req_embed', tenantId: 'acme' } }, | ||
| model: new MockEmbeddingModelV3({ | ||
| doEmbed: async () => ({ embeddings: [[0.1, 0.2, 0.3]], usage: { tokens: 10 } }), | ||
| }), | ||
| value: 'Embed me', | ||
| }); | ||
|
|
||
| await embedMany({ | ||
| experimental_telemetry: { isEnabled: true, metadata: { requestId: 'req_embed_many', tenantId: 'acme' } }, | ||
| model: new MockEmbeddingModelV3({ | ||
| maxEmbeddingsPerCall: 5, | ||
| doEmbed: async () => ({ | ||
| embeddings: [ | ||
| [0.1, 0.2, 0.3], | ||
| [0.4, 0.5, 0.6], | ||
| ], | ||
| usage: { tokens: 20 }, | ||
| }), | ||
| }), | ||
| values: ['First', 'Second'], | ||
| }); | ||
| }); | ||
|
|
||
| await Sentry.flush(2000); | ||
| } | ||
|
|
||
| run(); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,7 +33,7 @@ import { | |
| GEN_AI_INVOKE_AGENT, | ||
| GEN_AI_RERANK, | ||
| } from '@sentry/conventions/op'; | ||
| import type { Span, SpanAttributes } from '@sentry/core'; | ||
| import type { Span, SpanAttributes, SpanAttributeValue } from '@sentry/core'; | ||
| import { | ||
| _INTERNAL_skipAiProviderWrapping, | ||
| captureException, | ||
|
|
@@ -86,6 +86,7 @@ type GenAiOperation = keyof typeof GEN_AI_OPERATION_SPAN_OPS; | |
| const VERCEL_AI_OPERATION_ID_ATTRIBUTE = 'vercel.ai.operationId'; | ||
| const VERCEL_AI_MODEL_PROVIDER_ATTRIBUTE = 'vercel.ai.model.provider'; | ||
| const VERCEL_AI_SETTINGS_MAX_RETRIES_ATTRIBUTE = 'vercel.ai.settings.maxRetries'; | ||
| const VERCEL_AI_TELEMETRY_METADATA_ATTRIBUTE_PREFIX = 'vercel.ai.telemetry.metadata.'; | ||
|
|
||
| // Tracks the top-level operationId (and whether it streams) per `callId` so a model-call span can | ||
| // name its `doGenerate`/`doStream` operation the same way the OTel integration does. `isStream` is | ||
|
|
@@ -417,8 +418,9 @@ export function createSpanFromMessage( | |
| recordToolDescriptions(callId, event.tools); | ||
| } | ||
|
|
||
| const baseAttributes: Record<string, string | number | boolean> = { | ||
| const baseAttributes: SpanAttributes = { | ||
| [SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: ORIGIN, | ||
| ...telemetryMetadataAttributes(event.telemetryMetadata), | ||
| ...(provider ? { [GEN_AI_PROVIDER_NAME]: provider, [VERCEL_AI_MODEL_PROVIDER_ATTRIBUTE]: provider } : {}), | ||
| ...(modelId ? { [GEN_AI_REQUEST_MODEL]: modelId } : {}), | ||
| ...(maxRetries !== undefined ? { [VERCEL_AI_SETTINGS_MAX_RETRIES_ATTRIBUTE]: maxRetries } : {}), | ||
|
|
@@ -466,6 +468,21 @@ export function createSpanFromMessage( | |
| } | ||
| } | ||
|
|
||
| /** | ||
| * `experimental_telemetry.metadata` (`ai` <= 6) as `vercel.ai.telemetry.metadata.<key>`, the names the OTel | ||
| * integration produced from the SDK's `ai.telemetry.metadata.*`. Only the orchestrion adapter sets | ||
| * `event.telemetryMetadata`; `ai` 7 has no `telemetry.metadata`. | ||
| */ | ||
| function telemetryMetadataAttributes(metadata: unknown): SpanAttributes { | ||
| const attributes: SpanAttributes = {}; | ||
| if (isObjectLike(metadata)) { | ||
| for (const [key, value] of Object.entries(metadata)) { | ||
| attributes[`${VERCEL_AI_TELEMETRY_METADATA_ATTRIBUTE_PREFIX}${key}`] = value as SpanAttributeValue; | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: The Suggested FixBefore assigning the value as a span attribute, check if it is a primitive type. If it is not a primitive, attempt to serialize it using Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
| } | ||
| return attributes; | ||
| } | ||
|
|
||
| /** Start a `gen_ai.<operation>` span named `<operation> <suffix>` (or just `<operation>` when no suffix). */ | ||
| function startGenAiSpan(operation: GenAiOperation, suffix: string | undefined, attributes: SpanAttributes): Span { | ||
| return startInactiveSpan({ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if this should be gated on recordInputs or dataCollection.genAI