Skip to content

Enable unit_config authoring on BillingRateCard behind config flag - OM-392 - #4560

Merged
rolosp merged 2 commits into
mainfrom
feat/om-392-unit-config-switch
Jun 24, 2026
Merged

rolosp merged 2 commits into
mainfrom
feat/om-392-unit-config-switch

Conversation

@rolosp

@rolosp rolosp commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

Overview

This PR is the ground work for enabling unit_config on BillingRateCard for authoring. With this in place we don't need to ship it in one go, but in smaller PRs.

Summary by CodeRabbit

  • New Features
    • Added a UnitConfig feature flag to enable/disable unit configuration support per deployment.
    • Rate card unit_config can now be provided on create and update when the feature flag is enabled.
  • Documentation
    • Updated rate card unit_config documentation to reflect feature-gated create/update behavior.
    • Added unitConfig.enabled to the example configuration.
  • Tests
    • Added an end-to-end test asserting plan creation is rejected (400) when unit_config is sent while the flag is disabled.

@rolosp
rolosp requested a review from a team as a code owner June 23, 2026 16:21
@coderabbitai

coderabbitai Bot commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 29bb8545-95cc-476a-b246-8549042e32a8

📥 Commits

Reviewing files that changed from the base of the PR and between cbda60c and 11743ab.

📒 Files selected for processing (1)
  • app/config/unit_config.go

📝 Walkthrough

Walkthrough

Adds a UnitConfig feature flag (unitConfig.enabled, default false) to app config, threads it through server wiring into the v3 plans handler, and gates unit_config acceptance on plan create/update. The API spec and generated client docs are updated to match.

Changes

UnitConfig Feature Flag Gate

Layer / File(s) Summary
UnitConfig config and defaults
app/config/unit_config.go, app/config/config.go, app/config/config_test.go, config.example.yaml
Defines UnitConfigConfiguration, wires it into config validation and Viper defaults, and updates the example and expected config shape.
Server wiring for UnitConfig
openmeter/server/router/router.go, cmd/server/main.go, openmeter/server/server.go, api/v3/server/server.go
Threads UnitConfig from startup config into the v3 server and plans handler constructor.
Plans handler flag gate
api/v3/handlers/plans/handler.go, api/v3/handlers/plans/create.go, api/v3/handlers/plans/update.go, api/v3/handlers/plans/convert.go, api/v3/handlers/plans/convert_test.go, e2e/plans_v3_test.go
Adds unitConfigEnabled to the plans handler, rejects UnitConfig in create/update when disabled, removes the prior conversion-time rejection, and updates tests.
API spec and generated docs
api/spec/packages/aip/src/productcatalog/ratecard.tsp, api/spec/packages/aip-client-javascript/src/models/types.ts
Expands unit_config lifecycle visibility and updates doc comments to the new conditional acceptance wording.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Suggested labels

release-note/feature, area/billing, kind/feature

Suggested reviewers

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

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 22.22% 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 summarizes the main change: enabling BillingRateCard unit_config authoring behind a config flag.
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 docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/om-392-unit-config-switch

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.

@rolosp rolosp added the release-note/misc Miscellaneous changes label Jun 23, 2026
@greptile-apps

greptile-apps Bot commented Jun 23, 2026 •

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR adds infrastructure for feature-gating unit_config authoring on BillingRateCard behind a new unitConfig.enabled config flag (defaulting to false), replacing the blanket rejection in FromAPIBillingRateCard with per-handler checks in the create and update paths.

  • Adds UnitConfigConfiguration to app config with viper wiring, plumbs it through router.Config → api/v3/server.Config → plans.handler, and exposes it in config.example.yaml.
  • Removes readOnly: true from the unit_config OpenAPI field and updates docs/TypeSpec to reflect the flag-gated accept-on-write behaviour.
  • Adds an e2e test verifying the rejection when the flag is disabled; also removes a pre-existing duplicate ConfigureCredits call from SetViperDefaults.

Confidence Score: 4/5

