Repository navigation
feat: introduce feature gate into the subscription sync reconciler - #4445
Conversation
|
Note Reviews pausedIt 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 Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
📝 WalkthroughWalkthroughAdds 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. ChangesNamespace-scoped Prepaid Credits Routing
Sequence DiagramsequenceDiagram
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)
🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Suggested labels
Suggested reviewers
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
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 aNamespaceOrgimplementation 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.
There was a problem hiding this comment.
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
📒 Files selected for processing (3)
openmeter/billing/worker/subscriptionsync/service/reconciler/patch.goopenmeter/billing/worker/subscriptionsync/service/reconciler/patch_test.gopkg/featuregate/featuregate.go
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (1)
app/config/config.go (1)
230-230: ⚡ Quick winRemove duplicated
ConfigureCreditscall.
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
📒 Files selected for processing (7)
app/common/billing.goapp/config/config.goapp/config/credits.goopenmeter/billing/worker/subscriptionsync/service/reconciler/patch.goopenmeter/billing/worker/subscriptionsync/service/reconciler/reconciler.goopenmeter/billing/worker/subscriptionsync/service/service.gopkg/featuregate/featuregate.go
💤 Files with no reviewable changes (1)
- pkg/featuregate/featuregate.go
d7d815d to
cb9a127
Compare
Summary by CodeRabbit
New Features
Behavior
Configuration
Tests