mirror of
https://github.com/prometheus/alertmanager
synced 2024-12-25 15:42:18 +00:00
Filter empty matchers correctly
This commit is contained in:
parent
1413927c3f
commit
f5df55666b
@ -696,10 +696,16 @@ func matchFilterLabels(matchers []*labels.Matcher, sms map[string]string) bool {
|
||||
v, prs := sms[m.Name]
|
||||
switch m.Type {
|
||||
case labels.MatchNotEqual, labels.MatchNotRegexp:
|
||||
if string(v) == "" && prs {
|
||||
return true
|
||||
}
|
||||
if !m.Matches(string(v)) {
|
||||
return false
|
||||
}
|
||||
default:
|
||||
if string(v) == "" && !prs {
|
||||
return true
|
||||
}
|
||||
if !prs || !m.Matches(string(v)) {
|
||||
return false
|
||||
}
|
||||
|
@ -312,6 +312,38 @@ func TestReceiversMatchFilter(t *testing.T) {
|
||||
require.False(t, receiversMatchFilter(receivers, filter))
|
||||
}
|
||||
|
||||
func TestMatchFilterLabels(t *testing.T) {
|
||||
testCases := []struct {
|
||||
matcher labels.MatchType
|
||||
expected bool
|
||||
}{
|
||||
{labels.MatchEqual, true},
|
||||
{labels.MatchRegexp, true},
|
||||
{labels.MatchNotEqual, false},
|
||||
{labels.MatchNotRegexp, false},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
l, err := labels.NewMatcher(tc.matcher, "foo", "")
|
||||
require.NoError(t, err)
|
||||
sms := map[string]string{
|
||||
"baz": "bar",
|
||||
}
|
||||
ls := []*labels.Matcher{l}
|
||||
|
||||
require.Equal(t, tc.expected, matchFilterLabels(ls, sms))
|
||||
|
||||
l, err = labels.NewMatcher(tc.matcher, "foo", "")
|
||||
require.NoError(t, err)
|
||||
sms = map[string]string{
|
||||
"baz": "bar",
|
||||
"foo": "quux",
|
||||
}
|
||||
ls = []*labels.Matcher{l}
|
||||
require.NotEqual(t, tc.expected, matchFilterLabels(ls, sms))
|
||||
}
|
||||
}
|
||||
|
||||
func newMatcher(labelSet model.LabelSet) types.Matchers {
|
||||
matchers := make([]*types.Matcher, 0, len(labelSet))
|
||||
for key, val := range labelSet {
|
||||
|
@ -9,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
re = regexp.MustCompile(`(?:\s?)(\w+)(=|=~|!=|!~)(?:\"([^"=~!]+)\"|([^"=~!]+))`)
|
||||
re = regexp.MustCompile(`(?:\s?)(\w+)(=|=~|!=|!~)(?:\"([^"=~!]+)\"|([^"=~!]+)|\"\")`)
|
||||
typeMap = map[string]labels.MatchType{
|
||||
"=": labels.MatchEqual,
|
||||
"!=": labels.MatchNotEqual,
|
||||
@ -66,7 +66,7 @@ func Input(s string) (name, value string, matchType labels.MatchType, err error)
|
||||
value = ms[4]
|
||||
}
|
||||
|
||||
if name == "" || value == "" || !prs {
|
||||
if name == "" || !prs {
|
||||
return "", "", labels.MatchEqual, fmt.Errorf("failed to parse")
|
||||
}
|
||||
|
||||
|
@ -101,6 +101,14 @@ func TestMatchers(t *testing.T) {
|
||||
return append(ms, m, m2)
|
||||
}(),
|
||||
},
|
||||
{
|
||||
input: `{foo=""}`,
|
||||
want: func() []*labels.Matcher {
|
||||
ms := []*labels.Matcher{}
|
||||
m, _ := labels.NewMatcher(labels.MatchEqual, "foo", "")
|
||||
return append(ms, m)
|
||||
}(),
|
||||
},
|
||||
}
|
||||
|
||||
for i, tc := range testCases {
|
||||
|
Loading…
Reference in New Issue
Block a user