Skip to content

Commit 48c07eb

Browse files
committed
this is much cleaner, get model from context
1 parent 0b4698c commit 48c07eb

File tree

3 files changed

+19
-41
lines changed

3 files changed

+19
-41
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ class MockGoogleGenAI {
4141
};
4242

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

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,3 @@ export const GOOGLE_GENAI_SYSTEM_NAME = 'google_genai';
99
export const CHATS_CREATE_METHOD = 'chats.create';
1010
export const CHAT_PATH = 'chat';
1111

12-
// Span attribute key for storing chat context
13-
export const GOOGLE_GENAI_SCOPE_MODEL_KEY = 'google_genai.chat.model';

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

Lines changed: 16 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { captureException } from '../../exports';
33
import { SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN } from '../../semanticAttributes';
44
import { startSpan } from '../../tracing/trace';
55
import type { Span, SpanAttributeValue } from '../../types-hoist/span';
6-
import { getActiveSpan, getRootSpan, spanToJSON } from '../../utils/spanUtils';
76
import {
87
GEN_AI_OPERATION_NAME_ATTRIBUTE,
98
GEN_AI_REQUEST_FREQUENCY_PENALTY_ATTRIBUTE,
@@ -22,13 +21,7 @@ import {
2221
} from '../ai/gen-ai-attributes';
2322
import { buildMethodPath, getFinalOperationName, getSpanOperation } from '../ai/utils';
2423
import { isThenable } from '../is';
25-
import {
26-
CHAT_PATH,
27-
CHATS_CREATE_METHOD,
28-
GOOGLE_GENAI_INTEGRATION_NAME,
29-
GOOGLE_GENAI_SCOPE_MODEL_KEY,
30-
GOOGLE_GENAI_SYSTEM_NAME,
31-
} from './constants';
24+
import { CHAT_PATH, CHATS_CREATE_METHOD, GOOGLE_GENAI_INTEGRATION_NAME, GOOGLE_GENAI_SYSTEM_NAME } from './constants';
3225
import type {
3326
Candidate,
3427
ContentPart,
@@ -40,35 +33,26 @@ import type {
4033
import { shouldInstrument } from './utils';
4134

4235
/**
43-
* Store model information on the root span for later retrieval by chat.sendMessage
44-
*/
45-
function storeModelOnRootSpan(model: string): void {
46-
const activeSpan = getActiveSpan();
47-
if (activeSpan) {
48-
const rootSpan = getRootSpan(activeSpan);
49-
rootSpan.setAttributes({
50-
[GOOGLE_GENAI_SCOPE_MODEL_KEY]: model,
51-
});
52-
}
53-
}
54-
55-
/**
56-
* Extract model from parameters or root span attributes
57-
* For chat instances, the model is stored on the root span during chat creation
36+
* Extract model from parameters or chat context object
37+
* For chat instances, the model is available on the chat object as 'model' (older versions) or 'modelVersion' (newer versions)
5838
*/
59-
export function extractModel(params: Record<string, unknown>, _context?: unknown): string {
39+
export function extractModel(params: Record<string, unknown>, context?: unknown): string {
6040
if ('model' in params && typeof params.model === 'string') {
6141
return params.model;
6242
}
6343

64-
// Try to get model from root span attributes
65-
const activeSpan = getActiveSpan();
66-
if (activeSpan) {
67-
const rootSpan = getRootSpan(activeSpan);
68-
const spanData = spanToJSON(rootSpan);
69-
const modelFromSpan = spanData.data?.[GOOGLE_GENAI_SCOPE_MODEL_KEY];
70-
if (typeof modelFromSpan === 'string') {
71-
return modelFromSpan;
44+
// Try to get model from chat context object (chat instance has model property)
45+
if (context && typeof context === 'object') {
46+
const contextObj = context as Record<string, unknown>;
47+
48+
// Check for 'model' property (older versions, and streaming)
49+
if ('model' in contextObj && typeof contextObj.model === 'string') {
50+
return contextObj.model;
51+
}
52+
53+
// Check for 'modelVersion' property (newer versions)
54+
if ('modelVersion' in contextObj && typeof contextObj.modelVersion === 'string') {
55+
return contextObj.modelVersion;
7256
}
7357
}
7458

@@ -255,11 +239,6 @@ function instrumentMethod<T extends unknown[], R>(
255239
}
256240
const result = (originalMethod as (...args: T) => R).apply(context, args);
257241

258-
if (typeof model === 'string' && model !== 'unknown') {
259-
// Store model information on root span for later retrieval by chat.sendMessage
260-
storeModelOnRootSpan(model);
261-
}
262-
263242
// No response attributes for create (returns object of chat instance, not generated content)
264243
return result;
265244
} catch (error) {

0 commit comments

Comments
 (0)