Skip to content

feat: add fs for disabling invocing on a namespace - #2768

Merged
turip merged 2 commits into
mainfrom
feat/billing-lockdown-fs
May 3, 2025
Merged

turip merged 2 commits into
mainfrom
feat/billing-lockdown-fs

Conversation

@turip

@turip turip commented May 3, 2025 •

Copy link
Copy Markdown
Member

Overview

Allow temporary suspending all billing activity on a namespace.

Summary by CodeRabbit

  • New Features
    • Introduced the ability to lock specific namespaces from billing operations, preventing invoice creation or progression for those namespaces.
    • Added configuration options to manage locked namespaces for billing.
  • Bug Fixes
    • Improved error handling to gracefully skip billing actions and log warnings when a namespace is locked.
  • Tests
    • Added tests to verify correct behavior when billing is attempted in locked namespaces.

@coderabbitai

coderabbitai Bot commented May 3, 2025 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This change introduces a namespace lockdown feature in the billing subsystem, allowing certain namespaces to be locked and excluded from billing operations. New configuration structs and validation logic are added to support a list of locked namespaces, which is now part of the billing configuration and injected throughout the application using dependency injection. The billing service and related interfaces are extended to handle locked namespaces, and error handling is updated to recognize and process the new "namespace locked" error. Invoice state transitions and invoice creation methods are modified to enforce lockdown restrictions. Tests are added and updated to verify the correct handling of locked namespaces during invoice creation and progression.

Changes

Files/Groups Change Summary
app/config/billing.go Added FeatureSwitches field to BillingConfiguration, introduced new struct BillingFeatureSwitchesConfiguration with NamespaceLockdown field, and updated validation logic.
app/common/billing.go Updated BillingService function to accept and pass feature switches configuration, specifically handling namespace lockdowns.
app/common/config.go Updated dependency injection to include FeatureSwitches from billing configuration.
cmd/billing-worker/wire_gen.go, cmd/jobs/internal/wire_gen.go, cmd/server/wire_gen.go Modified billing service initialization to pass feature switches configuration, including namespace lockdowns.
openmeter/billing/errors.go Added new validation error variable ErrNamespaceLocked.
openmeter/billing/service.go Extended ConfigService interface with WithAdvancementStrategy and WithLockedNamespaces methods.
openmeter/billing/service/service.go Added fsNamespaceLockdown to Service and FSNamespaceLockdown to Config; implemented new methods for setting advancement strategy and locked namespaces.
openmeter/billing/service/invoice.go Added check in InvoicePendingLines to return error if namespace is locked.
openmeter/billing/service/invoicestate.go Added FSNamespaceLockdown to InvoiceStateMachine and enforced lockdown checks in key invoice state transition methods.
openmeter/billing/worker/collect/collect.go Added handling for ErrNamespaceLocked, logging and skipping invoice collection for locked namespaces.
openmeter/billing/worker/subscription/handlers.go Added handling for ErrNamespaceLocked, logging and skipping invoice creation for locked namespaces in subscription event handlers.
openmeter/server/server_test.go Extended NoopBillingService with WithAdvancementStrategy and WithLockedNamespaces methods as no-op implementations.
test/billing/invoice_test.go Added tests for namespace lockdown: verifying blocked invoice creation and proper error reporting during invoice progression.
test/billing/suite.go Renamed and refactored invoice creation helpers to support new test scenarios for locked namespaces.

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 golangci-lint (1.64.8)

Error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2
Failed executing command with error: you are using a configuration file for golangci-lint v2 with golangci-lint v1: please use golangci-lint v2


📜 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 4954687 and ce2389a.

📒 Files selected for processing (16)
  • app/common/billing.go (3 hunks)
  • app/common/config.go (1 hunks)
  • app/config/billing.go (2 hunks)
  • cmd/billing-worker/wire_gen.go (1 hunks)
  • cmd/jobs/internal/wire_gen.go (1 hunks)
  • cmd/server/wire_gen.go (1 hunks)
  • openmeter/billing/errors.go (1 hunks)
  • openmeter/billing/service.go (1 hunks)
  • openmeter/billing/service/invoice.go (1 hunks)
  • openmeter/billing/service/invoicestate.go (8 hunks)
  • openmeter/billing/service/service.go (4 hunks)
  • openmeter/billing/worker/collect/collect.go (1 hunks)
  • openmeter/billing/worker/subscription/handlers.go (2 hunks)
  • openmeter/server/server_test.go (1 hunks)
  • test/billing/invoice_test.go (1 hunks)
  • test/billing/suite.go (2 hunks)
