Skip to content

Commit 2a58c6a

Browse files
committed
some refacotor
1 parent f79ba78 commit 2a58c6a

File tree

5 files changed

+30
-46
lines changed

5 files changed

+30
-46
lines changed

dev-packages/node-integration-tests/suites/tracing/google-genai/scenario.mjs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ class MockGoogleGenAI {
2222
content: {
2323
parts: [
2424
{
25-
text: params.contents
26-
? 'The capital of France is Paris.'
27-
: 'Mock response from Google GenAI!',
25+
text: params.contents ? 'The capital of France is Paris.' : 'Mock response from Google GenAI!',
2826
},
2927
],
3028
role: 'model',
@@ -43,9 +41,10 @@ class MockGoogleGenAI {
4341
};
4442

4543
this.chats = {
46-
create: () => {
47-
// Return a chat instance with sendMessage method
44+
create: (options) => {
45+
// Return a chat instance with sendMessage method and model info
4846
return {
47+
model: options?.model || 'unknown', // Include model from create options
4948
sendMessage: async () => {
5049
// Simulate processing time
5150
await new Promise(resolve => setTimeout(resolve, 10));

packages/core/src/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,12 @@ export { ANTHROPIC_AI_INTEGRATION_NAME } from './utils/anthropic-ai/constants';
133133
export { instrumentGoogleGenAIClient } from './utils/google-genai';
134134
export { GOOGLE_GENAI_INTEGRATION_NAME } from './utils/google-genai/constants';
135135
export type { OpenAiClient, OpenAiOptions, InstrumentedMethod } from './utils/openai/types';
136-
export type { AnthropicAiClient, AnthropicAiOptions, AnthropicAiInstrumentedMethod } from './utils/anthropic-ai/types';
136+
export type {
137+
AnthropicAiClient,
138+
AnthropicAiOptions,
139+
AnthropicAiInstrumentedMethod,
140+
AnthropicAiResponse,
141+
} from './utils/anthropic-ai/types';
137142
export type {
138143
GoogleGenAIClient,
139144
GoogleGenAIChat,

packages/core/src/utils/ai/gen-ai-attributes.ts

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,3 @@ export const OPENAI_OPERATIONS = {
178178
* The response timestamp from Anthropic AI (ISO string)
179179
*/
180180
export const ANTHROPIC_AI_RESPONSE_TIMESTAMP_ATTRIBUTE = 'anthropic.response.timestamp';
181-
182-
// =============================================================================
183-
// GOOGLE GENAI OPERATIONS
184-
// =============================================================================
185-
186-
/**
187-
* Google GenAI API operations
188-
*/
189-
export const GOOGLE_GENAI_OPERATIONS = {
190-
GENERATE_CONTENT: 'generateContent',
191-
STREAM_GENERATE_CONTENT: 'streamGenerateContent',
192-
} as const;

packages/core/src/utils/google-genai/constants.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const GOOGLE_GENAI_INTEGRATION_NAME = 'Google_GenAI';
55
export const GOOGLE_GENAI_INSTRUMENTED_METHODS = ['models.generateContent', 'chats.create', 'sendMessage'] as const;
66

77
// Constants for internal use
8-
export const GOOGLE_GENAI_MODEL_PROPERTY = '_sentryGoogleGenAIModel';
98
export const GOOGLE_GENAI_SYSTEM_NAME = 'google_genai';
109
export const CHATS_CREATE_METHOD = 'chats.create';
1110
export const CHAT_PATH = 'chat';
11+

packages/core/src/utils/google-genai/index.ts

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,7 @@ import {
2020
GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE,
2121
} from '../ai/gen-ai-attributes';
2222
import { buildMethodPath, getFinalOperationName, getSpanOperation } from '../ai/utils';
23-
import { isThenable } from '../is';
24-
import {
25-
CHAT_PATH,
26-
CHATS_CREATE_METHOD,
27-
GOOGLE_GENAI_INTEGRATION_NAME,
28-
GOOGLE_GENAI_MODEL_PROPERTY,
29-
GOOGLE_GENAI_SYSTEM_NAME,
30-
} from './constants';
23+
import { CHAT_PATH, CHATS_CREATE_METHOD, GOOGLE_GENAI_INTEGRATION_NAME, GOOGLE_GENAI_SYSTEM_NAME } from './constants';
3124
import type {
3225
Candidate,
3326
ContentPart,
@@ -39,21 +32,26 @@ import type {
3932
import { shouldInstrument } from './utils';
4033

4134
/**
42-
* Extract model from parameters or context
43-
* For chat instances, the model is stored during chat creation and retrieved from context
35+
* Extract model from parameters or chat context object
36+
* For chat instances, the model is available on the chat object as 'model' (older versions) or 'modelVersion' (newer versions)
4437
*/
4538
export function extractModel(params: Record<string, unknown>, context?: unknown): string {
4639
if ('model' in params && typeof params.model === 'string') {
4740
return params.model;
4841
}
4942

50-
// For chat instances, try to get the model from the chat context
51-
// This is because the model is set during chat creation
52-
// and not passed as a parameter to the chat.sendMessage method
43+
// Try to get model from chat context object (chat instance has model property)
5344
if (context && typeof context === 'object') {
54-
const chatObj = context as Record<string, unknown>;
55-
if (chatObj[GOOGLE_GENAI_MODEL_PROPERTY] && typeof chatObj[GOOGLE_GENAI_MODEL_PROPERTY] === 'string') {
56-
return chatObj[GOOGLE_GENAI_MODEL_PROPERTY] as string;
45+
const contextObj = context as Record<string, unknown>;
46+
47+
// Check for 'model' property (older versions, and streaming)
48+
if ('model' in contextObj && typeof contextObj.model === 'string') {
49+
return contextObj.model;
50+
}
51+
52+
// Check for 'modelVersion' property (newer versions)
53+
if ('modelVersion' in contextObj && typeof contextObj.modelVersion === 'string') {
54+
return contextObj.modelVersion;
5755
}
5856
}
5957

@@ -217,7 +215,7 @@ function instrumentMethod<T extends unknown[], R>(
217215
context: unknown,
218216
options?: GoogleGenAIOptions,
219217
): (...args: T) => R | Promise<R> {
220-
const isSyncCreate = !isThenable(originalMethod) && methodPath === CHATS_CREATE_METHOD;
218+
const isSyncCreate = methodPath === CHATS_CREATE_METHOD
221219

222220
const run = (...args: T): R | Promise<R> => {
223221
const finalOptions = options || getRecordingOptionsFromIntegration();
@@ -238,13 +236,7 @@ function instrumentMethod<T extends unknown[], R>(
238236
if (finalOptions.recordInputs && args[0] && typeof args[0] === 'object') {
239237
addPrivateRequestAttributes(span, args[0] as Record<string, unknown>);
240238
}
241-
const result = (originalMethod as (...args: T) => R).apply(context, args) as R;
242-
243-
if (typeof model === 'string' && model !== 'unknown' && typeof result === 'object') {
244-
// We store the model in the result object so that it can be accessed later
245-
// This is because the model is not passed as a parameter to the chat.sendMessage method
246-
(result as Record<string, unknown>)[GOOGLE_GENAI_MODEL_PROPERTY] = model;
247-
}
239+
const result = (originalMethod as (...args: T) => R).apply(context, args);
248240

249241
// No response attributes for create (returns object of chat instance, not generated content)
250242
return result;
@@ -255,7 +247,7 @@ function instrumentMethod<T extends unknown[], R>(
255247
throw error;
256248
}
257249
},
258-
) as R;
250+
);
259251
}
260252

261253
// Async/content-producing path
@@ -273,15 +265,15 @@ function instrumentMethod<T extends unknown[], R>(
273265

274266
const result = await Promise.resolve((originalMethod as (...args: T) => Promise<R>).apply(context, args));
275267
addResponseAttributes(span, result as GoogleGenAIResponse, finalOptions.recordOutputs);
276-
return result as R;
268+
return result;
277269
} catch (error) {
278270
captureException(error, {
279271
mechanism: { handled: false, type: 'auto.ai.google_genai', data: { function: methodPath } },
280272
});
281273
throw error;
282274
}
283275
},
284-
) as Promise<R>;
276+
);
285277
};
286278

287279
return run;

0 commit comments

Comments
 (0)