Skip to content

fix: Normalize token and cost units for Claude compatibility#14

Merged
digitalfiz merged 2 commits intoDEVtheOPS:mainfrom
Xanonymous-GitHub:fix/metric-unit-compat
Mar 16, 2026
Merged

fix: Normalize token and cost units for Claude compatibility#14
digitalfiz merged 2 commits intoDEVtheOPS:mainfrom
Xanonymous-GitHub:fix/metric-unit-compat

Conversation

@Xanonymous-GitHub
Copy link
Copy Markdown

@Xanonymous-GitHub Xanonymous-GitHub commented Mar 16, 2026

Why

This PR fixes #13

Root cause

The mismatch comes from instrument unit strings used for these metrics:

  • [USD] on cost metrics
  • {token} on token metrics

In OTLP->Prometheus translation, bracket/annotation-style units can lead to:

  • [USD] can produce a double underscore after Prometheus exporter translation: with UnderscoreEscapingWithSuffixes, [ and ] are converted to _, so the unit part becomes _USD_; then the counter suffix _total is appended, resulting in ..._USD__total.
  • dropped token unit suffix (missing tokens in the final name)

What changed

In src/otel.ts, units were normalized to plain values:

  • token.usage: {token} -> tokens
  • cost.usage: [USD] -> USD

I also applied the same normalization to related session histograms for consistency:

  • session.token.total: {token} -> tokens
  • session.cost.total: [USD] -> USD

No instrument names, attributes, event handling, or aggregation logic were changed.

Expected outcome

Prometheus series names align with Claude Code conventions, including:

  • claude_code_token_usage_tokens_total
  • claude_code_cost_usage_USD_total

and consistent session histogram naming without the extra underscore artifact.

Validation

  • bun run typecheck
  • bun test

Summary by CodeRabbit

  • Bug Fixes
    • Corrected unit labels for metrics tracking tokens and costs to display in standard formats.

@coderabbitai
Copy link
Copy Markdown

coderabbitai bot commented Mar 16, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ef7d4bc7-0a5c-46a1-bf05-e1be0f994bed

📥 Commits

Reviewing files that changed from the base of the PR and between ea55a11 and 12bfafe.

📒 Files selected for processing (1)
  • src/otel.ts

📝 Walkthrough

Walkthrough

Updated metric unit values in OpenTelemetry configuration from bracketed and braced formats to plain strings, standardizing tokenCounter and costCounter units alongside their gauge equivalents to align with industry conventions.

Changes

Cohort / File(s) Summary
Metric Unit Normalization
src/otel.ts
Updated four metric unit declarations: tokenCounter and sessionTokenGauge units changed from "{token}" to "tokens"; costCounter and sessionCostGauge units changed from "[USD]" to "USD".

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~3 minutes

Poem

🐰 The units were tangled in brackets and braces,
Now tokens and USD claim their proper places,
Four little strings dancing in harmony true,
Prometheus metrics now match through and through! ✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: normalizing token and cost unit strings for Claude compatibility.
Linked Issues check ✅ Passed The changes directly address issue #13 by normalizing metric units ({token}→tokens, [USD]→USD) to match Claude Code's Prometheus output.
Out of Scope Changes check ✅ Passed All changes are scoped to unit string normalization in src/otel.ts with no unrelated modifications to other areas.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
📝 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.

Tip

CodeRabbit can suggest fixes for GitHub Check annotations.

Configure the reviews.tools.github-checks setting to adjust the time to wait for GitHub Checks to complete.

@digitalfiz digitalfiz merged commit a8b35dc into DEVtheOPS:main Mar 16, 2026
2 checks passed
@digitalfiz
Copy link
Copy Markdown
Contributor

lgtm, thank you for the PR!

@Xanonymous-GitHub Xanonymous-GitHub deleted the fix/metric-unit-compat branch March 17, 2026 02:27
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.

Bug: metric names differ from Claude Code under same collector config

2 participants