refactor: make log/event structure clear (#10467)

This commit is contained in:
Christie Warwick (Wilson)
2025-10-09 16:02:58 -07:00
committed by GitHub
parent cce245738e
commit 83075b2800
10 changed files with 831 additions and 517 deletions
+10 -8
View File
@@ -39,6 +39,7 @@ import {
logUserPrompt, logUserPrompt,
AuthType, AuthType,
getOauthClient, getOauthClient,
UserPromptEvent,
} from '@google/gemini-cli-core'; } from '@google/gemini-cli-core';
import { import {
initializeApp, initializeApp,
@@ -436,14 +437,15 @@ export async function main() {
} }
const prompt_id = Math.random().toString(16).slice(2); const prompt_id = Math.random().toString(16).slice(2);
logUserPrompt(config, { logUserPrompt(
'event.name': 'user_prompt', config,
'event.timestamp': new Date().toISOString(), new UserPromptEvent(
prompt: input, input.length,
prompt_id, prompt_id,
auth_type: config.getContentGeneratorConfig()?.authType, config.getContentGeneratorConfig()?.authType,
prompt_length: input.length, input,
}); ),
);
const nonInteractiveConfig = await validateNonInteractiveAuth( const nonInteractiveConfig = await validateNonInteractiveAuth(
settings.merged.security?.auth?.selectedType, settings.merged.security?.auth?.selectedType,
@@ -26,6 +26,7 @@ import {
MCPServerConfig, MCPServerConfig,
DiscoveredMCPTool, DiscoveredMCPTool,
StreamEventType, StreamEventType,
ToolCallEvent,
DEFAULT_GEMINI_MODEL, DEFAULT_GEMINI_MODEL,
DEFAULT_GEMINI_MODEL_AUTO, DEFAULT_GEMINI_MODEL_AUTO,
DEFAULT_GEMINI_FLASH_MODEL, DEFAULT_GEMINI_FLASH_MODEL,
@@ -369,20 +370,21 @@ class Session {
const errorResponse = (error: Error) => { const errorResponse = (error: Error) => {
const durationMs = Date.now() - startTime; const durationMs = Date.now() - startTime;
logToolCall(this.config, { logToolCall(
'event.name': 'tool_call', this.config,
'event.timestamp': new Date().toISOString(), new ToolCallEvent(
prompt_id: promptId, undefined,
function_name: fc.name ?? '', fc.name ?? '',
function_args: args, args,
duration_ms: durationMs, durationMs,
success: false, false,
error: error.message, promptId,
tool_type:
typeof tool !== 'undefined' && tool instanceof DiscoveredMCPTool typeof tool !== 'undefined' && tool instanceof DiscoveredMCPTool
? 'mcp' ? 'mcp'
: 'native', : 'native',
}); error.message,
),
);
return [ return [
{ {
@@ -488,19 +490,20 @@ class Session {
}); });
const durationMs = Date.now() - startTime; const durationMs = Date.now() - startTime;
logToolCall(this.config, { logToolCall(
'event.name': 'tool_call', this.config,
'event.timestamp': new Date().toISOString(), new ToolCallEvent(
function_name: fc.name, undefined,
function_args: args, fc.name ?? '',
duration_ms: durationMs, args,
success: true, durationMs,
prompt_id: promptId, true,
tool_type: promptId,
typeof tool !== 'undefined' && tool instanceof DiscoveredMCPTool typeof tool !== 'undefined' && tool instanceof DiscoveredMCPTool
? 'mcp' ? 'mcp'
: 'native', : 'native',
}); ),
);
return convertToFunctionResponse(fc.name, callId, toolResult.llmContent); return convertToFunctionResponse(fc.name, callId, toolResult.llmContent);
} catch (e) { } catch (e) {
-42
View File
@@ -5,45 +5,3 @@
*/ */
export const SERVICE_NAME = 'gemini-cli'; export const SERVICE_NAME = 'gemini-cli';
export const EVENT_USER_PROMPT = 'gemini_cli.user_prompt';
export const EVENT_TOOL_CALL = 'gemini_cli.tool_call';
export const EVENT_API_REQUEST = 'gemini_cli.api_request';
export const EVENT_API_ERROR = 'gemini_cli.api_error';
export const EVENT_API_RESPONSE = 'gemini_cli.api_response';
export const EVENT_CLI_CONFIG = 'gemini_cli.config';
export const EVENT_EXTENSION_DISABLE = 'gemini_cli.extension_disable';
export const EVENT_EXTENSION_ENABLE = 'gemini_cli.extension_enable';
export const EVENT_EXTENSION_INSTALL = 'gemini_cli.extension_install';
export const EVENT_EXTENSION_UNINSTALL = 'gemini_cli.extension_uninstall';
export const EVENT_FLASH_FALLBACK = 'gemini_cli.flash_fallback';
export const EVENT_RIPGREP_FALLBACK = 'gemini_cli.ripgrep_fallback';
export const EVENT_NEXT_SPEAKER_CHECK = 'gemini_cli.next_speaker_check';
export const EVENT_SLASH_COMMAND = 'gemini_cli.slash_command';
export const EVENT_IDE_CONNECTION = 'gemini_cli.ide_connection';
export const EVENT_CONVERSATION_FINISHED = 'gemini_cli.conversation_finished';
export const EVENT_CHAT_COMPRESSION = 'gemini_cli.chat_compression';
export const EVENT_MALFORMED_JSON_RESPONSE =
'gemini_cli.malformed_json_response';
export const EVENT_INVALID_CHUNK = 'gemini_cli.chat.invalid_chunk';
export const EVENT_CONTENT_RETRY = 'gemini_cli.chat.content_retry';
export const EVENT_CONTENT_RETRY_FAILURE =
'gemini_cli.chat.content_retry_failure';
export const EVENT_FILE_OPERATION = 'gemini_cli.file_operation';
export const EVENT_TOOL_OUTPUT_TRUNCATED = 'gemini_cli.tool_output_truncated';
export const EVENT_MODEL_SLASH_COMMAND = 'gemini_cli.slash_command.model';
export const EVENT_SMART_EDIT_STRATEGY = 'gemini_cli.smart_edit.strategy';
export const EVENT_MODEL_ROUTING = 'gemini_cli.model_routing';
export const EVENT_SMART_EDIT_CORRECTION = 'gemini_cli.smart_edit.correction';
export const EVENT_WEB_FETCH_FALLBACK_ATTEMPT =
'gemini_cli.web_fetch_fallback_attempt';
// Agent Events
export const EVENT_AGENT_START = 'gemini_cli.agent.start';
export const EVENT_AGENT_FINISH = 'gemini_cli.agent.finish';
// Performance Events
export const EVENT_STARTUP_PERFORMANCE = 'gemini_cli.startup.performance';
export const EVENT_MEMORY_USAGE = 'gemini_cli.memory.usage';
export const EVENT_PERFORMANCE_BASELINE = 'gemini_cli.performance.baseline';
export const EVENT_PERFORMANCE_REGRESSION = 'gemini_cli.performance.regression';
+18 -20
View File
@@ -22,26 +22,6 @@ import { OutputFormat } from '../output/types.js';
import { logs } from '@opentelemetry/api-logs'; import { logs } from '@opentelemetry/api-logs';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions'; import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import type { Config } from '../config/config.js'; import type { Config } from '../config/config.js';
import {
EVENT_API_REQUEST,
EVENT_API_RESPONSE,
EVENT_CLI_CONFIG,
EVENT_TOOL_CALL,
EVENT_USER_PROMPT,
EVENT_FLASH_FALLBACK,
EVENT_MALFORMED_JSON_RESPONSE,
EVENT_FILE_OPERATION,
EVENT_RIPGREP_FALLBACK,
EVENT_MODEL_ROUTING,
EVENT_EXTENSION_ENABLE,
EVENT_EXTENSION_DISABLE,
EVENT_EXTENSION_INSTALL,
EVENT_EXTENSION_UNINSTALL,
EVENT_TOOL_OUTPUT_TRUNCATED,
EVENT_AGENT_START,
EVENT_AGENT_FINISH,
EVENT_WEB_FETCH_FALLBACK_ATTEMPT,
} from './constants.js';
import { import {
logApiRequest, logApiRequest,
logApiResponse, logApiResponse,
@@ -65,6 +45,24 @@ import {
} from './loggers.js'; } from './loggers.js';
import { ToolCallDecision } from './tool-call-decision.js'; import { ToolCallDecision } from './tool-call-decision.js';
import { import {
EVENT_API_REQUEST,
EVENT_API_RESPONSE,
EVENT_CLI_CONFIG,
EVENT_TOOL_CALL,
EVENT_USER_PROMPT,
EVENT_FLASH_FALLBACK,
EVENT_MALFORMED_JSON_RESPONSE,
EVENT_FILE_OPERATION,
EVENT_RIPGREP_FALLBACK,
EVENT_MODEL_ROUTING,
EVENT_EXTENSION_ENABLE,
EVENT_EXTENSION_DISABLE,
EVENT_EXTENSION_INSTALL,
EVENT_EXTENSION_UNINSTALL,
EVENT_TOOL_OUTPUT_TRUNCATED,
EVENT_AGENT_START,
EVENT_AGENT_FINISH,
EVENT_WEB_FETCH_FALLBACK_ATTEMPT,
ApiRequestEvent, ApiRequestEvent,
ApiResponseEvent, ApiResponseEvent,
StartSessionEvent, StartSessionEvent,
+76 -382
View File
@@ -4,43 +4,15 @@
* SPDX-License-Identifier: Apache-2.0 * SPDX-License-Identifier: Apache-2.0
*/ */
import type { LogRecord, LogAttributes } from '@opentelemetry/api-logs'; import type { LogRecord } from '@opentelemetry/api-logs';
import { logs } from '@opentelemetry/api-logs'; import { logs } from '@opentelemetry/api-logs';
import { SemanticAttributes } from '@opentelemetry/semantic-conventions';
import type { Config } from '../config/config.js'; import type { Config } from '../config/config.js';
import { SERVICE_NAME } from './constants.js';
import { import {
EVENT_API_ERROR, EVENT_API_ERROR,
EVENT_API_REQUEST,
EVENT_API_RESPONSE, EVENT_API_RESPONSE,
EVENT_CLI_CONFIG,
EVENT_EXTENSION_UNINSTALL,
EVENT_EXTENSION_ENABLE,
EVENT_IDE_CONNECTION,
EVENT_TOOL_CALL, EVENT_TOOL_CALL,
EVENT_USER_PROMPT, } from './types.js';
EVENT_FLASH_FALLBACK,
EVENT_NEXT_SPEAKER_CHECK,
SERVICE_NAME,
EVENT_SLASH_COMMAND,
EVENT_CONVERSATION_FINISHED,
EVENT_CHAT_COMPRESSION,
EVENT_MALFORMED_JSON_RESPONSE,
EVENT_INVALID_CHUNK,
EVENT_CONTENT_RETRY,
EVENT_CONTENT_RETRY_FAILURE,
EVENT_FILE_OPERATION,
EVENT_TOOL_OUTPUT_TRUNCATED,
EVENT_RIPGREP_FALLBACK,
EVENT_MODEL_ROUTING,
EVENT_EXTENSION_INSTALL,
EVENT_MODEL_SLASH_COMMAND,
EVENT_EXTENSION_DISABLE,
EVENT_SMART_EDIT_STRATEGY,
EVENT_SMART_EDIT_CORRECTION,
EVENT_AGENT_START,
EVENT_AGENT_FINISH,
EVENT_WEB_FETCH_FALLBACK_ATTEMPT,
} from './constants.js';
import type { import type {
ApiErrorEvent, ApiErrorEvent,
ApiRequestEvent, ApiRequestEvent,
@@ -95,20 +67,6 @@ import { isTelemetrySdkInitialized } from './sdk.js';
import type { UiEvent } from './uiTelemetry.js'; import type { UiEvent } from './uiTelemetry.js';
import { uiTelemetryService } from './uiTelemetry.js'; import { uiTelemetryService } from './uiTelemetry.js';
import { ClearcutLogger } from './clearcut-logger/clearcut-logger.js'; import { ClearcutLogger } from './clearcut-logger/clearcut-logger.js';
import { safeJsonStringify } from '../utils/safeJsonStringify.js';
import { UserAccountManager } from '../utils/userAccountManager.js';
const shouldLogUserPrompts = (config: Config): boolean =>
config.getTelemetryLogPromptsEnabled();
function getCommonAttributes(config: Config): LogAttributes {
const userAccountManager = new UserAccountManager();
const email = userAccountManager.getCachedGoogleAccount();
return {
'session.id': config.getSessionId(),
...(email && { 'user.email': email }),
};
}
export function logCliConfiguration( export function logCliConfiguration(
config: Config, config: Config,
@@ -117,31 +75,10 @@ export function logCliConfiguration(
ClearcutLogger.getInstance(config)?.logStartSessionEvent(event); ClearcutLogger.getInstance(config)?.logStartSessionEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
'event.name': EVENT_CLI_CONFIG,
'event.timestamp': new Date().toISOString(),
model: event.model,
embedding_model: event.embedding_model,
sandbox_enabled: event.sandbox_enabled,
core_tools_enabled: event.core_tools_enabled,
approval_mode: event.approval_mode,
api_key_enabled: event.api_key_enabled,
vertex_ai_enabled: event.vertex_ai_enabled,
log_user_prompts_enabled: event.telemetry_log_user_prompts_enabled,
file_filtering_respect_git_ignore: event.file_filtering_respect_git_ignore,
debug_mode: event.debug_enabled,
mcp_servers: event.mcp_servers,
mcp_servers_count: event.mcp_servers_count,
mcp_tools: event.mcp_tools,
mcp_tools_count: event.mcp_tools_count,
output_format: event.output_format,
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: 'CLI configuration loaded.', body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
} }
@@ -150,26 +87,10 @@ export function logUserPrompt(config: Config, event: UserPromptEvent): void {
ClearcutLogger.getInstance(config)?.logNewPromptEvent(event); ClearcutLogger.getInstance(config)?.logNewPromptEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
'event.name': EVENT_USER_PROMPT,
'event.timestamp': new Date().toISOString(),
prompt_length: event.prompt_length,
prompt_id: event.prompt_id,
};
if (event.auth_type) {
attributes['auth_type'] = event.auth_type;
}
if (shouldLogUserPrompts(config)) {
attributes['prompt'] = event.prompt;
}
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `User prompt. Length: ${event.prompt_length}.`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
} }
@@ -184,24 +105,10 @@ export function logToolCall(config: Config, event: ToolCallEvent): void {
ClearcutLogger.getInstance(config)?.logToolCallEvent(event); ClearcutLogger.getInstance(config)?.logToolCallEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_TOOL_CALL,
'event.timestamp': new Date().toISOString(),
function_args: safeJsonStringify(event.function_args, 2),
};
if (event.error) {
attributes['error.message'] = event.error;
if (event.error_type) {
attributes['error.type'] = event.error_type;
}
}
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Tool call: ${event.function_name}${event.decision ? `. Decision: ${event.decision}` : ''}. Success: ${event.success}. Duration: ${event.duration_ms}ms.`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
recordToolCallMetrics(config, event.duration_ms, { recordToolCallMetrics(config, event.duration_ms, {
@@ -227,17 +134,10 @@ export function logToolOutputTruncated(
ClearcutLogger.getInstance(config)?.logToolOutputTruncatedEvent(event); ClearcutLogger.getInstance(config)?.logToolOutputTruncatedEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_TOOL_OUTPUT_TRUNCATED,
'event.timestamp': new Date().toISOString(),
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Tool output truncated for ${event.tool_name}.`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
} }
@@ -249,31 +149,10 @@ export function logFileOperation(
ClearcutLogger.getInstance(config)?.logFileOperationEvent(event); ClearcutLogger.getInstance(config)?.logFileOperationEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
'event.name': EVENT_FILE_OPERATION,
'event.timestamp': new Date().toISOString(),
tool_name: event.tool_name,
operation: event.operation,
};
if (event.lines) {
attributes['lines'] = event.lines;
}
if (event.mimetype) {
attributes['mimetype'] = event.mimetype;
}
if (event.extension) {
attributes['extension'] = event.extension;
}
if (event.programming_language) {
attributes['programming_language'] = event.programming_language;
}
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `File operation: ${event.operation}. Lines: ${event.lines}.`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
@@ -290,17 +169,10 @@ export function logApiRequest(config: Config, event: ApiRequestEvent): void {
ClearcutLogger.getInstance(config)?.logApiRequestEvent(event); ClearcutLogger.getInstance(config)?.logApiRequestEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_API_REQUEST,
'event.timestamp': new Date().toISOString(),
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `API request to ${event.model}.`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
} }
@@ -312,17 +184,10 @@ export function logFlashFallback(
ClearcutLogger.getInstance(config)?.logFlashFallbackEvent(); ClearcutLogger.getInstance(config)?.logFlashFallbackEvent();
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_FLASH_FALLBACK,
'event.timestamp': new Date().toISOString(),
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Switching to flash as Fallback.`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
} }
@@ -334,17 +199,10 @@ export function logRipgrepFallback(
ClearcutLogger.getInstance(config)?.logRipgrepFallbackEvent(); ClearcutLogger.getInstance(config)?.logRipgrepFallbackEvent();
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_RIPGREP_FALLBACK,
'event.timestamp': new Date().toISOString(),
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Switching to grep as fallback.`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
} }
@@ -359,27 +217,10 @@ export function logApiError(config: Config, event: ApiErrorEvent): void {
ClearcutLogger.getInstance(config)?.logApiErrorEvent(event); ClearcutLogger.getInstance(config)?.logApiErrorEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_API_ERROR,
'event.timestamp': new Date().toISOString(),
['error.message']: event.error,
model_name: event.model,
duration: event.duration_ms,
};
if (event.error_type) {
attributes['error.type'] = event.error_type;
}
if (typeof event.status_code === 'number') {
attributes[SemanticAttributes.HTTP_STATUS_CODE] = event.status_code;
}
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `API error for ${event.model}. Error: ${event.error}. Duration: ${event.duration_ms}ms.`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
recordApiErrorMetrics(config, event.duration_ms, { recordApiErrorMetrics(config, event.duration_ms, {
@@ -409,25 +250,11 @@ export function logApiResponse(config: Config, event: ApiResponseEvent): void {
uiTelemetryService.addEvent(uiEvent); uiTelemetryService.addEvent(uiEvent);
ClearcutLogger.getInstance(config)?.logApiResponseEvent(event); ClearcutLogger.getInstance(config)?.logApiResponseEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_API_RESPONSE,
'event.timestamp': new Date().toISOString(),
};
if (event.response_text) {
attributes['response_text'] = event.response_text;
}
if (event.status_code) {
if (typeof event.status_code === 'number') {
attributes[SemanticAttributes.HTTP_STATUS_CODE] = event.status_code;
}
}
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `API response from ${event.model}. Status: ${event.status_code || 'N/A'}. Duration: ${event.duration_ms}ms.`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
@@ -463,24 +290,27 @@ export function logLoopDetected(
ClearcutLogger.getInstance(config)?.logLoopDetectedEvent(event); ClearcutLogger.getInstance(config)?.logLoopDetectedEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Loop detected. Type: ${event.loop_type}.`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
} }
export function logLoopDetectionDisabled( export function logLoopDetectionDisabled(
config: Config, config: Config,
_event: LoopDetectionDisabledEvent, event: LoopDetectionDisabledEvent,
): void { ): void {
ClearcutLogger.getInstance(config)?.logLoopDetectionDisabledEvent(); ClearcutLogger.getInstance(config)?.logLoopDetectionDisabledEvent();
if (!isTelemetrySdkInitialized()) return;
const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = {
body: event.toLogBody(),
attributes: event.toOpenTelemetryAttributes(config),
};
logger.emit(logRecord);
} }
export function logNextSpeakerCheck( export function logNextSpeakerCheck(
@@ -490,16 +320,10 @@ export function logNextSpeakerCheck(
ClearcutLogger.getInstance(config)?.logNextSpeakerCheck(event); ClearcutLogger.getInstance(config)?.logNextSpeakerCheck(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_NEXT_SPEAKER_CHECK,
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Next speaker check.`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
} }
@@ -511,16 +335,10 @@ export function logSlashCommand(
ClearcutLogger.getInstance(config)?.logSlashCommandEvent(event); ClearcutLogger.getInstance(config)?.logSlashCommandEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_SLASH_COMMAND,
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Slash command: ${event.command}.`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
} }
@@ -532,16 +350,10 @@ export function logIdeConnection(
ClearcutLogger.getInstance(config)?.logIdeConnectionEvent(event); ClearcutLogger.getInstance(config)?.logIdeConnectionEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_IDE_CONNECTION,
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Ide connection. Type: ${event.connection_type}.`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
} }
@@ -553,16 +365,10 @@ export function logConversationFinishedEvent(
ClearcutLogger.getInstance(config)?.logConversationFinishedEvent(event); ClearcutLogger.getInstance(config)?.logConversationFinishedEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_CONVERSATION_FINISHED,
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Conversation finished.`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
} }
@@ -573,16 +379,10 @@ export function logChatCompression(
): void { ): void {
ClearcutLogger.getInstance(config)?.logChatCompressionEvent(event); ClearcutLogger.getInstance(config)?.logChatCompressionEvent(event);
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_CHAT_COMPRESSION,
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Chat compression (Saved ${event.tokens_before - event.tokens_after} tokens)`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
@@ -598,14 +398,10 @@ export function logKittySequenceOverflow(
): void { ): void {
ClearcutLogger.getInstance(config)?.logKittySequenceOverflowEvent(event); ClearcutLogger.getInstance(config)?.logKittySequenceOverflowEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Kitty sequence buffer overflow: ${event.sequence_length} bytes`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
} }
@@ -617,16 +413,10 @@ export function logMalformedJsonResponse(
ClearcutLogger.getInstance(config)?.logMalformedJsonResponseEvent(event); ClearcutLogger.getInstance(config)?.logMalformedJsonResponseEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_MALFORMED_JSON_RESPONSE,
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Malformed JSON response from ${event.model}.`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
} }
@@ -638,20 +428,10 @@ export function logInvalidChunk(
ClearcutLogger.getInstance(config)?.logInvalidChunkEvent(event); ClearcutLogger.getInstance(config)?.logInvalidChunkEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
'event.name': EVENT_INVALID_CHUNK,
'event.timestamp': event['event.timestamp'],
};
if (event.error_message) {
attributes['error.message'] = event.error_message;
}
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Invalid chunk received from stream.`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
recordInvalidChunk(config); recordInvalidChunk(config);
@@ -664,16 +444,10 @@ export function logContentRetry(
ClearcutLogger.getInstance(config)?.logContentRetryEvent(event); ClearcutLogger.getInstance(config)?.logContentRetryEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_CONTENT_RETRY,
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Content retry attempt ${event.attempt_number} due to ${event.error_type}.`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
recordContentRetry(config); recordContentRetry(config);
@@ -686,16 +460,10 @@ export function logContentRetryFailure(
ClearcutLogger.getInstance(config)?.logContentRetryFailureEvent(event); ClearcutLogger.getInstance(config)?.logContentRetryFailureEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_CONTENT_RETRY_FAILURE,
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `All content retries failed after ${event.total_attempts} attempts.`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
recordContentRetryFailure(config); recordContentRetryFailure(config);
@@ -708,16 +476,10 @@ export function logModelRouting(
ClearcutLogger.getInstance(config)?.logModelRoutingEvent(event); ClearcutLogger.getInstance(config)?.logModelRoutingEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_MODEL_ROUTING,
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Model routing decision. Model: ${event.decision_model}, Source: ${event.decision_source}`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
recordModelRoutingMetrics(config, event); recordModelRoutingMetrics(config, event);
@@ -730,16 +492,10 @@ export function logModelSlashCommand(
ClearcutLogger.getInstance(config)?.logModelSlashCommandEvent(event); ClearcutLogger.getInstance(config)?.logModelSlashCommandEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_MODEL_SLASH_COMMAND,
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Model slash command. Model: ${event.model_name}`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
recordModelSlashCommand(config, event); recordModelSlashCommand(config, event);
@@ -752,21 +508,10 @@ export function logExtensionInstallEvent(
ClearcutLogger.getInstance(config)?.logExtensionInstallEvent(event); ClearcutLogger.getInstance(config)?.logExtensionInstallEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_EXTENSION_INSTALL,
'event.timestamp': new Date().toISOString(),
extension_name: event.extension_name,
extension_version: event.extension_version,
extension_source: event.extension_source,
status: event.status,
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Installed extension ${event.extension_name}`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
} }
@@ -778,17 +523,10 @@ export function logExtensionUninstall(
ClearcutLogger.getInstance(config)?.logExtensionUninstallEvent(event); ClearcutLogger.getInstance(config)?.logExtensionUninstallEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_EXTENSION_UNINSTALL,
'event.timestamp': new Date().toISOString(),
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Uninstalled extension ${event.extension_name}`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
} }
@@ -800,17 +538,10 @@ export function logExtensionEnable(
ClearcutLogger.getInstance(config)?.logExtensionEnableEvent(event); ClearcutLogger.getInstance(config)?.logExtensionEnableEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_EXTENSION_ENABLE,
'event.timestamp': new Date().toISOString(),
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Enabled extension ${event.extension_name}`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
} }
@@ -822,17 +553,10 @@ export function logExtensionDisable(
ClearcutLogger.getInstance(config)?.logExtensionDisableEvent(event); ClearcutLogger.getInstance(config)?.logExtensionDisableEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_EXTENSION_DISABLE,
'event.timestamp': new Date().toISOString(),
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Disabled extension ${event.extension_name}`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
} }
@@ -844,16 +568,10 @@ export function logSmartEditStrategy(
ClearcutLogger.getInstance(config)?.logSmartEditStrategyEvent(event); ClearcutLogger.getInstance(config)?.logSmartEditStrategyEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_SMART_EDIT_STRATEGY,
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Smart Edit Tool Strategy: ${event.strategy}`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
} }
@@ -865,16 +583,10 @@ export function logSmartEditCorrectionEvent(
ClearcutLogger.getInstance(config)?.logSmartEditCorrectionEvent(event); ClearcutLogger.getInstance(config)?.logSmartEditCorrectionEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_SMART_EDIT_CORRECTION,
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Smart Edit Correction`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
} }
@@ -883,16 +595,10 @@ export function logAgentStart(config: Config, event: AgentStartEvent): void {
ClearcutLogger.getInstance(config)?.logAgentStartEvent(event); ClearcutLogger.getInstance(config)?.logAgentStartEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_AGENT_START,
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Agent ${event.agent_name} started. ID: ${event.agent_id}`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
} }
@@ -901,16 +607,10 @@ export function logAgentFinish(config: Config, event: AgentFinishEvent): void {
ClearcutLogger.getInstance(config)?.logAgentFinishEvent(event); ClearcutLogger.getInstance(config)?.logAgentFinishEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_AGENT_FINISH,
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Agent ${event.agent_name} finished. Reason: ${event.terminate_reason}. Duration: ${event.duration_ms}ms. Turns: ${event.turn_count}.`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
@@ -924,16 +624,10 @@ export function logWebFetchFallbackAttempt(
ClearcutLogger.getInstance(config)?.logWebFetchFallbackAttemptEvent(event); ClearcutLogger.getInstance(config)?.logWebFetchFallbackAttemptEvent(event);
if (!isTelemetrySdkInitialized()) return; if (!isTelemetrySdkInitialized()) return;
const attributes: LogAttributes = {
...getCommonAttributes(config),
...event,
'event.name': EVENT_WEB_FETCH_FALLBACK_ATTEMPT,
};
const logger = logs.getLogger(SERVICE_NAME); const logger = logs.getLogger(SERVICE_NAME);
const logRecord: LogRecord = { const logRecord: LogRecord = {
body: `Web fetch fallback attempt. Reason: ${event.reason}`, body: event.toLogBody(),
attributes, attributes: event.toOpenTelemetryAttributes(config),
}; };
logger.emit(logRecord); logger.emit(logRecord);
} }
+2 -1
View File
@@ -6,7 +6,8 @@
import type { Attributes, Meter, Counter, Histogram } from '@opentelemetry/api'; import type { Attributes, Meter, Counter, Histogram } from '@opentelemetry/api';
import { diag, metrics, ValueType } from '@opentelemetry/api'; import { diag, metrics, ValueType } from '@opentelemetry/api';
import { SERVICE_NAME, EVENT_CHAT_COMPRESSION } from './constants.js'; import { SERVICE_NAME } from './constants.js';
import { EVENT_CHAT_COMPRESSION } from './types.js';
import type { Config } from '../config/config.js'; import type { Config } from '../config/config.js';
import type { import type {
ModelRoutingEvent, ModelRoutingEvent,
@@ -0,0 +1,18 @@
/**
* @license
* Copyright 2025 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import type { LogAttributes } from '@opentelemetry/api-logs';
import type { Config } from '../config/config.js';
import { UserAccountManager } from '../utils/userAccountManager.js';
export function getCommonAttributes(config: Config): LogAttributes {
const userAccountManager = new UserAccountManager();
const email = userAccountManager.getCachedGoogleAccount();
return {
'session.id': config.getSessionId(),
...(email && { 'user.email': email }),
};
}
File diff suppressed because it is too large Load Diff
@@ -13,7 +13,7 @@ import {
EVENT_API_ERROR, EVENT_API_ERROR,
EVENT_API_RESPONSE, EVENT_API_RESPONSE,
EVENT_TOOL_CALL, EVENT_TOOL_CALL,
} from './constants.js'; } from './types.js';
import type { import type {
CompletedToolCall, CompletedToolCall,
ErroredToolCall, ErroredToolCall,
+1 -1
View File
@@ -9,7 +9,7 @@ import {
EVENT_API_ERROR, EVENT_API_ERROR,
EVENT_API_RESPONSE, EVENT_API_RESPONSE,
EVENT_TOOL_CALL, EVENT_TOOL_CALL,
} from './constants.js'; } from './types.js';
import { ToolCallDecision } from './tool-call-decision.js'; import { ToolCallDecision } from './tool-call-decision.js';
import type { import type {