Skip to content
Merged
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,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,
});
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
8 changes: 6 additions & 2 deletions packages/server-utils/src/integrations/aws-sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -63,8 +64,11 @@ function setMetadataAttributes(span: Span, metadata: Record<string, any> | 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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<string, ServiceExtension> = new Map<string, ServiceExtension>([
['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<string, ServiceExtension>;

public constructor(genAiOptions?: GenAiOptions) {
this._services = new Map<string, ServiceExtension>([
['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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -71,6 +71,12 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
// client span, without the model call on it.
private _piAiRequests: WeakSet<NormalizedRequest> = new WeakSet();

public constructor(private readonly _options: GenAiOptions = {}) {}

private _recordingOptions(): Required<GenAiOptions> {
return resolveAIRecordingOptions(this._options);
}

public requestPreSpanHook(request: NormalizedRequest): RequestMetadata {
if (isInsidePiAiSpan()) {
this._piAiRequests.add(request);
Expand Down Expand Up @@ -152,7 +158,7 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
}
}

if (resolveAIRecordingOptions().recordInputs) {
if (this._recordingOptions().recordInputs) {
Object.assign(spanAttributes, getConverseInputAttributes(request.commandInput));
}

Expand Down Expand Up @@ -281,7 +287,7 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
}
}

if (resolveAIRecordingOptions().recordInputs) {
if (this._recordingOptions().recordInputs) {
Object.assign(spanAttributes, getInvokeModelInputAttributes(modelId, requestBody));
}
}
Expand All @@ -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(
Expand Down Expand Up @@ -326,6 +332,7 @@ export class BedrockRuntimeServiceExtension implements ServiceExtension {
response.data.stream as AsyncIterable<ConverseStreamOutput>,
span,
response.request.abortSignal,
this._recordingOptions().recordOutputs,
processConverseStreamEvent,
);
}
Expand Down Expand Up @@ -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));
}
}
Expand All @@ -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);
Expand Down Expand Up @@ -474,13 +482,14 @@ async function* instrumentStream<T>(
stream: AsyncIterable<T>,
span: Span,
abortSignal: AbortSignal | undefined,
recordOutputs: boolean,
processItem: (item: T, state: BedrockStreamState) => void,
): AsyncGenerator<T> {
const state: BedrockStreamState = {
finishReasons: [],
responseTexts: [],
toolCalls: [],
parts: resolveAIRecordingOptions().recordOutputs ? [] : undefined,
parts: recordOutputs ? [] : undefined,
};
try {
for await (const item of stream) {
Expand Down
113 changes: 72 additions & 41 deletions packages/server-utils/test/integrations/aws-sdk-bedrock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -32,8 +33,12 @@ const CLAUDE_REQUEST_BODY = {
messages: [{ role: 'user', content: 'What is the capital of France?' }],
};

function preSpanHook(commandName: string, commandInput: Record<string, unknown>): RequestMetadata {
return new BedrockRuntimeServiceExtension().requestPreSpanHook({
function preSpanHook(
commandName: string,
commandInput: Record<string, unknown>,
options?: GenAiOptions,
): RequestMetadata {
return new BedrockRuntimeServiceExtension(options).requestPreSpanHook({
serviceName: 'BedrockRuntime',
commandName,
commandInput,
Expand Down Expand Up @@ -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);
});
Expand All @@ -243,13 +258,14 @@ function respond(
commandInput: Record<string, unknown>,
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<unknown>): Promise<void> {
Expand Down Expand Up @@ -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<unknown>; 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<unknown>);
}

expect(spanToJSON(span).attributes).not.toHaveProperty(GEN_AI_OUTPUT_MESSAGES);
});
expect(spanToJSON(span).attributes).not.toHaveProperty(GEN_AI_OUTPUT_MESSAGES);
},
);
});
Loading