Skip to content

fix: use sorted first tier for zero-usage graduated pricing - #5347

Merged
turip merged 2 commits into
openmeterio:mainfrom
arian-gogani:codex/tiered-zero-usage-order
Oct 26, 2026
Merged

turip merged 2 commits into
openmeterio:mainfrom
arian-gogani:codex/tiered-zero-usage-order

Conversation

@arian-gogani

@arian-gogani arian-gogani commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Problem

GraduatedTiered.TieredPriceCalculator walks sorted tiers but selects the zero-usage fallback from the unsorted input. TieredPrice.Validate() accepts reversed tiers, so a mandatory first-bucket flat fee can be missed when usage is zero.

The new test uses the same tiers in ascending and reverse storage order. Before the fix, both prices validate, but the reverse-order case returns 0 instead of 100. The fix reads both paths from one sorted view. This matches the first-bucket flat-fee behavior described in #2119; that PR is context for intended behavior, not the source of this defect.

Verification

  • Before: go test ./openmeter/billing/rating/service/rate/ -run TestTieredOrderIndependence -v exited 1; ascending case passed, reversed case failed (expected 100, actual 0).
  • After: the same command exited 0, both subtests passed.
  • go test ./openmeter/billing/rating/service/rate/ exited 0.

Found by reading the two tier access paths, then tested against current source. This is a rating-package reproduction; I did not run an end-to-end invoice flow.

Summary by CodeRabbit

  • Bug Fixes
    • Corrected flat-fee calculations when usage is zero, ensuring the fee is based on the lowest-priced tier even when tiers are listed in a different order.

RetriggerView in GreptileConfidence Score: 5/5

The PR appears safe to merge.

Summary

The calculator now uses the same sorted tiers for the normal tier walk and the zero-usage fallback.

  • Zero-usage graduated prices use the sorted first tier.

Reviews (2) · Last reviewed commit: "test: consolidate graduated tier order r..." · Reviewed by Greptile

@arian-gogani
arian-gogani requested a review from a team as a code owner October 26, 2026 05:56
@coderabbitai

coderabbitai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
Commits

Reviewing files that changed from the base of the PR and between 65830dd and 22e9f32.

Files selected for processing: 1
  • openmeter/billing/rating/service/rate/tieredgraduated_test.go

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

Walkthrough

The calculator now uses sorted tiers to select the flat-price range when quantity is zero. A regression test checks that reversing the tier order still produces a total of 100.

Changes: Graduated tier pricing

Layer / File(s) Summary
Sorted tier calculation and regression test
openmeter/billing/rating/service/rate/tieredgraduated.go, openmeter/billing/rating/service/rate/tieredgraduated_test.go
The calculator uses the sorted tier list to select the zero-quantity flat-price range. The test checks that reversing the tier order still produces a total of 100.

Priority: Low

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix · Severity of issue fixed: Low

Suggested labels: kind/bug, release-note/bug-fix

Suggested reviewers: galexihu, chrisgacsal, mark-vass-konghq

Merge Risk: Minimal · up to 22e9f

The change includes regression coverage for both ascending and reversed tier orders.

Pre-merge checks: Passed 4
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the main change: using the sorted first tier for zero-usage graduated pricing.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Finishing Touches: 💡 1
🛠️ Fix failing CI checks: 💡
  • Commit to this branch
  • Create a new PR
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

@turip turip added release-note/bug-fix Release note: Bug Fixes area/billing labels Oct 26, 2026

@turip turip left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the contribution! I took the liberty to consolidate the tests, but it's a nice catch!

@turip
turip merged commit dc480ec into openmeterio:main Oct 26, 2026
32 of 35 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants