diff --git a/dev-packages/cloudflare-integration-tests/suites/tracing/workers-ai/test.ts b/dev-packages/cloudflare-integration-tests/suites/tracing/workers-ai/test.ts index 18d5f5a72543..01b6a7cdc0f3 100644 --- a/dev-packages/cloudflare-integration-tests/suites/tracing/workers-ai/test.ts +++ b/dev-packages/cloudflare-integration-tests/suites/tracing/workers-ai/test.ts @@ -8,6 +8,7 @@ import { GEN_AI_RESPONSE_MODEL, GEN_AI_RESPONSE_STREAMING, GEN_AI_RESPONSE_TEXT, + GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, GEN_AI_USAGE_INPUT_TOKENS, GEN_AI_USAGE_OUTPUT_TOKENS, GEN_AI_USAGE_TOTAL_TOKENS, @@ -122,6 +123,7 @@ it('traces a streaming Workers AI text generation request', async ({ signal }) = type: 'string', value: 'The capital of France is Paris.', }, + [GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK]: { value: expect.any(Number), type: 'double' }, [SENTRY_IS_LOCALHOST]: { value: true, type: 'boolean' }, [SENTRY_TRACE_LIFECYCLE]: { value: 'stream', type: 'string' }, [SENTRY_SEGMENT_NAME]: { value: segmentSpan!.name, type: 'string' }, diff --git a/dev-packages/node-integration-tests/suites/tracing/anthropic/test.ts b/dev-packages/node-integration-tests/suites/tracing/anthropic/test.ts index 54dd27321cd1..3657b275a680 100644 --- a/dev-packages/node-integration-tests/suites/tracing/anthropic/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/anthropic/test.ts @@ -11,6 +11,7 @@ import { GEN_AI_RESPONSE_ID, GEN_AI_RESPONSE_MODEL, GEN_AI_RESPONSE_STREAMING, + GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, GEN_AI_RESPONSE_TEXT, GEN_AI_RESPONSE_TOOL_CALLS, GEN_AI_SYSTEM_INSTRUCTIONS, @@ -116,6 +117,7 @@ describe('Anthropic integration', () => { expect(streamingSpan!.name).toBe('chat claude-3-haiku-20240307'); expect(streamingSpan!.status).toBe('ok'); expect(streamingSpan!.attributes[GEN_AI_RESPONSE_STREAMING].value).toBe(true); + expect(streamingSpan!.attributes[GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK]?.value).toEqual(expect.any(Number)); }); }); diff --git a/dev-packages/node-integration-tests/suites/tracing/google-genai/test.ts b/dev-packages/node-integration-tests/suites/tracing/google-genai/test.ts index afc273ab86c5..644422aead61 100644 --- a/dev-packages/node-integration-tests/suites/tracing/google-genai/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/google-genai/test.ts @@ -14,6 +14,7 @@ import { GEN_AI_RESPONSE_ID, GEN_AI_RESPONSE_MODEL, GEN_AI_RESPONSE_STREAMING, + GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, GEN_AI_RESPONSE_TEXT, GEN_AI_RESPONSE_TOOL_CALLS, GEN_AI_SYSTEM_INSTRUCTIONS, @@ -338,6 +339,7 @@ describe('Google GenAI integration', () => { expect(generateContentSpan!.status).toBe('ok'); expect(generateContentSpan!.attributes[GEN_AI_OPERATION_NAME].value).toBe('generate_content'); expect(generateContentSpan!.attributes[GEN_AI_RESPONSE_STREAMING].value).toBe(true); + expect(generateContentSpan!.attributes[GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK]?.value).toEqual(expect.any(Number)); expect(generateContentSpan!.attributes[GEN_AI_REQUEST_TEMPERATURE].value).toBe(0.7); expect(generateContentSpan!.attributes[GEN_AI_REQUEST_TOP_P].value).toBe(0.9); expect(generateContentSpan!.attributes[GEN_AI_REQUEST_MAX_TOKENS].value).toBe(100); diff --git a/dev-packages/node-integration-tests/suites/tracing/mastra/nesting/test.ts b/dev-packages/node-integration-tests/suites/tracing/mastra/nesting/test.ts index 5e2aee55e7d8..1d27cdaeda00 100644 --- a/dev-packages/node-integration-tests/suites/tracing/mastra/nesting/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/mastra/nesting/test.ts @@ -1,4 +1,10 @@ -import { DB_OPERATION_NAME, GEN_AI_TOOL_NAME, SENTRY_OP, URL_FULL } from '@sentry/conventions/attributes'; +import { + DB_OPERATION_NAME, + GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, + GEN_AI_TOOL_NAME, + SENTRY_OP, + URL_FULL, +} from '@sentry/conventions/attributes'; import { GEN_AI_CHAT, GEN_AI_EXECUTE_TOOL } from '@sentry/conventions/op'; import { afterAll, expect, describe } from 'vitest'; import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../../utils/runner'; @@ -94,6 +100,7 @@ describe.runIf(supports({ min: 22 }))('Mastra integration', () => { const cacheGetParentIds = loadSpans.map(span => span.parent_span_id); const chat = container.items.find(span => span.attributes[SENTRY_OP]?.value === GEN_AI_CHAT)!; expect(chat).toBeDefined(); + expect(chat.attributes[GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK]?.value).toEqual(expect.any(Number)); const chatSpanId = chat.span_id; const executeTool = container.items.find( diff --git a/dev-packages/node-integration-tests/suites/tracing/mistral/test.ts b/dev-packages/node-integration-tests/suites/tracing/mistral/test.ts index 51fd5b80a3bf..2f4619bb0683 100644 --- a/dev-packages/node-integration-tests/suites/tracing/mistral/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/mistral/test.ts @@ -11,6 +11,7 @@ import { GEN_AI_RESPONSE_ID, GEN_AI_RESPONSE_MODEL, GEN_AI_RESPONSE_STREAMING, + GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, GEN_AI_OUTPUT_MESSAGES, GEN_AI_RESPONSE_TEXT, GEN_AI_RESPONSE_TOOL_CALLS, @@ -70,6 +71,7 @@ describe('Mistral integration', () => { expect(streamSpan!.name).toBe('chat mistral-large-latest'); expect(streamSpan!.attributes[GEN_AI_OPERATION_NAME]?.value).toBe('chat'); expect(streamSpan!.attributes[GEN_AI_RESPONSE_STREAMING]?.value).toBe(true); + expect(streamSpan!.attributes[GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK]?.value).toEqual(expect.any(Number)); expect(streamSpan!.attributes[GEN_AI_USAGE_TOTAL_TOKENS]?.value).toBe(30); // `chat.stream()` takes no `stream` request field, so the flag has to come from the method. diff --git a/dev-packages/node-integration-tests/suites/tracing/openai/v5/chat/test.ts b/dev-packages/node-integration-tests/suites/tracing/openai/v5/chat/test.ts index 33f80469f745..0b4be1199e21 100644 --- a/dev-packages/node-integration-tests/suites/tracing/openai/v5/chat/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/openai/v5/chat/test.ts @@ -11,6 +11,7 @@ import { GEN_AI_RESPONSE_ID, GEN_AI_RESPONSE_MODEL, GEN_AI_RESPONSE_STREAMING, + GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, GEN_AI_RESPONSE_TEXT, GEN_AI_RESPONSE_TOOL_CALLS, GEN_AI_SYSTEM_INSTRUCTIONS, @@ -236,6 +237,9 @@ describe('OpenAI integration', () => { type: 'boolean', value: true, }); + expect(streamingChatCompletionSpan!.attributes[GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK]?.value).toEqual( + expect.any(Number), + ); const streamingResponsesSpan = spans.find( span => span.attributes[GEN_AI_RESPONSE_ID]?.value === 'resp_stream_456', @@ -294,6 +298,9 @@ describe('OpenAI integration', () => { type: 'boolean', value: true, }); + expect(streamingResponsesSpan!.attributes[GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK]?.value).toEqual( + expect.any(Number), + ); const streamingErrorSpan = spans.find( span => span.name === 'chat error-model' && span.attributes[GEN_AI_REQUEST_STREAM_ATTRIBUTE]?.value === true, diff --git a/dev-packages/node-integration-tests/suites/tracing/pi-ai/test.ts b/dev-packages/node-integration-tests/suites/tracing/pi-ai/test.ts index 995bcb7858fa..1bff015fffba 100644 --- a/dev-packages/node-integration-tests/suites/tracing/pi-ai/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/pi-ai/test.ts @@ -7,6 +7,7 @@ import { GEN_AI_REQUEST_MODEL, GEN_AI_RESPONSE_FINISH_REASONS, GEN_AI_RESPONSE_STREAMING, + GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, GEN_AI_SYSTEM_INSTRUCTIONS, GEN_AI_TOOL_DEFINITIONS, GEN_AI_USAGE_INPUT_TOKENS, @@ -60,6 +61,7 @@ describe.runIf(supports({ min: 22 }))('pi-ai integration', () => { expect(toolCall!.attributes[GEN_AI_PROVIDER_NAME]?.value).toBe('faux'); expect(toolCall!.attributes[GEN_AI_REQUEST_MODEL]?.value).toBe('faux-model'); expect(toolCall!.attributes[GEN_AI_RESPONSE_STREAMING]?.value).toBe(true); + expect(toolCall!.attributes[GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK]?.value).toBeGreaterThan(0); expect(toolCall!.attributes[GEN_AI_SYSTEM_INSTRUCTIONS]?.value).toBe('You are a weather assistant.'); expect(toolCall!.attributes[GEN_AI_TOOL_DEFINITIONS]?.value).toContain('get_weather'); expect(toolCall!.attributes[GEN_AI_RESPONSE_FINISH_REASONS]?.value).toBe('["tool_call"]'); @@ -75,6 +77,7 @@ describe.runIf(supports({ min: 22 }))('pi-ai integration', () => { ]); expect(answer!.attributes[GEN_AI_RESPONSE_STREAMING]).toBeUndefined(); + expect(answer!.attributes[GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK]).toBeUndefined(); expect(answer!.attributes[GEN_AI_RESPONSE_FINISH_REASONS]?.value).toBe('["stop"]'); expect( JSON.parse(String(answer!.attributes[GEN_AI_INPUT_MESSAGES]?.value)).map( diff --git a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/suite.ts b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/suite.ts index e90fc23136d9..8f2eac8f34b4 100644 --- a/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/suite.ts +++ b/dev-packages/node-integration-tests/suites/tracing/vercelai/v6_v7/suite.ts @@ -11,6 +11,7 @@ import { GEN_AI_PROVIDER_NAME, GEN_AI_REQUEST_MODEL, GEN_AI_RESPONSE_FINISH_REASONS, + GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, GEN_AI_RESPONSE_MODEL, GEN_AI_SYSTEM_INSTRUCTIONS, GEN_AI_TOOL_CALL_ARGUMENTS, @@ -1123,6 +1124,9 @@ export function registerVercelAiStreamingTests(version: '6' | '7', vercelAiVersi expect(generateContent.attributes[GEN_AI_OUTPUT_MESSAGES]?.value).toBe( '[{"role":"assistant","parts":[{"type":"text","content":"Stream response!"}],"finish_reason":"stop"}]', ); + expect(generateContent.attributes[GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK]?.value).toEqual( + expect.any(Number), + ); // The summed usage and output also land on the parent invoke_agent span, whose own // channel result is otherwise undefined for a stream. diff --git a/packages/server-utils/src/ai/anthropic-ai/streaming.ts b/packages/server-utils/src/ai/anthropic-ai/streaming.ts index 8931d6098b8d..134a764eafc1 100644 --- a/packages/server-utils/src/ai/anthropic-ai/streaming.ts +++ b/packages/server-utils/src/ai/anthropic-ai/streaming.ts @@ -1,6 +1,6 @@ import { captureException, SPAN_STATUS_ERROR } from '@sentry/core'; import type { Span } from '@sentry/core'; -import { endStreamSpan, getErrorStatus } from '../core/utils'; +import { endStreamSpan, getErrorStatus, recordTimeToFirstChunk } from '../core/utils'; import type { AnthropicAiStreamingEvent } from './types'; import { mapAnthropicErrorToStatusMessage } from './utils'; @@ -206,6 +206,8 @@ function processEvent( const isError = isErrorEvent(event, span); if (isError) return; + recordTimeToFirstChunk(span); + handleMessageMetadata(event, state); // Tool call events are sent via 3 separate events: diff --git a/packages/server-utils/src/ai/core/utils.ts b/packages/server-utils/src/ai/core/utils.ts index 816c023ab08e..4b9e56e7347b 100644 --- a/packages/server-utils/src/ai/core/utils.ts +++ b/packages/server-utils/src/ai/core/utils.ts @@ -2,7 +2,15 @@ /** * Shared utils for AI integrations (OpenAI, Anthropic, Verce.AI, etc.) */ -import { getClient, isObjectLike, isThenable, SPAN_STATUS_ERROR, stringify } from '@sentry/core'; +import { + getClient, + isObjectLike, + isThenable, + SPAN_STATUS_ERROR, + spanToJSON, + stringify, + timestampInSeconds, +} from '@sentry/core'; import type { Span, SpanStatus } from '@sentry/core'; import { GEN_AI_OUTPUT_MESSAGES, @@ -11,6 +19,7 @@ import { GEN_AI_RESPONSE_MODEL, GEN_AI_RESPONSE_STREAMING, GEN_AI_RESPONSE_TEXT, + GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, GEN_AI_RESPONSE_TOOL_CALLS, GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, @@ -264,6 +273,17 @@ export function markCancelledIfAborted(span: Span, abortSignal: AbortSignal | un } } +/** + * Sets `gen_ai.response.time_to_first_chunk` on a streamed span, in seconds from the span start. + * Call it for each received chunk. Only the first call per span records a value. + */ +export function recordTimeToFirstChunk(span: Span): void { + const { attributes, start_timestamp } = spanToJSON(span); + if (attributes[GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK] === undefined) { + span.setAttribute(GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, timestampInSeconds() - start_timestamp); + } +} + /** * Ends a streaming span by setting all accumulated response attributes and ending the span. * Shared across OpenAI, Anthropic, and Google GenAI streaming implementations. diff --git a/packages/server-utils/src/ai/google-genai/streaming.ts b/packages/server-utils/src/ai/google-genai/streaming.ts index 2a822e3a08c2..61d122521b80 100644 --- a/packages/server-utils/src/ai/google-genai/streaming.ts +++ b/packages/server-utils/src/ai/google-genai/streaming.ts @@ -1,6 +1,6 @@ import { captureException, SPAN_STATUS_ERROR } from '@sentry/core'; import type { Span } from '@sentry/core'; -import { endStreamSpan, getErrorStatus } from '../core/utils'; +import { endStreamSpan, getErrorStatus, recordTimeToFirstChunk } from '../core/utils'; import type { GoogleGenAIResponse } from './types'; import type { MessagePart } from './utils'; import { candidatesToMessageParts, setOutputMessagesAttribute } from './utils'; @@ -111,6 +111,7 @@ function handleCandidateContent(chunk: GoogleGenAIResponse, state: StreamingStat */ function processChunk(chunk: GoogleGenAIResponse, state: StreamingState, recordOutputs: boolean, span: Span): void { if (!chunk || isErrorChunk(chunk, span)) return; + recordTimeToFirstChunk(span); handleResponseMetadata(chunk, state); handleCandidateContent(chunk, state, recordOutputs); } diff --git a/packages/server-utils/src/ai/langchain/index.ts b/packages/server-utils/src/ai/langchain/index.ts index e05ea6f38a81..58d5501ad0b8 100644 --- a/packages/server-utils/src/ai/langchain/index.ts +++ b/packages/server-utils/src/ai/langchain/index.ts @@ -28,7 +28,7 @@ import { } from '@sentry/conventions/attributes'; import { GEN_AI_CHAT, GEN_AI_EXECUTE_TOOL, GEN_AI_INVOKE_AGENT } from '@sentry/conventions/op'; import { DEBUG_BUILD } from '../../debug-build'; -import { resolveAIRecordingOptions } from '../core/utils'; +import { recordTimeToFirstChunk, resolveAIRecordingOptions } from '../core/utils'; import { LANGCHAIN_ORIGIN, MAX_TRACKED_LANGCHAIN_RUNS } from './constants'; import { addTypeSafeClassifierResponseAttributes, @@ -269,6 +269,14 @@ export function createLangChainCallbackHandler(options: LangChainOptions = {}): ); }, + // Called for each streamed chunk. + handleLLMNewToken(_token: string, _idx: unknown, runId: string) { + const span = runs.get(runId)?.span; + if (span) { + recordTimeToFirstChunk(span); + } + }, + // LLM End Handler - note: handleLLMEnd with capital LLM (used by both LLMs and chat models!) handleLLMEnd( output: unknown, diff --git a/packages/server-utils/src/ai/mastra/types.ts b/packages/server-utils/src/ai/mastra/types.ts index 5de6ad91efcc..56bd69fbd257 100644 --- a/packages/server-utils/src/ai/mastra/types.ts +++ b/packages/server-utils/src/ai/mastra/types.ts @@ -48,6 +48,8 @@ export interface MastraSpanAttributes { responseId?: string; finishReason?: string; streaming?: boolean; + /** When the first streamed chunk arrived. */ + completionStartTime?: Date; usage?: MastraUsageStats; parameters?: { temperature?: number; diff --git a/packages/server-utils/src/ai/mastra/utils.ts b/packages/server-utils/src/ai/mastra/utils.ts index 3f6d32ed8424..ddf66db60968 100644 --- a/packages/server-utils/src/ai/mastra/utils.ts +++ b/packages/server-utils/src/ai/mastra/utils.ts @@ -22,6 +22,7 @@ import { GEN_AI_RESPONSE_ID, GEN_AI_RESPONSE_MODEL, GEN_AI_RESPONSE_STREAMING, + GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, GEN_AI_RESPONSE_TEXT, GEN_AI_SYSTEM_INSTRUCTIONS, GEN_AI_TOOL_CALL_ARGUMENTS, @@ -212,6 +213,9 @@ function addModelAttributes( attributes[GEN_AI_RESPONSE_MODEL] = attrs.responseModel; attributes[GEN_AI_RESPONSE_ID] = attrs.responseId; attributes[GEN_AI_RESPONSE_STREAMING] = attrs.streaming; + attributes[GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK] = attrs.completionStartTime + ? (attrs.completionStartTime.getTime() - span.startTime.getTime()) / 1000 + : undefined; attributes[GEN_AI_RESPONSE_FINISH_REASONS] = attrs.finishReason ? JSON.stringify([attrs.finishReason]) : undefined; attributes[GEN_AI_AGENT_NAME] = span.entityName; diff --git a/packages/server-utils/src/ai/mistral/streaming.ts b/packages/server-utils/src/ai/mistral/streaming.ts index 47ff72038867..8eb1a7a6a661 100644 --- a/packages/server-utils/src/ai/mistral/streaming.ts +++ b/packages/server-utils/src/ai/mistral/streaming.ts @@ -1,5 +1,5 @@ import type { Span } from '@sentry/core'; -import { endStreamSpan, getErrorStatus, setOutputMessagesAttribute } from '../core/utils'; +import { endStreamSpan, getErrorStatus, recordTimeToFirstChunk, setOutputMessagesAttribute } from '../core/utils'; import type { MistralCompletionChunk, MistralToolCall } from './types'; import { contentToString } from './utils'; @@ -248,7 +248,9 @@ export function instrumentEventStream( let settled = false; let cancelled = false; + // Called for each received event. const claim = (candidate: StreamConsumer): boolean => { + recordTimeToFirstChunk(span); consumer ??= candidate; return consumer === candidate; }; diff --git a/packages/server-utils/src/ai/openai/streaming.ts b/packages/server-utils/src/ai/openai/streaming.ts index dcd25abbf612..55549bd5c6eb 100644 --- a/packages/server-utils/src/ai/openai/streaming.ts +++ b/packages/server-utils/src/ai/openai/streaming.ts @@ -1,7 +1,7 @@ import { captureException, SPAN_STATUS_ERROR } from '@sentry/core'; import type { Span } from '@sentry/core'; import { getToolCallKey } from '../core/streaming-tool-calls'; -import { endStreamSpan } from '../core/utils'; +import { endStreamSpan, recordTimeToFirstChunk } from '../core/utils'; import { RESPONSE_EVENT_TYPES } from './constants'; import type { ChatCompletionChunk, @@ -241,6 +241,7 @@ export async function* instrumentStream( try { for await (const event of stream) { + recordTimeToFirstChunk(span); if (isChatCompletionChunk(event)) { processChatCompletionChunk(event, state, recordOutputs); } else if (isResponsesApiStreamEvent(event)) { diff --git a/packages/server-utils/src/ai/pi-ai/chat.ts b/packages/server-utils/src/ai/pi-ai/chat.ts index c7f8b10dc511..742b90320b65 100644 --- a/packages/server-utils/src/ai/pi-ai/chat.ts +++ b/packages/server-utils/src/ai/pi-ai/chat.ts @@ -28,6 +28,7 @@ import { SERVER_PORT, } from '@sentry/conventions/attributes'; import { GEN_AI_CHAT } from '@sentry/conventions/op'; +import { recordTimeToFirstChunk } from '../core/utils'; import { PI_AI_ORIGIN } from './constants'; import type { PiAiContext } from './messages'; import { @@ -37,7 +38,7 @@ import { piAiSystemInstructions, piAiToolDefinitions, } from './messages'; -import type { PiAssistantMessage, PiModel, PiStreamOptions } from './types'; +import type { PiAssistantMessage, PiEventStream, PiModel, PiStreamOptions } from './types'; import { setPiAiUsageAttributes } from './usage'; interface PiAiChatRequest { @@ -118,3 +119,23 @@ function getRequestContentAttributes(context: PiAiContext): Record | undefined): void { + const push = events?.push; + if (!events || typeof push !== 'function') { + return; + } + events.push = function (this: unknown, event: unknown) { + // Providers push `start` before the response arrives. + if ((event as { type?: unknown } | undefined)?.type !== 'start') { + recordTimeToFirstChunk(span); + events.push = push; + } + return push.call(this, event); + }; +} diff --git a/packages/server-utils/src/ai/pi-ai/types.ts b/packages/server-utils/src/ai/pi-ai/types.ts index f2355d1044d8..37791a0fcce2 100644 --- a/packages/server-utils/src/ai/pi-ai/types.ts +++ b/packages/server-utils/src/ai/pi-ai/types.ts @@ -32,4 +32,5 @@ export interface PiStreamOptions { export interface PiEventStream { result(): Promise; + push(event: unknown): void; } diff --git a/packages/server-utils/src/ai/workers-ai/streaming.ts b/packages/server-utils/src/ai/workers-ai/streaming.ts index 3eed14d5384b..b73b5d97f961 100644 --- a/packages/server-utils/src/ai/workers-ai/streaming.ts +++ b/packages/server-utils/src/ai/workers-ai/streaming.ts @@ -1,6 +1,6 @@ import type { Span } from '@sentry/core'; import { getToolCallKey } from '../core/streaming-tool-calls'; -import { endStreamSpan, getErrorStatus, type StreamResponseState } from '../core/utils'; +import { endStreamSpan, getErrorStatus, recordTimeToFirstChunk, type StreamResponseState } from '../core/utils'; import type { WorkersAiUsage } from './types'; import { setOutputMessagesAttribute } from './utils'; @@ -232,6 +232,7 @@ export function instrumentWorkersAiStream( return; } + recordTimeToFirstChunk(span); buffer += decoder.decode(value, { stream: true }); flushBuffer(false); controller.enqueue(value); diff --git a/packages/server-utils/src/integrations/pi-ai.ts b/packages/server-utils/src/integrations/pi-ai.ts index 68bc1bdca1d0..d49f22037f87 100644 --- a/packages/server-utils/src/integrations/pi-ai.ts +++ b/packages/server-utils/src/integrations/pi-ai.ts @@ -2,7 +2,7 @@ import * as diagnosticsChannel from '../utils/diagnosticsChannel'; import type { IntegrationFn, Span } from '@sentry/core'; import { defineIntegration, isThenable, startInactiveSpan } from '@sentry/core'; import type { GenAiOptions } from '../ai/core/utils'; -import { getPiAiChatSpanOptions, setPiAiResponseAttributes } from '../ai/pi-ai/chat'; +import { getPiAiChatSpanOptions, observePiAiFirstChunk, setPiAiResponseAttributes } from '../ai/pi-ai/chat'; import { PI_AI_INTEGRATION_NAME } from '../ai/pi-ai/constants'; import type { PiAiContext } from '../ai/pi-ai/messages'; import { isInsidePiAiSpan } from '../ai/pi-ai/providers'; @@ -66,6 +66,7 @@ function instrumentPiAi(options: PiAiOptions): void { if (!isThenable(settled)) { return false; } + observePiAiFirstChunk(span, events); settled.then(message => { setResponse(span, data, message); end(); diff --git a/packages/server-utils/src/integrations/vercel-ai/util.ts b/packages/server-utils/src/integrations/vercel-ai/util.ts index 6034749cafaf..07cbb8b4d82b 100644 --- a/packages/server-utils/src/integrations/vercel-ai/util.ts +++ b/packages/server-utils/src/integrations/vercel-ai/util.ts @@ -1,6 +1,8 @@ /** Shared, state-free helpers for the Vercel AI (`ai`) channel subscribers, plus the streamed model-call tap. */ +import type { Span } from '@sentry/core'; import { isObjectLike } from '@sentry/core'; +import { recordTimeToFirstChunk } from '../../ai/core/utils'; /** Narrow to a string, or `undefined` for anything else. */ export function asString(value: unknown): string | undefined { @@ -75,6 +77,7 @@ export { isReadableStream } from '../../ai/core/utils'; * mid-stream failure would be worse than a slightly-less-enriched one. */ export function tapModelCallStream( + span: Span, stream: ReadableStream, onFinal: (result: StreamedModelCallResult) => void, onError: (error: unknown) => void, @@ -113,6 +116,10 @@ export function tapModelCallStream( return; } + // The model emits `stream-start` before the response arrives. + if ((value as StreamChunk | undefined)?.type !== 'stream-start') { + recordTimeToFirstChunk(span); + } text += accumulateChunk(state, value) ?? ''; controller.enqueue(value); } catch (error) { diff --git a/packages/server-utils/src/integrations/vercel-ai/vercel-ai-dc-subscriber.ts b/packages/server-utils/src/integrations/vercel-ai/vercel-ai-dc-subscriber.ts index efd55f98874a..294ed231ee73 100644 --- a/packages/server-utils/src/integrations/vercel-ai/vercel-ai-dc-subscriber.ts +++ b/packages/server-utils/src/integrations/vercel-ai/vercel-ai-dc-subscriber.ts @@ -363,6 +363,7 @@ function deferStreamedModelCallEnd( const callId = asString(data.event.callId); const { recordOutputs } = getRecordingOptions(data.event, options, data.result === undefined); result.stream = tapModelCallStream( + span, stream, final => { // Reshape the aggregate into the result `enrichSpanOnEnd` expects, then let `end` run it (it calls diff --git a/packages/server-utils/src/integrations/vercel-ai/vercel-ai-orchestrion-subscriber.ts b/packages/server-utils/src/integrations/vercel-ai/vercel-ai-orchestrion-subscriber.ts index 84fb1ac96c70..207c4d73cc4a 100644 --- a/packages/server-utils/src/integrations/vercel-ai/vercel-ai-orchestrion-subscriber.ts +++ b/packages/server-utils/src/integrations/vercel-ai/vercel-ai-orchestrion-subscriber.ts @@ -565,6 +565,7 @@ function patchModelMethod( // separately by `deferStreamTextOperationEnd`, which awaits the operation's own result promises. if (method === 'doStream' && isObjectLike(value) && isReadableStream(value.stream)) { value.stream = tapModelCallStream( + span, value.stream, final => { message.result = { ...value, ...streamedResultToChannelResult(final) }; diff --git a/packages/server-utils/test/ai/lib/tracing/anthropic-ai-usage.test.ts b/packages/server-utils/test/ai/lib/tracing/anthropic-ai-usage.test.ts index 5d06a19c1e80..b1414ef2c273 100644 --- a/packages/server-utils/test/ai/lib/tracing/anthropic-ai-usage.test.ts +++ b/packages/server-utils/test/ai/lib/tracing/anthropic-ai-usage.test.ts @@ -2,6 +2,7 @@ import { GEN_AI_RESPONSE_ID, GEN_AI_RESPONSE_MODEL, GEN_AI_RESPONSE_STREAMING, + GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, GEN_AI_USAGE_INPUT_TOKENS, @@ -21,6 +22,7 @@ function createMockSpan(): { span: Span; attributes: Record } { let ended = false; const span = { isRecording: () => !ended, + spanContext: () => ({ traceId: '0', spanId: '0', traceFlags: 0 }), setAttribute: (key: string, value: unknown) => { attributes[key] = value; }, @@ -121,7 +123,9 @@ describe.each(['response', 'async iterable', 'message stream'] as const)('Anthro [GEN_AI_USAGE_OUTPUT_TOKENS]: 120, [GEN_AI_USAGE_TOTAL_TOKENS]: totalTokens, ...cacheAttributes, - ...(mode === 'response' ? {} : { [GEN_AI_RESPONSE_STREAMING]: true }), + ...(mode === 'response' + ? {} + : { [GEN_AI_RESPONSE_STREAMING]: true, [GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK]: expect.any(Number) }), }); }); }); @@ -305,6 +309,7 @@ describe('instrumentMessageStream', () => { expect(endSpy).toHaveBeenCalledTimes(1); expect(attributes).toEqual({ [GEN_AI_RESPONSE_STREAMING]: true, + [GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK]: expect.any(Number), [GEN_AI_RESPONSE_ID]: 'msg_aborted', [GEN_AI_RESPONSE_MODEL]: 'claude-sonnet-4-6', [GEN_AI_USAGE_INPUT_TOKENS]: 42, diff --git a/packages/server-utils/test/ai/lib/tracing/cache-token-usage.test.ts b/packages/server-utils/test/ai/lib/tracing/cache-token-usage.test.ts index 6e88ac2f7421..eca356a176b6 100644 --- a/packages/server-utils/test/ai/lib/tracing/cache-token-usage.test.ts +++ b/packages/server-utils/test/ai/lib/tracing/cache-token-usage.test.ts @@ -2,6 +2,7 @@ import { GEN_AI_RESPONSE_ID, GEN_AI_RESPONSE_MODEL, GEN_AI_RESPONSE_STREAMING, + GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK, GEN_AI_USAGE_CACHE_CREATION_INPUT_TOKENS, GEN_AI_USAGE_CACHE_READ_INPUT_TOKENS, GEN_AI_USAGE_INPUT_TOKENS, @@ -27,6 +28,7 @@ describe.each(['openai chat', 'openai responses', 'google'] as const)('cache usa const span = { isRecording: () => true, setAttributes: (values: Record) => Object.assign(attributes, values), + spanContext: () => ({ traceId: '0', spanId: '0', traceFlags: 0 }), setAttribute: (key: string, value: unknown) => { attributes[key] = value; }, @@ -99,7 +101,9 @@ describe.each(['openai chat', 'openai responses', 'google'] as const)('cache usa ...(provider === 'google' ? {} : { [GEN_AI_RESPONSE_ID]: 'response_cache_usage', [GEN_AI_RESPONSE_MODEL]: 'gpt-4o' }), - ...(mode === 'response' ? {} : { [GEN_AI_RESPONSE_STREAMING]: true }), + ...(mode === 'response' + ? {} + : { [GEN_AI_RESPONSE_STREAMING]: true, [GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK]: expect.any(Number) }), }); }); }); @@ -144,6 +148,7 @@ describe.each(['response', 'stream'] as const)('Google reasoning usage (%s)', mo const span = { isRecording: () => true, setAttributes: (values: Record) => Object.assign(attributes, values), + spanContext: () => ({ traceId: '0', spanId: '0', traceFlags: 0 }), setAttribute: (key: string, value: unknown) => { attributes[key] = value; }, @@ -180,7 +185,9 @@ describe.each(['response', 'stream'] as const)('Google reasoning usage (%s)', mo [GEN_AI_USAGE_OUTPUT_TOKENS]: output, [GEN_AI_USAGE_REASONING_OUTPUT_TOKENS]: thoughts, [GEN_AI_USAGE_TOTAL_TOKENS]: expectedTotal, - ...(mode === 'stream' ? { [GEN_AI_RESPONSE_STREAMING]: true } : {}), + ...(mode === 'stream' + ? { [GEN_AI_RESPONSE_STREAMING]: true, [GEN_AI_RESPONSE_TIME_TO_FIRST_CHUNK]: expect.any(Number) } + : {}), }); }); }); diff --git a/packages/server-utils/test/ai/lib/tracing/google-genai-streaming.test.ts b/packages/server-utils/test/ai/lib/tracing/google-genai-streaming.test.ts index 763667555672..4523f49ac376 100644 --- a/packages/server-utils/test/ai/lib/tracing/google-genai-streaming.test.ts +++ b/packages/server-utils/test/ai/lib/tracing/google-genai-streaming.test.ts @@ -15,6 +15,7 @@ function createMockSpan(): { span: Span; attributes: Record } { let isEnded = false; const span = { isRecording: () => !isEnded, + spanContext: () => ({ traceId: '0', spanId: '0', traceFlags: 0 }), setAttribute: (key: string, value: unknown) => { attributes[key] = value; }, diff --git a/packages/server-utils/test/ai/lib/tracing/openai-streaming-tool-calls.test.ts b/packages/server-utils/test/ai/lib/tracing/openai-streaming-tool-calls.test.ts index 36aca7b8a9c7..9ba3e572a306 100644 --- a/packages/server-utils/test/ai/lib/tracing/openai-streaming-tool-calls.test.ts +++ b/packages/server-utils/test/ai/lib/tracing/openai-streaming-tool-calls.test.ts @@ -9,6 +9,7 @@ function createMockSpan(): { span: Span; attributes: Record } { let isEnded = false; const span = { isRecording: () => !isEnded, + spanContext: () => ({ traceId: '0', spanId: '0', traceFlags: 0 }), setAttribute: (key: string, value: unknown) => { attributes[key] = value; }, diff --git a/packages/server-utils/test/ai/lib/tracing/stream-errors.test.ts b/packages/server-utils/test/ai/lib/tracing/stream-errors.test.ts index 71a93354535c..3f6d5bb2cc86 100644 --- a/packages/server-utils/test/ai/lib/tracing/stream-errors.test.ts +++ b/packages/server-utils/test/ai/lib/tracing/stream-errors.test.ts @@ -16,6 +16,7 @@ function createMockSpan(): { span: Span; attributes: Record; se let isEnded = false; const span = { isRecording: () => !isEnded, + spanContext: () => ({ traceId: '0', spanId: '0', traceFlags: 0 }), setAttribute: (key: string, value: unknown) => { attributes[key] = value; }, diff --git a/packages/server-utils/test/ai/lib/tracing/workers-ai-streaming.test.ts b/packages/server-utils/test/ai/lib/tracing/workers-ai-streaming.test.ts index 33f0ceacba83..75986b2280d0 100644 --- a/packages/server-utils/test/ai/lib/tracing/workers-ai-streaming.test.ts +++ b/packages/server-utils/test/ai/lib/tracing/workers-ai-streaming.test.ts @@ -18,6 +18,7 @@ function createMockSpan(): { span: Span; attributes: Record; en let isEnded = false; const span = { isRecording: () => !isEnded, + spanContext: () => ({ traceId: '0', spanId: '0', traceFlags: 0 }), setAttribute: (key: string, value: unknown) => { attributes[key] = value; },