fix: Normalize token and cost units for Claude compatibility#14
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughUpdated 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
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
📝 Coding Plan
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. Comment Tip CodeRabbit can suggest fixes for GitHub Check annotations.Configure the |
|
lgtm, thank you for the PR! |
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 metricsIn OTLP->Prometheus translation, bracket/annotation-style units can lead to:
[USD]can produce a double underscore after Prometheus exporter translation: withUnderscoreEscapingWithSuffixes,[and]are converted to_, so the unit part becomes_USD_; then the counter suffix_totalis appended, resulting in..._USD__total.tokensin the final name)What changed
In
src/otel.ts, units were normalized to plain values:{token}->tokens[USD]->USDI also applied the same normalization to related session histograms for consistency:
{token}->tokens[USD]->USDNo 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_totalclaude_code_cost_usage_USD_totaland consistent session histogram naming without the extra underscore artifact.
Validation
bun run typecheckbun testSummary by CodeRabbit