Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
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,72 @@
import { instrumentAnthropicAiClient } from '@sentry/core';
import * as Sentry from '@sentry/node';

class MockAnthropic {
constructor(config) {
this.apiKey = config.apiKey;
this.baseURL = config.baseURL;

// Create messages object with create method
this.messages = {
create: this._messagesCreate.bind(this),
};
}

/**
* Create a mock message
*/
async _messagesCreate(params) {
// Simulate processing time
await new Promise(resolve => setTimeout(resolve, 10));

return {
id: 'msg-truncation-test',
type: 'message',
role: 'assistant',
content: [
{
type: 'text',
text: 'Response to truncated messages',
},
],
model: params.model,
stop_reason: 'end_turn',
stop_sequence: null,
usage: {
input_tokens: 10,
output_tokens: 15,
},
};
}
}

async function run() {
await Sentry.startSpan({ op: 'function', name: 'main' }, async () => {
const mockClient = new MockAnthropic({
apiKey: 'mock-api-key',
});

const client = instrumentAnthropicAiClient(mockClient);

// Create 3 large messages where:
// - First 2 messages are very large (will be dropped)
// - Last message is large but will be truncated to fit within the 20KB limit
const largeContent1 = 'A'.repeat(15000); // ~15KB
const largeContent2 = 'B'.repeat(15000); // ~15KB
const largeContent3 = 'C'.repeat(25000); // ~25KB (will be truncated)

await client.messages.create({
model: 'claude-3-haiku-20240307',
max_tokens: 100,
messages: [
{ role: 'user', content: largeContent1 },
{ role: 'assistant', content: largeContent2 },
{ role: 'user', content: largeContent3 },
],
temperature: 0.7,
});
});
}

run();

Original file line number Diff line number Diff line change
Expand Up @@ -497,4 +497,40 @@ describe('Anthropic integration', () => {
await createRunner().ignore('event').expect({ transaction: EXPECTED_ERROR_SPANS }).start().completed();
});
});

createEsmAndCjsTests(
__dirname,
'scenario-message-truncation.mjs',
'instrument-with-pii.mjs',
(createRunner, test) => {
test('truncates messages when they exceed byte limit - keeps only last message and crops it', async () => {
await createRunner()
.ignore('event')
.expect({
transaction: {
transaction: 'main',
spans: expect.arrayContaining([
expect.objectContaining({
data: expect.objectContaining({
'gen_ai.operation.name': 'messages',
'sentry.op': 'gen_ai.messages',
'sentry.origin': 'auto.ai.anthropic',
'gen_ai.system': 'anthropic',
'gen_ai.request.model': 'claude-3-haiku-20240307',
// Messages should be present (truncation happened) and should be a JSON array
'gen_ai.request.messages': expect.stringMatching(/^\[\{"role":"user","content":"C+"\}\]$/),
}),
description: 'messages claude-3-haiku-20240307',
op: 'gen_ai.messages',
origin: 'auto.ai.anthropic',
status: 'ok',
}),
]),
},
})
.start()
.completed();
});
},
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { instrumentGoogleGenAIClient } from '@sentry/core';
import * as Sentry from '@sentry/node';

class MockGoogleGenerativeAI {
constructor(config) {
this.apiKey = config.apiKey;

this.models = {
generateContent: this._generateContent.bind(this),
};
}

async _generateContent() {
await new Promise(resolve => setTimeout(resolve, 10));

return {
response: {
text: () => 'Response to truncated messages',
usageMetadata: {
promptTokenCount: 10,
candidatesTokenCount: 15,
totalTokenCount: 25,
},
candidates: [
{
content: {
parts: [{ text: 'Response to truncated messages' }],
role: 'model',
},
finishReason: 'STOP',
},
],
},
};
}
}

async function run() {
await Sentry.startSpan({ op: 'function', name: 'main' }, async () => {
const mockClient = new MockGoogleGenerativeAI({
apiKey: 'mock-api-key',
});

const client = instrumentGoogleGenAIClient(mockClient);

// Create 3 large messages where:
// - First 2 messages are very large (will be dropped)
// - Last message is large but will be truncated to fit within the 20KB limit
const largeContent1 = 'A'.repeat(15000); // ~15KB
const largeContent2 = 'B'.repeat(15000); // ~15KB
const largeContent3 = 'C'.repeat(25000); // ~25KB (will be truncated)

await client.models.generateContent({
model: 'gemini-1.5-flash',
config: {
temperature: 0.7,
topP: 0.9,
maxOutputTokens: 100,
},
contents: [
{ role: 'user', parts: [{ text: largeContent1 }] },
{ role: 'model', parts: [{ text: largeContent2 }] },
{ role: 'user', parts: [{ text: largeContent3 }] },
],
});
});
}

run();

Original file line number Diff line number Diff line change
Expand Up @@ -486,4 +486,42 @@ describe('Google GenAI integration', () => {
.completed();
});
});

