diff --git a/pkg/logql/log/parser_test.go b/pkg/logql/log/parser_test.go index 3e5de0f70941..654e35bb13e7 100644 --- a/pkg/logql/log/parser_test.go +++ b/pkg/logql/log/parser_test.go @@ -39,6 +39,34 @@ func Test_jsonParser_Parse(t *testing.T) { ), NoParserHints(), }, + { + "whitespace key value", + []byte(`{" ": {"foo":"bar"}}`), + labels.EmptyLabels(), + labels.FromStrings("foo", "bar"), + NoParserHints(), + }, + { + "whitespace key and whitespace subkey values", + []byte(`{" ": {" ":"bar"}}`), + labels.EmptyLabels(), + labels.FromStrings("", "bar"), + NoParserHints(), + }, + { + "whitespace key and empty subkey values", + []byte(`{" ": {"":"bar"}}`), + labels.EmptyLabels(), + labels.FromStrings("", "bar"), + NoParserHints(), + }, + { + "empty key and empty subkey values", + []byte(`{"": {"":"bar"}}`), + labels.EmptyLabels(), + labels.FromStrings("", "bar"), + NoParserHints(), + }, { "escaped", []byte(`{"counter":1,"foo":"foo\\\"bar", "price": {"_net_":5.56909}}`), diff --git a/pkg/logql/log/util.go b/pkg/logql/log/util.go index c4115eb060c5..67904f48e0fe 100644 --- a/pkg/logql/log/util.go +++ b/pkg/logql/log/util.go @@ -40,10 +40,11 @@ func sanitizeLabelKey(key string, isPrefix bool) string { // appendSanitize appends the sanitized key to the slice. func appendSanitized(to, key []byte) []byte { + key = bytes.TrimSpace(key) + if len(key) == 0 { return to } - key = bytes.TrimSpace(key) if len(to) == 0 && key[0] >= '0' && key[0] <= '9' { to = append(to, '_')