Prometheus: Check for errors on json response parsing (#73788)

- The util/converter Prometheus response json parse was not checking for errors while parsing. It now does. In particular, if `[dataproxy]/response_limit` is set in Grafana's config, it will now recognize the limit error.
- Fixes #73747
- Adds `jsonitere` package, which wraps json-iterator/go's Iterator's Methods with methods that return errors, so errcheck linting can be relied upon
- Impact:
  - If something was sending malformed JSON to the prometheus or loki datasources, the previous code might have accepted that and partially processed the data
  - Before there may have been partial data with no error, where as no there may be errors but they will have no partial results, just the error.
This commit is contained in:
Kyle Brandt
2023-08-28 15:53:50 +03:00
committed by GitHub
parent 97c3cd1af3
commit 38603b1a9e
5 changed files with 550 additions and 160 deletions
+3 -12
View File
@@ -5,7 +5,6 @@ import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"testing"
@@ -74,7 +73,7 @@ func TestIntegrationPrometheus(t *testing.T) {
"datasource": map[string]interface{}{
"uid": uid,
},
"expr": "up",
"expr": "1",
"instantQuery": true,
})
buf1 := &bytes.Buffer{}
@@ -88,13 +87,9 @@ func TestIntegrationPrometheus(t *testing.T) {
// nolint:gosec
resp, err := http.Post(u, "application/json", buf1)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
t.Cleanup(func() {
err := resp.Body.Close()
require.NoError(t, err)
_ = resp.Body.Close()
})
_, err = io.ReadAll(resp.Body)
require.NoError(t, err)
require.NotNil(t, outgoingRequest)
require.Equal(t, "/api/v1/query_range?q1=1&q2=2", outgoingRequest.URL.String())
@@ -124,13 +119,9 @@ func TestIntegrationPrometheus(t *testing.T) {
// nolint:gosec
resp, err := http.Post(u, "application/json", buf1)
require.NoError(t, err)
require.Equal(t, http.StatusOK, resp.StatusCode)
t.Cleanup(func() {
err := resp.Body.Close()
require.NoError(t, err)
_ = resp.Body.Close()
})
_, err = io.ReadAll(resp.Body)
require.NoError(t, err)
require.NotNil(t, outgoingRequest)
require.Equal(t, "/api/v1/query_range", outgoingRequest.URL.Path)