Filter empty matchers correctly

This commit is contained in:
Stuart Nelson 2018-03-20 10:08:20 +01:00
parent 1413927c3f
commit f5df55666b
4 changed files with 48 additions and 2 deletions

View File

@ -696,10 +696,16 @@ func matchFilterLabels(matchers []*labels.Matcher, sms map[string]string) bool {
v, prs := sms[m.Name] v, prs := sms[m.Name]
switch m.Type { switch m.Type {
case labels.MatchNotEqual, labels.MatchNotRegexp: case labels.MatchNotEqual, labels.MatchNotRegexp:
if string(v) == "" && prs {
return true
}
if !m.Matches(string(v)) { if !m.Matches(string(v)) {
return false return false
} }
default: default:
if string(v) == "" && !prs {
return true
}
if !prs || !m.Matches(string(v)) { if !prs || !m.Matches(string(v)) {
return false return false
} }

View File

@ -312,6 +312,38 @@ func TestReceiversMatchFilter(t *testing.T) {
require.False(t, receiversMatchFilter(receivers, filter)) 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 { func newMatcher(labelSet model.LabelSet) types.Matchers {
matchers := make([]*types.Matcher, 0, len(labelSet)) matchers := make([]*types.Matcher, 0, len(labelSet))
for key, val := range labelSet { for key, val := range labelSet {

View File

@ -9,7 +9,7 @@ import (
) )
var ( var (
re = regexp.MustCompile(`(?:\s?)(\w+)(=|=~|!=|!~)(?:\"([^"=~!]+)\"|([^"=~!]+))`) re = regexp.MustCompile(`(?:\s?)(\w+)(=|=~|!=|!~)(?:\"([^"=~!]+)\"|([^"=~!]+)|\"\")`)
typeMap = map[string]labels.MatchType{ typeMap = map[string]labels.MatchType{
"=": labels.MatchEqual, "=": labels.MatchEqual,
"!=": labels.MatchNotEqual, "!=": labels.MatchNotEqual,
@ -66,7 +66,7 @@ func Input(s string) (name, value string, matchType labels.MatchType, err error)
value = ms[4] value = ms[4]
} }
if name == "" || value == "" || !prs { if name == "" || !prs {
return "", "", labels.MatchEqual, fmt.Errorf("failed to parse") return "", "", labels.MatchEqual, fmt.Errorf("failed to parse")
} }

View File

@ -101,6 +101,14 @@ func TestMatchers(t *testing.T) {
return append(ms, m, m2) 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 { for i, tc := range testCases {