Alerting: Add rule_limit parameter to the list rules API (#114055)

This commit is contained in:
Alexander Akhmetov
2025-11-18 15:56:40 +01:00
committed by GitHub
parent 66259bdede
commit 633c9a9cb0
6 changed files with 333 additions and 3 deletions
@@ -492,10 +492,22 @@ func PrepareRuleGroupStatusesV2(log log.Logger, store ListAlertRulesStoreV2, opt
ruleType = ngmodels.RuleTypeFilterAll
}
// Pagination limits
//
// group_limit: Maximum number of rule groups to return
// - Returns exactly this many groups or fewer if not enough exist
//
// rule_limit: Maximum number of rules to return across all groups
// - Returns complete groups until total rules meets or exceeds this limit
// - May exceed the rule limit if needed to include the final complete group
// - Example: rule_limit=15 with groups of [10, 10, 10] rules returns first 2 groups, 20 rules total
//
// When both limits are specified, whichever limit is reached first takes precedence.
maxGroups := getInt64WithDefault(opts.Query, "group_limit", -1)
maxRules := getInt64WithDefault(opts.Query, "rule_limit", -1)
nextToken := opts.Query.Get("group_next_token")
if maxGroups == 0 {
if maxGroups == 0 || maxRules == 0 {
return ruleResponse
}
@@ -512,6 +524,7 @@ func PrepareRuleGroupStatusesV2(log log.Logger, store ListAlertRulesStoreV2, opt
},
RuleType: ruleType,
Limit: maxGroups,
RuleLimit: maxRules,
ContinueToken: nextToken,
}
ruleList, continueToken, err := store.ListAlertRulesByGroup(opts.Ctx, &byGroupQuery)
@@ -557,7 +570,7 @@ func PrepareRuleGroupStatusesV2(log log.Logger, store ListAlertRulesStoreV2, opt
ruleResponse.Data.NextToken = continueToken
// Only return Totals if there is no pagination
if maxGroups == -1 {
if maxGroups == -1 && maxRules == -1 {
ruleResponse.Data.Totals = rulesTotals
}