Skip to content

Commit

Permalink
Fix(query-frontend): LokiCodec encode/decode works correctly with lab…
Browse files Browse the repository at this point in the history
…el values endpoint (#6084)

When encode/decode `http.Request` into `querybase.Request` we re-create request.Path.

Before this PR, both `/labels/` and `/labels/{name}/values` endpoint was decoding into same URL path `/labels/` before
forwarding to the querier. So it always returns label names as response.

This bug became visible after enabling all the middleware for `/labels/{name}/values` endpoint as well via #6072

Signed-off-by: Kaviraj <kavirajkanagaraj@gmail.com>
  • Loading branch information
kavirajk authored May 3, 2022
1 parent 6392ddb commit 8b15632
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/querier/queryrange/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ func (Codec) EncodeRequest(ctx context.Context, r queryrangebase.Request) (*http
}

u := &url.URL{
Path: "/loki/api/v1/labels",
Path: request.Path, // NOTE: this could be either /label or /label/{name}/values endpoint. So forward the original path as it is.
RawQuery: params.Encode(),
}
req := &http.Request{
Expand Down
20 changes: 20 additions & 0 deletions pkg/querier/queryrange/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,26 @@ func Test_codec_labels_EncodeRequest(t *testing.T) {
require.Equal(t, toEncode.StartTs, req.(*LokiLabelNamesRequest).StartTs)
require.Equal(t, toEncode.EndTs, req.(*LokiLabelNamesRequest).EndTs)
require.Equal(t, "/loki/api/v1/labels", req.(*LokiLabelNamesRequest).Path)

// Test labels values endpoint
toEncode = &LokiLabelNamesRequest{
Path: "/loki/api/v1/labels/__name__/values",
StartTs: start,
EndTs: end,
}
got, err = LokiCodec.EncodeRequest(ctx, toEncode)
require.NoError(t, err)
require.Equal(t, ctx, got.Context())
require.Equal(t, "/loki/api/v1/labels/__name__/values", got.URL.Path)
require.Equal(t, fmt.Sprintf("%d", start.UnixNano()), got.URL.Query().Get("start"))
require.Equal(t, fmt.Sprintf("%d", end.UnixNano()), got.URL.Query().Get("end"))

// testing a full roundtrip
req, err = LokiCodec.DecodeRequest(context.TODO(), got, nil)
require.NoError(t, err)
require.Equal(t, toEncode.StartTs, req.(*LokiLabelNamesRequest).StartTs)
require.Equal(t, toEncode.EndTs, req.(*LokiLabelNamesRequest).EndTs)
require.Equal(t, "/loki/api/v1/labels/__name__/values", req.(*LokiLabelNamesRequest).Path)
}

func Test_codec_EncodeResponse(t *testing.T) {
Expand Down

0 comments on commit 8b15632

Please sign in to comment.