Skip to content

feat: change internal feature & meter query filters type - #3462

Merged
tothandras merged 1 commit into
mainfrom
feat/api-features-v2
Oct 6, 2025
Merged

tothandras merged 1 commit into
mainfrom
feat/api-features-v2

Conversation

@tothandras

@tothandras tothandras commented Oct 5, 2025 •

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • New Features

    • Group-by filters now support richer structured specs (equality and multi-value "in") with automatic conversion from legacy inputs.
  • Improvements

    • Stronger validation and clearer error messages for invalid group-by filters.
    • Reduced allocations and improved performance when processing group-by filters.
    • Consistent conversion/serialization between legacy and new filter representations.
  • Tests

    • Updated tests to cover new filter semantics, conversions, SQL generation, and validation.

@tothandras
tothandras requested a review from a team as a code owner October 5, 2025 17:34
@coderabbitai

coderabbitai Bot commented Oct 5, 2025 •

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

MeterGroupByFilters/FilterGroupBy types replaced from simple string/slice maps to map[string]filter.FilterString across the codebase; helpers, validation, tests, HTTP mapping, entitlement/billing, and ClickHouse SQL generation were updated to use and validate FilterString and produce parameterized WHERE expressions.

Changes

Cohort / File(s) Summary
Core feature type & helpers
openmeter/productcatalog/feature/feature.go
MeterGroupByFilters changed from map[string]string → map[string]filter.FilterString; updated FeatureInvalidFiltersError.RequestedFilters; added ConvertMapStringToMeterGroupByFilters and ConvertMeterGroupByFiltersToMapString.
Streaming engine & params
openmeter/streaming/clickhouse/meter_query.go, openmeter/streaming/clickhouse/meter_query_test.go, openmeter/streaming/query_params.go
FilterGroupBy/QueryParams.FilterGroupBy now map[string]filter.FilterString; SQL JSON path escaping added; filters validated via FilterString.Validate() and turned into WHERE clauses via SelectWhereExpr; tests updated.
HTTP/API mapping & drivers
openmeter/meter/httphandler/mapping.go, openmeter/productcatalog/driver/feature.go, openmeter/productcatalog/driver/parser.go
Request/response parsing and driver handlers updated to convert and round-trip MeterGroupByFilters using the new helpers; response types adjusted to use api.Feature mapping.
Product catalog adapters & tests
openmeter/productcatalog/adapter/feature.go, openmeter/productcatalog/adapter/feature_test.go, openmeter/productcatalog/testutils/feature.go
Adapter uses conversion helpers and conditional conversion for non-empty filters; tests updated to construct filter.FilterString values (imports for filter and lo added).
Entitlement metered logic & tests
openmeter/entitlement/metered/grant_owner_adapter.go, openmeter/entitlement/metered/*.go (tests: balance_test.go, lateevents_test.go, reset_test.go)
Query params now receive FilterGroupBy directly from feature MeterGroupByFilters (no per-key slice wrapping); tests updated to filter.FilterString.
Billing service
openmeter/billing/service/lineservice/meters.go
Assigns meterQueryParams.FilterGroupBy directly from in.Feature.MeterGroupByFilters instead of building per-key []string slices.
Notification tests
test/notification/consumer_balance.go, test/notification/event.go, test/notification/rule.go
Tests now create features with feature.ConvertMapStringToMeterGroupByFilters(m.GroupBy) rather than direct m.GroupBy.
Misc tests & imports
openmeter/... (various tests)
Added github.com/openmeterio/openmeter/pkg/filter and github.com/samber/lo imports; updated fixtures to new filter shape.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Pre-merge checks and finishing touches

❌ 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%. You can run @coderabbitai generate docstrings to improve docstring coverage.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title succinctly highlights the core change of migrating the internal feature and meter query filters to a new type, follows conventional commit style, and captures the primary update without extraneous details.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch feat/api-features-v2

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.

@tothandras tothandras added the release-note/misc Miscellaneous changes label Oct 5, 2025

@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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
test/notification/consumer_balance.go (1)

135-141: Fix pointer aliasing in ConvertMapStringToMeterGroupByFilters.

feature.ConvertMapStringToMeterGroupByFilters currently builds each FilterString as {Eq: &v} where v is the range variable. Go reuses the same storage for v, so every entry returned by this helper ends up pointing to the last iterated value. The moment you call it here the resulting filters collapse to a single value, breaking all downstream filtering. Please copy the value before taking its address:

diff --git a/openmeter/productcatalog/feature/feature.go b/openmeter/productcatalog/feature/feature.go
@@
-	for k, v := range m {
-		result[k] = filter.FilterString{Eq: &v}
-	}
+	for k, v := range m {
+		value := v
+		result[k] = filter.FilterString{Eq: &value}
+	}
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 3f0f1af and 00a050e.

⛔ Files ignored due to path filters (2)
  • go.sum is excluded by !**/*.sum
  • tools/migrate/migrations/atlas.sum is excluded by !**/*.sum
📒 Files selected for processing (26)
  • openmeter/billing/service/lineservice/meters.go (1 hunks)
  • openmeter/ent/db/feature.go (2 hunks)
  • openmeter/ent/db/feature_create.go (5 hunks)
  • openmeter/ent/db/feature_update.go (3 hunks)
  • openmeter/ent/db/mutation.go (5 hunks)
  • openmeter/ent/db/setorclear.go (2 hunks)
  • openmeter/ent/schema/feature.go (2 hunks)
  • openmeter/entitlement/metered/balance_test.go (3 hunks)
  • openmeter/entitlement/metered/grant_owner_adapter.go (1 hunks)
  • openmeter/entitlement/metered/lateevents_test.go (2 hunks)
  • openmeter/entitlement/metered/reset_test.go (2 hunks)
  • openmeter/meter/httphandler/mapping.go (2 hunks)
  • openmeter/productcatalog/adapter/feature_test.go (5 hunks)
  • openmeter/productcatalog/driver/feature.go (1 hunks)
  • openmeter/productcatalog/driver/parser.go (1 hunks)
  • openmeter/productcatalog/feature/feature.go (4 hunks)
  • openmeter/productcatalog/testutils/feature.go (1 hunks)
  • openmeter/streaming/clickhouse/meter_query.go (3 hunks)
  • openmeter/streaming/clickhouse/meter_query_test.go (5 hunks)
  • openmeter/streaming/query_params.go (3 hunks)
  • test/notification/consumer_balance.go (3 hunks)
  • test/notification/event.go (1 hunks)
  • test/notification/rule.go (1 hunks)
  • tools/migrate/feature_meter_group_by_filters_test.go (1 hunks)
  • tools/migrate/migrations/20251005111411_feature-meter-group-by-filters.down.sql (1 hunks)
  • tools/migrate/migrations/20251005111411_feature-meter-group-by-filters.up.sql (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (19)
openmeter/entitlement/metered/grant_owner_adapter.go (1)
openmeter/productcatalog/feature/feature.go (1)
  • MeterGroupByFilters (67-67)
openmeter/billing/service/lineservice/meters.go (2)
openmeter/ent/db/feature.go (2)
  • Feature (18-46)
  • Feature (89-104)
openmeter/productcatalog/feature/feature.go (2)
  • Feature (120-143)
  • MeterGroupByFilters (67-67)
openmeter/ent/db/setorclear.go (2)
openmeter/ent/db/feature_update.go (2)
  • FeatureUpdate (23-27)
  • FeatureUpdateOne (471-476)
api/api.gen.go (1)
  • FilterString (3576-3618)
openmeter/streaming/clickhouse/meter_query.go (2)
api/api.gen.go (2)
  • FilterString (3576-3618)
  • Meter (4949-4998)
openmeter/meter/meter.go (1)
  • Meter (139-148)
openmeter/streaming/clickhouse/meter_query_test.go (2)
api/api.gen.go (1)
  • FilterString (3576-3618)
api/client/go/client.gen.go (1)
  • FilterString (3230-3272)
test/notification/rule.go (1)
openmeter/productcatalog/feature/feature.go (2)
  • MeterGroupByFilters (67-67)
  • ConvertMapStringToMeterGroupByFilters (87-98)
test/notification/consumer_balance.go (1)
openmeter/productcatalog/feature/feature.go (2)
  • MeterGroupByFilters (67-67)
  • ConvertMapStringToMeterGroupByFilters (87-98)
openmeter/streaming/query_params.go (2)
api/api.gen.go (1)
  • FilterString (3576-3618)
pkg/models/validator.go (1)
  • Validate (16-26)
openmeter/entitlement/metered/reset_test.go (2)
openmeter/productcatalog/feature/feature.go (1)
  • MeterGroupByFilters (67-67)
api/api.gen.go (1)
  • FilterString (3576-3618)
openmeter/productcatalog/adapter/feature_test.go (2)
openmeter/productcatalog/feature/feature.go (1)
  • MeterGroupByFilters (67-67)
api/api.gen.go (1)
  • FilterString (3576-3618)
openmeter/productcatalog/testutils/feature.go (1)
openmeter/productcatalog/feature/feature.go (2)
  • MeterGroupByFilters (67-67)
  • ConvertMapStringToMeterGroupByFilters (87-98)
openmeter/entitlement/metered/balance_test.go (2)
openmeter/productcatalog/feature/feature.go (1)
  • MeterGroupByFilters (67-67)
api/api.gen.go (1)
  • FilterString (3576-3618)
openmeter/ent/db/feature.go (1)
openmeter/productcatalog/feature/feature.go (1)
  • MeterGroupByFilters (67-67)
openmeter/productcatalog/driver/parser.go (3)
openmeter/ent/db/feature.go (2)
  • Feature (18-46)
  • Feature (89-104)
openmeter/productcatalog/feature/feature.go (3)
  • Feature (120-143)
  • ConvertMeterGroupByFiltersToMapString (102-116)
  • MeterGroupByFilters (67-67)
api/api.gen.go (2)
  • Feature (3432-3461)
  • Metadata (4946-4946)
openmeter/meter/httphandler/mapping.go (1)
api/api.gen.go (1)
  • FilterString (3576-3618)
test/notification/event.go (1)
openmeter/productcatalog/feature/feature.go (2)
  • MeterGroupByFilters (67-67)
  • ConvertMapStringToMeterGroupByFilters (87-98)
openmeter/ent/db/mutation.go (3)
api/api.gen.go (1)
  • FilterString (3576-3618)
api/client/go/client.gen.go (1)
  • FilterString (3230-3272)
openmeter/productcatalog/feature/feature.go (1)
  • MeterGroupByFilters (67-67)
openmeter/entitlement/metered/lateevents_test.go (2)
openmeter/productcatalog/feature/feature.go (1)
  • MeterGroupByFilters (67-67)
api/api.gen.go (1)
  • FilterString (3576-3618)
openmeter/productcatalog/driver/feature.go (3)
openmeter/productcatalog/feature/feature.go (2)
  • ConvertMapStringToMeterGroupByFilters (87-98)
  • MeterGroupByFilters (67-67)
openmeter/productcatalog/feature/connector.go (1)
  • CreateFeatureInputs (17-24)
openmeter/ent/db/feature/where.go (1)
  • MeterSlug (99-101)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (7)
  • GitHub Check: Artifacts / Container image
  • GitHub Check: Test
  • GitHub Check: Lint
  • GitHub Check: Migration Checks
  • GitHub Check: Code Generators
  • GitHub Check: Build
  • GitHub Check: Analyze (go)
🔇 Additional comments (13)
openmeter/streaming/query_params.go (3)

9-9: LGTM: Filter package import added.

The import supports the new filter.FilterString type used in FilterGroupBy.


19-19: LGTM: Type migration to FilterString.

The migration from map[string][]string to map[string]filter.FilterString provides a more structured and type-safe representation for group-by filters.


61-66: LGTM: Per-filter validation added.

The validation loop ensures that each FilterString in FilterGroupBy is validated individually, accumulating errors appropriately. This is a necessary addition to maintain data integrity with the new filter type.

openmeter/productcatalog/testutils/feature.go (1)

30-30: LGTM: Conversion helper applied correctly.

The use of ConvertMapStringToMeterGroupByFilters properly converts the legacy meter.GroupBy map to the new MeterGroupByFilters type.

openmeter/ent/db/feature.go (2)

14-14: LGTM: Filter import added to generated code.

The import supports the updated MeterGroupByFilters field type.


39-39: LGTM: Generated field type updated.

The MeterGroupByFilters field type has been updated to map[string]filter.FilterString in the generated code, consistent with the schema changes.

openmeter/billing/service/lineservice/meters.go (1)

63-65: LGTM: Simplified assignment pattern.

The direct assignment is cleaner and more efficient than the previous approach of iterating and building a new map. This is possible because Feature.MeterGroupByFilters is now already of the correct type (map[string]filter.FilterString).

test/notification/rule.go (1)

107-107: LGTM: Conversion helper applied correctly.

The use of ConvertMapStringToMeterGroupByFilters properly converts the legacy meter.GroupBy map to the new MeterGroupByFilters type, consistent with the broader migration pattern.

openmeter/entitlement/metered/grant_owner_adapter.go (1)

108-110: LGTM: Simplified assignment pattern.

The direct assignment is cleaner and more efficient than iterating and building a new map. This works because feature.MeterGroupByFilters is now already of type map[string]filter.FilterString.

openmeter/productcatalog/driver/feature.go (1)

104-111: LGTM: Clean conversion and assignment pattern.

The code properly converts the incoming MeterGroupByFilters from the request body using ConvertMapStringToMeterGroupByFilters, with appropriate nil handling via lo.FromPtrOr. Computing the value before struct construction improves readability.

test/notification/event.go (1)

200-200: LGTM: Conversion helper applied correctly.

The use of ConvertMapStringToMeterGroupByFilters properly converts the legacy m.GroupBy map to the new MeterGroupByFilters type, maintaining consistency with the migration pattern used across the codebase.

openmeter/ent/schema/feature.go (1)

32-32: Confirm zero-value JSON default works

field.JSON(..., map[string]filter.FilterString{}) uses the zero value of our new filter type. Please double-check Ent still serializes this as {} rather than null, otherwise existing rows w/out filters may fail validation.

openmeter/meter/httphandler/mapping.go (1)

164-176: Handle empty filter arrays explicitly

If the client passes FilterGroupBy[k]=[], we now drop the key entirely instead of translating it. Previously we kept []string{} which meant “no values ⇒ match nothing/ everything?”. Please ensure this behavior change is intentional and covered by tests. If empty slices should translate to $in: [], populate FilterString{In: lo.ToPtr([]string{})} instead.

@tothandras
tothandras force-pushed the feat/api-features-v2 branch 3 times, most recently from 99edf29 to d69c188 Compare October 5, 2025 19:06

@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 (1)
openmeter/productcatalog/driver/feature.go (1)

72-76: LGTM! Proper error handling and type conversion.

The handler correctly:

  • Fetches the feature from the connector
  • Returns early on error
  • Maps the internal type to the API response type

The dereference on line 76 is safe assuming GetFeature follows Go conventions (returning non-nil pointer when error is nil). For extra defensive programming, you could add a nil check, but it's not required given standard Go patterns.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d69c188 and 5188162.

📒 Files selected for processing (1)
  • openmeter/productcatalog/driver/feature.go (4 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
openmeter/productcatalog/driver/feature.go (5)
openmeter/productcatalog/feature/feature.go (3)
  • Feature (120-143)
  • ConvertMapStringToMeterGroupByFilters (87-98)
  • MeterGroupByFilters (67-67)
api/api.gen.go (2)
  • Feature (3432-3461)
  • Metadata (4946-4946)
openmeter/productcatalog/driver/parser.go (1)
  • MapFeatureToResponse (10-25)
pkg/convert/ptr.go (1)
  • DerefHeaderPtr (59-64)
pkg/framework/commonhttp/encoder.go (1)
  • JSONResponseEncoderWithStatus (65-69)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (7)
  • GitHub Check: E2E
  • GitHub Check: Quickstart
  • GitHub Check: Lint
  • GitHub Check: Test
  • GitHub Check: Code Generators
  • GitHub Check: Build
  • GitHub Check: Analyze (go)
🔇 Additional comments (4)
openmeter/productcatalog/driver/feature.go (4)

52-52: LGTM! Type change aligns with API separation.

The change from *feature.Feature to api.Feature properly separates internal domain types from API responses, improving the boundary between layers.


89-89: LGTM! Consistent type change.

The response type change to api.Feature maintains consistency with the GetFeature handler and properly separates API types from internal domain types.


108-115: LGTM! Proper conversion from API to internal types.

The implementation correctly:

  • Handles optional API field with lo.FromPtrOr providing a safe default
  • Converts from map[string]string to the new MeterGroupByFilters type
  • Aligns with the PR objective to migrate to map[string]filter.FilterString

The conversion logic properly handles nil/empty cases and maintains type safety.


119-126: LGTM! Consistent handler implementation.

The handler implementation correctly:

  • Creates the feature through the connector
  • Handles errors appropriately
  • Maps the internal type to the API response using MapFeatureToResponse
  • Uses explicit type parameter on line 126 for clarity

The pattern is consistent with GetFeature and properly separates internal domain logic from API responses.

Comment thread openmeter/meter/httphandler/mapping.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)
openmeter/productcatalog/driver/parser.go (1)

4-5: Inconsistent pointer utility usage.

The introduction of lo.ToPtr alongside the existing convert.MapToPointer creates inconsistency in how map pointers are handled across this function. Consider standardizing on one approach for consistency.

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 6de5ac4 and 94425e7.

📒 Files selected for processing (1)
  • openmeter/productcatalog/driver/parser.go (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
openmeter/productcatalog/driver/parser.go (3)
openmeter/productcatalog/feature/feature.go (3)
  • Feature (120-143)
  • ConvertMeterGroupByFiltersToMapString (102-116)
  • MeterGroupByFilters (67-67)
api/api.gen.go (2)
  • Feature (3432-3461)
  • Metadata (4946-4946)
pkg/convert/ptr.go (1)
  • MapToPointer (16-21)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (9)
  • GitHub Check: Artifacts / Benthos Collector Container image
  • GitHub Check: Migration Checks
  • GitHub Check: Artifacts / Container image
  • GitHub Check: Test
  • GitHub Check: Code Generators
  • GitHub Check: Lint
  • GitHub Check: Build
  • GitHub Check: Repository Scan
  • GitHub Check: Analyze (go)
🔇 Additional comments (2)
openmeter/productcatalog/driver/parser.go (2)

23-23: LGTM!

The use of convert.MapToPointer on the converted meterGroupByFilters correctly handles the case where the conversion returns nil or an empty map, ensuring the API field is omitted appropriately.


12-13: ```bash
#!/bin/bash

Find and inspect ConvertMeterGroupByFiltersToMapString implementation

rg -n "ConvertMeterGroupByFiltersToMapString" -C3


</blockquote></details>

</blockquote></details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->

@tothandras
tothandras force-pushed the feat/api-features-v2 branch from a77997d to f0cbefd Compare October 6, 2025 14:09
@tothandras tothandras changed the title feat: change internal feature filters type feat: change internal feature & meter query filters type Oct 6, 2025
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.

4 participants