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

query-frontend: add option to preserve specific request headers #4486

Merged
merged 17 commits into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## master / unreleased

* [ENHANCEMENT] Querier: Retain Headers from request while decoding http requests to PrometheusRequests by prometheusCodec. #4416
* [CHANGE] Memberlist: Expose default configuration values to the command line options. Note that setting these explicitly to zero will no longer cause the default to be used. If the default is desired, then do set the option. The following are affected: #4276
- `-memberlist.stream-timeout`
- `-memberlist.retransmit-factor`
Expand Down
20 changes: 19 additions & 1 deletion pkg/querier/queryrange/query_range.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,16 @@ func (prometheusCodec) DecodeRequest(_ context.Context, r *http.Request) (Reques
result.Query = r.FormValue("query")
result.Path = r.URL.Path

// Include the headers from http request in prometheusRequest.
// We don't include the following header in the codec -
// accept-encoding - defaultroundtripper uses the response_compression_enabled setting to inflate/deflate responses
for h, hv := range r.Header {
if strings.ToLower(h) == "accept-encoding" {
continue
}
result.Headers = append(result.Headers, &PrometheusRequestHeader{Name: h, Values: hv})
}

for _, value := range r.Header.Values(cacheControlHeader) {
if strings.Contains(value, noStoreValue) {
result.CachingOptions.Disabled = true
Expand All @@ -247,12 +257,20 @@ func (prometheusCodec) EncodeRequest(ctx context.Context, r Request) (*http.Requ
Path: promReq.Path,
RawQuery: params.Encode(),
}
var h = http.Header{}

for _, hv := range promReq.Headers {
for _, v := range hv.Values {
h.Add(hv.Name, v)
}
}

req := &http.Request{
Method: "GET",
RequestURI: u.String(), // This is what the httpgrpc code looks at.
URL: u,
Body: http.NoBody,
Header: http.Header{},
Header: h,
}

return req.WithContext(ctx), nil
Expand Down
7 changes: 5 additions & 2 deletions pkg/querier/queryrange/query_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ import (
)

func TestRequest(t *testing.T) {
r := *parsedRequest
r.Headers = reqHeaders
for i, tc := range []struct {
url string
expected Request
expectedErr error
}{
{
url: query,
expected: parsedRequest,
expected: &r,
},
{
url: "api/v1/query_range?start=foo",
Expand Down Expand Up @@ -55,9 +57,10 @@ func TestRequest(t *testing.T) {
t.Run(strconv.Itoa(i), func(t *testing.T) {
r, err := http.NewRequest("GET", tc.url, nil)
require.NoError(t, err)
r.Header.Add("Test-Header", "test")

ctx := user.InjectOrgID(context.Background(), "1")
r = r.WithContext(ctx)
r = r.Clone(ctx)

req, err := PrometheusCodec.DecodeRequest(ctx, r)
if err != nil {
Expand Down
Loading