From 57272f84b1d96d677e0ef8908ba976ed08ffff67 Mon Sep 17 00:00:00 2001 From: Jerop Kipruto Date: Tue, 23 Sep 2025 01:40:30 +0900 Subject: [PATCH] fix(telemetry): disable OTLP when telemetry-outfile is set (#9117) --- docs/telemetry.md | 18 +++++++++--------- packages/core/src/telemetry/sdk.test.ts | 18 ++++++++++++++++++ packages/core/src/telemetry/sdk.ts | 2 +- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/docs/telemetry.md b/docs/telemetry.md index 59524fec1b..6baa4a8894 100644 --- a/docs/telemetry.md +++ b/docs/telemetry.md @@ -51,15 +51,15 @@ observability framework — Gemini CLI's observability system provides: All telemetry behavior is controlled through your `.gemini/settings.json` file and can be overridden with CLI flags: -| Setting | Values | Default | CLI Override | Description | -| -------------- | ----------------- | ----------------------- | -------------------------------------------------------- | ---------------------------------------------------- | -| `enabled` | `true`/`false` | `false` | `--telemetry` / `--no-telemetry` | Enable or disable telemetry | -| `target` | `"gcp"`/`"local"` | `"local"` | `--telemetry-target ` | Where to send telemetry data | -| `otlpEndpoint` | URL string | `http://localhost:4317` | `--telemetry-otlp-endpoint ` | OTLP collector endpoint | -| `otlpProtocol` | `"grpc"`/`"http"` | `"grpc"` | `--telemetry-otlp-protocol ` | OTLP transport protocol | -| `outfile` | file path | - | `--telemetry-outfile ` | Save telemetry to file (requires `otlpEndpoint: ""`) | -| `logPrompts` | `true`/`false` | `true` | `--telemetry-log-prompts` / `--no-telemetry-log-prompts` | Include prompts in telemetry logs | -| `useCollector` | `true`/`false` | `false` | - | Use external OTLP collector (advanced) | +| Setting | Values | Default | CLI Override | Description | +| -------------- | ----------------- | ----------------------- | -------------------------------------------------------- | ------------------------------------------------- | +| `enabled` | `true`/`false` | `false` | `--telemetry` / `--no-telemetry` | Enable or disable telemetry | +| `target` | `"gcp"`/`"local"` | `"local"` | `--telemetry-target ` | Where to send telemetry data | +| `otlpEndpoint` | URL string | `http://localhost:4317` | `--telemetry-otlp-endpoint ` | OTLP collector endpoint | +| `otlpProtocol` | `"grpc"`/`"http"` | `"grpc"` | `--telemetry-otlp-protocol ` | OTLP transport protocol | +| `outfile` | file path | - | `--telemetry-outfile ` | Save telemetry to file (overrides `otlpEndpoint`) | +| `logPrompts` | `true`/`false` | `true` | `--telemetry-log-prompts` / `--no-telemetry-log-prompts` | Include prompts in telemetry logs | +| `useCollector` | `true`/`false` | `false` | - | Use external OTLP collector (advanced) | For detailed information about all configuration options, see the [Configuration Guide](./cli/configuration.md). diff --git a/packages/core/src/telemetry/sdk.test.ts b/packages/core/src/telemetry/sdk.test.ts index 8dae131507..8808ee1704 100644 --- a/packages/core/src/telemetry/sdk.test.ts +++ b/packages/core/src/telemetry/sdk.test.ts @@ -21,6 +21,9 @@ import { } from './gcp-exporters.js'; import { TelemetryTarget } from './index.js'; +import * as os from 'node:os'; +import * as path from 'node:path'; + vi.mock('@opentelemetry/exporter-trace-otlp-grpc'); vi.mock('@opentelemetry/exporter-logs-otlp-grpc'); vi.mock('@opentelemetry/exporter-metrics-otlp-grpc'); @@ -218,4 +221,19 @@ describe('Telemetry SDK', () => { } } }); + + it('should not use OTLP exporters when telemetryOutfile is set', () => { + vi.spyOn(mockConfig, 'getTelemetryOutfile').mockReturnValue( + path.join(os.tmpdir(), 'test.log'), + ); + initializeTelemetry(mockConfig); + + expect(OTLPTraceExporter).not.toHaveBeenCalled(); + expect(OTLPLogExporter).not.toHaveBeenCalled(); + expect(OTLPMetricExporter).not.toHaveBeenCalled(); + expect(OTLPTraceExporterHttp).not.toHaveBeenCalled(); + expect(OTLPLogExporterHttp).not.toHaveBeenCalled(); + expect(OTLPMetricExporterHttp).not.toHaveBeenCalled(); + expect(NodeSDK.prototype.start).toHaveBeenCalled(); + }); }); diff --git a/packages/core/src/telemetry/sdk.ts b/packages/core/src/telemetry/sdk.ts index bd26be485f..acc76692d0 100644 --- a/packages/core/src/telemetry/sdk.ts +++ b/packages/core/src/telemetry/sdk.ts @@ -95,8 +95,8 @@ export function initializeTelemetry(config: Config): void { const telemetryTarget = config.getTelemetryTarget(); const useCollector = config.getTelemetryUseCollector(); const parsedEndpoint = parseOtlpEndpoint(otlpEndpoint, otlpProtocol); - const useOtlp = !!parsedEndpoint; const telemetryOutfile = config.getTelemetryOutfile(); + const useOtlp = !!parsedEndpoint && !telemetryOutfile; const gcpProjectId = process.env['OTLP_GOOGLE_CLOUD_PROJECT'] ||