Safe to merge with the flag kept at its default of false; enabling the flag before the domain-model persistence work lands will cause silent data loss.

The flag gate is correctly wired end-to-end and the default is off, so no existing production behaviour changes. However, FromAPIBillingRateCard does not map rc.UnitConfig to any field in the domain model — the productcatalog package has no UnitConfig field — so any environment that enables the flag before the persistence implementation lands will accept unit_config writes and return 200 OK while silently discarding the value.

api/v3/handlers/plans/convert.go — the unit_config field from the request is accepted when the flag is enabled but never forwarded to the domain model.

Important Files Changed

Filename Overview
api/v3/handlers/plans/convert.go Removes the blanket unit_config rejection from FromAPIBillingRateCard, but does not add any mapping of rc.UnitConfig to the domain model — the field is silently dropped on the write path even when the flag is enabled.
api/v3/handlers/plans/create.go Adds unitConfig flag gate before namespace resolution; gate iterates all phases/rate cards and returns a validation error when unit_config is present and the flag is disabled.
api/v3/handlers/plans/update.go Identical flag gate added for update path; mirrors create.go behavior correctly.
api/v3/handlers/plans/handler.go Adds unitConfigEnabled bool to handler struct and constructor; cleanly plumbed from server config.
app/config/unit_config.go New UnitConfigConfiguration with a single Enabled bool; Validate() is a no-op stub; ConfigureUnitConfig sets the viper default to false.
app/config/config.go Adds UnitConfig field to Configuration, wires Validate(), and removes a duplicate ConfigureCredits call that pre-existed the PR.
api/v3/openapi.yaml Removes readOnly: true from unit_config and updates the description to reflect the feature-flag-gated behaviour.
e2e/plans_v3_test.go Adds TestV3PlanRejectsUnitConfigWhenFlagDisabled covering the create path; update path coverage intentionally deferred per team preference for unit tests.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[HTTP Request\ncreate/update plan] --> B{unitConfigEnabled?}
    B -- false --> C{any rc.UnitConfig != nil?}
    C -- yes --> D[Return 400\nValidationError]
    C -- no --> E[FromAPIUpsertPlanRequest]
    B -- true --> E
    E --> F[FromAPIBillingRateCard\nper rate card]
    F --> G{price type}
    G -- flat/free --> H[FlatFeeRateCard\nno UnitConfig field]
    G -- unit/graduated/volume --> I[UsageBasedRateCard\nno UnitConfig field]
    H --> J[⚠️ rc.UnitConfig silently dropped]
    I --> J
    J --> K[plan.Service.CreatePlan / UpdatePlan]
    K --> L[200 OK]
Loading
%%{init: {'theme': 'base', 'themeVariables': {"darkMode": true, "background": "#0d1117", "primaryColor": "#21262d", "primaryTextColor": "#e6edf3", "primaryBorderColor": "#8b949e", "lineColor": "#8b949e", "textColor": "#e6edf3", "edgeLabelBackground": "#161b22", "actorBkg": "#21262d", "actorBorder": "#8b949e", "actorTextColor": "#e6edf3", "actorLineColor": "#8b949e", "signalColor": "#8b949e", "signalTextColor": "#e6edf3", "noteBkgColor": "#373320", "noteBorderColor": "#d4a72c", "noteTextColor": "#f0e6c0", "labelBoxBkgColor": "#21262d", "labelBoxBorderColor": "#8b949e", "labelTextColor": "#e6edf3", "loopTextColor": "#e6edf3", "activationBkgColor": "#30363d", "activationBorderColor": "#8b949e"}}}%%
flowchart TD
    A[HTTP Request\ncreate/update plan] --> B{unitConfigEnabled?}
    B -- false --> C{any rc.UnitConfig != nil?}
    C -- yes --> D[Return 400\nValidationError]
    C -- no --> E[FromAPIUpsertPlanRequest]
    B -- true --> E
    E --> F[FromAPIBillingRateCard\nper rate card]
    F --> G{price type}
    G -- flat/free --> H[FlatFeeRateCard\nno UnitConfig field]
    G -- unit/graduated/volume --> I[UsageBasedRateCard\nno UnitConfig field]
    H --> J[⚠️ rc.UnitConfig silently dropped]
    I --> J
    J --> K[plan.Service.CreatePlan / UpdatePlan]
    K --> L[200 OK]
