Skip to content

Commit

Permalink
rules: Add parameter controlling the max number of alerts rules can g…
Browse files Browse the repository at this point in the history
…enerate

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]>
  • Loading branch information
aknuds1 committed Dec 29, 2022
1 parent fa868fd commit 767fbcb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions model/rulefmt/rulefmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ type RuleGroup struct {
Interval model.Duration `yaml:"interval,omitempty"`
EvaluationDelay *model.Duration `yaml:"evaluation_delay,omitempty"`
Limit int `yaml:"limit,omitempty"`
AlertLimit int `yaml:"alert_limit,omitempty"`
Rules []RuleNode `yaml:"rules"`
SourceTenants []string `yaml:"source_tenants,omitempty"`
}
Expand Down
19 changes: 18 additions & 1 deletion rules/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ type Group struct {
interval time.Duration
evaluationDelay *time.Duration
limit int
alertLimit int
rules []Rule
sourceTenants []string
seriesInPreviousEval []map[string]labels.Labels // One per Rule.
Expand Down Expand Up @@ -280,6 +281,7 @@ type GroupOptions struct {
Name, File string
Interval time.Duration
Limit int
AlertLimit int
Rules []Rule
SourceTenants []string
ShouldRestore bool
Expand Down Expand Up @@ -313,6 +315,7 @@ func NewGroup(o GroupOptions) *Group {
interval: o.Interval,
evaluationDelay: o.EvaluationDelay,
limit: o.Limit,
alertLimit: o.AlertLimit,
rules: o.Rules,
shouldRestore: o.ShouldRestore,
opts: o.Opts,
Expand Down Expand Up @@ -348,6 +351,9 @@ func (g *Group) Interval() time.Duration { return g.interval }
// Limit returns the group's limit.
func (g *Group) Limit() int { return g.limit }

// AlertLimit returns the group's alert limit.
func (g *Group) AlertLimit() int { return g.alertLimit }

// SourceTenants returns the source tenants for the group.
// If it's empty or nil, then the owning user/tenant is considered to be the source tenant.
func (g *Group) SourceTenants() []string { return g.sourceTenants }
Expand Down Expand Up @@ -637,7 +643,13 @@ func (g *Group) Eval(ctx context.Context, ts time.Time) {

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

vector, err := rule.Eval(ctx, evaluationDelay, ts, g.opts.QueryFunc, g.opts.ExternalURL, g.Limit())
limit := g.Limit()
if _, ok := rule.(*AlertingRule); ok {
if g.AlertLimit() > 0 {
limit = g.AlertLimit()
}
}
vector, err := rule.Eval(ctx, evaluationDelay, ts, g.opts.QueryFunc, g.opts.ExternalURL, limit)
if err != nil {
rule.SetHealth(HealthBad)
rule.SetLastError(err)
Expand Down Expand Up @@ -894,6 +906,10 @@ func (g *Group) Equals(ng *Group) bool {
return false
}

if g.alertLimit != ng.alertLimit {
return false
}

if len(g.rules) != len(ng.rules) {
return false
}
Expand Down Expand Up @@ -1170,6 +1186,7 @@ func (m *Manager) LoadGroups(
File: fn,
Interval: itv,
Limit: rg.Limit,
AlertLimit: rg.AlertLimit,
Rules: rules,
SourceTenants: rg.SourceTenants,
ShouldRestore: shouldRestore,
Expand Down

0 comments on commit 767fbcb

Please sign in to comment.