Skip to content

Commit

Permalink
Ensure that a nil Matrix is not encoded as null but empty list.
Browse files Browse the repository at this point in the history
  • Loading branch information
jeschkies committed Nov 9, 2023
1 parent 7ab40e0 commit 54976d2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/querier/queryrange/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -911,6 +911,28 @@ func Test_codec_EncodeResponse(t *testing.T) {
},
}, seriesVolumeString, false, nil,
},
{
"empty matrix", "/loki/api/v1/query_range",
&LokiPromResponse{
Response: &queryrangebase.PrometheusResponse{
Status: loghttp.QueryStatusSuccess,
Data: queryrangebase.PrometheusData{
ResultType: loghttp.ResultTypeMatrix,
Result: nil,
},
},
Statistics: statsResult,
},
`{
"data": {
` + statsResultString + `
"resultType": "matrix",
"result": []
},
"status": "success"
}`,
false, nil,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
6 changes: 6 additions & 0 deletions pkg/querier/queryrange/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,12 @@ func (p *LokiPromResponse) marshalVector() ([]byte, error) {
}

func (p *LokiPromResponse) marshalMatrix() ([]byte, error) {

// Make sure nil is not encoded as null.
if p.Response.Data.Result == nil {
p.Response.Data.Result = []queryrangebase.SampleStream{}
}

// embed response and add statistics.
return jsonStd.Marshal(struct {
Status string `json:"status"`
Expand Down

0 comments on commit 54976d2

Please sign in to comment.