✅ Files skipped from review due to trivial changes (1)
  • cmd/jobs/internal/wire_gen.go
🚧 Files skipped from review as they are similar to previous changes (15)
  • openmeter/billing/worker/collect/collect.go
  • app/common/config.go
  • openmeter/billing/errors.go
  • cmd/billing-worker/wire_gen.go
  • openmeter/billing/service.go
  • app/common/billing.go
  • openmeter/billing/service/invoice.go
  • cmd/server/wire_gen.go
  • openmeter/billing/worker/subscription/handlers.go
  • test/billing/suite.go
  • openmeter/server/server_test.go
  • app/config/billing.go
  • openmeter/billing/service/service.go
  • test/billing/invoice_test.go
  • openmeter/billing/service/invoicestate.go
⏰ Context from checks skipped due to timeout of 90000ms (11)
  • GitHub Check: Quickstart
  • GitHub Check: E2E
  • GitHub Check: CI
  • GitHub Check: Developer environment
  • GitHub Check: Commit hooks
  • GitHub Check: Test
  • GitHub Check: Migration Checks
  • GitHub Check: Analyze (javascript)
  • GitHub Check: Lint
  • GitHub Check: Analyze (go)
  • GitHub Check: Build
✨ Finishing Touches
  • 📝 Generate Docstrings

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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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 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/billing-lockdown-fs branch from 6b930b5 to 36602ba Compare May 3, 2025 16:14
@turip
turip marked this pull request as ready for review May 3, 2025 16:14
@turip
turip requested a review from a team as a code owner May 3, 2025 16:14
@turip turip added the release-note/misc Miscellaneous changes label May 3, 2025
@turip
turip enabled auto-merge (squash) May 3, 2025 16:15

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
app/common/billing.go (1)

64-64: LGTM! Configuration properly passed to service.

The namespace lockdown configuration is correctly passed to the billing service implementation.

Consider adding a comment explaining the purpose of FSNamespaceLockdown for improved code documentation:

 service, err := billingservice.New(billingservice.Config{
 	Adapter:             billingAdapter,
 	AppService:          appService,
 	CustomerService:     customerService,
 	FeatureService:      featureConnector,
 	Logger:              logger,
 	MeterService:        meterService,
 	StreamingConnector:  streamingConnector,
 	Publisher:           eventPublisher,
 	AdvancementStrategy: advancementStrategy,
+	// FSNamespaceLockdown contains a list of namespace IDs that should be excluded from billing operations
 	FSNamespaceLockdown: fsConfig.NamespaceLockdown,
 })
openmeter/billing/service.go (1)

90-91: Interface methods added for billing service configuration.

The addition of these interface methods is appropriate for supporting the new namespace lockdown feature and maintaining a clean configuration approach.

