Skip to content

Commit 699c734

Browse files
committed
simplify
1 parent a1a53a5 commit 699c734

6 files changed

Lines changed: 5 additions & 110 deletions

File tree

dev-packages/browser-integration-tests/suites/tracing/ai-providers/google-genai/mocks.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,8 @@ export class MockGoogleGenAI {
5353
embeddings: [
5454
{
5555
values: [0.1, 0.2, 0.3, 0.4, 0.5],
56-
statistics: {
57-
tokenCount: 8,
58-
truncated: false,
59-
},
6056
},
6157
],
62-
metadata: {
63-
billableCharacterCount: 30,
64-
},
6558
};
6659
},
6760
generateContentStream: async () => {

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,6 @@ function startMockGoogleGenAIServer() {
2020
values: [0.1, 0.2, 0.3, 0.4, 0.5],
2121
},
2222
],
23-
usageMetadata: {
24-
promptTokenCount: 8,
25-
totalTokenCount: 8,
26-
},
2723
});
2824
});
2925

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -614,8 +614,6 @@ describe('Google GenAI integration', () => {
614614
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ai.google_genai',
615615
[GEN_AI_SYSTEM_ATTRIBUTE]: 'google_genai',
616616
[GEN_AI_REQUEST_MODEL_ATTRIBUTE]: 'text-embedding-004',
617-
[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE]: 8,
618-
[GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE]: 8,
619617
},
620618
description: 'embeddings text-embedding-004',
621619
op: 'gen_ai.embeddings',
@@ -644,8 +642,6 @@ describe('Google GenAI integration', () => {
644642
[SEMANTIC_ATTRIBUTE_SENTRY_ORIGIN]: 'auto.ai.google_genai',
645643
[GEN_AI_SYSTEM_ATTRIBUTE]: 'google_genai',
646644
[GEN_AI_REQUEST_MODEL_ATTRIBUTE]: 'text-embedding-004',
647-
[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE]: 8,
648-
[GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE]: 8,
649645
},
650646
description: 'embeddings text-embedding-004',
651647
op: 'gen_ai.embeddings',
@@ -667,8 +663,6 @@ describe('Google GenAI integration', () => {
667663
[GEN_AI_SYSTEM_ATTRIBUTE]: 'google_genai',
668664
[GEN_AI_REQUEST_MODEL_ATTRIBUTE]: 'text-embedding-004',
669665
[GEN_AI_EMBEDDINGS_INPUT_ATTRIBUTE]: 'What is the capital of France?',
670-
[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE]: 8,
671-
[GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE]: 8,
672666
},
673667
description: 'embeddings text-embedding-004',
674668
op: 'gen_ai.embeddings',
@@ -699,8 +693,6 @@ describe('Google GenAI integration', () => {
699693
[GEN_AI_SYSTEM_ATTRIBUTE]: 'google_genai',
700694
[GEN_AI_REQUEST_MODEL_ATTRIBUTE]: 'text-embedding-004',
701695
[GEN_AI_EMBEDDINGS_INPUT_ATTRIBUTE]: expect.any(String),
702-
[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE]: 8,
703-
[GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE]: 8,
704696
},
705697
description: 'embeddings text-embedding-004',
706698
op: 'gen_ai.embeddings',
Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
1-
import {
2-
GEN_AI_EMBEDDINGS_INPUT_ATTRIBUTE,
3-
GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE,
4-
GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE,
5-
} from '../ai/gen-ai-attributes';
1+
import { GEN_AI_EMBEDDINGS_INPUT_ATTRIBUTE } from '../ai/gen-ai-attributes';
62
import type { Span } from '../../types-hoist/span';
7-
import type { GoogleGenAIEmbedContentResponse } from './types';
83

94
/**
105
* Add private request attributes for embeddings methods.
@@ -35,43 +30,3 @@ export function addEmbeddingsRequestAttributes(span: Span, params: Record<string
3530
typeof contents === 'string' ? contents : JSON.stringify(contents),
3631
);
3732
}
38-
39-
/**
40-
* Add response attributes from the Google GenAI embedContent response.
41-
* Token counts come from usageMetadata (Gemini API) or embeddings[].statistics.tokenCount (Vertex AI).
42-
* @see https://ai.google.dev/api/embeddings#EmbedContentResponse
43-
*/
44-
export function addEmbedContentResponseAttributes(span: Span, response: unknown): void {
45-
if (!response || typeof response !== 'object') return;
46-
47-
const embedResponse = response as GoogleGenAIEmbedContentResponse;
48-
49-
// Try usageMetadata first (same shape as GenerateContentResponse)
50-
if (embedResponse.usageMetadata && typeof embedResponse.usageMetadata === 'object') {
51-
const usage = embedResponse.usageMetadata;
52-
if (typeof usage.promptTokenCount === 'number') {
53-
span.setAttribute(GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE, usage.promptTokenCount);
54-
}
55-
if (typeof usage.totalTokenCount === 'number') {
56-
span.setAttribute(GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE, usage.totalTokenCount);
57-
}
58-
return;
59-
}
60-
61-
// Fallback: sum token counts from individual embedding statistics (Vertex AI)
62-
if (Array.isArray(embedResponse.embeddings)) {
63-
let totalTokenCount = 0;
64-
for (const embedding of embedResponse.embeddings) {
65-
if (embedding.statistics && typeof embedding.statistics.tokenCount === 'number') {
66-
totalTokenCount += embedding.statistics.tokenCount;
67-
}
68-
}
69-
70-
if (totalTokenCount > 0) {
71-
span.setAttributes({
72-
[GEN_AI_USAGE_INPUT_TOKENS_ATTRIBUTE]: totalTokenCount,
73-
[GEN_AI_USAGE_TOTAL_TOKENS_ATTRIBUTE]: totalTokenCount,
74-
});
75-
}
76-
}
77-
}

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import {
2929
import { truncateGenAiMessages } from '../ai/messageTruncation';
3030
import { buildMethodPath, extractSystemInstructions, getFinalOperationName, getSpanOperation } from '../ai/utils';
3131
import { CHAT_PATH, CHATS_CREATE_METHOD, GOOGLE_GENAI_SYSTEM_NAME } from './constants';
32-
import { addEmbedContentResponseAttributes, addEmbeddingsRequestAttributes } from './embeddings';
32+
import { addEmbeddingsRequestAttributes } from './embeddings';
3333
import { instrumentStream } from './streaming';
3434
import type {
3535
Candidate,
@@ -324,12 +324,9 @@ function instrumentMethod<T extends unknown[], R>(
324324
() => {},
325325
result => {
326326
// Only add response attributes for content-producing methods, not for chats.create
327-
if (!isSyncCreate) {
328-
if (isEmbeddings) {
329-
addEmbedContentResponseAttributes(span, result);
330-
} else {
331-
addResponseAttributes(span, result, options.recordOutputs);
332-
}
327+
// Note: embeddings responses don't include usage metadata or model version
328+
if (!isSyncCreate && !isEmbeddings) {
329+
addResponseAttributes(span, result, options.recordOutputs);
333330
}
334331
},
335332
);

packages/core/src/tracing/google-genai/types.ts

Lines changed: 0 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -181,43 +181,5 @@ export interface GoogleGenAIChat {
181181

182182
export type GoogleGenAIIstrumentedMethod = (typeof GOOGLE_GENAI_INSTRUMENTED_METHODS)[number];
183183

184-
/**
185-
* Google GenAI Content Embedding
186-
* @see https://googleapis.github.io/js-genai/release_docs/classes/types.ContentEmbedding.html
187-
*/
188-
type ContentEmbeddingType = {
189-
/** The embedding values. */
190-
values?: number[];
191-
/** Statistics about the embedding. */
192-
statistics?: {
193-
/** The number of tokens in the content. */
194-
tokenCount?: number;
195-
/** Whether the content was truncated. */
196-
truncated?: boolean;
197-
};
198-
};
199-
200-
/**
201-
* Google GenAI Embed Content Response
202-
* @see https://ai.google.dev/api/embeddings#EmbedContentResponse
203-
*/
204-
export type GoogleGenAIEmbedContentResponse = {
205-
[key: string]: unknown;
206-
/** The generated embeddings. */
207-
embeddings?: ContentEmbeddingType[];
208-
/** Metadata about the embeddings. */
209-
metadata?: {
210-
/** Billable character count. */
211-
billableCharacterCount?: number;
212-
};
213-
/** Usage metadata (same shape as GenerateContentResponse). */
214-
usageMetadata?: {
215-
/** Number of tokens in the request. */
216-
promptTokenCount?: number;
217-
/** Total token count. */
218-
totalTokenCount?: number;
219-
};
220-
};
221-
222184
// Export the response type for use in instrumentation
223185
export type GoogleGenAIResponse = GenerateContentResponse;

0 commit comments

Comments
 (0)