Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
fix(vercel-ai): prevent tool call span map memory leak#19328
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
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
60d9f62
fix(vercel-ai): prevent tool call span map memory leak
lithdew 82d683c
some renames
nicohrubec 9214034
rename
nicohrubec a10e647
fix typo
nicohrubec 744d63d
remove not needed stuff and update tests
nicohrubec 3ef0c4a
renames
nicohrubec f698420
refactor
nicohrubec 2d814ef
yarn fix
nicohrubec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 63 additions & 47 deletions
110 packages/node/src/integrations/tracing/vercelai/instrumentation.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,9 +1,8 @@ | ||
| import type { InstrumentationConfig, InstrumentationModuleDefinition } from '@opentelemetry/instrumentation'; | ||
| import { InstrumentationBase, InstrumentationNodeModuleDefinition } from '@opentelemetry/instrumentation'; | ||
| import type { Span } from '@sentry/core'; | ||
| import { | ||
| _INTERNAL_cleanupToolCallSpan, | ||
| _INTERNAL_getSpanForToolCallId, | ||
| _INTERNAL_cleanupToolCallSpanContext, | ||
| _INTERNAL_getSpanContextForToolCallId, | ||
| addNonEnumerableProperty, | ||
| captureException, | ||
| getActiveSpan, | ||
| @@ -71,10 +70,12 @@ function isToolError(obj: unknown): obj is ToolError { | ||
| } | ||
| /** | ||
| * Check for tool errors in the result and capture them | ||
| * Tool errors are not rejected in Vercel V5, it is added as metadata to the result content | ||
| * Process tool call results: capture tool errors and clean up span context mappings. | ||
| * | ||
| * Error checking runs first (needs span context for linking), then cleanup removes all entries. | ||
| * Tool errors are not rejected in Vercel AI V5 — they appear as metadata in the result content. | ||
| */ | ||
| function checkResultForToolErrors(result: unknown): void { | ||
| export function processToolCallResults(result: unknown): void { | ||
| if (typeof result !== 'object' || result === null || !('content' in result)) { | ||
| return; | ||
| } | ||
| @@ -84,53 +85,68 @@ function checkResultForToolErrors(result: unknown): void { | ||
| return; | ||
| } | ||
| for (const item of resultObj.content) { | ||
| if (isToolError(item)) { | ||
| // Try to get the span associated with this tool call ID | ||
| const associatedSpan = _INTERNAL_getSpanForToolCallId(item.toolCallId) as Span; | ||
| captureToolErrors(resultObj.content); | ||
| cleanupToolCallSpanContexts(resultObj.content); | ||
| } | ||
| if (associatedSpan) { | ||
| // We have the span, so link the error using span and trace IDs from the span | ||
| const spanContext = associatedSpan.spanContext(); | ||
| function captureToolErrors(content: Array<object>): void { | ||
| for (const item of content) { | ||
| if (!isToolError(item)) { | ||
| continue; | ||
| } | ||
| withScope(scope => { | ||
| // Set the span and trace context for proper linking | ||
| scope.setContext('trace', { | ||
| trace_id: spanContext.traceId, | ||
| span_id: spanContext.spanId, | ||
| }); | ||
| // Try to get the span context associated with this tool call ID | ||
| const spanContext = _INTERNAL_getSpanContextForToolCallId(item.toolCallId); | ||
| scope.setTag('vercel.ai.tool.name', item.toolName); | ||
| scope.setTag('vercel.ai.tool.callId', item.toolCallId); | ||
| if (spanContext) { | ||
| // We have the span context, so link the error using span and trace IDs | ||
| withScope(scope => { | ||
| scope.setContext('trace', { | ||
| trace_id: spanContext.traceId, | ||
| span_id: spanContext.spanId, | ||
| }); | ||
| scope.setLevel('error'); | ||
| scope.setTag('vercel.ai.tool.name', item.toolName); | ||
| scope.setTag('vercel.ai.tool.callId', item.toolCallId); | ||
| scope.setLevel('error'); | ||
| captureException(item.error, { | ||
| mechanism: { | ||
| type: 'auto.vercelai.otel', | ||
| handled: false, | ||
| }, | ||
| }); | ||
| captureException(item.error, { | ||
| mechanism: { | ||
| type: 'auto.vercelai.otel', | ||
| handled: false, | ||
| }, | ||
| }); | ||
| // Clean up the span mapping since we've processed this tool error | ||
| // We won't get multiple { type: 'tool-error' } parts for the same toolCallId. | ||
| _INTERNAL_cleanupToolCallSpan(item.toolCallId); | ||
| } else { | ||
| // Fallback: capture without span linking | ||
| withScope(scope => { | ||
| scope.setTag('vercel.ai.tool.name', item.toolName); | ||
| scope.setTag('vercel.ai.tool.callId', item.toolCallId); | ||
| scope.setLevel('error'); | ||
| captureException(item.error, { | ||
| mechanism: { | ||
| type: 'auto.vercelai.otel', | ||
| handled: false, | ||
| }, | ||
| }); | ||
| }); | ||
| } else { | ||
| // Fallback: capture without span linking | ||
| withScope(scope => { | ||
| scope.setTag('vercel.ai.tool.name', item.toolName); | ||
| scope.setTag('vercel.ai.tool.callId', item.toolCallId); | ||
| scope.setLevel('error'); | ||
| captureException(item.error, { | ||
| mechanism: { | ||
| type: 'auto.vercelai.otel', | ||
| handled: false, | ||
| }, | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
| } | ||
| } | ||
| /** | ||
| * Remove span context entries for all completed tool calls in the content array. | ||
| */ | ||
| export function cleanupToolCallSpanContexts(content: Array<object>): void { | ||
| for (const item of content) { | ||
| if ( | ||
| typeof item === 'object' && | ||
| item !== null && | ||
| 'toolCallId' in item && | ||
| typeof (item as Record<string, unknown>).toolCallId === 'string' | ||
| ) { | ||
| _INTERNAL_cleanupToolCallSpanContext((item as Record<string, unknown>).toolCallId as string); | ||
| } | ||
| } | ||
| } | ||
| @@ -252,7 +268,7 @@ export class SentryVercelAiInstrumentation extends InstrumentationBase { | ||
| }, | ||
| () => {}, | ||
| result => { | ||
| checkResultForToolErrors(result); | ||
| processToolCallResults(result); | ||
| }, | ||
| ); | ||
| }, | ||
sentry[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
55 changes: 53 additions & 2 deletions
55 packages/node/test/integrations/tracing/vercelai/instrumentation.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.