Loading

Comments Outside Diff (1)

  1. api/v3/handlers/plans/convert.go, line 603-694 (link)

    P1 UnitConfig silently discarded when flag is enabled

    The rejection guard was moved from FromAPIBillingRateCard to the HTTP handler, but FromAPIBillingRateCard never maps rc.UnitConfig to any field on the returned productcatalog.UsageBasedRateCard or productcatalog.FlatFeeRateCard. The domain model (RateCardMeta, UsageBasedRateCard) has no UnitConfig field (confirmed by searching openmeter/productcatalog/). When unitConfigEnabled = true, a client that sends unit_config receives a 200 OK response but the value is silently dropped — it will not be persisted or appear on subsequent reads. Any environment that enables the flag before the persistence implementation lands will produce silent data loss.

    Fix in Claude Code

Fix All in Claude Code

Reviews (2): Last reviewed commit: "chore: review fix for config Validate" | Re-trigger Greptile

Comment thread app/config/unit_config.go Resolved
Comment thread e2e/plans_v3_test.go
Comment on lines 604 to +626
})
}

// TestV3PlanRejectsUnitConfigWhenFlagDisabled verifies that, while the UnitConfig
// feature flag is off (the default in the e2e environment), authoring a plan whose
// rate card carries a unit_config is rejected on create. unit_config stays
// read-only — synthesized from v1 dynamic/package prices on the response path only
// — until the flag is enabled per environment. The reject guard fires before price
// validation, so a flat rate card is enough to exercise it (no feature needed).
func TestV3PlanRejectsUnitConfigWhenFlagDisabled(t *testing.T) {
c := newV3Client(t)

body := validPlanRequest("unit_config_rejected")
body.Phases[0].RateCards[0].UnitConfig = &apiv3.BillingUnitConfig{
Operation: apiv3.BillingUnitConfigOperationMultiply,
ConversionFactor: apiv3.Numeric("1.2"),
}

status, plan, problem := c.CreatePlan(body)
require.Equal(t, http.StatusBadRequest, status, "problem: %+v", problem)
require.Nil(t, plan)
assertProblemDetail(t, problem, "unit_config")
}

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.

P2 E2E gate test covers create only; update path is untested

TestV3PlanRejectsUnitConfigWhenFlagDisabled verifies that the flag gate fires on create. The update handler (update.go) contains an identical gate, but there is no corresponding test that exercises the update path with unit_config when the flag is disabled. A plan can be created first (without unit_config) and then updated with unit_config in its rate card — if the gate in UpdatePlan regresses, only the create test would pass.

Fix in Claude Code

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That's fine. Prefer unit tests (integration level service test) over e2e tests as much as possible.

@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 (1)
e2e/plans_v3_test.go (1)

606-626: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Nice test coverage for the CREATE rejection path!

The test correctly validates that unit_config is rejected when the flag is disabled. Since the handler comment (lines 32 and 35 in create.go/update.go) explicitly states the guard "is applied for both create and update," consider adding a parallel test case for the UPDATE path to ensure both endpoints remain consistently guarded.

Something like:

t.Run("rejects on update", func(t *testing.T) {
    // Create a plan without unit_config first
    // Then attempt to update it with unit_config set
    // Assert same 400 + "unit_config" rejection
})
🤖 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 `@e2e/plans_v3_test.go` around lines 606 - 626, The test
TestV3PlanRejectsUnitConfigWhenFlagDisabled currently only covers the CREATE
path for rejecting unit_config when the flag is disabled, but the handler guard
is applied to both create and update operations. Add a parallel test case
(either as a nested t.Run subtest or as a separate test function) that follows
the same pattern: first create a valid plan without unit_config using
validPlanRequest, then attempt to update that plan by adding unit_config to its
rate card, and assert that the update call (via c.UpdatePlan or similar) returns
http.StatusBadRequest with a problem detail containing "unit_config". This
ensures both the CreatePlan and UpdatePlan endpoints are consistently guarded.
🤖 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.

