Skip to content

Commit ca89a96

Browse files
RulaKhaledclaude
andauthored
fix(server-utils): Support @google/genai v3 in auto-instrumentation (#26441)
Raises the `@google/genai` orchestrion range from `<3` to `<4`, so Node apps on 3.x get `gen_ai` spans. The functions we hook look the same in 3.x and the response shapes are unchanged, so the range is the only change needed. The new `google-genai-v3` suite pins `^3` and runs only on Node 22+, which 3.x requires. Its mock server serves `/v1` routes, because 3.x calls the `v1` API by default instead of `v1beta`. Fixes #26404 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 5e2ad90 commit ca89a96

7 files changed

Lines changed: 331 additions & 7 deletions

File tree

‎dev-packages/bun-integration-tests/node-suites/excludes.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export const NO_AUTO_INSTRUMENTATION = [
8686
'suites/tracing/genericPool/test.ts',
8787
'suites/tracing/is-localhost/test.ts',
8888
'suites/tracing/google-genai-v2/test.ts',
89+
'suites/tracing/google-genai-v3/test.ts',
8990
'suites/tracing/google-genai/test.ts',
9091
'suites/tracing/groq/test.ts',
9192
'suites/tracing/hapi/test.ts',
@@ -240,6 +241,7 @@ const BUN_BUILD_NOT_TRIAGED = [
240241
'suites/hono-sdk/test.ts',
241242
'suites/pino/test.ts',
242243
'suites/tracing/google-genai-v2/test.ts',
244+
'suites/tracing/google-genai-v3/test.ts',
243245
'suites/tracing/google-genai/test.ts',
244246
'suites/tracing/langchain/v1/test.ts',
245247
'suites/tracing/langgraph/v1/test.ts',

‎dev-packages/node-integration-tests/suites/tracing/google-genai-v2/test.ts‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runn
1717
const EXPECTED_ORIGIN = 'auto.ai.google_genai';
1818

1919
// `@google/genai` v2 restructured the `Models` class so `embedContent` is a constructor-assigned arrow
20-
// property rather than a class method (v1 shape). The orchestrion config caps at `<3`, so the code
21-
// transformer only injects the diagnostics channels for v2 when the range includes it — this suite pins
22-
// `^2` and re-runs the core auto-instrumentation assertions to guard that path. The v1 suite lives in
23-
// `../google-genai`; the scenario files are byte-identical because v2 kept the public API surface.
20+
// property rather than a class method (v1 shape). The code transformer only injects the diagnostics
21+
// channels for v2 when the orchestrion range includes it, so this suite pins `^2` and re-runs the core
22+
// auto-instrumentation assertions to guard that path. The v1 suite lives in `../google-genai`; the
23+
// scenario files are byte-identical because v2 kept the public API surface.
2424
describe('Google GenAI integration (v2)', () => {
2525
afterAll(() => {
2626
cleanupChildProcesses();
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import * as Sentry from '@sentry/node';
2+
import { loggingTransport } from '@sentry-internal/node-integration-tests';
3+
4+
Sentry.init({
5+
dsn: 'https://public@dsn.ingest.sentry.io/1337',
6+
release: '1.0',
7+
tracesSampleRate: 1.0,
8+
dataCollection: { genAI: { inputs: false, outputs: false } },
9+
transport: loggingTransport,
10+
});
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import { GoogleGenAI } from '@google/genai';
2+
import * as Sentry from '@sentry/node';
3+
import express from 'express';
4+
5+
function startMockGoogleGenAIServer() {
6+
const app = express();
7+
app.use(express.json());
8+
9+
app.post('/v1/models/:model\\:batchEmbedContents', (req, res) => {
10+
const model = req.params.model;
11+
12+
if (model === 'error-model') {
13+
res.status(404).set('x-request-id', 'mock-request-123').end('Model not found');
14+
return;
15+
}
16+
17+
res.send({
18+
embeddings: [
19+
{
20+
values: [0.1, 0.2, 0.3, 0.4, 0.5],
21+
},
22+
],
23+
});
24+
});
25+
26+
return new Promise(resolve => {
27+
const server = app.listen(0, () => {
28+
resolve(server);
29+
});
30+
});
31+
}
32+
33+
async function run() {
34+
const server = await startMockGoogleGenAIServer();
35+
36+
await Sentry.startSpan({ op: 'function', name: 'main' }, async () => {
37+
const client = new GoogleGenAI({
38+
apiKey: 'mock-api-key',
39+
httpOptions: { baseUrl: `http://localhost:${server.address().port}` },
40+
});
41+
42+
// Test 1: Basic embedContent with string contents
43+
await client.models.embedContent({
44+
model: 'text-embedding-004',
45+
contents: 'What is the capital of France?',
46+
});
47+
48+
// Test 2: Error handling
49+
try {
50+
await client.models.embedContent({
51+
model: 'error-model',
52+
contents: 'This will fail',
53+
});
54+
} catch {
55+
// Expected error
56+
}
57+
58+
// Test 3: embedContent with array contents
59+
await client.models.embedContent({
60+
model: 'text-embedding-004',
61+
contents: [
62+
{
63+
role: 'user',
64+
parts: [{ text: 'First input text' }],
65+
},
66+
{
67+
role: 'user',
68+
parts: [{ text: 'Second input text' }],
69+
},
70+
],
71+
});
72+
});
73+
74+
server.close();
75+
}
76+
77+
run();
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import { GoogleGenAI } from '@google/genai';
2+
import * as Sentry from '@sentry/node';
3+
import express from 'express';
4+
5+
function startMockGoogleGenAIServer() {
6+
const app = express();
7+
app.use(express.json());
8+
9+
app.post('/v1/models/:model\\:generateContent', (req, res) => {
10+
const model = req.params.model;
11+
12+
if (model === 'error-model') {
13+
res.status(404).set('x-request-id', 'mock-request-123').end('Model not found');
14+
return;
15+
}
16+
17+
res.send({
18+
candidates: [
19+
{
20+
content: {
21+
parts: [
22+
{
23+
text: 'Mock response from Google GenAI!',
24+
},
25+
],
26+
role: 'model',
27+
},
28+
finishReason: 'stop',
29+
index: 0,
30+
},
31+
],
32+
usageMetadata: {
33+
promptTokenCount: 8,
34+
candidatesTokenCount: 12,
35+
totalTokenCount: 20,
36+
},
37+
});
38+
});
39+
40+
return new Promise(resolve => {
41+
const server = app.listen(0, () => {
42+
resolve(server);
43+
});
44+
});
45+
}
46+
47+
async function run() {
48+
const server = await startMockGoogleGenAIServer();
49+
50+
await Sentry.startSpan({ op: 'function', name: 'main' }, async () => {
51+
const client = new GoogleGenAI({
52+
apiKey: 'mock-api-key',
53+
httpOptions: { baseUrl: `http://localhost:${server.address().port}` },
54+
});
55+
56+
// Test 1: chats.create and sendMessage flow
57+
// `chats.create()` builds a local object and emits no span; only `sendMessage` does, and it
58+
// reports the config given here.
59+
const chat = client.chats.create({
60+
model: 'gemini-1.5-pro',
61+
config: {
62+
temperature: 0.8,
63+
topP: 0.9,
64+
maxOutputTokens: 150,
65+
systemInstruction: 'You are a friendly robot who likes to be funny.',
66+
},
67+
history: [
68+
{
69+
role: 'user',
70+
parts: [{ text: 'Hello, how are you?' }],
71+
},
72+
],
73+
});
74+
75+
await chat.sendMessage({
76+
message: 'Tell me a joke',
77+
});
78+
79+
// Test 2: models.generateContent
80+
await client.models.generateContent({
81+
model: 'gemini-1.5-flash',
82+
config: {
83+
temperature: 0.7,
84+
topP: 0.9,
85+
maxOutputTokens: 100,
86+
},
87+
contents: [
88+
{
89+
role: 'user',
90+
parts: [{ text: 'What is the capital of France?' }],
91+
},
92+
],
93+
});
94+
95+
// Test 3: Error handling
96+
try {
97+
await client.models.generateContent({
98+
model: 'error-model',
99+
contents: [
100+
{
101+
role: 'user',
102+
parts: [{ text: 'This will fail' }],
103+
},
104+
],
105+
});
106+
} catch {
107+
// Expected error
108+
}
109+
});
110+
111+
server.close();
112+
}
113+
114+
run();
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
import { afterAll, describe, expect } from 'vitest';
2+
import {
3+
GEN_AI_OPERATION_NAME,
4+
GEN_AI_PROVIDER_NAME,
5+
GEN_AI_REQUEST_MAX_TOKENS,
6+
GEN_AI_REQUEST_MODEL,
7+
GEN_AI_REQUEST_TEMPERATURE,
8+
GEN_AI_REQUEST_TOP_P,
9+
GEN_AI_USAGE_INPUT_TOKENS,
10+
GEN_AI_USAGE_OUTPUT_TOKENS,
11+
GEN_AI_USAGE_TOTAL_TOKENS,
12+
SENTRY_OP,
13+
SENTRY_ORIGIN,
14+
} from '@sentry/conventions/attributes';
15+
import { GEN_AI_CHAT, GEN_AI_EMBEDDINGS, GEN_AI_GENERATE_CONTENT } from '@sentry/conventions/op';
16+
import { supports } from '../../../utils';
17+
import { cleanupChildProcesses, createEsmAndCjsTests } from '../../../utils/runner';
18+
19+
const EXPECTED_ORIGIN = 'auto.ai.google_genai';
20+
21+
// `@google/genai` v3 needs Node 22 and calls the `v1` API instead of `v1beta`, so the scenarios here
22+
// differ from `../google-genai-v2` only in the mock server routes.
23+
describe.runIf(supports({ min: 22 }))('Google GenAI integration (v3)', () => {
24+
afterAll(() => {
25+
cleanupChildProcesses();
26+
});
27+
28+
createEsmAndCjsTests(
29+
__dirname,
30+
'scenario.mjs',
31+
'instrument.mjs',
32+
(createRunner, test) => {
33+
test('auto-instruments chat and generateContent on @google/genai v3', async () => {
34+
const runner = createRunner().ignore('event');
35+
const spansPromise = runner.collectStreamedSpansUntilSegment('main');
36+
37+
await runner.start().completed();
38+
39+
const spans = (await spansPromise).filter(span => span.attributes[SENTRY_ORIGIN]?.value === EXPECTED_ORIGIN);
40+
expect(spans).toHaveLength(3);
41+
expect(spans.map(span => span.name).sort()).toEqual([
42+
'chat gemini-1.5-pro',
43+
'generate_content error-model',
44+
'generate_content gemini-1.5-flash',
45+
]);
46+
47+
const chatSpan = spans.find(span => span.name === 'chat gemini-1.5-pro');
48+
expect(chatSpan!.status).toBe('ok');
49+
expect(chatSpan!.attributes[SENTRY_OP].value).toBe(GEN_AI_CHAT);
50+
expect(chatSpan!.attributes[GEN_AI_OPERATION_NAME].value).toBe('chat');
51+
expect(chatSpan!.attributes[SENTRY_ORIGIN].value).toBe(EXPECTED_ORIGIN);
52+
expect(chatSpan!.attributes[GEN_AI_PROVIDER_NAME].value).toBe('google_genai');
53+
expect(chatSpan!.attributes[GEN_AI_REQUEST_MODEL].value).toBe('gemini-1.5-pro');
54+
expect(chatSpan!.attributes[GEN_AI_USAGE_INPUT_TOKENS].value).toBe(8);
55+
expect(chatSpan!.attributes[GEN_AI_USAGE_OUTPUT_TOKENS].value).toBe(12);
56+
expect(chatSpan!.attributes[GEN_AI_USAGE_TOTAL_TOKENS].value).toBe(20);
57+
58+
const generateContentSpan = spans.find(span => span.name === 'generate_content gemini-1.5-flash');
59+
expect(generateContentSpan!.status).toBe('ok');
60+
expect(generateContentSpan!.attributes[SENTRY_OP].value).toBe(GEN_AI_GENERATE_CONTENT);
61+
expect(generateContentSpan!.attributes[GEN_AI_OPERATION_NAME].value).toBe('generate_content');
62+
expect(generateContentSpan!.attributes[SENTRY_ORIGIN].value).toBe(EXPECTED_ORIGIN);
63+
expect(generateContentSpan!.attributes[GEN_AI_PROVIDER_NAME].value).toBe('google_genai');
64+
expect(generateContentSpan!.attributes[GEN_AI_REQUEST_MODEL].value).toBe('gemini-1.5-flash');
65+
expect(generateContentSpan!.attributes[GEN_AI_REQUEST_TEMPERATURE].value).toBe(0.7);
66+
expect(generateContentSpan!.attributes[GEN_AI_REQUEST_TOP_P].value).toBe(0.9);
67+
expect(generateContentSpan!.attributes[GEN_AI_REQUEST_MAX_TOKENS].value).toBe(100);
68+
expect(generateContentSpan!.attributes[GEN_AI_USAGE_INPUT_TOKENS].value).toBe(8);
69+
expect(generateContentSpan!.attributes[GEN_AI_USAGE_OUTPUT_TOKENS].value).toBe(12);
70+
expect(generateContentSpan!.attributes[GEN_AI_USAGE_TOTAL_TOKENS].value).toBe(20);
71+
72+
const errorSpan = spans.find(span => span.name === 'generate_content error-model');
73+
expect(errorSpan!.status).toBe('error');
74+
expect(errorSpan!.attributes[SENTRY_OP].value).toBe(GEN_AI_GENERATE_CONTENT);
75+
expect(errorSpan!.attributes[GEN_AI_OPERATION_NAME].value).toBe('generate_content');
76+
expect(errorSpan!.attributes[GEN_AI_REQUEST_MODEL].value).toBe('error-model');
77+
});
78+
},
79+
{ additionalDependencies: { '@google/genai': '^3' } },
80+
);
81+
82+
createEsmAndCjsTests(
83+
__dirname,
84+
'scenario-embeddings.mjs',
85+
'instrument.mjs',
86+
(createRunner, test) => {
87+
test('auto-instruments embedContent on @google/genai v3', async () => {
88+
const runner = createRunner().ignore('event');
89+
const spansPromise = runner.collectStreamedSpansUntilSegment('main');
90+
91+
await runner.start().completed();
92+
93+
const spans = (await spansPromise).filter(span => span.attributes[SENTRY_ORIGIN]?.value === EXPECTED_ORIGIN);
94+
expect(spans).toHaveLength(3);
95+
expect(spans.map(span => span.name).sort()).toEqual([
96+
'embeddings error-model',
97+
'embeddings text-embedding-004',
98+
'embeddings text-embedding-004',
99+
]);
100+
101+
const successfulSpans = spans.filter(
102+
span => span.name === 'embeddings text-embedding-004' && span.status === 'ok',
103+
);
104+
expect(successfulSpans).toHaveLength(2);
105+
for (const span of successfulSpans) {
106+
expect(span.attributes[SENTRY_OP].value).toBe(GEN_AI_EMBEDDINGS);
107+
expect(span.attributes[GEN_AI_OPERATION_NAME].value).toBe('embeddings');
108+
expect(span.attributes[SENTRY_ORIGIN].value).toBe(EXPECTED_ORIGIN);
109+
expect(span.attributes[GEN_AI_PROVIDER_NAME].value).toBe('google_genai');
110+
expect(span.attributes[GEN_AI_REQUEST_MODEL].value).toBe('text-embedding-004');
111+
}
112+
113+
const errorSpan = spans.find(span => span.name === 'embeddings error-model');
114+
expect(errorSpan!.status).toBe('error');
115+
expect(errorSpan!.attributes[SENTRY_OP].value).toBe(GEN_AI_EMBEDDINGS);
116+
expect(errorSpan!.attributes[GEN_AI_OPERATION_NAME].value).toBe('embeddings');
117+
});
118+
},
119+
{ additionalDependencies: { '@google/genai': '^3' } },
120+
);
121+
});

‎packages/server-utils/src/orchestrion/config/google-genai.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ export const googleGenAiConfig = [
1414
...NODE_DIST_FILES.flatMap(filePath =>
1515
(['generateContent', 'generateContentStream'] as const).map(expressionName => ({
1616
channelName: 'generate-content',
17-
module: { name: '@google/genai', versionRange: '>=0.10.0 <3', filePath },
17+
module: { name: '@google/genai', versionRange: '>=0.10.0 <4', filePath },
1818
functionQuery: { expressionName, kind: 'Auto' as const },
1919
})),
2020
),
2121
// `embedContent` is a real class method in v1 but a constructor-assigned arrow in v2; the
2222
// `className`/`methodName` selector matches both shapes.
2323
...NODE_DIST_FILES.map(filePath => ({
2424
channelName: 'embed-content',
25-
module: { name: '@google/genai', versionRange: '>=0.10.0 <3', filePath },
25+
module: { name: '@google/genai', versionRange: '>=0.10.0 <4', filePath },
2626
functionQuery: { className: 'Models', methodName: 'embedContent', kind: 'Auto' as const },
2727
})),
2828
// `sendMessage`/`sendMessageStream` internally delegate to `Models.generateContent(Stream)`; the
2929
// subscriber suppresses that nested `generate-content` event so a chat call yields a single span.
3030
...NODE_DIST_FILES.flatMap(filePath =>
3131
(['sendMessage', 'sendMessageStream'] as const).map(methodName => ({
3232
channelName: 'chat',
33-
module: { name: '@google/genai', versionRange: '>=0.10.0 <3', filePath },
33+
module: { name: '@google/genai', versionRange: '>=0.10.0 <4', filePath },
3434
functionQuery: { className: 'Chat', methodName, kind: 'Auto' as const },
3535
})),
3636
),

0 commit comments

Comments
 (0)