diff --git a/dev-packages/node-integration-tests/suites/aws-serverless/bedrock/instrument-integration-options.mjs b/dev-packages/node-integration-tests/suites/aws-serverless/bedrock/instrument-integration-options.mjs new file mode 100644 index 000000000000..380d0f24f721 --- /dev/null +++ b/dev-packages/node-integration-tests/suites/aws-serverless/bedrock/instrument-integration-options.mjs @@ -0,0 +1,11 @@ +import * as Sentry from '@sentry/aws-serverless'; +import { loggingTransport } from '@sentry-internal/node-integration-tests'; + +Sentry.init({ + dsn: 'https://public@dsn.ingest.sentry.io/1337', + release: '1.0', + tracesSampleRate: 1.0, + dataCollection: { genAI: { inputs: false, outputs: false } }, + integrations: [Sentry.awsIntegration({ recordInputs: true, recordOutputs: true })], + transport: loggingTransport, +}); diff --git a/dev-packages/node-integration-tests/suites/aws-serverless/bedrock/test.ts b/dev-packages/node-integration-tests/suites/aws-serverless/bedrock/test.ts index 18ed915a9312..d021a54859b3 100644 --- a/dev-packages/node-integration-tests/suites/aws-serverless/bedrock/test.ts +++ b/dev-packages/node-integration-tests/suites/aws-serverless/bedrock/test.ts @@ -125,6 +125,16 @@ describe('awsIntegration - Bedrock', () => { }); }); + createEsmAndCjsTests(__dirname, 'scenario.mjs', 'instrument-integration-options.mjs', (createTestRunner, test) => { + test( + 'records Bedrock inputs and outputs when awsIntegration options override dataCollection', + { timeout: 90_000 }, + async () => { + await createTestRunner().ignore('event').expect({ span: assertBedrockContent }).start().completed(); + }, + ); + }); + createEsmAndCjsTests(__dirname, 'scenario-stream.mjs', 'instrument.mjs', (createTestRunner, test) => { test('ends Bedrock stream spans when the streams finish, fail, or are aborted', { timeout: 90_000 }, async () => { await createTestRunner().ignore('event').expect({ span: assertStreamSpans }).start().completed(); diff --git a/packages/server-utils/src/integrations/aws-sdk/index.ts b/packages/server-utils/src/integrations/aws-sdk/index.ts index e98c00a6ee6f..1088f7881aee 100644 --- a/packages/server-utils/src/integrations/aws-sdk/index.ts +++ b/packages/server-utils/src/integrations/aws-sdk/index.ts @@ -10,6 +10,7 @@ import { SENTRY_ORIGIN, } from '@sentry/conventions/attributes'; import { RPC } from '@sentry/conventions/op'; +import type { GenAiOptions } from '../../ai/core/utils'; import type { OrchestrionChannelContext } from '../../orchestrion/types'; import { CHANNELS } from '../../orchestrion/channels'; import { awsSdkModuleNames } from '../../orchestrion/config/aws-sdk'; @@ -63,8 +64,11 @@ function setMetadataAttributes(span: Span, metadata: Record | undef } } -const _awsIntegration = (() => { - const servicesExtensions = new ServicesExtensions(); +// `recordInputs` and `recordOutputs` apply to Bedrock Runtime calls, the only AWS calls that carry prompts and responses. +export type AwsIntegrationOptions = GenAiOptions; + +const _awsIntegration = ((options?: AwsIntegrationOptions) => { + const servicesExtensions = new ServicesExtensions(options); return { name: INTEGRATION_NAME, diff --git a/packages/server-utils/src/integrations/aws-sdk/services/ServicesExtensions.ts b/packages/server-utils/src/integrations/aws-sdk/services/ServicesExtensions.ts index 36dfcc884892..a322ab457820 100644 --- a/packages/server-utils/src/integrations/aws-sdk/services/ServicesExtensions.ts +++ b/packages/server-utils/src/integrations/aws-sdk/services/ServicesExtensions.ts @@ -1,4 +1,5 @@ import type { Span } from '@sentry/core'; +import type { GenAiOptions } from '../../../ai/core/utils'; import type { NormalizedRequest, NormalizedResponse, RequestMetadata } from '../types'; import { BedrockRuntimeServiceExtension } from './bedrock-runtime'; import { DynamodbServiceExtension } from './dynamodb'; @@ -14,17 +15,21 @@ import { StepFunctionsServiceExtension } from './stepfunctions'; export class ServicesExtensions implements ServiceExtension { // Per-service extensions, keyed by the client's `serviceId` (e.g. `'S3'`). Services without a // registered extension still get the base rpc span from the subscriber. - private _services: Map = new Map([ - ['SecretsManager', new SecretsManagerServiceExtension()], - ['SFN', new StepFunctionsServiceExtension()], - ['SQS', new SqsServiceExtension()], - ['SNS', new SnsServiceExtension()], - ['DynamoDB', new DynamodbServiceExtension()], - ['Lambda', new LambdaServiceExtension()], - ['S3', new S3ServiceExtension()], - ['Kinesis', new KinesisServiceExtension()], - ['BedrockRuntime', new BedrockRuntimeServiceExtension()], - ]); + private _services: Map; + + public constructor(genAiOptions?: GenAiOptions) { + this._services = new Map([ + ['SecretsManager', new SecretsManagerServiceExtension()], + ['SFN', new StepFunctionsServiceExtension()], + ['SQS', new SqsServiceExtension()], + ['SNS', new SnsServiceExtension()], + ['DynamoDB', new DynamodbServiceExtension()], + ['Lambda', new LambdaServiceExtension()], + ['S3', new S3ServiceExtension()], + ['Kinesis', new KinesisServiceExtension()], + ['BedrockRuntime', new BedrockRuntimeServiceExtension(genAiOptions)], + ]); + } public requestPreSpanHook(request: NormalizedRequest): RequestMetadata { const serviceExtension = this._services.get(request.serviceName); diff --git a/packages/server-utils/src/integrations/aws-sdk/services/bedrock-runtime.ts b/packages/server-utils/src/integrations/aws-sdk/services/bedrock-runtime.ts index 13c7d0f63594..e822b5945bdc 100644 --- a/packages/server-utils/src/integrations/aws-sdk/services/bedrock-runtime.ts +++ b/packages/server-utils/src/integrations/aws-sdk/services/bedrock-runtime.ts @@ -13,7 +13,7 @@ import { GEN_AI_USAGE_OUTPUT_TOKENS, } from '@sentry/conventions/attributes'; import { GEN_AI_CHAT, GEN_AI_GENERATE_CONTENT } from '@sentry/conventions/op'; -import type { StreamResponseState } from '../../../ai/core/utils'; +import type { GenAiOptions, StreamResponseState } from '../../../ai/core/utils'; import type { GenAiMessagePart } from '../../../ai/types'; import { endStreamSpan, @@ -71,6 +71,12 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension { // client span, without the model call on it. private _piAiRequests: WeakSet = new WeakSet(); + public constructor(private readonly _options: GenAiOptions = {}) {} + + private _recordingOptions(): Required { + return resolveAIRecordingOptions(this._options); + } + public requestPreSpanHook(request: NormalizedRequest): RequestMetadata { if (isInsidePiAiSpan()) { this._piAiRequests.add(request); @@ -152,7 +158,7 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension { } } - if (resolveAIRecordingOptions().recordInputs) { + if (this._recordingOptions().recordInputs) { Object.assign(spanAttributes, getConverseInputAttributes(request.commandInput)); } @@ -281,7 +287,7 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension { } } - if (resolveAIRecordingOptions().recordInputs) { + if (this._recordingOptions().recordInputs) { Object.assign(spanAttributes, getInvokeModelInputAttributes(modelId, requestBody)); } } @@ -298,7 +304,7 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension { const { stopReason, usage, output } = response.data; setStopReason(span, stopReason); - if (resolveAIRecordingOptions().recordOutputs) { + if (this._recordingOptions().recordOutputs) { setOutputMessages(span, contentBlocksToParts(output?.message?.content)); } span.setAttributes( @@ -326,6 +332,7 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension { response.data.stream as AsyncIterable, span, response.request.abortSignal, + this._recordingOptions().recordOutputs, processConverseStreamEvent, ); } @@ -393,7 +400,7 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension { span.setAttribute(GEN_AI_USAGE_OUTPUT_TOKENS, Math.ceil(responseBody.outputs[0].text.length / 6)); } } - if (resolveAIRecordingOptions().recordOutputs) { + if (this._recordingOptions().recordOutputs) { setOutputMessages(span, getInvokeModelOutputParts(responseBody)); } } @@ -417,6 +424,7 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension { stream as AsyncIterable<{ chunk?: { bytes?: Uint8Array } }>, span, response.request.abortSignal, + this._recordingOptions().recordOutputs, (item, state) => { if (processChunk) { const parsedChunk = parseChunk(item?.chunk?.bytes); @@ -474,13 +482,14 @@ async function* instrumentStream( stream: AsyncIterable, span: Span, abortSignal: AbortSignal | undefined, + recordOutputs: boolean, processItem: (item: T, state: BedrockStreamState) => void, ): AsyncGenerator { const state: BedrockStreamState = { finishReasons: [], responseTexts: [], toolCalls: [], - parts: resolveAIRecordingOptions().recordOutputs ? [] : undefined, + parts: recordOutputs ? [] : undefined, }; try { for await (const item of stream) { diff --git a/packages/server-utils/test/integrations/aws-sdk-bedrock.test.ts b/packages/server-utils/test/integrations/aws-sdk-bedrock.test.ts index f96cb98a6066..a5ef64146989 100644 --- a/packages/server-utils/test/integrations/aws-sdk-bedrock.test.ts +++ b/packages/server-utils/test/integrations/aws-sdk-bedrock.test.ts @@ -19,6 +19,7 @@ import { GEN_AI_GENERATE_CONTENT } from '@sentry/conventions/op'; import type { Span } from '@sentry/core'; import { setCurrentClient, spanToJSON, startInactiveSpan } from '@sentry/core'; import { afterEach, beforeEach, describe, expect, it } from 'vitest'; +import type { GenAiOptions } from '../../src/ai/core/utils'; import { BedrockRuntimeServiceExtension } from '../../src/integrations/aws-sdk/services/bedrock-runtime'; import type { NormalizedResponse, RequestMetadata } from '../../src/integrations/aws-sdk/types'; import { getDefaultTestClientOptions, TestClient } from '../mocks/client'; @@ -32,8 +33,12 @@ const CLAUDE_REQUEST_BODY = { messages: [{ role: 'user', content: 'What is the capital of France?' }], }; -function preSpanHook(commandName: string, commandInput: Record): RequestMetadata { - return new BedrockRuntimeServiceExtension().requestPreSpanHook({ +function preSpanHook( + commandName: string, + commandInput: Record, + options?: GenAiOptions, +): RequestMetadata { + return new BedrockRuntimeServiceExtension(options).requestPreSpanHook({ serviceName: 'BedrockRuntime', commandName, commandInput, @@ -215,18 +220,28 @@ describe('BedrockRuntimeServiceExtension input recording', () => { ); }); - it.each([ - { commandName: 'Converse', commandInput: { messages: [{ role: 'user', content: [{ text: 'Kawasaki' }] }] } }, - { - commandName: 'InvokeModel', - commandInput: { modelId: CLAUDE_MODEL_ID, body: JSON.stringify(CLAUDE_REQUEST_BODY) }, - }, - ])('records no $commandName input when dataCollection.genAI.inputs is off', ({ commandName, commandInput }) => { - const client = new TestClient(getDefaultTestClientOptions({ dataCollection: { genAI: { inputs: false } } })); + it.each( + [ + { commandName: 'Converse', commandInput: { messages: [{ role: 'user', content: [{ text: 'Kawasaki' }] }] } }, + { + commandName: 'InvokeModel', + commandInput: { modelId: CLAUDE_MODEL_ID, body: JSON.stringify(CLAUDE_REQUEST_BODY) }, + }, + ].flatMap(command => [ + { + ...command, + setting: 'dataCollection.genAI.inputs', + dataCollection: { genAI: { inputs: false } }, + options: undefined, + }, + { ...command, setting: 'recordInputs', dataCollection: undefined, options: { recordInputs: false } }, + ]), + )('records no $commandName input when $setting is off', ({ commandName, commandInput, dataCollection, options }) => { + const client = new TestClient(getDefaultTestClientOptions({ dataCollection })); setCurrentClient(client); client.init(); - const { spanAttributes } = preSpanHook(commandName, commandInput); + const { spanAttributes } = preSpanHook(commandName, commandInput, options); expect(spanAttributes).not.toHaveProperty(GEN_AI_INPUT_MESSAGES); }); @@ -243,13 +258,14 @@ function respond( commandInput: Record, data: unknown, abortSignal?: AbortSignal, + options?: GenAiOptions, ): void { const response: NormalizedResponse = { data, request: { serviceName: 'BedrockRuntime', commandName, commandInput, abortSignal }, requestId: 'b0a5e5a2-2c2b-4f56-9d0e-8c7c2d3f4a10', }; - new BedrockRuntimeServiceExtension().responseHook(response, span); + new BedrockRuntimeServiceExtension(options).responseHook(response, span); } async function drain(stream: AsyncIterable): Promise { @@ -796,35 +812,50 @@ describe('BedrockRuntimeServiceExtension output recording', () => { ]); }); - it.each([ - { commandName: 'Converse', makeData: () => ({ output: { message: { content: [{ text: 'Paris.' }] } } }) }, - { - commandName: 'InvokeModel', - makeData: () => ({ body: encoder.encode(JSON.stringify({ outputs: [{ text: 'Paris.' }] })) }), - }, - { - commandName: 'ConverseStream', - makeData: () => ({ - stream: (async function* () { - yield { contentBlockStart: { contentBlockIndex: 0, start: { toolUse: { toolUseId: 't', name: 'f' } } } }; - yield { contentBlockDelta: { contentBlockIndex: 0, delta: { toolUse: { input: '{}' } } } }; - })(), - }), - }, - ])('records no $commandName output when dataCollection.genAI.outputs is off', async ({ commandName, makeData }) => { - const client = new TestClient( - getDefaultTestClientOptions({ tracesSampleRate: 1, dataCollection: { genAI: { outputs: false } } }), - ); - setCurrentClient(client); - client.init(); - const span = startInactiveSpan({ name: 'bedrock' }); - const data = makeData(); + it.each( + [ + { commandName: 'Converse', makeData: () => ({ output: { message: { content: [{ text: 'Paris.' }] } } }) }, + { + commandName: 'InvokeModel', + makeData: () => ({ body: encoder.encode(JSON.stringify({ outputs: [{ text: 'Paris.' }] })) }), + }, + { + commandName: 'ConverseStream', + makeData: () => ({ + stream: (async function* () { + yield { contentBlockStart: { contentBlockIndex: 0, start: { toolUse: { toolUseId: 't', name: 'f' } } } }; + yield { contentBlockDelta: { contentBlockIndex: 0, delta: { toolUse: { input: '{}' } } } }; + })(), + }), + }, + { + commandName: 'InvokeModelWithResponseStream', + makeData: () => ({ body: streamOf([{ outputs: [{ text: 'Paris.' }] }]) }), + }, + ].flatMap(command => [ + { + ...command, + setting: 'dataCollection.genAI.outputs', + dataCollection: { genAI: { outputs: false } }, + options: undefined, + }, + { ...command, setting: 'recordOutputs', dataCollection: undefined, options: { recordOutputs: false } }, + ]), + )( + 'records no $commandName output when $setting is off', + async ({ commandName, makeData, dataCollection, options }) => { + const client = new TestClient(getDefaultTestClientOptions({ tracesSampleRate: 1, dataCollection })); + setCurrentClient(client); + client.init(); + const span = startInactiveSpan({ name: 'bedrock' }); + const data = makeData() as { stream?: AsyncIterable; body?: unknown }; - respond(span, commandName, { modelId: 'mistral.mistral-large-2402-v1:0' }, data); - if ('stream' in data) { - await drain(data.stream); - } + respond(span, commandName, { modelId: 'mistral.mistral-large-2402-v1:0' }, data, undefined, options); + if (commandName.endsWith('Stream')) { + await drain((data.stream ?? data.body) as AsyncIterable); + } - expect(spanToJSON(span).attributes).not.toHaveProperty(GEN_AI_OUTPUT_MESSAGES); - }); + expect(spanToJSON(span).attributes).not.toHaveProperty(GEN_AI_OUTPUT_MESSAGES); + }, + ); });