Adds a script - `scripts/telemetry_gcp.js` - to simplify setting up a local OpenTelemetry collector that forwards data to Google Cloud. This is a follow up to the script for local telemetry `scripts/local_telemetry.js` added in #1015. This script automates downloading necessary binaries, configuring the collector, and updating workspace settings. Also includes `scripts/telemetry_utils.js` with shared helper functions for telemetry scripts. Will refactor `scripts/local_t elemetry.js` in next steps to use this shared functionality. Updates `docs/core/telemetry.md` to include: - A new "Quick Start" section - Detailed instructions for the new GCP automated script - Reorganization of existing sections for clarity #750 --- ``` ✨ Starting Local Telemetry Exporter for Google Cloud ✨ ⚙️ Enabled telemetry in workspace settings. 🔧 Set telemetry OTLP endpoint to http://localhost:4317. ✅ Workspace settings updated. ✅ Using Google Cloud Project ID: foo-bar 🔑 Please ensure you are authenticated with Google Cloud: - Run `gcloud auth application-default login` OR ensure `GOOGLE_APPLICATION_CREDENTIALS` environment variable points to a valid service account key. - The account needs "Cloud Trace Agent", "Monitoring Metric Writer", and "Logs Writer" roles. ✅ otelcol-contrib already exists at /Users/jerop/github/gemini-cli/.gemini/otel/bin/otelcol-contrib 🧹 Cleaning up old processes and logs... ✅ Deleted old GCP collector log. 📄 Wrote OTEL collector config to /Users/jerop/github/gemini-cli/.gemini/otel/collector-gcp.yaml 🚀 Starting OTEL collector for GCP... Logs: /Users/jerop/github/gemini-cli/.gemini/otel/collector-gcp.log ⏳ Waiting for OTEL collector to start (PID: 65145)... ✅ OTEL collector started successfully on port 4317. ✨ Local OTEL collector for GCP is running. 📄 Collector logs are being written to: /Users/jerop/github/gemini-cli/.gemini/otel/collector-gcp.log 📊 View your telemetry data in Google Cloud Console: - Traces: https://console.cloud.google.com/traces/list?project=foo-bar - Metrics: https://console.cloud.google.com/monitoring/metrics-explorer?project=foo-bar - Logs: https://console.cloud.google.com/logs/query;query=logName%3D%22projects%2Ffoo-bar%2Flogs%2Fgemini_cli%22?project=foo-bar Press Ctrl+C to exit. ^C 👋 Shutting down... ⚙️ Disabled telemetry in workspace settings. 🔧 Cleared telemetry OTLP endpoint. ✅ Workspace settings updated. 🛑 Stopping otelcol-contrib (PID: 65145)... ✅ otelcol-contrib stopped. ```
9.1 KiB
Gemini CLI Observability Guide
Telemetry provides crucial data about the Gemini CLI's performance, health, and usage. By enabling it, you can monitor operations, debug issues, and optimize tool usage through traces, metrics, and structured logs.
This entire system is built on the OpenTelemetry (OTEL) standard, allowing you to send data to any compatible backend, from your local terminal to a cloud service.
Quick Start
Telemetry with Google Cloud
- Ensure Prerequisites:
Ensure that:
- You have set the
GOOGLE_CLOUD_PROJECTenvironment variable. - You have authenticated with Google Cloud and have the necessary IAM roles. For full details, see the Google Cloud prerequisites.
- You have set the
- Run the Script: Execute the following command from the project root:
./scripts/telemetry_gcp.js - View Data: The script will provide links to view your telemetry data (traces, metrics, logs) in the Google Cloud Console.
- Details: Refer to documentation for telemetry in Google Cloud.
Local Telemetry with Jaeger UI (for Traces)
- Run the Script: Execute the following command from the project root:
./scripts/local_telemetry.js - View Logs/Metrics: Check the
.gemini/otel/collector.logfile for raw logs and metrics. - View Traces: Open your browser and go to
http://localhost:16686to see traces in the Jaeger UI. - Details: Refer to documentation for telemetry in Local.
Enabling Telemetry
You can enable telemetry in multiple ways. Configuration is primarily managed via the .gemini/settings.json file and environment variables, but CLI flags can override these settings for a specific session.
A Note on Sandbox Mode: Telemetry is not compatible with sandbox mode at this time. Turn off sandbox mode before enabling telemetry. Tracked in #894.
Order of Precedence:
- CLI Flag (
--telemetry): These override all other settings for the current session. - Workspace Settings File (
.gemini/settings.json): If no CLI flag is used, thetelemetryvalue from this project-specific file is used. - User Settings File (
~/.gemini/settings.json): If not set by a flag or workspace settings, the value from this global user file is used. - Default: If telemetry is not configured by a flag or in any settings file, it is disabled.
Add these lines to enable telemetry by in workspace (.gemini/settings.json) or user (~/.gemini/settings.json) settings:
{
"telemetry": true,
"sandbox": false
}
Running an OTEL Collector
An OTEL Collector is a service that receives, processes, and exports telemetry data. The CLI sends data using the OTLP/gRPC protocol.
Learn more about OTEL exporter standard configuration in documentation.
Configuration
- Install otelcol-contrib or use docker
- Create a folder for the OTEL configurations:
mkdir .gemini/otel
Local
Use the scripts/local_telemetry.js script that automates the entire process of setting up a local telemetry pipeline, including configuring the necessary settings in your .gemini/settings.json file. The script installs otelcol-contrib (The OpenTelemetry Collector) and jaeger (The Jaeger UI for viewing traces). To use it:
-
Run the Script: Execute the script from the root of the repository:
./scripts/local_telemetry.jsThe script will:
- Download Jaeger and OTEL if needed.
- Start a local Jaeger instance.
- Start an OTEL collector configured to receive data from the Gemini CLI.
- Automatically enable telemetry in your workspace settings.
- On exit, disable telemetry.
-
View Traces: Open your web browser and navigate to http://localhost:16686 to access the Jaeger UI. Here you can inspect detailed traces of Gemini CLI operations.
-
Inspect Logs and Metrics: The script redirects the OTEL collector's output (which includes logs and metrics) to
.gemini/otel/collector.log. You can monitor this file to see the raw telemetry data:tail -f .gemini/otel/collector.log -
Stop the Services: Press
Ctrl+Cin the terminal where the script is running to stop the OTEL Collector and Jaeger services.
Google Cloud
For a streamlined setup targeting Google Cloud, use the scripts/telemetry_gcp.js script which automates setting up a local OpenTelemetry collector that forwards data to your Google Cloud project.
-
Prerequisites:
- Ensure you have a Google Cloud Project ID.
- Set the
GOOGLE_CLOUD_PROJECTenvironment variable to your project ID. - Authenticate with Google Cloud (e.g., run
gcloud auth application-default loginor ensureGOOGLE_APPLICATION_CREDENTIALSis set). - Ensure your account/service account has the necessary roles: "Cloud Trace Agent", "Monitoring Metric Writer", and "Logs Writer".
-
Run the Script: Execute the script from the root of the repository:
./scripts/telemetry_gcp.jsThe script will:
- Download the
otelcol-contribbinary if needed. - Start an OTEL collector configured to receive data from the Gemini CLI and export it to your specified Google Cloud project.
- Automatically enable telemetry and disable sandbox mode in your workspace settings (
.gemini/settings.json). - Provide direct links to view traces, metrics, and logs in your Google Cloud Console.
- On exit (Ctrl+C), it will attempt to restore your original telemetry and sandbox settings.
- Download the
-
View Telemetry in Google Cloud: Use the links provided by the script to navigate to the Google Cloud Console and view your traces, metrics, and logs.
-
Inspect Local Collector Logs: The script redirects the local OTEL collector's output to
.gemini/otel/collector-gcp.log. You can monitor this file for detailed information or troubleshooting:tail -f .gemini/otel/collector-gcp.log -
Stop the Service: Press
Ctrl+Cin the terminal where the script is running to stop the OTEL Collector.
Data Reference: Logs & Metrics
A sessionId is included as a common attribute on all logs and metrics.
Logs
These are timestamped records of specific events.
-
gemini_cli.config: Fired once at startup with the CLI's configuration.- Attributes:
model(string)embedding_model(string)sandbox_enabled(boolean)core_tools_enabled(string)approval_mode(string)api_key_enabled(boolean)vertex_ai_enabled(boolean)code_assist_enabled(boolean)log_user_prompts_enabled(boolean)file_filtering_respect_git_ignore(boolean)debug_mode(boolean)mcp_servers(string)
- Attributes:
-
gemini_cli.user_prompt: Fired when a user submits a prompt.- Attributes:
prompt_lengthprompt(except iflog_user_prompts_enabledis false)
- Attributes:
-
gemini_cli.tool_call: Fired for every function call.- Attributes:
function_namefunction_argsduration_mssuccess(boolean)decision(string: "accept", "reject", or "modify", optional)error(optional)error_type(optional)
- Attributes:
-
gemini_cli.api_request: Fired when making a request to the Gemini API.- Attributes:
modelrequest_text(optional)
- Attributes:
-
gemini_cli.api_error: Fired if the API request fails.- Attributes:
modelerrorerror_typestatus_codeduration_ms
- Attributes:
-
gemini_cli.api_response: Fired upon receiving a response from the Gemini API.- Attributes:
modelstatus_codeduration_mserror(optional)input_token_countoutput_token_countcached_content_token_countthoughts_token_counttool_token_countresponse_text(optional)
- Attributes:
Metrics
These are numerical measurements of behavior over time.
-
gemini_cli.session.count(Counter, Int): Incremented once per CLI startup. -
gemini_cli.tool.call.count(Counter, Int): Counts tool calls.- Attributes:
function_namesuccess(boolean)decision(string: "accept", "reject", or "modify", optional)
- Attributes:
-
gemini_cli.tool.call.latency(Histogram, ms): Measures tool call latency.- Attributes:
function_namedecision(string: "accept", "reject", or "modify", optional)
- Attributes:
-
gemini_cli.api.request.count(Counter, Int): Counts all API requests.- Attributes:
modelstatus_codeerror_type(optional)
- Attributes:
-
gemini_cli.api.request.latency(Histogram, ms): Measures API request latency.- Attributes:
model
- Attributes:
-
gemini_cli.token.usage(Counter, Int): Counts the number of tokens used.- Attributes:
modeltype(string: "input", "output", "thought", "cache", or "tool")
- Attributes: