Skip to content

feat: introduce feature gate into the subscription sync reconciler - #4445

Merged
borosr merged 6 commits into
mainfrom
feat/credits-feature-flag-consumer
May 27, 2026
Merged

borosr merged 6 commits into
mainfrom
feat/credits-feature-flag-consumer

Conversation

@borosr

@borosr borosr commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features

    • Namespace-level control for enabling/disabling prepaid credits; added a configurable credits flag option.
  • Behavior

    • Default collection routing now checks per-namespace prepaid credits and skips line-item collection when prepaid credits are not enabled.
    • Prepaid credits default to enabled when no feature-gate is present; feature-gate can explicitly disable them.
  • Configuration

    • New creditsFlag configuration key and updated credits defaults handling.
  • Tests

    • Added unit tests covering enabled, disabled, and missing feature-gate scenarios.

Review Change Stack

@borosr
borosr requested review from Copilot and turip May 26, 2026 14:35
@borosr borosr self-assigned this May 26, 2026
@borosr
borosr requested a review from a team as a code owner May 26, 2026 14:35
@borosr borosr added the release-note/ignore Ignore this change when generating release notes label May 26, 2026
@coderabbitai

coderabbitai Bot commented May 26, 2026 •

Copy link
Copy Markdown
Contributor

Note

Reviews paused

It looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the reviews.auto_review.auto_pause_after_reviewed_commits setting.

Use the following commands to manage reviews:

  • @coderabbitai resume to resume automatic reviews.
  • @coderabbitai review to trigger a single review.

Use the checkboxes below for quick actions:

  • ▶️ Resume reviews
  • 🔍 Trigger review
📝 Walkthrough

Walkthrough

Adds namespace-scoped feature-gate identity and a creditsFlag path through config and services; reconciler consults per-namespace prepaid-credits enablement and returns lineCollection when credits are disabled; includes unit tests for the new check.

Changes

Namespace-scoped Prepaid Credits Routing

Layer / File(s) Summary
Feature gate namespace identity support
pkg/featuregate/featuregate.go
Added exported NamespaceOrg type implementing Org with deterministic SHA1-based ID, namespace-derived Key/OrgName, and no-op custom attributes. Added github.com/samber/lo import.
Credits config and service wiring
app/config/credits.go, app/config/config.go, app/common/billing.go, openmeter/billing/worker/subscriptionsync/service/service.go, openmeter/billing/worker/subscriptionsync/service/reconciler/reconciler.go, tests
Added FeatureFlag/CreditsFlag to credits config and FeatureFlags, refactored ConfigureCredits to accept prefixes, updated SetViperDefaults, propagated CreditsFlag through NewBillingSubscriptionSyncService → reconciler Config/Service, and adjusted validations that previously required a non-nil FeatureGate. Updated tests to pass the new flag.
Credits-gated default collection routing
openmeter/billing/worker/subscriptionsync/service/reconciler/patch.go
Relaxed patchCollectionRouterConfig.Validate to allow nil featureGate; added creditsFlag to router state, implemented isCreditsEnabled(ns) that checks the prepaid-credits flag scoped to a namespace, and updated ResolveDefaultCollection to return lineCollection when credits are disabled for the subscription namespace.
Prepaid-credits enablement test coverage
openmeter/billing/worker/subscriptionsync/service/reconciler/patch_test.go, other tests
Updated existing router test to include creditsFlag and added TestIsCreditEnabled with three subtests covering enabled, nil featureGate, and disabled flag scenarios. Other test suites updated to pass a credits flag where needed.

Sequence Diagram

sequenceDiagram
  participant Reconciler
  participant PatchCollectionRouter
  participant FeatureGate
  Reconciler->>PatchCollectionRouter: ResolveDefaultCollection(ns, subscription)
  PatchCollectionRouter->>FeatureGate: WithOrg(NamespaceOrg(ns)) -> EvaluateBool(creditsFlag, false)
  FeatureGate-->>PatchCollectionRouter: FlagValue(enabled/disabled)
  PatchCollectionRouter-->>Reconciler: Decision (lineCollection or continue routing)
Loading

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

  • openmeterio/openmeter#4136: Overlapping changes to ResolveDefaultCollection routing logic based on prepaid-credits and feature-flag gating.

Suggested labels

release-note/feature, area/billing

Suggested reviewers

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

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: introducing feature gate support into the subscription sync reconciler to enable credits-based routing.
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.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/credits-feature-flag-consumer

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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR introduces a feature-gated decision point in the subscription sync reconciler to determine whether credit/charge-based reconciliation should be used (scoped by namespace), while also adding supporting feature-gate primitives and tests.

Changes:

  • Added a new feature flag constant (metering-prepaid-credits) and a NamespaceOrg implementation to evaluate flags using a namespace-derived org context.
  • Updated the subscription sync patch collection router to consult the feature gate when deciding whether credits/charges are enabled.
  • Added unit tests around the new credits-enabled resolution logic.

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.

