Skip to content

Commit 767fbcb

Browse files
committed
rules: Add parameter controlling the max number of alerts rules can generate
Rule group parameter alert_limit controls the maximum number of alerts rules belonging to the group can generate. It's similar to the existing limit parameter, but doesn't affect recording rules. Signed-off-by: Arve Knudsen <[email protected]>
1 parent fa868fd commit 767fbcb

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

model/rulefmt/rulefmt.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ type RuleGroup struct {
139139
Interval model.Duration `yaml:"interval,omitempty"`
140140
EvaluationDelay *model.Duration `yaml:"evaluation_delay,omitempty"`
141141
Limit int `yaml:"limit,omitempty"`
142+
AlertLimit int `yaml:"alert_limit,omitempty"`
142143
Rules []RuleNode `yaml:"rules"`
143144
SourceTenants []string `yaml:"source_tenants,omitempty"`
144145
}

rules/manager.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ type Group struct {
249249
interval time.Duration
250250
evaluationDelay *time.Duration
251251
limit int
252+
alertLimit int
252253
rules []Rule
253254
sourceTenants []string
254255
seriesInPreviousEval []map[string]labels.Labels // One per Rule.
@@ -280,6 +281,7 @@ type GroupOptions struct {
280281
Name, File string
281282
Interval time.Duration
282283
Limit int
284+
AlertLimit int
283285
Rules []Rule
284286
SourceTenants []string
285287
ShouldRestore bool
@@ -313,6 +315,7 @@ func NewGroup(o GroupOptions) *Group {
313315
interval: o.Interval,
314316
evaluationDelay: o.EvaluationDelay,
315317
limit: o.Limit,
318+
alertLimit: o.AlertLimit,
316319
rules: o.Rules,
317320
shouldRestore: o.ShouldRestore,
318321
opts: o.Opts,
@@ -348,6 +351,9 @@ func (g *Group) Interval() time.Duration { return g.interval }
348351
// Limit returns the group's limit.
349352
func (g *Group) Limit() int { return g.limit }
350353

354+
// AlertLimit returns the group's alert limit.
355+
func (g *Group) AlertLimit() int { return g.alertLimit }
356+
351357
// SourceTenants returns the source tenants for the group.
352358
// If it's empty or nil, then the owning user/tenant is considered to be the source tenant.
353359
func (g *Group) SourceTenants() []string { return g.sourceTenants }
@@ -637,7 +643,13 @@ func (g *Group) Eval(ctx context.Context, ts time.Time) {
637643

638644
g.metrics.EvalTotal.WithLabelValues(GroupKey(g.File(), g.Name())).Inc()
639645

640-
vector, err := rule.Eval(ctx, evaluationDelay, ts, g.opts.QueryFunc, g.opts.ExternalURL, g.Limit())
646+
limit := g.Limit()
647+
if _, ok := rule.(*AlertingRule); ok {
648+
if g.AlertLimit() > 0 {
649+
limit = g.AlertLimit()
650+
}
651+
}
652+
vector, err := rule.Eval(ctx, evaluationDelay, ts, g.opts.QueryFunc, g.opts.ExternalURL, limit)
641653
if err != nil {
642654
rule.SetHealth(HealthBad)
643655
rule.SetLastError(err)
@@ -894,6 +906,10 @@ func (g *Group) Equals(ng *Group) bool {
894906
return false
895907
}
896908

909+
if g.alertLimit != ng.alertLimit {
910+
return false
911+
}
912+
897913
if len(g.rules) != len(ng.rules) {
898914
return false
899915
}
@@ -1170,6 +1186,7 @@ func (m *Manager) LoadGroups(
11701186
File: fn,
11711187
Interval: itv,
11721188
Limit: rg.Limit,
1189+
AlertLimit: rg.AlertLimit,
11731190
Rules: rules,
11741191
SourceTenants: rg.SourceTenants,
11751192
ShouldRestore: shouldRestore,

0 commit comments

Comments
 (0)