Skip to content

Commit 1f2df03

Browse files
authored
feat: add names to inhibit rules (#4628)
- add Name field to config.InhibitRule - add Name field to inhibit.InhibitRule - update docs for inhibit_rule The name will be used in new metrics to be added in a separate change. Signed-off-by: Siavash Safi <[email protected]>
1 parent 7adfff5 commit 1f2df03

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,8 @@ func (r *Route) UnmarshalYAML(unmarshal func(any) error) error {
955955
// target labels if an alert matching the source labels exists.
956956
// Both alerts have to have a set of labels being equal.
957957
type InhibitRule struct {
958+
// Name is an optional name for the inhibition rule.
959+
Name string `yaml:"name,omitempty" json:"name,omitempty"`
958960
// SourceMatch defines a set of labels that have to equal the given
959961
// value for source alerts. Deprecated. Remove before v1.0 release.
960962
SourceMatch map[string]string `yaml:"source_match,omitempty" json:"source_match,omitempty"`

docs/configuration.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,9 @@ source matchers in a way that alerts never match both sides. It is much easier
441441
to reason about and does not trigger this special case.
442442

443443
```yaml
444+
# Optional name of the inhibition rule.
445+
name: <string>
446+
444447
# DEPRECATED: Use target_matchers below.
445448
# Matchers that have to be fulfilled in the alerts to be muted.
446449
target_match:

inhibit/inhibit.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,8 @@ func (ih *Inhibitor) Mutes(lset model.LabelSet) bool {
153153
// from sending notifications if their meaning is logically a subset of a
154154
// higher-level alert.
155155
type InhibitRule struct {
156+
// Name is an optional name for the inhibition rule.
157+
Name string
156158
// The set of Filters which define the group of source alerts (which inhibit
157159
// the target alerts).
158160
SourceMatchers labels.Matchers
@@ -227,12 +229,14 @@ func NewInhibitRule(cr config.InhibitRule) *InhibitRule {
227229
}
228230

229231
rule := &InhibitRule{
232+
Name: cr.Name,
230233
SourceMatchers: sourcem,
231234
TargetMatchers: targetm,
232235
Equal: equal,
233236
scache: store.NewAlerts(),
234237
sindex: newIndex(),
235238
}
239+
236240
rule.scache.SetGCCallback(rule.gcCallback)
237241

238242
return rule

inhibit/inhibit_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/prometheus/client_golang/prometheus"
2121
"github.com/prometheus/common/model"
2222
"github.com/prometheus/common/promslog"
23+
"github.com/stretchr/testify/require"
2324

2425
"github.com/prometheus/alertmanager/config"
2526
"github.com/prometheus/alertmanager/pkg/labels"
@@ -345,6 +346,36 @@ func TestInhibitRuleMatchers(t *testing.T) {
345346
}
346347
}
347348

349+
func TestInhibitRuleName(t *testing.T) {
350+
t.Parallel()
351+
352+
config1 := config.InhibitRule{
353+
Name: "test-rule",
354+
SourceMatchers: []*labels.Matcher{
355+
{Type: labels.MatchEqual, Name: "severity", Value: "critical"},
356+
},
357+
TargetMatchers: []*labels.Matcher{
358+
{Type: labels.MatchEqual, Name: "severity", Value: "warning"},
359+
},
360+
Equal: []string{"instance"},
361+
}
362+
config2 := config.InhibitRule{
363+
SourceMatchers: []*labels.Matcher{
364+
{Type: labels.MatchEqual, Name: "severity", Value: "critical"},
365+
},
366+
TargetMatchers: []*labels.Matcher{
367+
{Type: labels.MatchEqual, Name: "severity", Value: "warning"},
368+
},
369+
Equal: []string{"instance"},
370+
}
371+
372+
rule1 := NewInhibitRule(config1)
373+
rule2 := NewInhibitRule(config2)
374+
375+
require.Equal(t, "test-rule", rule1.Name, "Expected named rule to have adopt name from config")
376+
require.Empty(t, rule2.Name, "Expected unnamed rule to have empty name")
377+
}
378+
348379
type fakeAlerts struct {
349380
alerts []*types.Alert
350381
finished chan struct{}

0 commit comments

Comments
 (0)