From 8ee0d62d9ee8c8c16a30fbe5eb43e0e619a0ac15 Mon Sep 17 00:00:00 2001 From: Cyril Tovena Date: Mon, 20 Jun 2022 09:50:29 +0200 Subject: [PATCH] Fixes bad initialization for uniqueString (#6432) --- pkg/logql/log/util.go | 2 +- pkg/logql/log/util_test.go | 13 ++++++++++++- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/pkg/logql/log/util.go b/pkg/logql/log/util.go index eb7eb33120aa3..8db9083b4fa53 100644 --- a/pkg/logql/log/util.go +++ b/pkg/logql/log/util.go @@ -6,7 +6,7 @@ import ( func uniqueString(s []string) []string { unique := make(map[string]bool, len(s)) - us := make([]string, len(unique)) + us := make([]string, 0, len(s)) for _, elem := range s { if len(elem) != 0 { if !unique[elem] { diff --git a/pkg/logql/log/util_test.go b/pkg/logql/log/util_test.go index beb9e2d3ae0e3..f71eeb6f4c18f 100644 --- a/pkg/logql/log/util_test.go +++ b/pkg/logql/log/util_test.go @@ -1,6 +1,10 @@ package log -import "testing" +import ( + "testing" + + "github.com/stretchr/testify/require" +) func Test_sanitizeLabelKey(t *testing.T) { tests := []struct { @@ -23,3 +27,10 @@ func Test_sanitizeLabelKey(t *testing.T) { }) } } + +func Test_UniqueStrings(t *testing.T) { + in := []string{"foo", "bar", "baz", "foo"} + out := uniqueString(in) + require.Equal(t, []string{"foo", "bar", "baz"}, out) + require.Equal(t, 4, cap(out)) +}