Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import * as Sentry from '@sentry/node';
import { loggingTransport } from '@sentry-internal/node-integration-tests';

Sentry.init({
dsn: 'https://public@dsn.ingest.sentry.io/1337',
release: '1.0',
tracesSampleRate: 1.0,
sendDefaultPii: true,
transport: loggingTransport,
integrations: [
Sentry.vercelAIIntegration({
recordInputs: true,
recordOutputs: true,
enableTruncation: false,
}),
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as Sentry from '@sentry/node';
import { generateText } from 'ai';
import { MockLanguageModelV1 } from 'ai/test';

async function run() {
await Sentry.startSpan({ op: 'function', name: 'main' }, async () => {
// Multiple messages with long content (would normally be truncated and popped to last message only)
const longContent = 'A'.repeat(50_000);
await generateText({
experimental_telemetry: { isEnabled: true },
model: new MockLanguageModelV1({
doGenerate: async () => ({
rawCall: { rawPrompt: null, rawSettings: {} },
finishReason: 'stop',
usage: { promptTokens: 10, completionTokens: 5 },
text: 'Response',
}),
}),
messages: [
{ role: 'user', content: longContent },
{ role: 'assistant', content: 'Some reply' },
{ role: 'user', content: 'Follow-up question' },
],
});
});
}

run();
Original file line number Diff line number Diff line change
Expand Up @@ -950,4 +950,37 @@ describe('Vercel AI integration', () => {
.completed();
});
});

const longContent = 'A'.repeat(50_000);

createEsmAndCjsTests(
__dirname,
'scenario-no-truncation.mjs',
'instrument-no-truncation.mjs',
(createRunner, test) => {
test('does not truncate input messages when enableTruncation is false', async () => {
await createRunner()
.expect({
transaction: {
transaction: 'main',
spans: expect.arrayContaining([
// Multiple messages should all be preserved (no popping to last message only)
expect.objectContaining({
data: expect.objectContaining({
[GEN_AI_INPUT_MESSAGES_ATTRIBUTE]: JSON.stringify([
{ role: 'user', content: longContent },
{ role: 'assistant', content: 'Some reply' },
{ role: 'user', content: 'Follow-up question' },
]),
[GEN_AI_INPUT_MESSAGES_ORIGINAL_LENGTH_ATTRIBUTE]: 3,
}),
}),
]),
},
})
.start()
.completed();
});
},
);
});
13 changes: 10 additions & 3 deletions packages/core/src/tracing/vercel-ai/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable max-lines */
import type { Client } from '../../client';
import { getClient } from '../../currentScopes';
import { SEMANTIC_ATTRIBUTE_SENTRY_OP, SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '../../semanticAttributes';
import type { Event } from '../../types-hoist/event';
import type { Span, SpanAttributes, SpanAttributeValue, SpanJSON } from '../../types-hoist/span';
Expand Down Expand Up @@ -114,7 +115,13 @@ function onVercelAiSpanStart(span: Span): void {
return;
}

processGenerateSpan(span, name, attributes);
const client = getClient();
const integration = client?.getIntegrationByName('VercelAI') as
| { options?: { enableTruncation?: boolean } }
| undefined;
const enableTruncation = integration?.options?.enableTruncation ?? true;

processGenerateSpan(span, name, attributes, enableTruncation);
Comment on lines +122 to +124
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The enableTruncation option for vercelAIIntegration is only implemented for Node.js. On other platforms (Cloudflare, Deno, Vercel Edge), truncation is always enabled and cannot be configured.
Severity: MEDIUM

Suggested Fix

To ensure consistent behavior, extend the vercelAIIntegration in the Cloudflare, Deno, and Vercel Edge packages to accept and store an options object, similar to the Node.js implementation. Alternatively, document that the enableTruncation option is only available for the Node.js SDK.

Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: packages/core/src/tracing/vercel-ai/index.ts#L122-L124

Potential issue: The core tracing logic attempts to read
`integration?.options?.enableTruncation` to control message truncation for the Vercel AI
integration. However, the integration factory function was only updated to accept and
store these options for the Node.js SDK. For other platforms like Cloudflare, Deno, and
Vercel Edge, the integration object has no `options` property. The code defensively
falls back to `true`, meaning truncation is permanently enabled on these non-Node
platforms with no way for users to disable it, creating a feature disparity.

Did we get this right? 👍 / 👎 to inform future reviews.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicohrubec this is legit, e.g.:

Doesn't take any options.

}

function vercelAiEventProcessor(event: Event): Event {
Expand Down Expand Up @@ -396,7 +403,7 @@ function processToolCallSpan(span: Span, attributes: SpanAttributes): void {
}
}

