Skip to content

chore: noop init - #4070

Merged
GAlexIHU merged 2 commits into
mainfrom
chore/noop-ledger-init
Apr 2, 2026
Merged

GAlexIHU merged 2 commits into
mainfrom
chore/noop-ledger-init

Conversation

@GAlexIHU

@GAlexIHU GAlexIHU commented Apr 2, 2026 •

Copy link
Copy Markdown
Contributor

Overview

Safe noop init when disabled

Fixes #(issue)

Notes for reviewer

Summary by CodeRabbit

  • New Features

    • Credits can be toggled off globally; when disabled, billing/ledger and customer balance behavior become no-op (zeroed balances, no ledger activity).
    • Added a lightweight fallback ledger and balance mode to ensure predictable behavior when credits are disabled.
  • Documentation

    • Clarified configuration guidance: ensure the credits setting is consistently applied across all components.

@GAlexIHU GAlexIHU added the release-note/misc Miscellaneous changes label Apr 2, 2026
@GAlexIHU
GAlexIHU requested a review from a team as a code owner April 2, 2026 14:55
@GAlexIHU
GAlexIHU enabled auto-merge (squash) April 2, 2026 14:56
@coderabbitai

coderabbitai Bot commented Apr 2, 2026 •

Copy link
Copy Markdown
Contributor

Caution

Review failed

Pull request was closed or merged during review

📝 Walkthrough

Walkthrough

Adds credits-gating across ledger and customer-balance components: when credits.enabled is false the system wires noop ledger/account/resolver/balance implementations; otherwise it constructs real ledger services. Refactors wiring to use small ledger abstractions and propagates the credits config through constructors.

Changes

Cohort / File(s) Summary
Documentation
AGENTS.md
Adds guidance clarifying credits.enabled must be guarded across ledger-backed customer credit handlers, customer ledger hooks, and namespace/default-account provisioning.
Ledger Abstraction & Wiring
app/common/ledger.go, cmd/billing-worker/wire_gen.go, cmd/jobs/internal/wire_gen.go, cmd/server/wire_gen.go
Introduce internal interfaces (ledgerReadWriter, customerLedgerProvisioner) and explicit constructors (NewLedgerService, NewLedgerQuerier, NewLedgerAccountResolver). Wire now branches to noop implementations when creditsConfig.Enabled is false; otherwise builds real ledger stack.
Customer Ledger Hook
app/common/customer.go
NewCustomerLedgerServiceHook signature updated to accept creditsConfig and a provisioner; returns a noop customer ledger hook when credits disabled, else registers real hook.
Customer Balance Service
app/common/customerbalance.go
NewCustomerBalanceService now accepts creditsConfig and returns a customerbalance.FacadeService; short-circuits to NewNoopService() when credits disabled. NewCustomerBalanceFacade updated to accept the FacadeService interface.
Balance Facade Abstraction
openmeter/ledger/customerbalance/facade.go
Adds exported FacadeService interface and changes Facade/NewFacade to depend on that interface rather than a concrete *Service.
No-op Implementations
openmeter/ledger/customerbalance/noop.go, openmeter/ledger/noop/noop.go
Adds a noop customer-balance service and a comprehensive noop ledger stack (ledger, querier, account resolver, account service, namespace handler) that return zero/empty results and deterministic placeholder accounts.

Sequence Diagram(s)

sequenceDiagram
    participant App as Application
    participant Config as CreditsConfiguration
    participant Wire as Wiring
    participant RW as LedgerReadWriter
    participant Querier as LedgerQuerier
    participant Provisioner as CustomerLedgerProvisioner
    participant Real as Real Ledger Stack
    participant Noop as No-op Ledger Stack
    participant Service as CustomerBalance / Hooks

    App->>Config: read credits.enabled
    App->>Wire: initialize components with creditsConfig

    alt credits.enabled == true
        Wire->>RW: NewLedgerHistoricalLedger(..., creditsConfig)
        RW->>Querier: NewLedgerQuerier(RW)
        RW->>Real: NewLedgerService(RW)
        Wire->>Provisioner: NewLedgerResolversService(creditsConfig,...)
        Provisioner->>Service: construct real balance service and ledger hooks
        Service-->>App: functional ledger + balance handlers
    else credits.enabled == false
        Wire->>Noop: return noop Ledger, noop AccountResolver
        Wire->>Service: NewNoopService() / NoopCustomerLedgerHook
        Service-->>App: no-op ledger + balance handlers
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~45 minutes

Possibly related PRs

Suggested labels

area/billing

Suggested reviewers

  • tothandras
  • turip
🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 13.64% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title 'chore: noop init' is extremely vague and doesn't clearly convey the main purpose of the changeset, which involves conditionally disabling credits-related initialization when a feature is turned off. Consider a more descriptive title like 'chore: add no-op initialization for disabled credits feature' to better communicate the actual change.
✅ Passed checks (1 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch chore/noop-ledger-init

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.

turip
turip previously approved these changes Apr 2, 2026

@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.

🧹 Nitpick comments (2)
openmeter/ledger/noop/noop.go (1)

208-228: Consider logging or adding a comment about silently swallowing errors.

When ledgeraccount.NewAccountFromData fails, you return an empty &ledgeraccount.Account{}. This is probably fine for a noop implementation, but it could make debugging tricky if someone is trying to figure out why things aren't working as expected.

Maybe a brief comment explaining this is intentional for the noop case would help future readers.

💡 Optional: Add a comment clarifying the error handling strategy
 func newAccount(namespace string, accountType ledger.AccountType, id string) *ledgeraccount.Account {
 	if accountType == "" {
 		accountType = ledger.AccountTypeCustomerAccrued
 	}

 	account, err := ledgeraccount.NewAccountFromData(ledgeraccount.AccountData{
 		ID: models.NamespacedID{
 			Namespace: normalizeNamespace(namespace),
 			ID:        normalizeID(id, "noop-account"),
 		},
 		AccountType: accountType,
 	}, ledgeraccount.AccountLiveServices{
 		Querier:           Ledger{},
 		SubAccountService: AccountService{},
 	})
 	if err != nil {
+		// Noop implementation: return empty account on construction error
+		// since this is only used when credits are disabled.
 		return &ledgeraccount.Account{}
 	}

 	return account
 }
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@openmeter/ledger/noop/noop.go` around lines 208 - 228, The newAccount
function currently swallows errors from ledgeraccount.NewAccountFromData and
returns an empty &ledgeraccount.Account{} which can confuse future debuggers;
update the newAccount function to either log the error or add a clear comment
explaining that swallowing the error and returning an empty account is
intentional for the noop implementation (reference newAccount,
ledgeraccount.NewAccountFromData, and the error return path), so maintainers
know this behavior is deliberate rather than a bug.
app/common/customerbalance.go (1)

50-52: Could we add a quick regression test for the disabled branch?

This is the main behavior change in the PR, and a focused test around NewCustomerBalanceService(... Enabled: false ...) would protect against future refactors accidentally constructing the DB-backed adapters before returning the noop service.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@app/common/customerbalance.go` around lines 50 - 52, Add a focused unit test
that constructs the creditsConfig with Enabled: false and calls
NewCustomerBalanceService to verify it returns the noop implementation
(customerbalance.NewNoopService) and does not instantiate any DB-backed
adapters; implement this by creating mocks/spies around the database/adapter
constructors used by NewCustomerBalanceService (or by injecting a fake factory)
and assert those constructors were not called, and assert the returned service
is the NoopService type or behaves like it.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@app/common/customerbalance.go`:
- Around line 50-52: Add a focused unit test that constructs the creditsConfig
with Enabled: false and calls NewCustomerBalanceService to verify it returns the
noop implementation (customerbalance.NewNoopService) and does not instantiate
any DB-backed adapters; implement this by creating mocks/spies around the
database/adapter constructors used by NewCustomerBalanceService (or by injecting
a fake factory) and assert those constructors were not called, and assert the
returned service is the NoopService type or behaves like it.

In `@openmeter/ledger/noop/noop.go`:
- Around line 208-228: The newAccount function currently swallows errors from
ledgeraccount.NewAccountFromData and returns an empty &ledgeraccount.Account{}
which can confuse future debuggers; update the newAccount function to either log
the error or add a clear comment explaining that swallowing the error and
returning an empty account is intentional for the noop implementation (reference
newAccount, ledgeraccount.NewAccountFromData, and the error return path), so
maintainers know this behavior is deliberate rather than a bug.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 7ae9d3d0-5797-4525-bfbb-fa808f21c922

📥 Commits

Reviewing files that changed from the base of the PR and between 9f763d9 and a72de50.

📒 Files selected for processing (10)
  • AGENTS.md
  • app/common/customer.go
  • app/common/customerbalance.go
  • app/common/ledger.go
  • cmd/billing-worker/wire_gen.go
  • cmd/jobs/internal/wire_gen.go
  • cmd/server/wire_gen.go
  • openmeter/ledger/customerbalance/facade.go
  • openmeter/ledger/customerbalance/noop.go
  • openmeter/ledger/noop/noop.go

@GAlexIHU
GAlexIHU merged commit 9bc4d0e into main Apr 2, 2026
22 of 23 checks passed
@GAlexIHU
GAlexIHU deleted the chore/noop-ledger-init branch April 2, 2026 16:02
@coderabbitai coderabbitai Bot mentioned this pull request Jul 9, 2026

This branch was previously deployed

1 inactive deployment
dev — 8b41a7fd Deployed Apr 2, 2026 by turip via Migration Checks #13174
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