From 29be929a8cde0a20fc2e639285db90cf9be88338 Mon Sep 17 00:00:00 2001 From: Karsten Jeschkies Date: Thu, 19 Nov 2020 09:14:51 +0100 Subject: [PATCH] Test label filter for bytes. (#2941) * Test label filter for bytes. Summary: - Adds a new test to `ast_test` for filtering by bytes.| - Test another case than `B`. * Test all combinations of byte sizes. --- pkg/logql/ast_test.go | 9 ++++++- pkg/logql/lex.go | 6 ++--- pkg/logql/log/label_filter_test.go | 40 +++++++++++++++++++++++++++--- 3 files changed, 48 insertions(+), 7 deletions(-) diff --git a/pkg/logql/ast_test.go b/pkg/logql/ast_test.go index 3eb4ccc9a8084..23cc407dfb62b 100644 --- a/pkg/logql/ast_test.go +++ b/pkg/logql/ast_test.go @@ -208,6 +208,13 @@ func Test_FilterMatcher(t *testing.T) { }, []linecheck{{"foo", true}, {"bar", false}, {"foobar", true}}, }, + { + `{app="foo"} | logfmt | duration > 1s and total_bytes < 1GB`, + []*labels.Matcher{ + mustNewMatcher(labels.MatchEqual, "app", "foo"), + }, + []linecheck{{"duration=5m total_bytes=5kB", true}, {"duration=1s total_bytes=256B", false}, {"duration=0s", false}}, + }, } { tt := tt t.Run(tt.q, func(t *testing.T) { @@ -223,7 +230,7 @@ func Test_FilterMatcher(t *testing.T) { sp := p.ForStream(labelBar) for _, lc := range tt.lines { _, _, ok := sp.Process([]byte(lc.l)) - assert.Equal(t, lc.e, ok) + assert.Equalf(t, lc.e, ok, "query for line '%s' was %v and not %v", lc.l, ok, lc.e) } } }) diff --git a/pkg/logql/lex.go b/pkg/logql/lex.go index ccc5c1a7d4639..86112235600eb 100644 --- a/pkg/logql/lex.go +++ b/pkg/logql/lex.go @@ -253,10 +253,10 @@ func tryScanBytes(number string, l *scanner.Scanner) (uint64, bool) { } func isBytesSizeRune(r rune) bool { - // B, kB, MB, GB, TB, PB, EB, ZB, YB - // KB, KiB, MiB, GiB, TiB, PiB, EiB, ZiB, YiB + // Accept: B, kB, MB, GB, TB, PB, KB, KiB, MiB, GiB, TiB, PiB + // Do not accept: EB, ZB, YB, PiB, ZiB and YiB. They are not supported since the value migh not be represented in an uint64 switch r { - case 'B', 'i', 'k', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y': + case 'B', 'i', 'k', 'K', 'M', 'G', 'T', 'P': return true default: return false diff --git a/pkg/logql/log/label_filter_test.go b/pkg/logql/log/label_filter_test.go index 371e6b601931e..2520221a40811 100644 --- a/pkg/logql/log/label_filter_test.go +++ b/pkg/logql/log/label_filter_test.go @@ -26,10 +26,10 @@ func TestBinary_Filter(t *testing.T) { labels.Labels{{Name: "foo", Value: "5"}, {Name: "bar", Value: "1s"}}, }, { - NewAndLabelFilter(NewNumericLabelFilter(LabelFilterEqual, "foo", 5), NewBytesLabelFilter(LabelFilterEqual, "bar", 42)), - labels.Labels{{Name: "foo", Value: "5"}, {Name: "bar", Value: "42B"}}, + NewAndLabelFilter(NewNumericLabelFilter(LabelFilterEqual, "foo", 5), NewBytesLabelFilter(LabelFilterEqual, "bar", 42000)), + labels.Labels{{Name: "foo", Value: "5"}, {Name: "bar", Value: "42kB"}}, true, - labels.Labels{{Name: "foo", Value: "5"}, {Name: "bar", Value: "42B"}}, + labels.Labels{{Name: "foo", Value: "5"}, {Name: "bar", Value: "42kB"}}, }, { NewAndLabelFilter( @@ -161,6 +161,40 @@ func TestBinary_Filter(t *testing.T) { } } +func TestBytes_Filter(t *testing.T) { + tests := []struct { + expectedBytes uint64 + label string + + want bool + wantLabel string + }{ + {42, "42B", true, "42B"}, + {42 * 1000, "42kB", true, "42kB"}, + {42 * 1000 * 1000, "42MB", true, "42MB"}, + {42 * 1000 * 1000 * 1000, "42GB", true, "42GB"}, + {42 * 1000 * 1000 * 1000 * 1000, "42TB", true, "42TB"}, + {42 * 1000 * 1000 * 1000 * 1000 * 1000, "42PB", true, "42PB"}, + {42 * 1024, "42KiB", true, "42KiB"}, + {42 * 1024 * 1024, "42MiB", true, "42MiB"}, + {42 * 1024 * 1024 * 1024, "42GiB", true, "42GiB"}, + {42 * 1024 * 1024 * 1024 * 1024, "42TiB", true, "42TiB"}, + {42 * 1024 * 1024 * 1024 * 1024 * 1024, "42PiB", true, "42PiB"}, + } + for _, tt := range tests { + f := NewBytesLabelFilter(LabelFilterEqual, "bar", tt.expectedBytes) + lbs := labels.Labels{{Name: "bar", Value: tt.label}} + t.Run(f.String(), func(t *testing.T) { + b := NewBaseLabelsBuilder().ForLabels(lbs, lbs.Hash()) + b.Reset() + _, got := f.Process(nil, b) + require.Equal(t, tt.want, got) + wantLbs := labels.Labels{{Name: "bar", Value: tt.wantLabel}} + require.Equal(t, wantLbs, b.Labels()) + }) + } +} + func TestErrorFiltering(t *testing.T) { tests := []struct { f LabelFilterer