Skip to content

Commit ced7949

Browse files
authored
Merge pull request prometheus#4177 from grobinson-grafana/grobinson/fix-inhibit-rules-equal-utf8
Fix UTF-8 not allowed in Equal field for inhibition rules
2 parents 176486e + ee55078 commit ced7949

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

config/config.go

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,11 @@ type InhibitRule struct {
942942
TargetMatchers Matchers `yaml:"target_matchers,omitempty" json:"target_matchers,omitempty"`
943943
// A set of labels that must be equal between the source and target alert
944944
// for them to be a match.
945-
Equal model.LabelNames `yaml:"equal,omitempty" json:"equal,omitempty"`
945+
Equal model.LabelNames `yaml:"-" json:"-"`
946+
// EqualStr allows us to validate the label depending on whether UTF-8 is
947+
// enabled or disabled. It should be removed when Alertmanager is updated
948+
// to use the validation modes in recent versions of prometheus/common.
949+
EqualStr []string `yaml:"equal,omitempty" json:"equal,omitempty"`
946950
}
947951

948952
// UnmarshalYAML implements the yaml.Unmarshaler interface for InhibitRule.
@@ -964,6 +968,14 @@ func (r *InhibitRule) UnmarshalYAML(unmarshal func(interface{}) error) error {
964968
}
965969
}
966970

971+
for _, l := range r.EqualStr {
972+
labelName := model.LabelName(l)
973+
if !compat.IsValidLabelName(labelName) {
974+
return fmt.Errorf("invalid label name %q in equal list", l)
975+
}
976+
r.Equal = append(r.Equal, labelName)
977+
}
978+
967979
return nil
968980
}
969981

config/config_test.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ import (
2525

2626
commoncfg "github.com/prometheus/common/config"
2727
"github.com/prometheus/common/model"
28+
"github.com/prometheus/common/promslog"
2829
"github.com/stretchr/testify/require"
2930
"gopkg.in/yaml.v2"
31+
32+
"github.com/prometheus/alertmanager/featurecontrol"
33+
"github.com/prometheus/alertmanager/matcher/compat"
3034
)
3135

3236
func TestLoadEmptyString(t *testing.T) {
@@ -1387,3 +1391,45 @@ func TestNilRegexp(t *testing.T) {
13871391
})
13881392
}
13891393
}
1394+
1395+
func TestInhibitRuleEqual(t *testing.T) {
1396+
c, err := LoadFile("testdata/conf.inhibit-equal.yml")
1397+
require.NoError(t, err)
1398+
1399+
// The inhibition rule should have the expected equal labels.
1400+
require.Len(t, c.InhibitRules, 1)
1401+
r := c.InhibitRules[0]
1402+
require.Equal(t, model.LabelNames{"qux", "corge"}, r.Equal)
1403+
1404+
// Should not be able to load configuration with UTF-8 in equals list.
1405+
_, err = LoadFile("testdata/conf.inhibit-equal-utf8.yml")
1406+
require.Error(t, err)
1407+
require.Equal(t, "invalid label name \"qux🙂\" in equal list", err.Error())
1408+
1409+
// Change the mode to UTF-8 mode.
1410+
ff, err := featurecontrol.NewFlags(promslog.NewNopLogger(), featurecontrol.FeatureUTF8StrictMode)
1411+
require.NoError(t, err)
1412+
compat.InitFromFlags(promslog.NewNopLogger(), ff)
1413+
1414+
// Restore the mode to classic at the end of the test.
1415+
ff, err = featurecontrol.NewFlags(promslog.NewNopLogger(), featurecontrol.FeatureClassicMode)
1416+
require.NoError(t, err)
1417+
defer compat.InitFromFlags(promslog.NewNopLogger(), ff)
1418+
1419+
c, err = LoadFile("testdata/conf.inhibit-equal.yml")
1420+
require.NoError(t, err)
1421+
1422+
// The inhibition rule should have the expected equal labels.
1423+
require.Len(t, c.InhibitRules, 1)
1424+
r = c.InhibitRules[0]
1425+
require.Equal(t, model.LabelNames{"qux", "corge"}, r.Equal)
1426+
1427+
// Should also be able to load configuration with UTF-8 in equals list.
1428+
c, err = LoadFile("testdata/conf.inhibit-equal-utf8.yml")
1429+
require.NoError(t, err)
1430+
1431+
// The inhibition rule should have the expected equal labels.
1432+
require.Len(t, c.InhibitRules, 1)
1433+
r = c.InhibitRules[0]
1434+
require.Equal(t, model.LabelNames{"qux🙂", "corge"}, r.Equal)
1435+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
route:
2+
receiver: test
3+
receivers:
4+
- name: test
5+
inhibit_rules:
6+
- source_matchers:
7+
- foo=bar
8+
target_matchers:
9+
- bar=baz
10+
equal: ['qux🙂', 'corge']
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
route:
2+
receiver: test
3+
receivers:
4+
- name: test
5+
inhibit_rules:
6+
- source_matchers:
7+
- foo=bar
8+
target_matchers:
9+
- bar=baz
10+
equal: ['qux', 'corge']

0 commit comments

Comments
 (0)