Consider adding doc comments to explain the purpose of these methods:

 type ConfigService interface {
 	GetAdvancementStrategy() AdvancementStrategy
+	// WithAdvancementStrategy returns a new service instance with the specified advancement strategy
 	WithAdvancementStrategy(strategy AdvancementStrategy) Service
+	// WithLockedNamespaces returns a service that will restrict billing operations for the specified namespaces
 	WithLockedNamespaces(namespaces []string) Service
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 06eaf89 and 36602ba.

📒 Files selected for processing (16)
  • app/common/billing.go (3 hunks)
  • app/common/config.go (1 hunks)
  • app/config/billing.go (2 hunks)
  • cmd/billing-worker/wire_gen.go (1 hunks)
  • cmd/jobs/internal/wire_gen.go (1 hunks)
  • cmd/server/wire_gen.go (1 hunks)
  • openmeter/billing/errors.go (1 hunks)
  • openmeter/billing/service.go (1 hunks)
  • openmeter/billing/service/invoice.go (1 hunks)
  • openmeter/billing/service/invoicestate.go (7 hunks)
  • openmeter/billing/service/service.go (4 hunks)
  • openmeter/billing/worker/collect/collect.go (1 hunks)
  • openmeter/billing/worker/subscription/handlers.go (2 hunks)
  • openmeter/server/server_test.go (1 hunks)
  • test/billing/invoice_test.go (1 hunks)
  • test/billing/suite.go (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (9)
app/common/config.go (1)
app/config/billing.go (1)
  • BillingConfiguration (12-16)
openmeter/billing/errors.go (1)
openmeter/billing/validationissue.go (1)
  • NewValidationError (75-81)
cmd/jobs/internal/wire_gen.go (1)
app/common/billing.go (1)
  • BillingService (41-92)
cmd/billing-worker/wire_gen.go (1)
app/common/billing.go (1)
  • BillingService (41-92)
app/common/billing.go (1)
app/config/billing.go (1)
  • BillingFeatureSwitchesConfiguration (35-37)
openmeter/billing/worker/collect/collect.go (2)
openmeter/billing/errors.go (1)
  • ErrNamespaceLocked (59-59)
openmeter/customer/customer.go (1)
  • CustomerID (74-74)
openmeter/billing/service.go (2)
openmeter/billing/serviceconfig.go (1)
  • AdvancementStrategy (8-8)
openmeter/billing/service/service.go (1)
  • Service (23-38)
openmeter/billing/worker/subscription/handlers.go (2)
openmeter/billing/errors.go (1)
  • ErrNamespaceLocked (59-59)
api/api.gen.go (1)
  • Customer (2117-2164)
openmeter/billing/service/service.go (1)
openmeter/billing/service.go (1)
  • Service (9-19)
⏰ Context from checks skipped due to timeout of 90000ms (11)
  • GitHub Check: CI
  • GitHub Check: Quickstart
  • GitHub Check: E2E
  • GitHub Check: Developer environment
  • GitHub Check: Commit hooks
  • GitHub Check: Migration Checks
  • GitHub Check: Lint
  • GitHub Check: Test
  • GitHub Check: Build
  • GitHub Check: Analyze (javascript)
  • GitHub Check: Analyze (go)
🔇 Additional comments (29)
app/common/config.go (1)

18-18: Configuration update for feature switches looks good.

This change adds support for FeatureSwitches dependency injection from the BillingConfiguration struct, enabling the namespace lockdown feature to be propagated throughout the billing system.

openmeter/billing/errors.go (1)

59-59: New validation error defined correctly.

The new ErrNamespaceLocked error is properly defined as a critical validation error, consistent with other validation errors in the system. This error will be used to indicate when a namespace has been locked for billing operations.

openmeter/billing/worker/collect/collect.go (1)

161-165: Appropriate error handling for locked namespaces.

This change adds proper handling of the new ErrNamespaceLocked error, allowing the system to gracefully skip invoice collection for locked namespaces. The warning message provides good visibility while maintaining system stability.

cmd/jobs/internal/wire_gen.go (1)

278-279: Wire configuration updated correctly for feature switches.

The dependency injection setup now correctly extracts and passes the billing feature switches configuration to the billing service. This change ensures the namespace lockdown feature will be properly initialized and available throughout the system.

cmd/billing-worker/wire_gen.go (1)

271-272: LGTM! Good integration of the namespace lockdown feature.

This change properly extracts the feature switches configuration from the billing configuration and passes it to the common.BillingService function, aligning with other initialization files.

app/common/billing.go (2)

8-8: LGTM! Import added correctly.

The config package import is necessary for the new parameter type.


52-52: LGTM! Proper parameter addition.

The new parameter for feature switches configuration extends the function signature correctly to support namespace lockdown functionality.

cmd/server/wire_gen.go (1)

272-273: LGTM! Consistent implementation across initialization files.

This change correctly extracts the feature switches configuration and passes it to the billing service, maintaining consistency with other initialization files.

openmeter/billing/service/invoice.go (1)

234-238: Good implementation of namespace lockdown validation.

The code correctly checks if a namespace is in the lockdown list before proceeding with invoice processing, returning an appropriate validation error when needed. The early validation ensures no processing resources are wasted on locked namespaces.

openmeter/billing/worker/subscription/handlers.go (2)

56-59: Appropriate error handling for locked namespaces.

The code correctly identifies and handles the namespace lockdown case, providing a clear warning log and gracefully continuing execution. This ensures operations aren't disrupted when a namespace is locked.


173-177: Consistent namespace lockdown error handling.

The implementation maintains the same error handling pattern for locked namespaces as in the HandleCancelledEvent method, ensuring consistency throughout the codebase while providing appropriate logging.

openmeter/server/server_test.go (1)

1482-1488: Correctly implemented interface methods for test compatibility.

The no-op implementations of WithAdvancementStrategy and WithLockedNamespaces methods maintain compatibility with the updated billing.Service interface while preserving the test double pattern. Both methods properly return the receiver to support method chaining.

app/config/billing.go (2)

15-15: Well-structured configuration for namespace lockdown feature.

The addition of the FeatureSwitches field and its validation in the BillingConfiguration struct provides a clean way to integrate the namespace lockdown feature into the configuration system.

Also applies to: 28-30


35-41: Clean implementation of feature switches configuration.

The new BillingFeatureSwitchesConfiguration struct is well-designed with a clear purpose. The empty Validate method provides a hook for future validation rules as the feature evolves.

test/billing/suite.go (2)

314-388: Renamed method with clear documentation

The CreateDraftInvoice method has been renamed to CreateGatheringInvoice, which better reflects its purpose of creating pending invoice lines without generating an actual invoice.


390-409: Well-designed method implementation

The new CreateDraftInvoice method appropriately reuses CreateGatheringInvoice and then calls InvoicePendingLines, maintaining the DRY principle while separating concerns. This refactoring enables tests to verify both gathering and draft invoice creation separately, which is essential for testing the new namespace lockdown functionality.

openmeter/billing/service/service.go (5)

37-37: Feature switch for namespace lockdown added

Added a field to store namespaces that should be locked for billing operations.


50-50: Configuration structure extended appropriately

The Config struct properly includes the feature switch configuration for namespace lockdown, maintaining consistency with the Service struct.


108-108: Consistent initialization in constructor

The namespace lockdown list is properly initialized from the configuration, ensuring consistency between the configuration and service state.


190-194: Added support for modifying advancement strategy

This method creates a copy of the service with a new advancement strategy, following the functional options pattern for immutable configuration changes.


196-200: Added support for modifying locked namespaces

This method allows dynamic configuration of which namespaces are locked for billing operations, providing flexibility for testing and runtime changes.

test/billing/invoice_test.go (2)

3661-3691: Comprehensive test for namespace lockdown on invoice creation

This test verifies that when a namespace is locked, attempts to create invoices fail with the appropriate ErrNamespaceLocked error. The test follows a clear arrange-act-assert pattern and properly cleans up after itself by restoring the original billing service.


3693-3737: Thorough test for namespace lockdown on invoice progression

This test effectively verifies that when a namespace becomes locked, existing invoices cannot be advanced and are properly marked as invalid with the appropriate validation error. It tests the full invoice lifecycle by first creating an invoice without lockdown, then attempting to advance it after locking the namespace.

openmeter/billing/service/invoicestate.go (6)

28-28: Feature switch field added to InvoiceStateMachine

Added field to store the list of locked namespaces, enabling the state machine to enforce lockdown restrictions during invoice operations.


252-252: Proper propagation of locked namespaces

The locked namespaces list is correctly propagated from the service to the state machine, ensuring consistency across the application.


535-537: Namespace lockdown check for validation

Appropriate check to prevent invoice validation when the namespace is locked, with a clear error message that includes the namespace name.


550-552: Namespace lockdown check for invoice syncing

Consistent implementation of the lockdown check before syncing draft invoices with external systems, using the same error format as other methods.


571-573: Namespace lockdown check for invoice finalization

Proper enforcement of namespace lockdown during invoice finalization, ensuring that locked namespaces cannot proceed to payment processing.


606-608: Namespace lockdown check for deleted invoice syncing

Complete implementation of the lockdown feature by also preventing sync operations for deleted invoices in locked namespaces.

@turip
turip force-pushed the feat/billing-lockdown-fs branch from 4954687 to ce2389a Compare May 3, 2025 16:38
@turip
turip merged commit b271d1f into main May 3, 2025
@turip
turip deleted the feat/billing-lockdown-fs branch May 3, 2025 16:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/misc Miscellaneous changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants