Skip to content

feat(config): add OPENCODE_DISABLE_METRICS to suppress individual metrics#11

Merged
digitalfiz merged 3 commits intomainfrom
feat/disable-metrics
Mar 15, 2026
Merged

feat(config): add OPENCODE_DISABLE_METRICS to suppress individual metrics#11
digitalfiz merged 3 commits intomainfrom
feat/disable-metrics

Conversation

@digitalfiz
Copy link
Copy Markdown
Contributor

@digitalfiz digitalfiz commented Mar 15, 2026

Summary

  • Adds OPENCODE_DISABLE_METRICS env var — a comma-separated list of metric name suffixes to suppress (e.g. cache.count,retry.count)
  • Disabling a metric skips the counter/histogram only — log events always fire regardless
  • Logs disabled metrics at startup at info level
  • Adds .pre-commit-config.yaml with standard checks (JSON, YAML, Markdown, TypeScript)

New env var

# Disable opencode-only metrics to keep a Claude Code dashboard clean
export OPENCODE_DISABLE_METRICS="cache.count,session.duration,session.token.total,session.cost.total,model.usage,retry.count,message.count"

Metric name suffixes (without OPENCODE_METRIC_PREFIX) — so the setting works regardless of prefix.

opencode-only metrics table (added to README)

Metric suffix Why it's opencode-only
cache.count Cache read/write occurrence counts — not a Claude Code signal
session.duration Session wall-clock duration — not emitted by Claude Code
session.token.total Per-session token histogram — not emitted by Claude Code
session.cost.total Per-session cost histogram — not emitted by Claude Code
model.usage Per-model message counter — not emitted by Claude Code
retry.count API retry counter — not emitted by Claude Code
message.count Completed message counter — not emitted by Claude Code

Implementation

  • loadConfig() parses OPENCODE_DISABLE_METRICS into a Set<string> (trims whitespace, ignores empty segments)
  • disabledMetrics threaded through HandlerContext — no globals
  • isMetricEnabled(name, ctx) helper in util.ts — O(1) Set.has() check
  • All 13 instrument call sites guarded across session.ts, message.ts, activity.ts
  • lines_of_code.count guard hoisted outside the diff loop

Tests

130 tests passing. New coverage:

  • config.test.ts — 5 tests for loadConfig parsing (whitespace, trailing commas, etc.)
  • util.test.ts — 5 direct unit tests for isMetricEnabled including unknown metric name
  • disabled-metrics.test.ts — per-metric disable tests + all-disabled integration test

Pre-commit hooks

  • pre-commit-hooks: trailing whitespace, EOF newline, merge conflicts, case conflicts, JSON validation + formatting, YAML, LF line endings
  • markdownlint: with .markdownlint.yaml (MD013/MD041 off, MD024 siblings-only for CHANGELOG)
  • typecheck: runs bun run typecheck on any TypeScript change

Summary by CodeRabbit

  • New Features

    • Added ability to disable specific metrics via OPENCODE_DISABLE_METRICS environment variable configuration option.
  • Documentation

    • Expanded metrics documentation with detailed descriptions, types, and new metric entries.
    • Added quick start guide with example commands for enabling telemetry and configuring OTLP endpoints.
  • Chores

    • Added pre-commit hooks and Markdownlint configuration for code quality.
    • Updated build configuration and manifest files.

…rics

Adds a comma-separated env var to disable any subset of metrics by
name suffix (without prefix), e.g. 'cache.count,retry.count'.

- Parse OPENCODE_DISABLE_METRICS into a Set<string> in loadConfig()
- Thread disabledMetrics through HandlerContext
- Add isMetricEnabled() helper in util.ts
- Guard all 13 instrument call sites across session, message, activity handlers
- Disabling a metric skips the counter/histogram only — log events still emit
- Add 23 tests covering parse, per-metric disable, and all-disabled scenario
- Update README with env var docs and opencode-only metrics table
- Move retry debug log inside isMetricEnabled guard (was firing even
  when retry.count was disabled, giving a misleading message)
- Hoist lines_of_code.count guard outside the diff loop in activity.ts
  (was evaluated twice per file; now evaluated once per event)
- Pass disabled metrics as array in startup log (more queryable in
  log backends than a comma-joined string)
- Restore user_prompt to README log events table (was accidentally
  dropped in the docs rewrite)
- Add OPENCODE_DISABLE_METRICS to config.test.ts env var cleanup list
  (prevents stale env state leaking across test files)
