fix: skipping label if it contains special symbol (#14068)

Co-authored-by: Vladyslav Diachenko <82767850+vlad-diachenko@users.noreply.github.com>
pull/14072/head
Ravishankar 8 months ago committed by GitHub
parent c65721e7ad
commit 55e374e85e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 22
      pkg/logql/log/parser.go
  2. 8
      pkg/logql/log/parser_test.go

@ -4,6 +4,7 @@ import (
"bytes"
"errors"
"fmt"
"strings"
"unicode/utf8"
"github.com/grafana/jsonparser"
@ -39,6 +40,14 @@ var (
errMissingCapture = errors.New("at least one named capture must be supplied")
errFoundAllLabels = errors.New("found all required labels")
errLabelDoesNotMatch = errors.New("found a label with a matcher that didn't match")
// the rune error replacement is rejected by Prometheus hence replacing them with space.
removeInvalidUtf = func(r rune) rune {
if r == utf8.RuneError {
return 32 // rune value for space
}
return r
}
)
type JSONParser struct {
@ -200,12 +209,11 @@ func unescapeJSONString(b []byte) string {
return ""
}
res := string(bU)
// rune error is rejected by Prometheus
for _, r := range res {
if r == utf8.RuneError {
return ""
}
if strings.ContainsRune(res, utf8.RuneError) {
res = strings.Map(removeInvalidUtf, res)
}
return res
}
@ -339,9 +347,9 @@ func (l *LogfmtParser) Process(_ int64, line []byte, lbs *LabelsBuilder) ([]byte
}
val := l.dec.Value()
// the rune error replacement is rejected by Prometheus, so we skip it.
if bytes.ContainsRune(val, utf8.RuneError) {
val = nil
val = bytes.Map(removeInvalidUtf, val)
}
if !l.keepEmpty && len(val) == 0 {

@ -83,7 +83,7 @@ func Test_jsonParser_Parse(t *testing.T) {
labels.EmptyLabels(),
labels.FromStrings("counter", "1",
"price__net_", "5.56909",
"foo", "",
"foo", " ",
),
NoParserHints(),
},
@ -802,7 +802,7 @@ func TestLogfmtParser_parse(t *testing.T) {
"utf8 error rune",
[]byte(`buzz=foo bar=<EFBFBD>f`),
labels.EmptyLabels(),
labels.FromStrings("buzz", "foo"),
labels.FromStrings("bar", " f", "buzz", "foo"),
nil,
NoParserHints(),
},
@ -1037,7 +1037,7 @@ func TestLogfmtParser_keepEmpty(t *testing.T) {
false,
labels.FromStrings("foo", "bar"),
labels.FromStrings("foo", "bar",
"bar", "buzz"),
"bar", "buzz", "foo_extracted", "b r"),
},
{
"utf8 error rune with keep empty",
@ -1045,7 +1045,7 @@ func TestLogfmtParser_keepEmpty(t *testing.T) {
true,
labels.FromStrings("foo", "bar"),
labels.FromStrings("foo", "bar",
"foo_extracted", "",
"foo_extracted", "b r",
"bar", "buzz"),
},
}

Loading…
Cancel
Save