Skip to content

Commit 6a43b31

Browse files
kirananiShnatu
andauthored
update(telemetry): OTel API response event with finish reasons (#13849)
Co-authored-by: Shnatu <[email protected]>
1 parent 450734e commit 6a43b31

File tree

3 files changed

+7
-3
lines changed

3 files changed

+7
-3
lines changed

docs/cli/telemetry.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,7 @@ Captures Gemini API requests, responses, and errors.
361361
- `response_text` (string, optional)
362362
- `prompt_id` (string)
363363
- `auth_type` (string)
364+
- `finish_reasons` (array of strings)
364365

365366
- `gemini_cli.api_error`: API request failed.
366367
- **Attributes**:

packages/core/src/telemetry/loggers.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,7 @@ describe('loggers', () => {
409409
attributes: expect.objectContaining({
410410
'event.name': EVENT_API_RESPONSE,
411411
prompt_id: 'prompt-id-1',
412+
finish_reasons: ['stop'],
412413
}),
413414
});
414415

packages/core/src/telemetry/types.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import type { AgentTerminateMode } from '../agents/types.js';
3131
import { getCommonAttributes } from './telemetryAttributes.js';
3232
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
3333
import { safeJsonStringify } from '../utils/safeJsonStringify.js';
34+
import type { OTelFinishReason } from './semantic.js';
3435
import {
3536
toInputMessages,
3637
toOutputMessages,
@@ -545,6 +546,7 @@ export class ApiResponseEvent implements BaseTelemetryEvent {
545546
prompt: GenAIPromptDetails;
546547
response: GenAIResponseDetails;
547548
usage: GenAIUsageDetails;
549+
finish_reasons: OTelFinishReason[];
548550

549551
constructor(
550552
model: string,
@@ -573,6 +575,7 @@ export class ApiResponseEvent implements BaseTelemetryEvent {
573575
tool_token_count: usage_data?.toolUsePromptTokenCount ?? 0,
574576
total_token_count: usage_data?.totalTokenCount ?? 0,
575577
};
578+
this.finish_reasons = toFinishReasons(this.response.candidates);
576579
}
577580

578581
toLogRecord(config: Config): LogRecord {
@@ -591,6 +594,7 @@ export class ApiResponseEvent implements BaseTelemetryEvent {
591594
prompt_id: this.prompt.prompt_id,
592595
auth_type: this.auth_type,
593596
status_code: this.status_code,
597+
finish_reasons: this.finish_reasons,
594598
};
595599
if (this.response_text) {
596600
attributes['response_text'] = this.response_text;
@@ -613,9 +617,7 @@ export class ApiResponseEvent implements BaseTelemetryEvent {
613617
'event.name': EVENT_GEN_AI_OPERATION_DETAILS,
614618
'event.timestamp': this['event.timestamp'],
615619
'gen_ai.response.id': this.response.response_id,
616-
'gen_ai.response.finish_reasons': toFinishReasons(
617-
this.response.candidates,
618-
),
620+
'gen_ai.response.finish_reasons': this.finish_reasons,
619621
'gen_ai.output.messages': JSON.stringify(
620622
toOutputMessages(this.response.candidates),
621623
),

0 commit comments

Comments
 (0)