Skip to content

Commit

Permalink
Fixes a panic with whitespace key. (#3372)
Browse files Browse the repository at this point in the history
* Fixes a panic with whitespace key.

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>

* Removes empty key.

Signed-off-by: Cyril Tovena <cyril.tovena@gmail.com>
  • Loading branch information
cyriltovena authored Feb 23, 2021
1 parent cb59069 commit f10535f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
5 changes: 4 additions & 1 deletion pkg/logql/log/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,9 @@ func readValue(iter *jsoniter.Iterator) string {

func addLabel(lbs *LabelsBuilder, key, value string) {
key = sanitizeLabelKey(key, true)
if len(key) == 0 {
return
}
if lbs.BaseHas(key) {
key = fmt.Sprintf("%s%s", key, duplicateSuffix)
}
Expand Down Expand Up @@ -266,7 +269,7 @@ type JSONExpressionParser struct {
}

func NewJSONExpressionParser(expressions []JSONExpression) (*JSONExpressionParser, error) {
var paths = make(map[string][]interface{})
paths := make(map[string][]interface{})

for _, exp := range expressions {
path, err := jsonexpr.Parse(exp.Expression, false)
Expand Down
3 changes: 3 additions & 0 deletions pkg/logql/log/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ func sanitizeLabelKey(key string, isPrefix bool) string {
return key
}
key = strings.TrimSpace(key)
if len(key) == 0 {
return key
}
if isPrefix && key[0] >= '0' && key[0] <= '9' {
key = "_" + key
}
Expand Down
1 change: 1 addition & 0 deletions pkg/logql/log/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ func Test_sanitizeLabelKey(t *testing.T) {
key string
want string
}{
{" ", ""},
{"1", "_1"},
{"1 1 1", "_1_1_1"},
{"abc", "abc"},
Expand Down

0 comments on commit f10535f

Please sign in to comment.