function processGenerateSpan(span: Span, name: string, attributes: SpanAttributes): void {
function processGenerateSpan(span: Span, name: string, attributes: SpanAttributes, enableTruncation: boolean): void {
span.setAttribute(SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN, 'auto.vercelai.otel');

const nameWthoutAi = name.replace('ai.', '');
Expand All @@ -408,7 +415,7 @@ function processGenerateSpan(span: Span, name: string, attributes: SpanAttribute
span.setAttribute('gen_ai.function_id', functionId);
}

requestMessagesFromPrompt(span, attributes);
requestMessagesFromPrompt(span, attributes, enableTruncation);

if (attributes[AI_MODEL_ID_ATTRIBUTE] && !attributes[GEN_AI_RESPONSE_MODEL_ATTRIBUTE]) {
span.setAttribute(GEN_AI_RESPONSE_MODEL_ATTRIBUTE, attributes[AI_MODEL_ID_ATTRIBUTE]);
Expand Down
20 changes: 12 additions & 8 deletions packages/core/src/tracing/vercel-ai/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE,
GEN_AI_USAGE_OUTPUT_TOKENS_ATTRIBUTE,
} from '../ai/gen-ai-attributes';
import { extractSystemInstructions, getTruncatedJsonString } from '../ai/utils';
import { extractSystemInstructions, getJsonString, getTruncatedJsonString } from '../ai/utils';
import { toolCallSpanContextMap } from './constants';
import type { TokenSummary, ToolCallSpanContext } from './types';
import { AI_PROMPT_ATTRIBUTE, AI_PROMPT_MESSAGES_ATTRIBUTE } from './vercel-ai-attributes';
Expand Down Expand Up @@ -227,7 +227,7 @@ export function convertUserInputToMessagesFormat(userInput: string): { role: str
* Generate a request.messages JSON array from the prompt field in the
* invoke_agent op
*/
export function requestMessagesFromPrompt(span: Span, attributes: SpanAttributes): void {
export function requestMessagesFromPrompt(span: Span, attributes: SpanAttributes, enableTruncation: boolean): void {
if (
typeof attributes[AI_PROMPT_ATTRIBUTE] === 'string' &&
!attributes[GEN_AI_INPUT_MESSAGES_ATTRIBUTE] &&
Expand All @@ -247,11 +247,13 @@ export function requestMessagesFromPrompt(span: Span, attributes: SpanAttributes
}

const filteredLength = Array.isArray(filteredMessages) ? filteredMessages.length : 0;
const truncatedMessages = getTruncatedJsonString(filteredMessages);
const messagesJson = enableTruncation
? getTruncatedJsonString(filteredMessages)
: getJsonString(filteredMessages);

span.setAttributes({
[AI_PROMPT_ATTRIBUTE]: truncatedMessages,
[GEN_AI_INPUT_MESSAGES_ATTRIBUTE]: truncatedMessages,
[AI_PROMPT_ATTRIBUTE]: messagesJson,
[GEN_AI_INPUT_MESSAGES_ATTRIBUTE]: messagesJson,
[GEN_AI_INPUT_MESSAGES_ORIGINAL_LENGTH_ATTRIBUTE]: filteredLength,
});
}
Expand All @@ -268,11 +270,13 @@ export function requestMessagesFromPrompt(span: Span, attributes: SpanAttributes
}

const filteredLength = Array.isArray(filteredMessages) ? filteredMessages.length : 0;
const truncatedMessages = getTruncatedJsonString(filteredMessages);
const messagesJson = enableTruncation
? getTruncatedJsonString(filteredMessages)
: getJsonString(filteredMessages);

span.setAttributes({
[AI_PROMPT_MESSAGES_ATTRIBUTE]: truncatedMessages,
[GEN_AI_INPUT_MESSAGES_ATTRIBUTE]: truncatedMessages,
[AI_PROMPT_MESSAGES_ATTRIBUTE]: messagesJson,
[GEN_AI_INPUT_MESSAGES_ATTRIBUTE]: messagesJson,
[GEN_AI_INPUT_MESSAGES_ORIGINAL_LENGTH_ATTRIBUTE]: filteredLength,
});
}
Expand Down
6 changes: 6 additions & 0 deletions packages/node/src/integrations/tracing/vercelai/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ export interface VercelAiOptions {
* If you want to register the span processors even when the ai package usage cannot be detected, you can set `force` to `true`.
*/
force?: boolean;

/**
* Enable or disable truncation of recorded input messages.
* Defaults to `true`.
*/
enableTruncation?: boolean;
}

export interface VercelAiIntegration extends Integration {
Expand Down
Loading