Fix for issue #2255 - matchTagFilters for positive empty-match filters (#2304)

* fix for issue 2255 - matchTagFilters for positive empty-match filters

* add example to comments

* formatting

* add test for positive empty match

* formatting
This commit is contained in:
jduncan0000 2022-03-18 05:58:22 -05:00 committed by GitHub
parent 65afe3b141
commit e5868b9c29
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 2 deletions

View file

@ -2152,14 +2152,20 @@ func matchTagFilters(mn *MetricName, tfs []*tagFilter, kb *bytesutil.ByteBuffer)
tagMatched = true tagMatched = true
break break
} }
if !tagSeen && tf.isNegative && !tf.isEmptyMatch { if !tagSeen && (!tf.isNegative && tf.isEmptyMatch || tf.isNegative && !tf.isEmptyMatch) {
// tf contains positive empty-match filter for non-existing tag key, i.e.
// {non_existing_tag_key=~"foobar|"}
//
// OR
//
// tf contains negative filter for non-exsisting tag key // tf contains negative filter for non-exsisting tag key
// and this filter doesn't match empty string, i.e. {non_existing_tag_key!="foobar"} // and this filter doesn't match empty string, i.e. {non_existing_tag_key!="foobar"}
// Such filter matches anything. // Such filter matches anything.
// //
// Note that the filter `{non_existing_tag_key!~"|foobar"}` shouldn't match anything, // Note that the filter `{non_existing_tag_key!~"|foobar"}` shouldn't match anything,
// since it is expected that it matches non-empty `non_existing_tag_key`. // since it is expected that it matches non-empty `non_existing_tag_key`.
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/546 for details. // See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/546 and
// https://github.com/VictoriaMetrics/VictoriaMetrics/issues/2255 for details.
continue continue
} }
if tagMatched { if tagMatched {

View file

@ -1182,6 +1182,19 @@ func TestMatchTagFilters(t *testing.T) {
t.Fatalf("Should match") t.Fatalf("Should match")
} }
// Positive empty match by non-existing tag
tfs.Reset()
if err := tfs.Add([]byte("non-existing-tag"), []byte("foobar|"), false, true); err != nil {
t.Fatalf("cannot add regexp, positive filter: %s", err)
}
ok, err = matchTagFilters(&mn, toTFPointers(tfs.tfs), &bb)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
if !ok {
t.Fatalf("Should match")
}
// Negative match by non-existing tag // Negative match by non-existing tag
tfs.Reset() tfs.Reset()
if err := tfs.Add([]byte("non-existing-tag"), []byte("foobar"), false, false); err != nil { if err := tfs.Add([]byte("non-existing-tag"), []byte("foobar"), false, false); err != nil {