Prometheus: Small improvements to the custom client (#51709)

* Add suggestions

* Fix tests
pull/51413/head
Andrej Ocenas 3 years ago committed by GitHub
parent 9797744c10
commit bf334ab777
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      pkg/tsdb/prometheus/client/client.go
  2. 9
      pkg/tsdb/prometheus/prometheus_test.go
  3. 9
      pkg/tsdb/prometheus/resource/resource.go

@ -138,10 +138,13 @@ func createRequest(ctx context.Context, method string, u *url.URL, body []byte,
if header != nil {
request.Header = header
}
// This may not be true but right now we don't have more information here and seems like we send just this type
// of encoding right now if it is a POST
if strings.ToUpper(method) == http.MethodPost {
// This may not be true but right now we don't have more information here and seems like we send just this type
// of encoding right now if it is a POST
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// This allows transport to retry request. See https://github.com/prometheus/client_golang/pull/1022
// It's set to nil so it is not actually sent over the wire, just used in Go http lib to retry requests.
request.Header["Idempotency-Key"] = nil
}
return request, nil
}

@ -95,7 +95,14 @@ func TestService(t *testing.T) {
sender := &fakeSender{}
err := service.CallResource(context.Background(), req, sender)
require.NoError(t, err)
require.Equal(t, http.Header{"Content-Type": {"application/x-www-form-urlencoded"}, "foo": {"bar"}}, httpProvider.Roundtripper.Req.Header)
require.Equal(
t,
http.Header{
"Content-Type": {"application/x-www-form-urlencoded"},
"Idempotency-Key": []string(nil),
"foo": {"bar"},
},
httpProvider.Roundtripper.Req.Header)
require.Equal(t, http.MethodPost, httpProvider.Roundtripper.Req.Method)
body, err := io.ReadAll(httpProvider.Roundtripper.Req.Body)
require.NoError(t, err)

@ -1,9 +1,9 @@
package resource
import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"
"github.com/grafana/grafana-plugin-sdk-go/backend"
@ -83,14 +83,17 @@ func (r *Resource) Execute(ctx context.Context, req *backend.CallResourceRequest
}
}()
data, err := ioutil.ReadAll(resp.Body)
var buf bytes.Buffer
// Should be more efficient than ReadAll. See https://github.com/prometheus/client_golang/pull/976
_, err = buf.ReadFrom(resp.Body)
body := buf.Bytes()
if err != nil {
return nil, err
}
callResponse := &backend.CallResourceResponse{
Status: resp.StatusCode,
Headers: resp.Header,
Body: data,
Body: body,
}
return callResponse, err

Loading…
Cancel
Save