Make filter labels consistent with Prometheus (#2403)

* Make filter labels consistent with Prometheus

Filtering the alert out when the label is missing precludes a
possible match for an empty value. This change allows the
match to be evaluated.

Closes #2342

Signed-off-by: Victor Araujo <vear91@gmail.com>

* Add tests for matchFilterLabels in v2 api

Signed-off-by: Victor Araujo <vear91@gmail.com>
This commit is contained in:
Victor Araujo 2020-11-06 11:26:32 +01:00 committed by GitHub
parent 6e7f922d3a
commit 846c04e807
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 41 additions and 2 deletions

View File

@ -662,7 +662,7 @@ func matchFilterLabels(matchers []*labels.Matcher, sms map[string]string) bool {
if string(m.Value) == "" && !prs {
continue
}
if !prs || !m.Matches(string(v)) {
if !m.Matches(string(v)) {
return false
}
}

View File

@ -551,7 +551,7 @@ func matchFilterLabels(matchers []*labels.Matcher, sms map[string]string) bool {
if m.Value == "" && !prs {
continue
}
if !prs || !m.Matches(v) {
if !m.Matches(v) {
return false
}
}

View File

@ -25,6 +25,7 @@ import (
open_api_models "github.com/prometheus/alertmanager/api/v2/models"
general_ops "github.com/prometheus/alertmanager/api/v2/restapi/operations/general"
"github.com/prometheus/alertmanager/config"
"github.com/prometheus/alertmanager/pkg/labels"
"github.com/prometheus/alertmanager/types"
)
@ -171,3 +172,41 @@ func TestAlertToOpenAPIAlert(t *testing.T) {
},
}, openAPIAlert)
}
func TestMatchFilterLabels(t *testing.T) {
sms := map[string]string{
"foo": "bar",
}
testCases := []struct {
matcher labels.MatchType
name string
val string
expected bool
}{
{labels.MatchEqual, "foo", "bar", true},
{labels.MatchEqual, "baz", "", true},
{labels.MatchEqual, "baz", "qux", false},
{labels.MatchEqual, "baz", "qux|", false},
{labels.MatchRegexp, "foo", "bar", true},
{labels.MatchRegexp, "baz", "", true},
{labels.MatchRegexp, "baz", "qux", false},
{labels.MatchRegexp, "baz", "qux|", true},
{labels.MatchNotEqual, "foo", "bar", false},
{labels.MatchNotEqual, "baz", "", false},
{labels.MatchNotEqual, "baz", "qux", true},
{labels.MatchNotEqual, "baz", "qux|", true},
{labels.MatchNotRegexp, "foo", "bar", false},
{labels.MatchNotRegexp, "baz", "", false},
{labels.MatchNotRegexp, "baz", "qux", true},
{labels.MatchNotRegexp, "baz", "qux|", false},
}
for _, tc := range testCases {
m, err := labels.NewMatcher(tc.matcher, tc.name, tc.val)
require.NoError(t, err)
ms := []*labels.Matcher{m}
require.Equal(t, tc.expected, matchFilterLabels(ms, sms))
}
}