diff --git a/dev-packages/node-integration-tests/suites/tracing/anthropic/instrument-with-recording-functions.mjs b/dev-packages/node-integration-tests/suites/tracing/anthropic/instrument-with-recording-functions.mjs new file mode 100644 index 000000000000..1817614f384e --- /dev/null +++ b/dev-packages/node-integration-tests/suites/tracing/anthropic/instrument-with-recording-functions.mjs @@ -0,0 +1,18 @@ +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, + transport: loggingTransport, + integrations: [ + Sentry.anthropicAIIntegration({ + recordInputs: call => + call.operation === 'execute_tool' + ? call.input?.city === 'Vienna' + : !!call.request?.metadata?.user_id?.startsWith('public'), + recordOutputs: () => Sentry.getCurrentScope().getScopeData().tags.privacy === 'public', + }), + ], +}); diff --git a/dev-packages/node-integration-tests/suites/tracing/anthropic/scenario-recording-functions.mjs b/dev-packages/node-integration-tests/suites/tracing/anthropic/scenario-recording-functions.mjs new file mode 100644 index 000000000000..b9e42c816f6c --- /dev/null +++ b/dev-packages/node-integration-tests/suites/tracing/anthropic/scenario-recording-functions.mjs @@ -0,0 +1,135 @@ +import Anthropic from '@anthropic-ai/sdk'; +import * as Sentry from '@sentry/node'; +import express from 'express'; + +const MODEL = 'claude-3-haiku-20240307'; + +function message(id, content, stopReason = 'end_turn') { + return { + id, + type: 'message', + role: 'assistant', + model: MODEL, + content, + stop_reason: stopReason, + usage: { input_tokens: 10, output_tokens: 5 }, + }; +} + +function writeStream(res, id) { + res.writeHead(200, { 'Content-Type': 'text/event-stream' }); + const { content, ...start } = message(id, []); + const events = [ + { type: 'message_start', message: { ...start, content } }, + { type: 'content_block_start', index: 0, content_block: { type: 'text', text: '' } }, + { type: 'content_block_delta', index: 0, delta: { type: 'text_delta', text: 'Paris.' } }, + { type: 'content_block_stop', index: 0 }, + { type: 'message_delta', delta: { stop_reason: 'end_turn', stop_sequence: null }, usage: { output_tokens: 5 } }, + { type: 'message_stop' }, + ]; + for (const event of events) { + res.write(`event: ${event.type}\ndata: ${JSON.stringify(event)}\n\n`); + } + res.end(); +} + +function startMockServer() { + const app = express(); + app.use(express.json()); + + app.post('/anthropic/v1/messages', (req, res) => { + const { metadata, stream, messages } = req.body; + + if (metadata.user_id === 'runner') { + const answeredTools = messages.some(m => Array.isArray(m.content) && m.content[0]?.type === 'tool_result'); + res.send( + answeredTools + ? message('msg_tool_final', [{ type: 'text', text: 'Sunny in both.' }]) + : message( + 'msg_tool_use', + [ + { type: 'tool_use', id: 'toolu_vienna', name: 'weather', input: { city: 'Vienna' } }, + { type: 'tool_use', id: 'toolu_paris', name: 'weather', input: { city: 'Paris' } }, + ], + 'tool_use', + ), + ); + return; + } + + const id = `msg_${metadata.user_id}${stream ? '-stream' : ''}`; + if (stream) { + writeStream(res, id); + return; + } + res.send(message(id, [{ type: 'text', text: 'Paris.' }])); + }); + + return new Promise(resolve => { + const server = app.listen(0, () => { + resolve(server); + }); + }); +} + +async function run() { + const server = await startMockServer(); + + await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { + const client = new Anthropic({ + apiKey: 'mock-api-key', + baseURL: `http://localhost:${server.address().port}/anthropic`, + }); + const request = userId => ({ + model: MODEL, + max_tokens: 100, + messages: [{ role: 'user', content: 'What is the capital of France?' }], + metadata: { user_id: userId }, + }); + + await Sentry.withScope(async scope => { + scope.setTag('privacy', 'public'); + await client.messages.create(request('public')); + }); + + await Sentry.withScope(async scope => { + scope.setTag('privacy', 'private'); + await client.messages.create(request('private')); + + // Changing the tag after a call started must not change its outputs decision. + const stream = await client.messages.create({ ...request('public'), stream: true }); + Sentry.getCurrentScope().setTag('privacy', 'public'); + for await (const event of stream) { + void event; + } + + Sentry.getCurrentScope().setTag('privacy', 'private'); + const helper = client.messages.stream(request('public-helper')); + Sentry.getCurrentScope().setTag('privacy', 'public'); + await helper.finalMessage(); + }); + + await Sentry.withScope(async scope => { + scope.setTag('privacy', 'public'); + const runner = client.beta.messages.toolRunner({ + ...request('runner'), + tools: [ + { + name: 'weather', + description: 'Get the weather by city', + input_schema: { type: 'object', properties: { city: { type: 'string' } } }, + run: () => 'sunny', + }, + ], + }); + for await (const msg of runner) { + void msg; + } + }); + }); + + await Sentry.flush(2000); + server.close(); +} + +run(); 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..c4db3135a003 100644 --- a/dev-packages/node-integration-tests/suites/tracing/anthropic/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/anthropic/test.ts @@ -18,10 +18,15 @@ import { GEN_AI_USAGE_INPUT_TOKENS, GEN_AI_USAGE_OUTPUT_TOKENS, GEN_AI_USAGE_TOTAL_TOKENS, + GEN_AI_TOOL_CALL_ARGUMENTS, + GEN_AI_TOOL_CALL_RESULT, SENTRY_OP, SENTRY_ORIGIN, } from '@sentry/conventions/attributes'; -import { GEN_AI_REQUEST_STREAM_ATTRIBUTE } from '../../../../../packages/server-utils/src/ai/core/gen-ai-attributes'; +import { + GEN_AI_REQUEST_STREAM_ATTRIBUTE, + GEN_AI_TOOL_CALL_ID_ATTRIBUTE, +} from '../../../../../packages/server-utils/src/ai/core/gen-ai-attributes'; import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner'; describe('Anthropic integration', () => { @@ -613,4 +618,43 @@ describe('Anthropic integration', () => { .completed(); }); }); + + createEsmAndCjsTests( + __dirname, + 'scenario-recording-functions.mjs', + 'instrument-with-recording-functions.mjs', + (createRunner, test) => { + test('decides content recording per call when the options are functions', async () => { + await createRunner() + .expect({ + span: container => { + const spans = container.items.filter( + span => span.attributes[SENTRY_ORIGIN]?.value === 'auto.ai.anthropic', + ); + const recordedById = Object.fromEntries( + spans.map(span => [ + span.attributes[GEN_AI_RESPONSE_ID]?.value ?? span.attributes[GEN_AI_TOOL_CALL_ID_ATTRIBUTE]?.value, + { + inputs: GEN_AI_INPUT_MESSAGES in span.attributes || GEN_AI_TOOL_CALL_ARGUMENTS in span.attributes, + outputs: GEN_AI_RESPONSE_TEXT in span.attributes || GEN_AI_TOOL_CALL_RESULT in span.attributes, + }, + ]), + ); + expect(recordedById).toEqual({ + msg_public: { inputs: true, outputs: true }, + msg_private: { inputs: false, outputs: false }, + 'msg_public-stream': { inputs: true, outputs: false }, + 'msg_public-helper-stream': { inputs: true, outputs: false }, + msg_tool_use: { inputs: false, outputs: true }, + msg_tool_final: { inputs: false, outputs: true }, + toolu_vienna: { inputs: true, outputs: true }, + toolu_paris: { inputs: false, outputs: true }, + }); + }, + }) + .start() + .completed(); + }); + }, + ); }); diff --git a/dev-packages/node-integration-tests/suites/tracing/google-genai/instrument-with-recording-functions.mjs b/dev-packages/node-integration-tests/suites/tracing/google-genai/instrument-with-recording-functions.mjs new file mode 100644 index 000000000000..d67b7cddf66d --- /dev/null +++ b/dev-packages/node-integration-tests/suites/tracing/google-genai/instrument-with-recording-functions.mjs @@ -0,0 +1,16 @@ +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, + transport: loggingTransport, + integrations: [ + Sentry.googleGenAIIntegration({ + recordInputs: ({ operation, request }) => + operation === 'generate_content' && !!request?.contents?.startsWith('Public'), + recordOutputs: () => Sentry.getCurrentScope().getScopeData().tags.privacy === 'public', + }), + ], +}); diff --git a/dev-packages/node-integration-tests/suites/tracing/google-genai/scenario-recording-functions.mjs b/dev-packages/node-integration-tests/suites/tracing/google-genai/scenario-recording-functions.mjs new file mode 100644 index 000000000000..3d06b23f6fd2 --- /dev/null +++ b/dev-packages/node-integration-tests/suites/tracing/google-genai/scenario-recording-functions.mjs @@ -0,0 +1,65 @@ +import { GoogleGenAI } from '@google/genai'; +import * as Sentry from '@sentry/node'; +import express from 'express'; + +const RESPONSE = { + candidates: [{ content: { parts: [{ text: 'Paris.' }], role: 'model' }, finishReason: 'STOP', index: 0 }], + usageMetadata: { promptTokenCount: 8, candidatesTokenCount: 2, totalTokenCount: 10 }, +}; + +function startMockServer() { + const app = express(); + app.use(express.json()); + + app.post('/v1beta/models/:model\\:generateContent', (_req, res) => { + res.send(RESPONSE); + }); + + app.post('/v1beta/models/:model\\:streamGenerateContent', (_req, res) => { + res.setHeader('Content-Type', 'application/json'); + res.write(`data: ${JSON.stringify(RESPONSE)}\n\n`); + res.end(); + }); + + return new Promise(resolve => { + const server = app.listen(0, () => { + resolve(server); + }); + }); +} + +async function run() { + const server = await startMockServer(); + + await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { + const client = new GoogleGenAI({ + apiKey: 'mock-api-key', + httpOptions: { baseUrl: `http://localhost:${server.address().port}` }, + }); + + await Sentry.withScope(async scope => { + scope.setTag('privacy', 'public'); + await client.models.generateContent({ model: 'gemini-public', contents: 'Public: capital of France?' }); + }); + + await Sentry.withScope(async scope => { + scope.setTag('privacy', 'private'); + await client.models.generateContent({ model: 'gemini-private', contents: 'Private: capital of France?' }); + + // Changing the tag after a call started must not change its outputs decision. + const stream = await client.models.generateContentStream({ + model: 'gemini-public-stream', + contents: 'Public: capital of France?', + }); + Sentry.getCurrentScope().setTag('privacy', 'public'); + for await (const chunk of stream) { + void chunk; + } + }); + }); + + await Sentry.flush(2000); + server.close(); +} + +run(); 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..ba43155fe344 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 @@ -624,4 +624,36 @@ describe('Google GenAI integration', () => { }); }, ); + + createEsmAndCjsTests( + __dirname, + 'scenario-recording-functions.mjs', + 'instrument-with-recording-functions.mjs', + (createRunner, test) => { + test('decides content recording per call when the options are functions', async () => { + await createRunner() + .expect({ + span: container => { + const spans = container.items.filter(span => span.attributes[SENTRY_ORIGIN]?.value === EXPECTED_ORIGIN); + const recordedByModel = Object.fromEntries( + spans.map(span => [ + span.attributes[GEN_AI_REQUEST_MODEL]?.value, + { + inputs: GEN_AI_INPUT_MESSAGES in span.attributes, + outputs: GEN_AI_RESPONSE_TEXT in span.attributes, + }, + ]), + ); + expect(recordedByModel).toEqual({ + 'gemini-public': { inputs: true, outputs: true }, + 'gemini-private': { inputs: false, outputs: false }, + 'gemini-public-stream': { inputs: true, outputs: false }, + }); + }, + }) + .start() + .completed(); + }); + }, + ); }); diff --git a/dev-packages/node-integration-tests/suites/tracing/mistral/instrument-with-recording-functions.mjs b/dev-packages/node-integration-tests/suites/tracing/mistral/instrument-with-recording-functions.mjs new file mode 100644 index 000000000000..11b2c29ecd67 --- /dev/null +++ b/dev-packages/node-integration-tests/suites/tracing/mistral/instrument-with-recording-functions.mjs @@ -0,0 +1,16 @@ +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, + transport: loggingTransport, + integrations: [ + Sentry.mistralAIIntegration({ + recordInputs: ({ operation, request }) => + operation === 'chat' && !!request?.messages?.[0]?.content?.startsWith('Public'), + recordOutputs: () => Sentry.getCurrentScope().getScopeData().tags.privacy === 'public', + }), + ], +}); diff --git a/dev-packages/node-integration-tests/suites/tracing/mistral/scenario-recording-functions.mjs b/dev-packages/node-integration-tests/suites/tracing/mistral/scenario-recording-functions.mjs new file mode 100644 index 000000000000..1be833a3a76b --- /dev/null +++ b/dev-packages/node-integration-tests/suites/tracing/mistral/scenario-recording-functions.mjs @@ -0,0 +1,88 @@ +import { Mistral } from '@mistralai/mistralai'; +import * as Sentry from '@sentry/node'; +import express from 'express'; + +function startMockServer() { + const app = express(); + app.use(express.json()); + + app.post('/v1/chat/completions', (req, res) => { + const { model, stream } = req.body; + const id = `cmpl-${model}`; + + if (!stream) { + res.send({ + id, + object: 'chat.completion', + created: 1677652288, + model, + choices: [{ index: 0, message: { role: 'assistant', content: 'Paris.' }, finish_reason: 'stop' }], + usage: { prompt_tokens: 10, completion_tokens: 2, total_tokens: 12 }, + }); + return; + } + + res.setHeader('Content-Type', 'text/event-stream'); + const chunks = [ + { + id, + object: 'chat.completion.chunk', + created: 1677652300, + model, + choices: [{ index: 0, delta: { content: 'Paris.' }, finish_reason: null }], + }, + { + id, + object: 'chat.completion.chunk', + created: 1677652300, + model, + choices: [{ index: 0, delta: {}, finish_reason: 'stop' }], + }, + ]; + for (const chunk of chunks) { + res.write(`data: ${JSON.stringify(chunk)}\n\n`); + } + res.write('data: [DONE]\n\n'); + res.end(); + }); + + return new Promise(resolve => { + const server = app.listen(0, () => { + resolve(server); + }); + }); +} + +async function run() { + const server = await startMockServer(); + + await Sentry.startSpan({ op: 'function', name: 'main' }, async () => { + const client = new Mistral({ + apiKey: 'mock-api-key', + serverURL: `http://localhost:${server.address().port}`, + }); + const messages = privacy => [{ role: 'user', content: `${privacy}: capital of France?` }]; + + await Sentry.withScope(async scope => { + scope.setTag('privacy', 'public'); + await client.chat.complete({ model: 'mistral-public', messages: messages('Public') }); + }); + + await Sentry.withScope(async scope => { + scope.setTag('privacy', 'private'); + await client.chat.complete({ model: 'mistral-private', messages: messages('Private') }); + + // Changing the tag after a call started must not change its outputs decision. + const stream = await client.chat.stream({ model: 'mistral-public-stream', messages: messages('Public') }); + Sentry.getCurrentScope().setTag('privacy', 'public'); + for await (const event of stream) { + void event; + } + }); + }); + + await Sentry.flush(2000); + server.close(); +} + +run(); 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 8b5373b22921..f2425430d1ed 100644 --- a/dev-packages/node-integration-tests/suites/tracing/mistral/test.ts +++ b/dev-packages/node-integration-tests/suites/tracing/mistral/test.ts @@ -314,4 +314,36 @@ describe('Mistral integration', () => { .completed(); }); }); + + createEsmTests( + __dirname, + 'scenario-recording-functions.mjs', + 'instrument-with-recording-functions.mjs', + (createRunner, test) => { + test('decides content recording per call when the options are functions', async () => { + await createRunner() + .expect({ + span: container => { + const spans = container.items.filter(span => span.attributes[SENTRY_ORIGIN]?.value === ORIGIN); + const recordedById = Object.fromEntries( + spans.map(span => [ + span.attributes[GEN_AI_RESPONSE_ID]?.value, + { + inputs: GEN_AI_INPUT_MESSAGES in span.attributes, + outputs: GEN_AI_RESPONSE_TEXT in span.attributes, + }, + ]), + ); + expect(recordedById).toEqual({ + 'cmpl-mistral-public': { inputs: true, outputs: true }, + 'cmpl-mistral-private': { inputs: false, outputs: false }, + 'cmpl-mistral-public-stream': { inputs: true, outputs: false }, + }); + }, + }) + .start() + .completed(); + }); + }, + ); }); diff --git a/packages/server-utils/src/ai/anthropic-ai/index.ts b/packages/server-utils/src/ai/anthropic-ai/index.ts index 4d987af2a474..6744490e5b4c 100644 --- a/packages/server-utils/src/ai/anthropic-ai/index.ts +++ b/packages/server-utils/src/ai/anthropic-ai/index.ts @@ -18,13 +18,13 @@ import { SENTRY_ORIGIN, } from '@sentry/conventions/attributes'; import { GEN_AI_REQUEST_STREAM_ATTRIBUTE } from '../core/gen-ai-attributes'; -import type { InstrumentedMethodEntry } from '../core/utils'; +import type { GenAiOptions, InstrumentedMethodEntry } from '../core/utils'; import { getErrorStatus, getGenAiSpanOp, getRequestSignal, getTokenUsageAttributes, - resolveAIRecordingOptions, + resolveGenAiCallRecording, wrapPromiseWithMethods, } from '../core/utils'; import { ANTHROPIC_AI_ORIGIN, ANTHROPIC_METHOD_REGISTRY } from './constants'; @@ -184,7 +184,7 @@ function handleStreamingRequest( args: T, spanConfig: { name: string; op: string; attributes: Record }, params: Record | undefined, - options: AnthropicAiOptions, + { recordInputs, recordOutputs }: Required, isStreamRequested: boolean, isStreamingMethod: boolean, ): R | Promise { @@ -196,7 +196,7 @@ function handleStreamingRequest( const instrumentedPromise = startSpanManual(spanConfig, (span: Span) => { originalResult = target.apply(invocationThis, args) as Promise; - if (options.recordInputs && params) { + if (recordInputs && params) { addPrivateRequestAttributes(span, params); } @@ -206,7 +206,7 @@ function handleStreamingRequest( return instrumentAsyncIterableStream( result as AsyncIterable, span, - options.recordOutputs ?? false, + recordOutputs, ) as unknown as R; } catch (error) { return handleStreamingError(error, span, abortSignal); @@ -218,7 +218,7 @@ function handleStreamingRequest( } else { return startSpanManual(spanConfig, span => { try { - if (options.recordInputs && params) { + if (recordInputs && params) { addPrivateRequestAttributes(span, params); } // The helper synchronously delegates to `create`; suppress that one internal call so it @@ -226,7 +226,7 @@ function handleStreamingRequest( suppressDelegatedCreate = true; const messageStream = target.apply(invocationThis, args); suppressDelegatedCreate = false; - return instrumentMessageStream(messageStream, span, options.recordOutputs ?? false, abortSignal); + return instrumentMessageStream(messageStream, span, recordOutputs, abortSignal); } catch (error) { suppressDelegatedCreate = false; return handleStreamingError(error, span, abortSignal); @@ -265,10 +265,12 @@ function instrumentMethod( } const operationName = instrumentedMethod.operation || 'unknown'; + const params = typeof args[0] === 'object' && args[0] !== null ? (args[0] as Record) : undefined; + const recording = resolveGenAiCallRecording(options, { operation: 'chat', request: params }); const requestAttributes = extractRequestAttributes( args, operationName, - !!options.recordInputs, + recording.recordInputs, getProviderName(invocationThis), ); const model = requestAttributes[GEN_AI_REQUEST_MODEL] || 'unknown'; @@ -283,7 +285,6 @@ function instrumentMethod( attributes: requestAttributes as Record, }; - const params = typeof args[0] === 'object' ? (args[0] as Record) : undefined; const isStreamRequested = Boolean(params?.stream); if (isStreamRequested || isStreamingMethod) { @@ -293,7 +294,7 @@ function instrumentMethod( args, spanConfig, params, - options, + recording, isStreamRequested, isStreamingMethod, ); @@ -305,13 +306,13 @@ function instrumentMethod( const instrumentedPromise = startSpan(spanConfig, span => { originalResult = target.apply(invocationThis, args) as Promise; - if (options.recordInputs && params) { + if (recording.recordInputs && params) { addPrivateRequestAttributes(span, params); } return originalResult.then( result => { - addResponseAttributes(span, result as AnthropicAiResponse, options.recordOutputs); + addResponseAttributes(span, result as AnthropicAiResponse, recording.recordOutputs); return result; }, error => { @@ -388,5 +389,5 @@ function instrumentClientInPlace(client: T, options: Anthropic * @returns The instrumented client with the same type as the input */ export function instrumentAnthropicAiClient(anthropicAiClient: T, options?: AnthropicAiOptions): T { - return instrumentClientInPlace(anthropicAiClient, resolveAIRecordingOptions(options)); + return instrumentClientInPlace(anthropicAiClient, options ?? {}); } diff --git a/packages/server-utils/src/ai/anthropic-ai/tool-runner.ts b/packages/server-utils/src/ai/anthropic-ai/tool-runner.ts index f373f79c2468..f2a07eeaeaf9 100644 --- a/packages/server-utils/src/ai/anthropic-ai/tool-runner.ts +++ b/packages/server-utils/src/ai/anthropic-ai/tool-runner.ts @@ -29,7 +29,7 @@ import { } from '@sentry/conventions/attributes'; import { GEN_AI_EXECUTE_TOOL, GEN_AI_INVOKE_AGENT } from '@sentry/conventions/op'; import { GEN_AI_TOOL_CALL_ID_ATTRIBUTE } from '../core/gen-ai-attributes'; -import { getErrorStatus, resolveAIRecordingOptions } from '../core/utils'; +import { getErrorStatus, resolveGenAiCallRecording } from '../core/utils'; import { withCleanScopes } from '../../utils/withCleanScopes'; import { ANTHROPIC_AI_ORIGIN } from './constants'; import type { AnthropicAiOptions } from './types'; @@ -188,8 +188,12 @@ function runWithSpan( args: unknown[], options: AnthropicAiOptions, ): unknown { - const { recordInputs, recordOutputs } = resolveAIRecordingOptions(options); const [input, context] = args as [unknown, RunContext]; + const { recordInputs, recordOutputs } = resolveGenAiCallRecording(options, { + operation: 'execute_tool', + tool, + input, + }); // SDK versions before `toolUse` existed pass only `toolUseBlock`. const callId = (context?.toolUse ?? context?.toolUseBlock)?.id; diff --git a/packages/server-utils/src/ai/anthropic-ai/types.ts b/packages/server-utils/src/ai/anthropic-ai/types.ts index ddee39046d57..36c4b212248d 100644 --- a/packages/server-utils/src/ai/anthropic-ai/types.ts +++ b/packages/server-utils/src/ai/anthropic-ai/types.ts @@ -1,7 +1,22 @@ -import type { GenAiOptions } from '../core/utils'; +import type { GenAiCallOptions } from '../core/utils'; + +/** The call that `recordInputs`/`recordOutputs` decide on: a model call, or a tool call of a tool runner. */ +export type AnthropicAiCallInfo = + | { + operation: 'chat'; + /** The request params, the first argument of the method (such as `messages.create`) as passed. */ + request: Record | undefined; + } + | { + operation: 'execute_tool'; + /** The runnable tool, as passed to the tool runner. */ + tool: { name: string }; + /** The input the model called the tool with. */ + input: unknown; + }; /** Options for the Anthropic AI integration. */ -export type AnthropicAiOptions = GenAiOptions; +export type AnthropicAiOptions = GenAiCallOptions; export type Message = { role: 'user' | 'assistant'; diff --git a/packages/server-utils/src/ai/google-genai/index.ts b/packages/server-utils/src/ai/google-genai/index.ts index d80559565ba4..e63cf1aed668 100644 --- a/packages/server-utils/src/ai/google-genai/index.ts +++ b/packages/server-utils/src/ai/google-genai/index.ts @@ -35,7 +35,7 @@ import { getErrorStatus, getGenAiSpanOp, getTokenUsageAttributes, - resolveAIRecordingOptions, + resolveGenAiCallRecording, } from '../core/utils'; import { GOOGLE_GENAI_METHOD_REGISTRY, GOOGLE_GENAI_SYSTEM_NAME } from './constants'; import { instrumentStream } from './streaming'; @@ -317,14 +317,13 @@ function instrumentMethod( apply(target, _, args: T): R | Promise { const operationName = instrumentedMethod.operation || 'unknown'; const params = args[0] as Record | undefined; + const { recordInputs, recordOutputs } = resolveGenAiCallRecording(options, { + operation: operationName, + request: params, + }); const attributeParams = resolveChatParams(operationName, params, context); const abortSignal = getAbortSignal(attributeParams); - const requestAttributes = extractRequestAttributes( - operationName, - attributeParams, - context, - !!options.recordInputs, - ); + const requestAttributes = extractRequestAttributes(operationName, attributeParams, context, recordInputs); const model = requestAttributes[GEN_AI_REQUEST_MODEL] || 'unknown'; const client = getClient(); // With span streaming, omit the `'unknown'` model sentinel so the name stays low-cardinality. @@ -344,11 +343,11 @@ function instrumentMethod( }, async (span: Span) => { try { - if (options.recordInputs && attributeParams) { + if (recordInputs && attributeParams) { addPrivateRequestAttributes(span, attributeParams, operationName); } const stream = await target.apply(context, args); - return instrumentStream(stream, span, Boolean(options.recordOutputs), abortSignal) as R; + return instrumentStream(stream, span, recordOutputs, abortSignal) as R; } catch (error) { span.setStatus(getErrorStatus(abortSignal)); span.end(); @@ -365,7 +364,7 @@ function instrumentMethod( attributes: requestAttributes, }, (span: Span) => { - if (options.recordInputs && attributeParams) { + if (recordInputs && attributeParams) { addPrivateRequestAttributes(span, attributeParams, operationName); } @@ -377,7 +376,7 @@ function instrumentMethod( result => { // Only add response attributes for content-producing methods, not for embeddings if (!isEmbeddings) { - addResponseAttributes(span, result, options.recordOutputs); + addResponseAttributes(span, result, recordOutputs); } }, ); @@ -476,5 +475,5 @@ function createDeepProxy(target: T, currentPath = '', options: * ``` */ export function instrumentGoogleGenAIClient(client: T, options?: GoogleGenAIOptions): T { - return createDeepProxy(client, '', resolveAIRecordingOptions(options)); + return createDeepProxy(client, '', options ?? {}); } diff --git a/packages/server-utils/src/ai/google-genai/types.ts b/packages/server-utils/src/ai/google-genai/types.ts index c5e0075955ac..444e28a7c8b0 100644 --- a/packages/server-utils/src/ai/google-genai/types.ts +++ b/packages/server-utils/src/ai/google-genai/types.ts @@ -1,7 +1,18 @@ -import type { GenAiOptions } from '../core/utils'; +import type { GenAiCallOptions } from '../core/utils'; + +/** The call that `recordInputs`/`recordOutputs` decide on. */ +export interface GoogleGenAICallInfo { + /** The gen_ai operation of the call: `generate_content`, `embeddings` or `chat`. */ + operation: string; + /** + * The request params, the first argument of the method (such as `models.generateContent` or + * `chat.sendMessage`) as passed. For a chat message this does not include the config of `chats.create()`. + */ + request: Record | undefined; +} /** Options for the Google GenAI integration. */ -export type GoogleGenAIOptions = GenAiOptions; +export type GoogleGenAIOptions = GenAiCallOptions; /** * Google GenAI Content Part diff --git a/packages/server-utils/src/ai/mistral/index.ts b/packages/server-utils/src/ai/mistral/index.ts index 4f6ba7b25f7e..2f3c2d51dfca 100644 --- a/packages/server-utils/src/ai/mistral/index.ts +++ b/packages/server-utils/src/ai/mistral/index.ts @@ -15,7 +15,7 @@ import { buildMethodPath, extractSystemInstructions, getGenAiSpanOp, - resolveAIRecordingOptions, + resolveGenAiCallRecording, wrapPromiseWithMethods, } from '../core/utils'; import { GEN_AI_REQUEST_STREAM_ATTRIBUTE } from '../core/gen-ai-attributes'; @@ -110,9 +110,12 @@ function instrumentMethod( return function instrumentedCall(...args: T): Promise { const operationName = instrumentedMethod.operation || 'unknown'; const isStreamRequested = !!instrumentedMethod.streaming; - const requestAttributes = extractRequestAttributes(args, operationName, !!options.recordInputs, isStreamRequested); - const params = args[0] as Record | undefined; + const { recordInputs, recordOutputs } = resolveGenAiCallRecording(options, { + operation: operationName, + request: params, + }); + const requestAttributes = extractRequestAttributes(args, operationName, recordInputs, isStreamRequested); const spanConfig = { name: getSpanName(operationName, requestAttributes), @@ -126,7 +129,7 @@ function instrumentMethod( const instrumentedPromise = startSpanManual(spanConfig, (span: Span) => { originalResult = originalMethod.apply(context, args); - if (options.recordInputs && params) { + if (recordInputs && params) { addRequestAttributes(span, params, operationName); } @@ -134,7 +137,7 @@ function instrumentMethod( result => { // Patched in place so the caller keeps the real `EventStream`; `instrumentEventStream` // takes over ending the span once the stream is drained. - if (!instrumentEventStream(result, span, options.recordOutputs ?? false)) { + if (!instrumentEventStream(result, span, recordOutputs)) { span.end(); } return result; @@ -155,12 +158,12 @@ function instrumentMethod( const instrumentedPromise = startSpan(spanConfig, (span: Span) => { originalResult = originalMethod.apply(context, args); - if (options.recordInputs && params) { + if (recordInputs && params) { addRequestAttributes(span, params, operationName); } return originalResult.then(result => { - addResponseAttributes(span, result, options.recordOutputs); + addResponseAttributes(span, result, recordOutputs); return result; }); }); @@ -202,5 +205,5 @@ function createDeepProxy(target: T, currentPath = '', options: * Can be used across Node.js, Cloudflare Workers, and Vercel Edge. */ export function instrumentMistralAiClient(client: T, options?: MistralOptions): T { - return createDeepProxy(client, '', resolveAIRecordingOptions(options)); + return createDeepProxy(client, '', options ?? {}); } diff --git a/packages/server-utils/src/ai/mistral/types.ts b/packages/server-utils/src/ai/mistral/types.ts index 840830b8e120..d2d600171a94 100644 --- a/packages/server-utils/src/ai/mistral/types.ts +++ b/packages/server-utils/src/ai/mistral/types.ts @@ -1,7 +1,15 @@ -import type { GenAiOptions } from '../core/utils'; +import type { GenAiCallOptions } from '../core/utils'; + +/** The call that `recordInputs`/`recordOutputs` decide on. */ +export interface MistralCallInfo { + /** The gen_ai operation of the call: `chat`, `embeddings` or `invoke_agent`. */ + operation: string; + /** The request params, the first argument of the method (such as `chat.complete`) as passed. */ + request: Record | undefined; +} /** Options for the Mistral integration. */ -export type MistralOptions = GenAiOptions; +export type MistralOptions = GenAiCallOptions; /** * A tool call as it appears in a response/stream delta. During streaming the `function.arguments` diff --git a/packages/server-utils/src/integrations/anthropic.ts b/packages/server-utils/src/integrations/anthropic.ts index 2a9e51d17d22..9677509e22dc 100644 --- a/packages/server-utils/src/integrations/anthropic.ts +++ b/packages/server-utils/src/integrations/anthropic.ts @@ -2,7 +2,7 @@ import { GEN_AI_REQUEST_MODEL, SENTRY_ORIGIN } from '@sentry/conventions/attribu import * as diagnosticsChannel from '../utils/diagnosticsChannel'; import type { IntegrationFn, Span, SpanAttributeValue } from '@sentry/core'; import { defineIntegration, getActiveSpan, getClient, hasSpanStreamingEnabled, startInactiveSpan } from '@sentry/core'; -import { getGenAiSpanOp, getRequestSignal, markCancelledIfAborted, resolveAIRecordingOptions } from '../ai/core/utils'; +import { getGenAiSpanOp, getRequestSignal, markCancelledIfAborted, resolveGenAiCallRecording } from '../ai/core/utils'; import { wrapApiPromiseResponse } from '../ai/core/apiPromise'; import { addPrivateRequestAttributes, addResponseAttributes, extractRequestAttributes } from '../ai/anthropic-ai'; import { ANTHROPIC_AI_ORIGIN } from '../ai/anthropic-ai/constants'; @@ -21,6 +21,9 @@ import { invokeOrchestrionInstrumentation } from '../orchestrion/instrumentation // integration is deduplicated out of the default set. const INTEGRATION_NAME = 'Anthropic_AI' as const; +// The recording decision is made once, when the span starts, in the caller's async context. +type AnthropicChannelContext = OrchestrionChannelContext & { _sentryRecordOutputs?: boolean }; + // `stream` determines how the span is ended const INSTRUMENTED_CHANNELS = [ { channel: CHANNELS.ANTHROPIC_CHAT, operation: 'chat', stream: 'async-iterable' }, @@ -57,32 +60,28 @@ function instrumentAnthropic(options: AnthropicAiOptions): void { disableNativeTracing(); for (const { channel, operation, stream } of INSTRUMENTED_CHANNELS) { bindTracingChannelToSpan( - diagnosticsChannel.tracingChannel(channel), + diagnosticsChannel.tracingChannel(channel), data => createGenAiSpan(data, operation, options, stream), { beforeSpanEnd: (span, data) => { if ('error' in data) { markCancelledIfAborted(span, getRequestSignal(data.arguments?.[1])); } - addResponseAttributes( - span, - data.result as AnthropicAiResponse, - resolveAIRecordingOptions(options).recordOutputs, - ); + addResponseAttributes(span, data.result as AnthropicAiResponse, !!data._sentryRecordOutputs); }, deferSpanEnd: ({ span, data, end }) => - wrapThreadStreamRequest(span, data, end, options) || + wrapThreadStreamRequest(span, data, end) || wrapApiPromiseResponse( data.result, response => { data.result = response; - if (!wrapStreamResult(span, data, stream, options)) { + if (!wrapStreamResult(span, data, stream)) { end(); } }, end, ) || - wrapStreamResult(span, data, stream, options), + wrapStreamResult(span, data, stream), }, ); } @@ -116,8 +115,8 @@ function instrumentAnthropic(options: AnthropicAiOptions): void { * Returning `undefined` opts the payload out so no span is opened. */ function createGenAiSpan( - data: OrchestrionChannelContext, - operation: string, + data: AnthropicChannelContext, + operation: 'chat', options: AnthropicAiOptions, stream: StreamMode, ): Span | undefined { @@ -139,7 +138,8 @@ function createGenAiSpan( const params = typeof args[0] === 'object' && args[0] !== null ? (args[0] as Record) : undefined; - const { recordInputs } = resolveAIRecordingOptions(options); + const { recordInputs, recordOutputs } = resolveGenAiCallRecording(options, { operation, request: params }); + data._sentryRecordOutputs = recordOutputs; const attributes = extractRequestAttributes(args, operation, recordInputs, getProviderName(data.self)); const model = (attributes[GEN_AI_REQUEST_MODEL] as string) || 'unknown'; @@ -222,12 +222,7 @@ const threadStreams = new WeakMap void, - options: AnthropicAiOptions, -): boolean { +function wrapThreadStreamRequest(span: Span, data: AnthropicChannelContext, end: (error?: unknown) => void): boolean { const signal = (data.arguments?.[1] as { signal?: AbortSignal } | undefined)?.signal; const threadStream = signal && threadStreams.get(signal); const request = data.result as { _responseProps?: () => Promise } | undefined; @@ -237,7 +232,7 @@ function wrapThreadStreamRequest( } const { stream, callerSignal } = threadStream; - const { recordOutputs } = resolveAIRecordingOptions(options); + const recordOutputs = !!data._sentryRecordOutputs; // Not `asResponse()`, which on 1.x also ends the SDK's own span when the user turned its tracing on. void request._responseProps().then( () => { @@ -265,13 +260,8 @@ function wrapThreadStreamRequest( * the span when iteration finishes. * - `message-stream`: `instrumentMessageStream` attaches `'message'`/`'error'`/`'end'` listeners that end the span. */ -function wrapStreamResult( - span: Span, - data: OrchestrionChannelContext, - stream: StreamMode, - options: AnthropicAiOptions, -): boolean { - const { recordOutputs } = resolveAIRecordingOptions(options); +function wrapStreamResult(span: Span, data: AnthropicChannelContext, stream: StreamMode): boolean { + const recordOutputs = !!data._sentryRecordOutputs; const result = data.result; if (stream === 'async-iterable' && isAsyncIterable(result)) { diff --git a/packages/server-utils/src/integrations/google-genai.ts b/packages/server-utils/src/integrations/google-genai.ts index 6ffae5f8df56..658690a9795f 100644 --- a/packages/server-utils/src/integrations/google-genai.ts +++ b/packages/server-utils/src/integrations/google-genai.ts @@ -9,7 +9,7 @@ import { spanToJSON, startInactiveSpan, } from '@sentry/core'; -import { getGenAiSpanOp, markCancelledIfAborted, resolveAIRecordingOptions } from '../ai/core/utils'; +import { getGenAiSpanOp, markCancelledIfAborted, resolveGenAiCallRecording } from '../ai/core/utils'; import { addPrivateRequestAttributes, addResponseAttributes, @@ -32,6 +32,9 @@ const INTEGRATION_NAME = 'Google_GenAI' as const; const ORIGIN = 'auto.ai.google_genai'; +// The recording decision is made once, when the span starts, in the caller's async context. +type GoogleGenAIChannelContext = OrchestrionChannelContext & { _sentryRecordOutputs?: boolean }; + // Each instrumented method maps to the gen_ai operation its span reports. const INSTRUMENTED_CHANNELS = [ { channel: CHANNELS.GOOGLE_GENAI_GENERATE_CONTENT, operation: 'generate_content' }, @@ -51,7 +54,7 @@ const _googleGenAIIntegration = ((options: GoogleGenAIOptions = {}) => { function instrumentGoogleGenai(options: GoogleGenAIOptions): void { for (const { channel, operation } of INSTRUMENTED_CHANNELS) { bindTracingChannelToSpan( - diagnosticsChannel.tracingChannel(channel), + diagnosticsChannel.tracingChannel(channel), data => createGenAiSpan(data, operation, options), { beforeSpanEnd: (span, data) => { @@ -65,14 +68,10 @@ function instrumentGoogleGenai(options: GoogleGenAIOptions): void { } // Embeddings responses carry no content attributes. if (operation !== 'embeddings') { - addResponseAttributes( - span, - data.result as GoogleGenAIResponse, - resolveAIRecordingOptions(options).recordOutputs, - ); + addResponseAttributes(span, data.result as GoogleGenAIResponse, !!data._sentryRecordOutputs); } }, - deferSpanEnd: ({ span, data }) => wrapStreamResult(span, data, operation, options), + deferSpanEnd: ({ span, data }) => wrapStreamResult(span, data, operation), }, ); } @@ -83,7 +82,7 @@ function instrumentGoogleGenai(options: GoogleGenAIOptions): void { * Returning `undefined` opts the payload out so no span is opened. */ function createGenAiSpan( - data: OrchestrionChannelContext, + data: GoogleGenAIChannelContext, operation: string, options: GoogleGenAIOptions, ): Span | undefined { @@ -109,9 +108,11 @@ function createGenAiSpan( const args = data.arguments ?? []; // `chat.sendMessage()` carries no config of its own unless the caller passes one, so recover the // one `chats.create()` left on the chat instance that the transform stashed in `data.self`. - const params = resolveChatParams(operation, args[0] as Record | undefined, data.self); + const request = args[0] as Record | undefined; + const params = resolveChatParams(operation, request, data.self); - const { recordInputs } = resolveAIRecordingOptions(options); + const { recordInputs, recordOutputs } = resolveGenAiCallRecording(options, { operation, request }); + data._sentryRecordOutputs = recordOutputs; const attributes = extractRequestAttributes(operation, params, data.self, recordInputs); const model = (attributes[GEN_AI_REQUEST_MODEL] as string) || 'unknown'; @@ -143,24 +144,18 @@ function isAsyncIterable(value: unknown): value is AsyncIterableStream { * For a stream we patch `result[Symbol.asyncIterator]` in place so `instrumentStream` ends the * span when iteration finishes. */ -function wrapStreamResult( - span: Span, - data: OrchestrionChannelContext, - operation: string, - options: GoogleGenAIOptions, -): boolean { +function wrapStreamResult(span: Span, data: GoogleGenAIChannelContext, operation: string): boolean { const result = data.result; if (!isAsyncIterable(result)) { return false; } - const { recordOutputs } = resolveAIRecordingOptions(options); const iterate = result[Symbol.asyncIterator].bind(result); const params = resolveChatParams(operation, data.arguments?.[0] as Record | undefined, data.self); const instrumented = instrumentStream( { [Symbol.asyncIterator]: iterate }, span, - recordOutputs ?? false, + !!data._sentryRecordOutputs, getAbortSignal(params), ); result[Symbol.asyncIterator] = () => instrumented; diff --git a/packages/server-utils/src/integrations/mistral.ts b/packages/server-utils/src/integrations/mistral.ts index 2092a689df77..542f630d1474 100644 --- a/packages/server-utils/src/integrations/mistral.ts +++ b/packages/server-utils/src/integrations/mistral.ts @@ -2,7 +2,7 @@ import { SENTRY_ORIGIN } from '@sentry/conventions/attributes'; import * as diagnosticsChannel from '../utils/diagnosticsChannel'; import type { IntegrationFn, Span, SpanAttributeValue } from '@sentry/core'; import { defineIntegration, startInactiveSpan } from '@sentry/core'; -import { getGenAiSpanOp, resolveAIRecordingOptions } from '../ai/core/utils'; +import { getGenAiSpanOp, resolveGenAiCallRecording } from '../ai/core/utils'; import { addRequestAttributes, extractRequestAttributes } from '../ai/mistral'; import { MISTRAL_INTEGRATION_NAME, MISTRAL_ORIGIN } from '../ai/mistral/constants'; import { instrumentEventStream } from '../ai/mistral/streaming'; @@ -17,6 +17,9 @@ import { bindTracingChannelToSpan } from '../tracing-channel'; const INTEGRATION_NAME = MISTRAL_INTEGRATION_NAME; +// The recording decision is made once, when the span starts, in the caller's async context. +type MistralChannelContext = OrchestrionChannelContext & { _sentryRecordOutputs?: boolean }; + // Each instrumented channel maps to the gen_ai operation its span reports. Streaming methods publish // on their own channel, so the span knows it is a stream before the result exists. const INSTRUMENTED_CHANNELS = [ @@ -39,15 +42,15 @@ const _mistralAIIntegration = ((options: MistralOptions = {}) => { function instrumentMistral(options: MistralOptions): void { for (const { channel, operation, streaming } of INSTRUMENTED_CHANNELS) { bindTracingChannelToSpan( - diagnosticsChannel.tracingChannel(channel), + diagnosticsChannel.tracingChannel(channel), data => createGenAiSpan(data, operation, streaming, options), { beforeSpanEnd: (span, data) => { - addResponseAttributes(span, data.result, resolveAIRecordingOptions(options).recordOutputs); + addResponseAttributes(span, data.result, !!data._sentryRecordOutputs); }, // Streaming: the result is an `EventStream` consumed later, so instrument it and let it end the span. deferSpanEnd: ({ span, data }) => - streaming && instrumentEventStream(data.result, span, resolveAIRecordingOptions(options).recordOutputs), + streaming && instrumentEventStream(data.result, span, !!data._sentryRecordOutputs), }, ); } @@ -58,7 +61,7 @@ function instrumentMistral(options: MistralOptions): void { * Returning `undefined` opts the payload out so no span is opened. */ function createGenAiSpan( - data: OrchestrionChannelContext, + data: MistralChannelContext, operation: string, streaming: boolean, options: MistralOptions, @@ -70,7 +73,8 @@ function createGenAiSpan( const args = data.arguments ?? []; const params = args[0] as Record | undefined; - const { recordInputs } = resolveAIRecordingOptions(options); + const { recordInputs, recordOutputs } = resolveGenAiCallRecording(options, { operation, request: params }); + data._sentryRecordOutputs = recordOutputs; const attributes = extractRequestAttributes(args, operation, recordInputs, streaming); attributes[SENTRY_ORIGIN] = MISTRAL_ORIGIN;