Skip to content

Commit

Permalink
Fix return values of Matrix and Vector during query range in QuerySha…
Browse files Browse the repository at this point in the history
…rdingMiddleware (#4457)

* Fix matrics `result` type being `null`

Signed-off-by: Kaviraj <kavirajkanagaraj@gmail.com>

* Add test for vector type as well

Signed-off-by: Kaviraj <kavirajkanagaraj@gmail.com>
  • Loading branch information
kavirajk authored Oct 12, 2021
1 parent 00168b3 commit 2e1279d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 4 deletions.
11 changes: 7 additions & 4 deletions pkg/querier/queryrange/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -728,10 +728,12 @@ func mergeOrderedNonOverlappingStreams(resps []*LokiResponse, limit uint32, dire
}

func toProtoMatrix(m loghttp.Matrix) []queryrange.SampleStream {
res := make([]queryrange.SampleStream, 0, len(m))

if len(m) == 0 {
return nil
return res
}
res := make([]queryrange.SampleStream, 0, len(m))

for _, stream := range m {
samples := make([]cortexpb.Sample, 0, len(stream.Values))
for _, s := range stream.Values {
Expand All @@ -749,10 +751,11 @@ func toProtoMatrix(m loghttp.Matrix) []queryrange.SampleStream {
}

func toProtoVector(v loghttp.Vector) []queryrange.SampleStream {
res := make([]queryrange.SampleStream, 0, len(v))

if len(v) == 0 {
return nil
return res
}
res := make([]queryrange.SampleStream, 0, len(v))
for _, s := range v {
res = append(res, queryrange.SampleStream{
Samples: []cortexpb.Sample{{
Expand Down
47 changes: 47 additions & 0 deletions pkg/querier/queryrange/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,36 @@ func Test_codec_DecodeResponse(t *testing.T) {
Statistics: statsResult,
}, false,
},
{
"matrix-empty-streams",
&http.Response{StatusCode: 200, Body: ioutil.NopCloser(strings.NewReader(matrixStringEmptyResult))},
nil,
&LokiPromResponse{
Response: &queryrange.PrometheusResponse{
Status: loghttp.QueryStatusSuccess,
Data: queryrange.PrometheusData{
ResultType: loghttp.ResultTypeMatrix,
Result: make([]queryrange.SampleStream, 0), // shouldn't be nil.
},
},
Statistics: statsResult,
}, false,
},
{
"vector-empty-streams",
&http.Response{StatusCode: 200, Body: ioutil.NopCloser(strings.NewReader(vectorStringEmptyResult))},
nil,
&LokiPromResponse{
Response: &queryrange.PrometheusResponse{
Status: loghttp.QueryStatusSuccess,
Data: queryrange.PrometheusData{
ResultType: loghttp.ResultTypeVector,
Result: make([]queryrange.SampleStream, 0), // shouldn't be nil.
},
},
Statistics: statsResult,
}, false,
},
{
"streams v1", &http.Response{StatusCode: 200, Body: ioutil.NopCloser(strings.NewReader(streamsString))},
&LokiRequest{Direction: logproto.FORWARD, Limit: 100, Path: "/loki/api/v1/query_range"},
Expand Down Expand Up @@ -905,6 +935,23 @@ var (
},
"status": "success"
}`
matrixStringEmptyResult = `{
"data": {
` + statsResultString + `
"resultType": "matrix",
"result": []
},
"status": "success"
}`
vectorStringEmptyResult = `{
"data": {
` + statsResultString + `
"resultType": "vector",
"result": []
},
"status": "success"
}`

sampleStreams = []queryrange.SampleStream{
{
Labels: []cortexpb.LabelAdapter{{Name: "filename", Value: "/var/hostlog/apport.log"}, {Name: "job", Value: "varlogs"}},
Expand Down

0 comments on commit 2e1279d

Please sign in to comment.