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
Expand Up @@ -14,5 +14,12 @@ Sentry.init({
enableTruncation: false,
}),
],
beforeSendTransaction: event => {
// Filter out mock express server transactions
if (event.transaction.includes('/v1beta/')) {
return null;
}
return event;
},
streamGenAiSpans: true,
});
Original file line number Diff line number Diff line change
@@ -1,48 +1,40 @@
import { instrumentGoogleGenAIClient } from '@sentry/core';
import { GoogleGenAI } from '@google/genai';
import * as Sentry from '@sentry/node';
import express from 'express';

class MockGoogleGenerativeAI {
constructor(config) {
this.apiKey = config.apiKey;
function startMockGoogleGenAIServer() {
const app = express();
app.use(express.json({ limit: '10mb' }));

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,
app.post('/v1beta/models/:model\\:generateContent', (req, res) => {
res.send({
candidates: [
{
content: { parts: [{ text: 'Response to truncated messages' }], role: 'model' },
finishReason: 'stop',
index: 0,
},
candidates: [
{
content: {
parts: [{ text: 'Response to truncated messages' }],
role: 'model',
},
finishReason: 'STOP',
},
],
},
};
}
],
usageMetadata: { promptTokenCount: 10, candidatesTokenCount: 15, totalTokenCount: 25 },
});
});

return new Promise(resolve => {
const server = app.listen(0, () => {
resolve(server);
});
});
}

async function run() {
const server = await startMockGoogleGenAIServer();

await Sentry.startSpan({ op: 'function', name: 'main' }, async () => {
const mockClient = new MockGoogleGenerativeAI({
const client = new GoogleGenAI({
apiKey: 'mock-api-key',
httpOptions: { baseUrl: `http://localhost:${server.address().port}` },
});

const client = instrumentGoogleGenAIClient(mockClient, { enableTruncation: true, recordInputs: true });

// Test 1: Given an array of messages only the last message should be kept
// The last message should be truncated to fit within the 20KB limit
const largeContent1 = 'A'.repeat(15000); // ~15KB
Expand Down Expand Up @@ -80,6 +72,10 @@ async function run() {
],
});
});

await Sentry.flush(2000);

server.close();
}

run();
Original file line number Diff line number Diff line change
@@ -1,35 +1,39 @@
import { instrumentGoogleGenAIClient } from '@sentry/core';
import { GoogleGenAI } from '@google/genai';
import * as Sentry from '@sentry/node';
import express from 'express';

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',
usageMetadata: { promptTokenCount: 10, candidatesTokenCount: 5, totalTokenCount: 15 },
candidates: [
{
content: { parts: [{ text: 'Response' }], role: 'model' },
finishReason: 'STOP',
},
],
},
};
}
function startMockGoogleGenAIServer() {
const app = express();
app.use(express.json({ limit: '10mb' }));

app.post('/v1beta/models/:model\\:generateContent', (req, res) => {
res.send({
candidates: [
{
content: { parts: [{ text: 'Response' }], role: 'model' },
finishReason: 'stop',
index: 0,
},
],
usageMetadata: { promptTokenCount: 10, candidatesTokenCount: 5, totalTokenCount: 15 },
});
});

return new Promise(resolve => {
const server = app.listen(0, () => {
resolve(server);
});
});
}

async function run() {
const server = await startMockGoogleGenAIServer();

await Sentry.startSpan({ op: 'function', name: 'main' }, async () => {
const mockClient = new MockGoogleGenerativeAI({ apiKey: 'mock-api-key' });
const client = instrumentGoogleGenAIClient(mockClient, { enableTruncation: false, recordInputs: true });
const client = new GoogleGenAI({
apiKey: 'mock-api-key',
httpOptions: { baseUrl: `http://localhost:${server.address().port}` },
});

// Long content that would normally be truncated
const longContent = 'A'.repeat(50_000);
Expand All @@ -42,6 +46,10 @@ async function run() {
],
});
});

await Sentry.flush(2000);

server.close();
}

run();
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
import { instrumentGoogleGenAIClient } from '@sentry/core';
import { GoogleGenAI } from '@google/genai';
import * as Sentry from '@sentry/node';
import express from 'express';

class MockGoogleGenAI {
constructor(config) {
this.apiKey = config.apiKey;
this.models = {
generateContent: async params => {
await new Promise(resolve => setTimeout(resolve, 10));
return {
response: {
text: () => 'Response',
modelVersion: params.model,
usageMetadata: {
promptTokenCount: 10,
candidatesTokenCount: 5,
totalTokenCount: 15,
},
candidates: [
{
content: {
parts: [{ text: 'Response' }],
role: 'model',
},
finishReason: 'STOP',
},
],
},
};
},
};
}
function startMockGoogleGenAIServer() {
const app = express();
app.use(express.json());

app.post('/v1beta/models/:model\\:generateContent', (req, res) => {
res.send({
candidates: [
{
content: { parts: [{ text: 'Response' }], role: 'model' },
finishReason: 'stop',
index: 0,
},
],
modelVersion: req.params.model,
usageMetadata: { promptTokenCount: 10, candidatesTokenCount: 5, totalTokenCount: 15 },
});
});

return new Promise(resolve => {
const server = app.listen(0, () => {
resolve(server);
});
});
}

async function run() {
const server = await startMockGoogleGenAIServer();

await Sentry.startSpan({ op: 'function', name: 'main' }, async () => {
const mockClient = new MockGoogleGenAI({ apiKey: 'mock-api-key' });
const client = instrumentGoogleGenAIClient(mockClient);
const client = new GoogleGenAI({
apiKey: 'mock-api-key',
httpOptions: { baseUrl: `http://localhost:${server.address().port}` },
});

await client.models.generateContent({
model: 'gemini-1.5-flash',
Expand All @@ -45,6 +44,10 @@ async function run() {
contents: [{ role: 'user', parts: [{ text: 'Hello' }] }],
});
});

await Sentry.flush(2000);

server.close();
}

run();
Loading