createEsmAndCjsTests(
__dirname,
'scenario-message-truncation.mjs',
'instrument-with-pii.mjs',
(createRunner, test) => {
test('truncates messages when they exceed byte limit - keeps only last message and crops it', async () => {
await createRunner()
.ignore('event')
.expect({
transaction: {
transaction: 'main',
spans: expect.arrayContaining([
expect.objectContaining({
data: expect.objectContaining({
'gen_ai.operation.name': 'models',
'sentry.op': 'gen_ai.models',
'sentry.origin': 'auto.ai.google_genai',
'gen_ai.system': 'google_genai',
'gen_ai.request.model': 'gemini-1.5-flash',
// Messages should be present (truncation happened) and should be a JSON array with parts
'gen_ai.request.messages': expect.stringMatching(
/^\[\{"role":"user","parts":\[\{"text":"C+"\}\]\}\]$/,
),
}),
description: 'models gemini-1.5-flash',
op: 'gen_ai.models',
origin: 'auto.ai.google_genai',
status: 'ok',
}),
]),
},
})
.start()
.completed();
});
},
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { instrumentOpenAiClient } from '@sentry/core';
import * as Sentry from '@sentry/node';

class MockOpenAI {
constructor(config) {
this.apiKey = config.apiKey;

this.chat = {
completions: {
create: async params => {
// Simulate processing time
await new Promise(resolve => setTimeout(resolve, 10));

return {
id: 'chatcmpl-truncation-test',
object: 'chat.completion',
created: 1677652288,
model: params.model,
system_fingerprint: 'fp_44709d6fcb',
choices: [
{
index: 0,
message: {
role: 'assistant',
content: 'Response to truncated messages',
},
finish_reason: 'stop',
},
],
usage: {
prompt_tokens: 10,
completion_tokens: 15,
total_tokens: 25,
},
};
},
},
};
}
}

async function run() {
await Sentry.startSpan({ op: 'function', name: 'main' }, async () => {
const mockClient = new MockOpenAI({
apiKey: 'mock-api-key',
});

const client = instrumentOpenAiClient(mockClient);

// Create 3 large messages where:
// - First 2 messages are very large (will be dropped)
// - Last message is large but will be truncated to fit within the 20KB limit
const largeContent1 = 'A'.repeat(15000); // ~15KB
const largeContent2 = 'B'.repeat(15000); // ~15KB
const largeContent3 = 'C'.repeat(25000); // ~25KB (will be truncated)

await client.chat.completions.create({
model: 'gpt-3.5-turbo',
messages: [
{ role: 'system', content: largeContent1 },
{ role: 'user', content: largeContent2 },
{ role: 'user', content: largeContent3 },
],
temperature: 0.7,
});
});
}

run();

36 changes: 36 additions & 0 deletions dev-packages/node-integration-tests/suites/tracing/openai/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,40 @@ describe('OpenAI integration', () => {
.completed();
});
});

createEsmAndCjsTests(
__dirname,
'scenario-message-truncation.mjs',
'instrument-with-pii.mjs',
(createRunner, test) => {
test('truncates messages when they exceed byte limit - keeps only last message and crops it', async () => {
await createRunner()
.ignore('event')
.expect({
transaction: {
transaction: 'main',
spans: expect.arrayContaining([
expect.objectContaining({
data: expect.objectContaining({
'gen_ai.operation.name': 'chat',
'sentry.op': 'gen_ai.chat',
'sentry.origin': 'auto.ai.openai',
'gen_ai.system': 'openai',
'gen_ai.request.model': 'gpt-3.5-turbo',
// Messages should be present (truncation happened) and should be a JSON array of a single index
'gen_ai.request.messages': expect.stringMatching(/^\[\{"role":"user","content":"C+"\}\]$/),
}),
description: 'chat gpt-3.5-turbo',
op: 'gen_ai.chat',
origin: 'auto.ai.openai',
status: 'ok',
}),
]),
},
})
.start()
.completed();
});
},
);
});
Loading
Loading