Skip to content

Commit 48122ac

Browse files
cyriltovenagouthamve
authored andcommitted
Pass Request to DecodeResponse in case it is required. (#1892)
* Pass Request to DecodeResponse in case it is required. Loki has the concept of log Direction which is essential to know when merging responses. This change allows Loki to inject the direction in the response which can be then used when merging multiple responses. Signed-off-by: Cyril Tovena <[email protected]> * Add more information about the change of the Decode function Signed-off-by: Cyril Tovena <[email protected]>
1 parent 1dd96fd commit 48122ac

File tree

4 files changed

+8
-6
lines changed

4 files changed

+8
-6
lines changed

pkg/querier/queryrange/marshaling_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func BenchmarkMarshalling(b *testing.B) {
1919
apiResp, err := PrometheusCodec.DecodeResponse(context.Background(), &http.Response{
2020
StatusCode: 200,
2121
Body: ioutil.NopCloser(bytes.NewReader(buf)),
22-
})
22+
}, nil)
2323
require.NoError(b, err)
2424

2525
resp, err := PrometheusCodec.EncodeResponse(context.Background(), apiResp)

pkg/querier/queryrange/query_range.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ type Codec interface {
4040
Merger
4141
// DecodeRequest decodes a Request from an http request.
4242
DecodeRequest(context.Context, *http.Request) (Request, error)
43-
// DecodeResponse decodes a Response from an http response..
44-
DecodeResponse(context.Context, *http.Response) (Response, error)
43+
// DecodeResponse decodes a Response from an http response.
44+
// The original request is also passed as a parameter this is useful for implementation that needs the request
45+
// to merge result or build the result correctly.
46+
DecodeResponse(context.Context, *http.Response, Request) (Response, error)
4547
// EncodeRequest encodes a Request into an http request.
4648
EncodeRequest(context.Context, Request) (*http.Request, error)
4749
// EncodeResponse encodes a Response into an http response.
@@ -198,7 +200,7 @@ func (prometheusCodec) EncodeRequest(ctx context.Context, r Request) (*http.Requ
198200
return req.WithContext(ctx), nil
199201
}
200202

201-
func (prometheusCodec) DecodeResponse(ctx context.Context, r *http.Response) (Response, error) {
203+
func (prometheusCodec) DecodeResponse(ctx context.Context, r *http.Response, _ Request) (Response, error) {
202204
if r.StatusCode/100 != 2 {
203205
body, _ := ioutil.ReadAll(r.Body)
204206
return nil, httpgrpc.Errorf(r.StatusCode, string(body))

pkg/querier/queryrange/query_range_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func TestResponse(t *testing.T) {
8989
Header: http.Header{"Content-Type": []string{"application/json"}},
9090
Body: ioutil.NopCloser(bytes.NewBuffer([]byte(tc.body))),
9191
}
92-
resp, err := PrometheusCodec.DecodeResponse(context.Background(), response)
92+
resp, err := PrometheusCodec.DecodeResponse(context.Background(), response, nil)
9393
require.NoError(t, err)
9494
assert.Equal(t, tc.expected, resp)
9595

pkg/querier/queryrange/roundtrip.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,5 +179,5 @@ func (q roundTripper) Do(ctx context.Context, r Request) (Response, error) {
179179
}
180180
defer func() { _ = response.Body.Close() }()
181181

182-
return q.codec.DecodeResponse(ctx, response)
182+
return q.codec.DecodeResponse(ctx, response, r)
183183
}

0 commit comments

Comments
 (0)