Skip to content

Pass Request to DecodeResponse in case it is required. #1892

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

Merged
merged 2 commits into from
Dec 11, 2019
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/querier/queryrange/marshaling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func BenchmarkMarshalling(b *testing.B) {
apiResp, err := PrometheusCodec.DecodeResponse(context.Background(), &http.Response{
StatusCode: 200,
Body: ioutil.NopCloser(bytes.NewReader(buf)),
})
}, nil)
require.NoError(b, err)

resp, err := PrometheusCodec.EncodeResponse(context.Background(), apiResp)
Expand Down
8 changes: 5 additions & 3 deletions pkg/querier/queryrange/query_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ type Codec interface {
Merger
// DecodeRequest decodes a Request from an http request.
DecodeRequest(context.Context, *http.Request) (Request, error)
// DecodeResponse decodes a Response from an http response..
DecodeResponse(context.Context, *http.Response) (Response, error)
// DecodeResponse decodes a Response from an http response.
// The original request is also passed as a parameter this is useful for implementation that needs the request
// to merge result or build the result correctly.
DecodeResponse(context.Context, *http.Response, Request) (Response, error)
// EncodeRequest encodes a Request into an http request.
EncodeRequest(context.Context, Request) (*http.Request, error)
// EncodeResponse encodes a Response into an http response.
Expand Down Expand Up @@ -198,7 +200,7 @@ func (prometheusCodec) EncodeRequest(ctx context.Context, r Request) (*http.Requ
return req.WithContext(ctx), nil
}

func (prometheusCodec) DecodeResponse(ctx context.Context, r *http.Response) (Response, error) {
func (prometheusCodec) DecodeResponse(ctx context.Context, r *http.Response, _ Request) (Response, error) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to add a comment to this function, explaining why we have _ Request, otherwise there's the risk we'll clean it up in the future because considered dead code.

Copy link
Contributor Author

@cyriltovena cyriltovena Dec 11, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added a note, but on the interface, I don't think here it matters.

if r.StatusCode/100 != 2 {
body, _ := ioutil.ReadAll(r.Body)
return nil, httpgrpc.Errorf(r.StatusCode, string(body))
Expand Down
2 changes: 1 addition & 1 deletion pkg/querier/queryrange/query_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestResponse(t *testing.T) {
Header: http.Header{"Content-Type": []string{"application/json"}},
Body: ioutil.NopCloser(bytes.NewBuffer([]byte(tc.body))),
}
resp, err := PrometheusCodec.DecodeResponse(context.Background(), response)
resp, err := PrometheusCodec.DecodeResponse(context.Background(), response, nil)
require.NoError(t, err)
assert.Equal(t, tc.expected, resp)

Expand Down
2 changes: 1 addition & 1 deletion pkg/querier/queryrange/roundtrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,5 +176,5 @@ func (q roundTripper) Do(ctx context.Context, r Request) (Response, error) {
}
defer func() { _ = response.Body.Close() }()

return q.codec.DecodeResponse(ctx, response)
return q.codec.DecodeResponse(ctx, response, r)
}