Nitpick comments:
In `@e2e/plans_v3_test.go`:
- Around line 606-626: The test TestV3PlanRejectsUnitConfigWhenFlagDisabled
currently only covers the CREATE path for rejecting unit_config when the flag is
disabled, but the handler guard is applied to both create and update operations.
Add a parallel test case (either as a nested t.Run subtest or as a separate test
function) that follows the same pattern: first create a valid plan without
unit_config using validPlanRequest, then attempt to update that plan by adding
unit_config to its rate card, and assert that the update call (via c.UpdatePlan
or similar) returns http.StatusBadRequest with a problem detail containing
"unit_config". This ensures both the CreatePlan and UpdatePlan endpoints are
consistently guarded.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: c392ab57-2afe-4a44-88da-f892af220411

📥 Commits

Reviewing files that changed from the base of the PR and between facf3b0 and cbda60c.

⛔ Files ignored due to path filters (1)
  • api/v3/openapi.yaml is excluded by !**/openapi.yaml
📒 Files selected for processing (17)
  • api/spec/packages/aip-client-javascript/src/models/types.ts
  • api/spec/packages/aip/src/productcatalog/ratecard.tsp
  • api/v3/api.gen.go
  • api/v3/handlers/plans/convert.go
  • api/v3/handlers/plans/convert_test.go
  • api/v3/handlers/plans/create.go
  • api/v3/handlers/plans/handler.go
  • api/v3/handlers/plans/update.go
  • api/v3/server/server.go
  • app/config/config.go
  • app/config/config_test.go
  • app/config/unit_config.go
  • cmd/server/main.go
  • config.example.yaml
  • e2e/plans_v3_test.go
  • openmeter/server/router/router.go
  • openmeter/server/server.go
💤 Files with no reviewable changes (2)
  • api/v3/handlers/plans/convert.go
  • api/v3/handlers/plans/convert_test.go

turip
turip previously approved these changes Jun 23, 2026
Comment thread app/config/unit_config.go
}

func ConfigureUnitConfig(v *viper.Viper, prefixes ...string) {
prefixer := NewViperKeyPrefixer(prefixes...)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

prefixer is an overkill here

Comment thread e2e/plans_v3_test.go
Comment on lines 604 to +626
})
}

// TestV3PlanRejectsUnitConfigWhenFlagDisabled verifies that, while the UnitConfig
// feature flag is off (the default in the e2e environment), authoring a plan whose
// rate card carries a unit_config is rejected on create. unit_config stays
// read-only — synthesized from v1 dynamic/package prices on the response path only
// — until the flag is enabled per environment. The reject guard fires before price
// validation, so a flat rate card is enough to exercise it (no feature needed).
func TestV3PlanRejectsUnitConfigWhenFlagDisabled(t *testing.T) {
c := newV3Client(t)

body := validPlanRequest("unit_config_rejected")
body.Phases[0].RateCards[0].UnitConfig = &apiv3.BillingUnitConfig{
Operation: apiv3.BillingUnitConfigOperationMultiply,
ConversionFactor: apiv3.Numeric("1.2"),
}

status, plan, problem := c.CreatePlan(body)
require.Equal(t, http.StatusBadRequest, status, "problem: %+v", problem)
require.Nil(t, plan)
assertProblemDetail(t, problem, "unit_config")
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That's fine. Prefer unit tests (integration level service test) over e2e tests as much as possible.

Comment thread app/config/unit_config.go Outdated
func (c UnitConfigConfiguration) Validate() error {
var errs []error

return errors.Join(errs...)

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.

Please, just return nil here.

@rolosp
rolosp merged commit a1c4a50 into main Jun 24, 2026
27 of 28 checks passed
@rolosp
rolosp deleted the feat/om-392-unit-config-switch branch June 24, 2026 08:08
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.

3 participants