Skip to content

fix: ent reset list logic - #4406

Merged
GAlexIHU merged 2 commits into
mainfrom
fix/entitlement-log-storm
May 21, 2026
Merged

GAlexIHU merged 2 commits into
mainfrom
fix/entitlement-log-storm

Conversation

@GAlexIHU

@GAlexIHU GAlexIHU commented May 21, 2026 •

Copy link
Copy Markdown
Contributor

Overview

TLDR we were returning more than we should have hence the log storm (the relevant change is only the extra filter, the TS comparison changes are just for semantic correctness)

Fixes #(issue)

Notes for reviewer

Summary by CodeRabbit

  • Bug Fixes
    • Tightened entitlement filtering so expired usage periods are identified more accurately: activity is evaluated at the current time, results are limited to metered entitlements, current usage period end must be set and ≤ highwatermark, deletion is considered relative to now, and eligibility requires a measure anchor or a usage-reset.
  • Tests
    • Updated tests to cover resettable metered entitlements, set the clock explicitly, and validate inclusion of reset-anchored items.

Review Change Stack

@GAlexIHU GAlexIHU added the release-note/bug-fix Release note: Bug Fixes label May 21, 2026
@GAlexIHU
GAlexIHU requested a review from a team as a code owner May 21, 2026 09:33
@coderabbitai

coderabbitai Bot commented May 21, 2026 •

Copy link
Copy Markdown
Contributor

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: 4b4693ee-e1a5-4375-a03c-7fc286206b64

📥 Commits

Reviewing files that changed from the base of the PR and between c2c6962 and 1fd6dad.

📒 Files selected for processing (1)
  • openmeter/entitlement/adapter/entitlement.go

📝 Walkthrough

Walkthrough

ListActiveEntitlementsWithExpiredUsagePeriod narrows its filters: it evaluates entitlement activity at now, limits results to Metered entitlements, requires a non-nil CurrentUsagePeriodEnd ≤ params.Highwatermark, applies deleted-at checks relative to now, and adds a reset-eligibility predicate (either MeasureUsageFrom is set or HasUsageReset() is true). Tests add a usageResetRepo, persist a reset-anchored metered entitlement, and assert two resettable metered entitlements are returned.

Changes

Resettable Entitlement Query Filtering

Layer / File(s) Summary
Query predicate changes
openmeter/entitlement/adapter/entitlement.go
ListActiveEntitlementsWithExpiredUsagePeriod now uses EntitlementActiveAt(now), restricts to Metered entitlements, requires CurrentUsagePeriodEnd non-nil and ≤ params.Highwatermark, compares deleted-at to now, and requires either MeasureUsageFrom or a recorded usage reset (HasUsageReset()).
Test updates for resettable entitlements
openmeter/entitlement/adapter/entitlement_test.go
Test deps include usageResetRepo initialized in setup(). TestListActiveEntitlementsWithExpiredUsagePeriod creates metered/static/boolean/malformed fixtures, saves a usage-reset anchor for a metered entitlement via usageResetRepo.Save, advances clock, and expects two resettable metered entitlements returned.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Suggested labels

area/entitlements

Suggested reviewers

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

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 20.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 'fix: ent reset list logic' directly addresses the main change—fixing entitlement reset list logic that caused a log storm—and aligns well with the PR objectives and code modifications.
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 fix/entitlement-log-storm

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.

@GAlexIHU
GAlexIHU enabled auto-merge (squash) May 21, 2026 09:33

@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)
openmeter/entitlement/adapter/entitlement_test.go (1)

350-467: ⚡ Quick win

Add a direct assertion for CurrentUsagePeriodEndNotNil behavior.

Nice coverage overall. One small gap: the test doesn’t explicitly prove that metered entitlements with CurrentUsagePeriod: nil are excluded. Adding that fixture would lock in the new predicate and prevent regressions.

Suggested test addition
+		nilCurrentPeriodEnt, err := repo.entRepo.CreateEntitlement(ctx, entitlement.CreateEntitlementRepoInputs{
+			Namespace:          ns,
+			FeatureID:          feature.ID,
+			FeatureKey:         featureKey,
+			UsageAttribution:   createCustomerWithSubject(t, repo.subjectRepo, repo.customerRepo, ns, "subject-nil-current").GetUsageAttribution(),
+			EntitlementType:    entitlement.EntitlementTypeMetered,
+			MeasureUsageFrom:   lo.ToPtr(time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC)),
+			UsagePeriod:        usagePeriod,
+			CurrentUsagePeriod: nil,
+		})
+		require.NoError(t, err)
+		require.NotNil(t, nilCurrentPeriodEnt)
...
 		require.Len(t, ents, 2)
 		require.Equal(t, ent1.ID, ents[0].ID)
 		require.Equal(t, resetAnchorEnt.ID, ents[1].ID)
+		require.NotContains(t, lo.Map(ents, func(e entitlement.Entitlement, _ int) string { return e.ID }), nilCurrentPeriodEnt.ID)

As per coding guidelines: **/*_test.go: Make sure the tests are comprehensive and cover the changes.

🤖 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 `@openmeter/entitlement/adapter/entitlement_test.go` around lines 350 - 467,
The test is missing an explicit fixture proving entitlements with
CurrentUsagePeriod == nil are excluded by the CurrentUsagePeriodEndNotNil
predicate; add a metered entitlement (use repo.entRepo.CreateEntitlement with
EntitlementTypeMetered and without CurrentUsagePeriod set) for a new customer
(e.g., custNoPeriod) before calling
ListActiveEntitlementsWithExpiredUsagePeriod, then assert the returned ents
slice length and contents do NOT include that entitlement (and keep the existing
assertions for ent1 and resetAnchorEnt); reference CreateEntitlement,
ListActiveEntitlementsWithExpiredUsagePeriod, and the CurrentUsagePeriod field
when adding the assertion.
🤖 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 `@openmeter/entitlement/adapter/entitlement_test.go`:
- Around line 350-467: The test is missing an explicit fixture proving
entitlements with CurrentUsagePeriod == nil are excluded by the
CurrentUsagePeriodEndNotNil predicate; add a metered entitlement (use
repo.entRepo.CreateEntitlement with EntitlementTypeMetered and without
CurrentUsagePeriod set) for a new customer (e.g., custNoPeriod) before calling
ListActiveEntitlementsWithExpiredUsagePeriod, then assert the returned ents
slice length and contents do NOT include that entitlement (and keep the existing
assertions for ent1 and resetAnchorEnt); reference CreateEntitlement,
ListActiveEntitlementsWithExpiredUsagePeriod, and the CurrentUsagePeriod field
when adding the assertion.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4a7888bb-1aab-47be-847f-461fb2fdb0d7

📥 Commits

Reviewing files that changed from the base of the PR and between 470f7dd and c2c6962.

📒 Files selected for processing (2)
  • openmeter/entitlement/adapter/entitlement.go
  • openmeter/entitlement/adapter/entitlement_test.go

@GAlexIHU
GAlexIHU merged commit 28dd7d9 into main May 21, 2026
28 checks passed
@GAlexIHU
GAlexIHU deleted the fix/entitlement-log-storm branch May 21, 2026 09:45

This branch was previously deployed

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

Labels

release-note/bug-fix Release note: Bug Fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants