Skip to content

Commit

Permalink
Check contains as per review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ravishankar15 committed Sep 9, 2024
1 parent 11b0ac0 commit 281976f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 19 deletions.
26 changes: 12 additions & 14 deletions pkg/logql/log/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,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 {
Expand Down Expand Up @@ -202,14 +210,9 @@ func unescapeJSONString(b []byte) string {
}
res := string(bU)

// rune error is rejected by Prometheus hence replacing them with space
removeInvalidUtf := func(r rune) rune {
if r == utf8.RuneError {
return -1
}
return r
if strings.ContainsRune(res, utf8.RuneError) {
res = strings.Map(removeInvalidUtf, res)
}
res = strings.Map(removeInvalidUtf, res)

return res
}
Expand Down Expand Up @@ -345,14 +348,9 @@ func (l *LogfmtParser) Process(_ int64, line []byte, lbs *LabelsBuilder) ([]byte

val := l.dec.Value()

// the rune error replacement is rejected by Prometheus hence skiping them.
removeInvalidUtf := func(r rune) rune {
if r == utf8.RuneError {
return -1
}
return r
if bytes.ContainsRune(val, utf8.RuneError) {
val = bytes.Map(removeInvalidUtf, val)
}
val = bytes.Map(removeInvalidUtf, val)

if !l.keepEmpty && len(val) == 0 {
continue
Expand Down
10 changes: 5 additions & 5 deletions pkg/logql/log/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,11 @@ func Test_jsonParser_Parse(t *testing.T) {
},
{
"utf8 error rune",
[]byte(`{"counter":1,"foo":"�f", "price": {"_net_":5.56909}}`),
[]byte(`{"counter":1,"foo":"�", "price": {"_net_":5.56909}}`),
labels.EmptyLabels(),
labels.FromStrings("counter", "1",
"price__net_", "5.56909",
"foo", "f",
"foo", " ",
),
NoParserHints(),
},
Expand Down Expand Up @@ -802,7 +802,7 @@ func TestLogfmtParser_parse(t *testing.T) {
"utf8 error rune",
[]byte(`buzz=foo bar=�f`),
labels.EmptyLabels(),
labels.FromStrings("bar", "f", "buzz", "foo"),
labels.FromStrings("bar", " f", "buzz", "foo"),
nil,
NoParserHints(),
},
Expand Down Expand Up @@ -1037,15 +1037,15 @@ func TestLogfmtParser_keepEmpty(t *testing.T) {
false,
labels.FromStrings("foo", "bar"),
labels.FromStrings("foo", "bar",
"bar", "buzz", "foo_extracted", "br"),
"bar", "buzz", "foo_extracted", "b r"),
},
{
"utf8 error rune with keep empty",
[]byte("foo=b�r bar=buzz"),
true,
labels.FromStrings("foo", "bar"),
labels.FromStrings("foo", "bar",
"foo_extracted", "br",
"foo_extracted", "b r",
"bar", "buzz"),
},
}
Expand Down

0 comments on commit 281976f

Please sign in to comment.