File Description
pkg/featuregate/featuregate.go Adds the prepaid credits flag constant and a namespace-based Org implementation for feature-gate evaluation context.
openmeter/billing/worker/subscriptionsync/service/reconciler/patch.go Adds feature-gated credits enablement logic and wires it into default patch collection resolution.
openmeter/billing/worker/subscriptionsync/service/reconciler/patch_test.go Adds tests for the new credits enablement resolution behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@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: 3

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@openmeter/billing/worker/subscriptionsync/service/reconciler/patch_test.go`:
- Around line 158-170: The test "credit_flag_disabled" currently sets
creditsEnabled: false so router.isCreditsEnabled("test") returns early and never
exercises the feature-gate path; update the subtest to also instantiate the
router with creditsEnabled: true and pass a feature-gate stub (replace
featuregate.NewNoop() with a stub implementing EvaluateBool) that returns false
for FlagMeteringPrepaidCredits, then call router.isCreditsEnabled("test") and
assert it returns false; modify the patchCollectionRouterConfig in this subtest
(used by newPatchCollectionRouter and checked by isCreditsEnabled) to cover both
the early-disabled branch and the feature-gate-disabled branch.

In `@openmeter/billing/worker/subscriptionsync/service/reconciler/patch.go`:
- Around line 137-140: In ResolveDefaultCollection, don't abort reconciliation
when isCreditsEnabled(...) returns an error; instead treat the lookup failure as
a non-fatal condition and route to the safe default c.lineCollection. Replace
the current early-return of the error in
patchCollectionRouter.ResolveDefaultCollection (the isCreditsEnabled call) with
code that logs or records the transient error and then returns c.lineCollection,
nil so reconciliation continues even if isCreditsEnabled fails.
- Around line 123-135: The isCreditsEnabled call repeatedly evaluates the
feature flag and should memoize per-namespace decisions for the lifetime of the
router to avoid hot-path re-evaluation; add a cache field (e.g., map[string]bool
plus map[string]error or a struct value) and a sync.RWMutex to
patchCollectionRouter, compute the gate once inside isCreditsEnabled (respecting
existing short-circuits: c.creditsEnabled and c.featureGate == nil), store the
result (or error) in the cache on first evaluation, and return cached results on
subsequent calls; ensure thread-safety by reading/writing the cache under the
mutex and reference the methods/fields patchCollectionRouter.isCreditsEnabled,
patchCollectionRouter.featureGate, and patchCollectionRouter.creditsEnabled when
implementing.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 2554645a-6a05-43d7-a9bd-54f0815c0b44

📥 Commits

Reviewing files that changed from the base of the PR and between 9b02984 and 62ef4c0.

📒 Files selected for processing (3)
  • openmeter/billing/worker/subscriptionsync/service/reconciler/patch.go
  • openmeter/billing/worker/subscriptionsync/service/reconciler/patch_test.go
  • pkg/featuregate/featuregate.go

Comment thread pkg/featuregate/featuregate.go Resolved

@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: 1

🧹 Nitpick comments (1)
app/config/config.go (1)

230-230: ⚡ Quick win

Remove duplicated ConfigureCredits call.

ConfigureCredits(v, "credits") is already called at Line 221, so this second call is redundant and makes defaults wiring harder to reason about.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@app/config/config.go` at line 230, Remove the redundant call to
ConfigureCredits(v, "credits") so only the original invocation remains; locate
the duplicate invocation of ConfigureCredits (using the same variable v and key
"credits") and delete it to avoid reapplying defaults or clobbering earlier
configuration wiring while leaving the first ConfigureCredits(v, "credits") call
intact.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@app/config/credits.go`:
- Line 12: The config allows an empty CreditsFlag which defers failure to
reconciliation; update CreditsConfiguration.Validate() to fail fast by returning
an error when credits are enabled and CreditsFlag is empty. Inside
CreditsConfiguration.Validate(), check the enabled flag (e.g., c.Enabled /
whatever boolean controls credits) and if true and c.CreditsFlag == "" return a
descriptive error (use fmt.Errorf) so validation fails early.

---

Nitpick comments:
In `@app/config/config.go`:
- Line 230: Remove the redundant call to ConfigureCredits(v, "credits") so only
the original invocation remains; locate the duplicate invocation of
ConfigureCredits (using the same variable v and key "credits") and delete it to
avoid reapplying defaults or clobbering earlier configuration wiring while
leaving the first ConfigureCredits(v, "credits") call intact.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: fb1eea90-12cc-40d6-9031-b6f36bad9b45

📥 Commits

Reviewing files that changed from the base of the PR and between 8f3d6da and f009bf5.

📒 Files selected for processing (7)
  • app/common/billing.go
  • app/config/config.go
  • app/config/credits.go
  • openmeter/billing/worker/subscriptionsync/service/reconciler/patch.go
  • openmeter/billing/worker/subscriptionsync/service/reconciler/reconciler.go
  • openmeter/billing/worker/subscriptionsync/service/service.go
  • pkg/featuregate/featuregate.go
💤 Files with no reviewable changes (1)
  • pkg/featuregate/featuregate.go

Comment thread app/config/credits.go Resolved
Comment thread app/config/credits.go Resolved
Comment thread app/config/credits.go Resolved
Comment thread pkg/featuregate/featuregate.go Resolved
turip
turip previously approved these changes May 27, 2026
@borosr
borosr force-pushed the feat/credits-feature-flag-consumer branch from d7d815d to cb9a127 Compare May 27, 2026 11:41
@borosr
borosr enabled auto-merge (squash) May 27, 2026 11:41
@borosr
borosr merged commit 69c9ce9 into main May 27, 2026
25 of 26 checks passed
@borosr
borosr deleted the feat/credits-feature-flag-consumer branch May 27, 2026 11:49

This branch was previously deployed

1 inactive deployment
dev — cb9a1277 Deployed May 27, 2026 by borosr via Migration Checks #14684
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release-note/ignore Ignore this change when generating release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants