Skip to content

Commit

Permalink
add forward header to label api
Browse files Browse the repository at this point in the history
Signed-off-by: lcasi <liuxin@bilibili.com>
  • Loading branch information
lcasi committed Mar 7, 2022
1 parent 1cdbca8 commit 3bb0445
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -12,6 +12,10 @@ We use *breaking :warning:* to mark changes that are not backward compatible (re

### Fixed

### Added

- [#5220](https://github.com/thanos-io/thanos/pull/5220) Query Frontend: Add `--query-frontend.forward-header` flag, forward headers to downstream querier.

### Changed

- [#5205](https://github.com/thanos-io/thanos/pull/5205) Rule: Add ruler labels as external labels in stateless ruler mode.
Expand Down
41 changes: 36 additions & 5 deletions pkg/queryfrontend/labels_codec.go
Expand Up @@ -107,7 +107,7 @@ func (c labelsCodec) MergeResponse(responses ...queryrange.Response) (queryrange
}
}

func (c labelsCodec) DecodeRequest(_ context.Context, r *http.Request, _ []string) (queryrange.Request, error) {
func (c labelsCodec) DecodeRequest(_ context.Context, r *http.Request, forwardHeaders []string) (queryrange.Request, error) {
if err := r.ParseForm(); err != nil {
return nil, httpgrpc.Errorf(http.StatusBadRequest, err.Error())
}
Expand All @@ -118,9 +118,9 @@ func (c labelsCodec) DecodeRequest(_ context.Context, r *http.Request, _ []strin
)
switch op := getOperation(r); op {
case labelNamesOp, labelValuesOp:
req, err = c.parseLabelsRequest(r, op)
req, err = c.parseLabelsRequest(r, op, forwardHeaders)
case seriesOp:
req, err = c.parseSeriesRequest(r)
req, err = c.parseSeriesRequest(r, forwardHeaders)
}
if err != nil {
return nil, err
Expand Down Expand Up @@ -167,6 +167,12 @@ func (c labelsCodec) EncodeRequest(ctx context.Context, r queryrange.Request) (*
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}

for _, hv := range thanosReq.Headers {
for _, v := range hv.Values {
req.Header.Add(hv.Name, v)
}
}

case *ThanosSeriesRequest:
var params = url.Values{
"start": []string{encodeTime(thanosReq.Start)},
Expand All @@ -187,6 +193,11 @@ func (c labelsCodec) EncodeRequest(ctx context.Context, r queryrange.Request) (*
return nil, httpgrpc.Errorf(http.StatusBadRequest, "error creating request: %s", err.Error())
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
for _, hv := range thanosReq.Headers {
for _, v := range hv.Values {
req.Header.Add(hv.Name, v)
}
}

default:
return nil, httpgrpc.Errorf(http.StatusInternalServerError, "invalid request format")
Expand Down Expand Up @@ -271,7 +282,7 @@ func (c labelsCodec) EncodeResponse(ctx context.Context, res queryrange.Response
return &resp, nil
}

func (c labelsCodec) parseLabelsRequest(r *http.Request, op string) (queryrange.Request, error) {
func (c labelsCodec) parseLabelsRequest(r *http.Request, op string, forwardHeaders []string) (queryrange.Request, error) {
var (
result ThanosLabelsRequest
err error
Expand Down Expand Up @@ -312,10 +323,20 @@ func (c labelsCodec) parseLabelsRequest(r *http.Request, op string) (queryrange.
}
}

// Include the specified headers from http request in prometheusRequest.
for _, header := range forwardHeaders {
for h, hv := range r.Header {
if strings.EqualFold(h, header) {
result.Headers = append(result.Headers, &RequestHeader{Name: h, Values: hv})
break
}
}
}

return &result, nil
}

func (c labelsCodec) parseSeriesRequest(r *http.Request) (queryrange.Request, error) {
func (c labelsCodec) parseSeriesRequest(r *http.Request, forwardHeaders []string) (queryrange.Request, error) {
var (
result ThanosSeriesRequest
err error
Expand Down Expand Up @@ -358,6 +379,16 @@ func (c labelsCodec) parseSeriesRequest(r *http.Request) (queryrange.Request, er
}
}

// Include the specified headers from http request in prometheusRequest.
for _, header := range forwardHeaders {
for h, hv := range r.Header {
if strings.EqualFold(h, header) {
result.Headers = append(result.Headers, &RequestHeader{Name: h, Values: hv})
break
}
}
}

return &result, nil
}

Expand Down
6 changes: 5 additions & 1 deletion pkg/queryfrontend/queryrange_codec.go
Expand Up @@ -169,7 +169,11 @@ func (c queryRangeCodec) EncodeRequest(ctx context.Context, r queryrange.Request
return nil, httpgrpc.Errorf(http.StatusBadRequest, "error creating request: %s", err.Error())
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

for _, hv := range thanosReq.Headers {
for _, v := range hv.Values {
req.Header.Add(hv.Name, v)
}
}
return req.WithContext(ctx), nil
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/queryfrontend/request.go
Expand Up @@ -113,6 +113,7 @@ type ThanosLabelsRequest struct {
StoreMatchers [][]*labels.Matcher
PartialResponse bool
CachingOptions queryrange.CachingOptions
Headers []*RequestHeader
}

// GetStart returns the start timestamp of the request in milliseconds.
Expand Down Expand Up @@ -184,6 +185,7 @@ type ThanosSeriesRequest struct {
Matchers [][]*labels.Matcher
StoreMatchers [][]*labels.Matcher
CachingOptions queryrange.CachingOptions
Headers []*RequestHeader
}

// GetStart returns the start timestamp of the request in milliseconds.
Expand Down

0 comments on commit 3bb0445

Please sign in to comment.