From 846c04e807309f15b8949e123ef9f4f9244c992a Mon Sep 17 00:00:00 2001 From: Victor Araujo Date: Fri, 6 Nov 2020 11:26:32 +0100 Subject: [PATCH] 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 * Add tests for matchFilterLabels in v2 api Signed-off-by: Victor Araujo --- api/v1/api.go | 2 +- api/v2/api.go | 2 +- api/v2/api_test.go | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 2 deletions(-) diff --git a/api/v1/api.go b/api/v1/api.go index 3a146643..0a1df61b 100644 --- a/api/v1/api.go +++ b/api/v1/api.go @@ -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 } } diff --git a/api/v2/api.go b/api/v2/api.go index 6cde1972..24f308d5 100644 --- a/api/v2/api.go +++ b/api/v2/api.go @@ -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 } } diff --git a/api/v2/api_test.go b/api/v2/api_test.go index 4cb4b743..ca55c59c 100644 --- a/api/v2/api_test.go +++ b/api/v2/api_test.go @@ -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)) + } +}