diff options
| author | Jerop Kipruto <[email protected]> | 2025-06-15 16:24:53 -0400 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-06-15 13:24:53 -0700 |
| commit | 714421c2da4f5d6b9c1c7060fdf5c47ba1c965ca (patch) | |
| tree | 488459669757dda99eda46b621a030ece38842dd /packages/core/src/telemetry/metrics.ts | |
| parent | 4421ef126fc6a2de89132aa35c261bf78cd481d2 (diff) | |
Add file operation telemetry (#1068)
Introduces telemetry for file create, read, and update operations.
This change adds the `gemini_cli.file.operation.count` metric, recorded by the `read-file`, `read-many-files`, and `write-file` tools.
The metric includes the following attributes:
- `operation` (string: `create`, `read`, `update`): The type of file operation.
- `lines` (optional, Int): Number of lines in the file.
- `mimetype` (optional, string): Mimetype of the file.
- `extension` (optional, string): File extension of the file.
Here is a stacked bar chart of file operations by extension (`js`, `ts`, `md`):

Here is a stacked bar chart of file operations by type (`create`, `read`, `update`):

#750
cc @allenhutchison as discussed
Diffstat (limited to 'packages/core/src/telemetry/metrics.ts')
| -rw-r--r-- | packages/core/src/telemetry/metrics.ts | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/packages/core/src/telemetry/metrics.ts b/packages/core/src/telemetry/metrics.ts index 59979ef3..124bc602 100644 --- a/packages/core/src/telemetry/metrics.ts +++ b/packages/core/src/telemetry/metrics.ts @@ -20,15 +20,23 @@ import { METRIC_API_REQUEST_LATENCY, METRIC_TOKEN_USAGE, METRIC_SESSION_COUNT, + METRIC_FILE_OPERATION_COUNT, } from './constants.js'; import { Config } from '../config/config.js'; +export enum FileOperation { + CREATE = 'create', + READ = 'read', + UPDATE = 'update', +} + let cliMeter: Meter | undefined; let toolCallCounter: Counter | undefined; let toolCallLatencyHistogram: Histogram | undefined; let apiRequestCounter: Counter | undefined; let apiRequestLatencyHistogram: Histogram | undefined; let tokenUsageCounter: Counter | undefined; +let fileOperationCounter: Counter | undefined; let isMetricsInitialized = false; function getCommonAttributes(config: Config): Attributes { @@ -75,7 +83,10 @@ export function initializeMetrics(config: Config): void { description: 'Counts the total number of tokens used.', valueType: ValueType.INT, }); - + fileOperationCounter = meter.createCounter(METRIC_FILE_OPERATION_COUNT, { + description: 'Counts file operations (create, read, update).', + valueType: ValueType.INT, + }); const sessionCounter = meter.createCounter(METRIC_SESSION_COUNT, { description: 'Count of CLI sessions started.', valueType: ValueType.INT, @@ -171,3 +182,21 @@ export function recordApiErrorMetrics( model, }); } + +export function recordFileOperationMetric( + config: Config, + operation: FileOperation, + lines?: number, + mimetype?: string, + extension?: string, +): void { + if (!fileOperationCounter || !isMetricsInitialized) return; + const attributes: Attributes = { + ...getCommonAttributes(config), + operation, + }; + if (lines !== undefined) attributes.lines = lines; + if (mimetype !== undefined) attributes.mimetype = mimetype; + if (extension !== undefined) attributes.extension = extension; + fileOperationCounter.add(1, attributes); +} |
