Skip to content

Commit 13854b6

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 4aa6d56 commit 13854b6

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
@@ -246,6 +246,7 @@ type Group struct {
246246
interval time.Duration
247247
evaluationDelay *time.Duration
248248
limit int
249+
alertLimit int
249250
rules []Rule
250251
sourceTenants []string
251252
seriesInPreviousEval []map[string]labels.Labels // One per Rule.
@@ -277,6 +278,7 @@ type GroupOptions struct {
277278
Name, File string
278279
Interval time.Duration
279280
Limit int
281+
AlertLimit int
280282
Rules []Rule
281283
SourceTenants []string
282284
ShouldRestore bool
@@ -310,6 +312,7 @@ func NewGroup(o GroupOptions) *Group {
310312
interval: o.Interval,
311313
evaluationDelay: o.EvaluationDelay,
312314
limit: o.Limit,
315+
alertLimit: o.AlertLimit,
313316
rules: o.Rules,
314317
shouldRestore: o.ShouldRestore,
315318
opts: o.Opts,
@@ -345,6 +348,9 @@ func (g *Group) Interval() time.Duration { return g.interval }
345348
// Limit returns the group's limit.
346349
func (g *Group) Limit() int { return g.limit }
347350

351+
// AlertLimit returns the group's alert limit.
352+
func (g *Group) AlertLimit() int { return g.alertLimit }
353+
348354
// SourceTenants returns the source tenants for the group.
349355
// If it's empty or nil, then the owning user/tenant is considered to be the source tenant.
350356
func (g *Group) SourceTenants() []string { return g.sourceTenants }
@@ -634,7 +640,13 @@ func (g *Group) Eval(ctx context.Context, ts time.Time) {
634640

635641
g.metrics.EvalTotal.WithLabelValues(GroupKey(g.File(), g.Name())).Inc()
636642

637-
vector, err := rule.Eval(ctx, evaluationDelay, ts, g.opts.QueryFunc, g.opts.ExternalURL, g.Limit())
643+
limit := g.Limit()
644+
if _, ok := rule.(*AlertingRule); ok {
645+
if g.AlertLimit() > 0 {
646+
limit = g.AlertLimit()
647+
}
648+
}
649+
vector, err := rule.Eval(ctx, evaluationDelay, ts, g.opts.QueryFunc, g.opts.ExternalURL, limit)
638650
if err != nil {
639651
rule.SetHealth(HealthBad)
640652
rule.SetLastError(err)
@@ -891,6 +903,10 @@ func (g *Group) Equals(ng *Group) bool {
891903
return false
892904
}
893905

906+
if g.alertLimit != ng.alertLimit {
907+
return false
908+
}
909+
894910
if len(g.rules) != len(ng.rules) {
895911
return false
896912
}
@@ -1163,6 +1179,7 @@ func (m *Manager) LoadGroups(
11631179
File: fn,
11641180
Interval: itv,
11651181
Limit: rg.Limit,
1182+
AlertLimit: rg.AlertLimit,
11661183
Rules: rules,
11671184
SourceTenants: rg.SourceTenants,
11681185
ShouldRestore: shouldRestore,

0 commit comments

Comments
 (0)