Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: rules: Add parameter controlling the max number of alerts rules can generate #329

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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