- Move loadConfig parsing tests for disabledMetrics into config.test.ts
  where env isolation is managed via beforeEach/afterEach
- Add direct unit tests for isMetricEnabled in util.test.ts including
  unknown metric name case
Adds .pre-commit-config.yaml with:
- pre-commit-hooks: trailing whitespace, end-of-file, merge conflicts,
  case conflicts, JSON validation, JSON formatting, YAML validation,
  LF line endings
- markdownlint: MD013 (line length) and MD041 (first-line-heading)
  disabled via .markdownlint.yaml; MD024 set to siblings_only for
  CHANGELOG compatibility
- local TypeScript typecheck hook via bun run typecheck

Also fixes all MD040 (missing fenced code language) violations in
AGENTS.md and CONTRIBUTING.md by adding 'text' language to plaintext
code blocks.
@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 15, 2026

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

Add environment-based metric disabling feature via OPENCODE_DISABLE_METRICS environment variable. Configuration parsing, context propagation, and handler-level guards using isMetricEnabled() allow selective suppression of metric emissions while preserving logging behavior.

Changes

Cohort / File(s) Summary
Configuration & Linting
.markdownlint.yaml, .pre-commit-config.yaml, .npmrc
New Markdownlint and pre-commit hook configurations added; NPM auth token removed.
Documentation
AGENTS.md, CHANGELOG.md, CONTRIBUTING.md, README.md
Formatting updates to code blocks with language specifications, expanded metrics table, new configuration section for OPENCODE_DISABLE_METRICS, quick-start guide, and opencode-only metrics documentation.
Package Metadata
package.json, release-please-config.json
Restored and reordered package fields; expanded changelog-sections configuration with explicit type and section mappings.
Core Implementation
src/config.ts, src/types.ts, src/util.ts, src/index.ts
Added disabledMetrics: Set<string> field to config and context; introduced isMetricEnabled() utility function; integrated disabled metrics parsing from environment and context propagation.
Handler Metric Gating
src/handlers/session.ts, src/handlers/message.ts, src/handlers/activity.ts
Wrapped metric emission calls (counters, histograms, gauges) with isMetricEnabled() guards for session, token, cost, message, model usage, tool duration, and lines-of-code metrics.
Test Infrastructure
tests/config.test.ts, tests/util.test.ts, tests/helpers.ts, tests/handlers/disabled-metrics.test.ts
Added tests for metric disabling parsing, isMetricEnabled() utility validation, and comprehensive disabled-metrics handler integration tests covering all affected metrics.

Sequence Diagram(s)

sequenceDiagram
    participant ENV as Environment
    participant Config as config.ts
    participant Plugin as OtelPlugin (index.ts)
    participant Context as HandlerContext
    participant Handler as Handlers<br/>(session/message/activity)
    participant Meter as OTel Meter

    ENV->>Config: OPENCODE_DISABLE_METRICS="metric1,metric2"
    activate Config
    Config->>Config: Parse comma-separated list
    Config->>Config: Populate Set<string>
    Config-->>Plugin: Return config.disabledMetrics
    deactivate Config

    Plugin->>Context: Attach disabledMetrics: Set<string>
    activate Context
    Handler->>Context: Access ctx.disabledMetrics
    deactivate Context

    Handler->>Handler: isMetricEnabled("session.count", ctx)
    alt Metric enabled
        Handler->>Meter: counter.add()
    else Metric disabled
        Handler->>Handler: Skip metric emission
    end
    Handler->>Handler: Continue with logging<br/>(always emitted)
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 Selective sensing, metrics wise,
Toggle toggles, no surprise!
Disabled names in sets we store,
Guards on gates from metrics' core,
Logging flows while counters pause,
Flexible telemetry—just because! 🎯

🚥 Pre-merge checks | ✅ 2 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 52.63% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The pull request title clearly and specifically describes the main feature: adding OPENCODE_DISABLE_METRICS environment variable to suppress individual metrics, which aligns with the changeset's primary objective.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
  • 📝 Generate docstrings (stacked PR)
  • 📝 Generate docstrings (commit on current branch)
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/disable-metrics
📝 Coding Plan
  • Generate coding plan for human review comments

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@digitalfiz digitalfiz merged commit 8ec7c48 into main Mar 15, 2026
2 of 3 checks passed
@digitalfiz digitalfiz deleted the feat/disable-metrics branch March 21, 2026 21:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant