Skip to content

feat: billing use streaming specified minimum resolution [OM-1469]#3149

Merged
turip merged 2 commits intomainfrom
feat/use-streaming-specified-minimum-resolution
Jul 31, 2025
Merged

feat: billing use streaming specified minimum resolution [OM-1469]#3149
turip merged 2 commits intomainfrom
feat/use-streaming-specified-minimum-resolution

Conversation

@turip
Copy link
Member

@turip turip commented Jul 28, 2025

Overview

This patch ensures that the whole billing stack uses 1s resolution for both usage_based/metered and usage_based/flat_fee lines.

This is required as due to collection moved to the draft invoices if an invoice contains both types, we might create two invoices.

The overal approach is that all inputs to billing is sanitized to use per second resolution entries, plus a migration is added so that subscription sync will fill the gaps between the lines of the same subscription item. This is done at multiple places:

  • LineService ensures that CreatePendingLines truncate the periods.
  • HTTPDriver ensures that Simulation and Invoice Update truncates the period.

We are not updating the old invoices to prevent changes in already issued invoices.

Summary by CodeRabbit

  • Bug Fixes

    • Improved accuracy and consistency of time period handling across billing and invoicing by standardizing truncation to a 1-second resolution.
    • Enhanced validation and comparison of invoice line periods to prevent issues with zero-length or misaligned service periods.
    • Updated test cases to reflect new time truncation logic and ensure reliable results.
  • Chores

    • Renamed internal constants for minimum window size duration to improve clarity.
    • Added a database migration to annotate certain invoice lines for synchronization purposes.
  • Documentation

    • Added and updated comments to clarify billing line handling and truncation behavior.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jul 28, 2025

📝 Walkthrough

Walkthrough

This change standardizes all billing, invoicing, and subscription time truncation and period comparison logic to use a unified 1-second resolution constant (streaming.MinimumWindowSizeDuration). It removes the previous DefaultMeterResolution, updates code and tests for consistent period handling, renames streaming constants, and introduces a migration to annotate affected invoice lines.

Changes

Cohort / File(s) Change Summary
Billing Defaults & Constants
openmeter/billing/defaults.go, openmeter/streaming/defaults.go
Removed DefaultMeterResolution from billing; renamed streaming constants to MinimumWindowSizeDuration and MinimumWindowSize.
Invoice Line Truncation Logic
openmeter/billing/httpdriver/invoiceline.go, openmeter/billing/invoiceline.go, openmeter/billing/service/lineservice/usagebasedline.go, openmeter/billing/service/lineservice/usagebasedlineflat.go
Replaced all time truncation and validation logic from using billing’s default resolution to streaming.MinimumWindowSizeDuration.
Invoice Line Service & Comments
openmeter/billing/service/lineservice/service.go
Added clarifying comment regarding fee line handling; no logic changed.
Subscription Worker Logic
openmeter/billing/worker/subscription/invoiceupdate.go, openmeter/billing/worker/subscription/phaseiterator.go, openmeter/billing/worker/subscription/sync.go
Updated all period comparisons and truncations to use streaming.MinimumWindowSizeDuration; revised argument passing for billing anchors; added TODO/comment for harmonization.
Subscription Worker Tests
openmeter/billing/worker/subscription/phaseiterator_test.go, openmeter/billing/worker/subscription/sync_test.go
Updated test cases to use microsecond/fractional second precision and align all period assertions with new truncation logic.
Subscription Spec API
openmeter/subscription/subscriptionspec.go
Changed GetFullServicePeriodAt to require non-pointer billing anchor and error on zero value; removed fallback logic.
Streaming Test Utilities
openmeter/streaming/testutils/streaming.go
Updated to use renamed streaming constants for truncation and window size.
Billing & Stripe Invoice Tests
test/billing/invoice_test.go, test/app/stripe/invoice_test.go
Refactored all period and invoice time assertions to use truncated times; updated mock event timestamps and expected values for consistency.
SQL Migration
tools/migrate/migrations/20250731160524_billing-second-resolution.up.sql
Adds annotation to certain usage-based invoice lines to signal sync logic to ignore them after period calculation changes.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~18 minutes

Possibly related PRs

  • openmeterio/openmeter#2932: Also modifies invoice line preparation and update logic, particularly for usage-based and flat fee lines, directly relating to this PR's invoice line handling changes.
  • openmeterio/openmeter#3087: Updates subscription billing synchronization and period correction logic, complementing this PR’s standardization of time truncation and period comparison in subscription invoice handling.

Note

⚡️ Unit Test Generation is now available in beta!

Learn more here, or try it out under "Finishing Touches" below.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3c8bbb8 and c7c0f07.

⛔ Files ignored due to path filters (1)
  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum
📒 Files selected for processing (17)
  • openmeter/billing/defaults.go (0 hunks)
  • openmeter/billing/httpdriver/invoiceline.go (4 hunks)
  • openmeter/billing/invoiceline.go (2 hunks)
  • openmeter/billing/service/lineservice/service.go (1 hunks)
  • openmeter/billing/service/lineservice/usagebasedline.go (5 hunks)
  • openmeter/billing/service/lineservice/usagebasedlineflat.go (2 hunks)
  • openmeter/billing/worker/subscription/invoiceupdate.go (2 hunks)
  • openmeter/billing/worker/subscription/phaseiterator.go (8 hunks)
  • openmeter/billing/worker/subscription/phaseiterator_test.go (4 hunks)
  • openmeter/billing/worker/subscription/sync.go (4 hunks)
  • openmeter/billing/worker/subscription/sync_test.go (4 hunks)
  • openmeter/streaming/defaults.go (1 hunks)
  • openmeter/streaming/testutils/streaming.go (2 hunks)
  • openmeter/subscription/subscriptionspec.go (2 hunks)
  • test/app/stripe/invoice_test.go (12 hunks)
  • test/billing/invoice_test.go (13 hunks)
  • tools/migrate/migrations/20250731160524_billing-second-resolution.up.sql (1 hunks)
💤 Files with no reviewable changes (1)
  • openmeter/billing/defaults.go
✅ Files skipped from review due to trivial changes (3)
  • openmeter/billing/service/lineservice/service.go
  • tools/migrate/migrations/20250731160524_billing-second-resolution.up.sql
  • openmeter/billing/worker/subscription/sync_test.go
🚧 Files skipped from review as they are similar to previous changes (13)
  • openmeter/billing/service/lineservice/usagebasedlineflat.go
  • openmeter/billing/service/lineservice/usagebasedline.go
  • openmeter/streaming/testutils/streaming.go
  • openmeter/billing/httpdriver/invoiceline.go
  • openmeter/streaming/defaults.go
  • openmeter/billing/invoiceline.go
  • openmeter/billing/worker/subscription/invoiceupdate.go
  • test/app/stripe/invoice_test.go
  • openmeter/billing/worker/subscription/sync.go
  • openmeter/billing/worker/subscription/phaseiterator.go
  • openmeter/billing/worker/subscription/phaseiterator_test.go
  • openmeter/subscription/subscriptionspec.go
  • test/billing/invoice_test.go
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)
  • GitHub Check: Commit hooks
  • GitHub Check: CI
  • GitHub Check: Lint
  • GitHub Check: Quickstart
  • GitHub Check: E2E
  • GitHub Check: Migration Checks
  • GitHub Check: Developer environment
  • GitHub Check: Build
  • GitHub Check: Test
  • GitHub Check: Analyze (javascript)
  • GitHub Check: Analyze (go)
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/use-streaming-specified-minimum-resolution

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@turip turip force-pushed the feat/use-streaming-specified-minimum-resolution branch 7 times, most recently from add45a6 to df1564b Compare July 31, 2025 13:12
@turip turip changed the title feat: use streaming specified minimum resolution feat: billing use streaming specified minimum resolution Jul 31, 2025
@turip turip added release-note/bug-fix Release note: Bug Fixes release-note/feature Release note: Exciting New Features area/billing labels Jul 31, 2025
@turip turip marked this pull request as ready for review July 31, 2025 13:17
@turip turip requested a review from a team as a code owner July 31, 2025 13:17
@turip turip changed the title feat: billing use streaming specified minimum resolution feat: billing use streaming specified minimum resolution [OM-1469] Jul 31, 2025
@turip turip requested a review from hekike July 31, 2025 14:22
@turip turip enabled auto-merge (squash) July 31, 2025 14:35
@turip turip requested review from chrisgacsal and tothandras July 31, 2025 15:40
@turip turip disabled auto-merge July 31, 2025 15:40
@turip turip force-pushed the feat/use-streaming-specified-minimum-resolution branch from 3c8bbb8 to c7c0f07 Compare July 31, 2025 16:09
@turip turip merged commit c7ccf83 into main Jul 31, 2025
22 checks passed
@turip turip deleted the feat/use-streaming-specified-minimum-resolution branch July 31, 2025 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/billing release-note/bug-fix Release note: Bug Fixes release-note/feature Release note: Exciting New Features

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants