fix: fix bug in query result marshaling for invalid utf8 characters (#14585)

Signed-off-by: Callum Styan <callumstyan@gmail.com>
pull/14619/head^2
Callum Styan 7 months ago committed by GitHub
parent 99c423a1f5
commit f411a0795a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 16
      pkg/util/marshal/query.go
  2. 19
      pkg/util/marshal/query_test.go

@ -1,8 +1,11 @@
package marshal
import (
"bytes"
"fmt"
"strconv"
"strings"
"unicode/utf8"
"unsafe"
jsoniter "github.com/json-iterator/go"
@ -20,6 +23,16 @@ import (
"github.com/grafana/loki/v3/pkg/util/httpreq"
)
var (
// 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
}
)
// NewResultValue constructs a ResultValue from a promql.Value
func NewResultValue(v parser.Value) (loghttp.ResultValue, error) {
var err error
@ -77,6 +90,9 @@ func NewStreams(s logqlmodel.Streams) (loghttp.Streams, error) {
ret := make([]loghttp.Stream, len(s))
for i, stream := range s {
if strings.ContainsRune(stream.Labels, utf8.RuneError) {
stream.Labels = string(bytes.Map(removeInvalidUtf, []byte(stream.Labels)))
}
ret[i], err = NewStream(stream)
if err != nil {

@ -0,0 +1,19 @@
package marshal
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/grafana/loki/v3/pkg/logqlmodel"
)
func TestNewStreams(t *testing.T) {
s, err := NewStreams(logqlmodel.Streams{
{
Labels: "{asdf=\"<EFBFBD>\"}",
},
})
require.NoError(t, err)
require.Equal(t, " ", s[0].Labels["asdf"], "expected only a space for label who only contained invalid UTF8 rune")
}
Loading…
Cancel
Save