Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prometheus: Small improvements to the custom client #51709

Merged
merged 2 commits into from Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions pkg/tsdb/prometheus/client/client.go
Expand Up @@ -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
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/tsdb/prometheus/prometheus_test.go
Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions pkg/tsdb/prometheus/resource/resource.go
@@ -1,9 +1,9 @@
package resource

import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net/http"

"github.com/grafana/grafana-plugin-sdk-go/backend"
Expand Down Expand Up @@ -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
Expand Down