From 198f690667382420ca5bf44a53f7a30070b0433c Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Tue, 19 Jul 2022 06:30:37 +0000 Subject: [PATCH] loki: better handle an empty-response (#52397) (#52431) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit (cherry picked from commit 46eec85b1a0e15c68540fab9fd746a890b8a41a1) Co-authored-by: GÑbor Farkas --- pkg/tsdb/loki/api.go | 8 ++++++++ pkg/tsdb/loki/framing_test.go | 2 ++ pkg/tsdb/loki/testdata/empty.golden.jsonc | 5 +++++ pkg/tsdb/loki/testdata/empty.json | 1 + 4 files changed, 16 insertions(+) create mode 100644 pkg/tsdb/loki/testdata/empty.golden.jsonc create mode 100644 pkg/tsdb/loki/testdata/empty.json diff --git a/pkg/tsdb/loki/api.go b/pkg/tsdb/loki/api.go index 79501b11146..5ad4402e915 100644 --- a/pkg/tsdb/loki/api.go +++ b/pkg/tsdb/loki/api.go @@ -163,6 +163,14 @@ func (api *LokiAPI) DataQuery(ctx context.Context, query lokiQuery) (data.Frames iter := jsoniter.Parse(jsoniter.ConfigDefault, resp.Body, 1024) res := converter.ReadPrometheusStyleResult(iter, converter.Options{MatrixWideSeries: false, VectorWideSeries: false}) + if res == nil { + // it's hard to say if this is an error-case or not. + // we know the http-response was a success-response + // (otherwise we wouldn't be here in the code), + // so we will go with a success, with no data. + return data.Frames{}, nil + } + if res.Error != nil { return nil, res.Error } diff --git a/pkg/tsdb/loki/framing_test.go b/pkg/tsdb/loki/framing_test.go index bb2b3bf16a7..cd057d375d5 100644 --- a/pkg/tsdb/loki/framing_test.go +++ b/pkg/tsdb/loki/framing_test.go @@ -48,6 +48,8 @@ func TestSuccessResponse(t *testing.T) { {name: "parse a simple streams response", filepath: "streams_simple", query: streamsQuery}, {name: "parse a streams response with parse errors", filepath: "streams_parse_errors", query: streamsQuery}, + + {name: "parse an empty response", filepath: "empty", query: matrixQuery}, } for _, test := range tt { diff --git a/pkg/tsdb/loki/testdata/empty.golden.jsonc b/pkg/tsdb/loki/testdata/empty.golden.jsonc new file mode 100644 index 00000000000..8850a3f1b08 --- /dev/null +++ b/pkg/tsdb/loki/testdata/empty.golden.jsonc @@ -0,0 +1,5 @@ +// 🌟 This was machine generated. Do not edit. 🌟 +// 🌟 This was machine generated. Do not edit. 🌟 +{ + "frames": [] +} \ No newline at end of file diff --git a/pkg/tsdb/loki/testdata/empty.json b/pkg/tsdb/loki/testdata/empty.json new file mode 100644 index 00000000000..9e26dfeeb6e --- /dev/null +++ b/pkg/tsdb/loki/testdata/empty.json @@ -0,0 +1 @@ +{} \ No newline at end of file