Skip to content

Commit

Permalink
Promclient: Use http.header type for headers
Browse files Browse the repository at this point in the history
Instead of using `map[string]string` for adding additional headers to
requests in `req2xx`.

Signed-off-by: Jacob Baungard Hansen <[email protected]>
  • Loading branch information
jacobbaungard committed Oct 3, 2023
1 parent 1b8351e commit 6065897
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
12 changes: 6 additions & 6 deletions pkg/promclient/promclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func NewWithTracingClient(logger log.Logger, httpClient *http.Client, userAgent

// req2xx sends a request to the given url.URL. If method is http.MethodPost then
// the raw query is encoded in the body and the appropriate Content-Type is set.
func (c *Client) req2xx(ctx context.Context, u *url.URL, method string, headers map[string]string) (_ []byte, _ int, err error) {
func (c *Client) req2xx(ctx context.Context, u *url.URL, method string, headers http.Header) (_ []byte, _ int, err error) {
var b io.Reader
if method == http.MethodPost {
rq := u.RawQuery
Expand All @@ -117,17 +117,17 @@ func (c *Client) req2xx(ctx context.Context, u *url.URL, method string, headers
if err != nil {
return nil, 0, errors.Wrapf(err, "create %s request", method)
}
if headers != nil {
req.Header = headers
}

if c.userAgent != "" {
req.Header.Set("User-Agent", c.userAgent)
}
if method == http.MethodPost {
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
}

for header, value := range headers {
req.Header.Set(header, value)
}

resp, err := c.Do(req.WithContext(ctx))
if err != nil {
return nil, 0, errors.Wrapf(err, "perform %s request against %s", method, u.String())
Expand Down Expand Up @@ -367,7 +367,7 @@ type QueryOptions struct {
MaxSourceResolution string
Engine string
Explain bool
HTTPHeaders map[string]string
HTTPHeaders http.Header
}

func (p *QueryOptions) AddTo(values url.Values) error {
Expand Down
9 changes: 7 additions & 2 deletions test/e2e/store_gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,13 @@ metafile_content_ttl: 0s`, memcached.InternalEndpoint("memcached"))
testutil.Ok(t, s1.WaitSumMetrics(e2emon.Equals(0), "thanos_bucket_store_block_load_failures_total"))

t.Run("query works", func(t *testing.T) {
tenant1Header := make(http.Header)
tenant1Header.Add("thanos-tenant", "test-tenant-1")
queryAndAssertSeries(t, ctx, q.Endpoint("http"), func() string { return fmt.Sprintf("%s @ end()", testQuery) },
time.Now, promclient.QueryOptions{
Deduplicate: false,
HTTPHeaders: map[string]string{"thanos-tenant": "test-tenant-1"},
HTTPHeaders: tenant1Header,
// map[string][]string{"thanos-tenant": "test-tenant-1"},
},
[]model.Metric{
{
Expand Down Expand Up @@ -173,10 +176,12 @@ metafile_content_ttl: 0s`, memcached.InternalEndpoint("memcached"))
testutil.Ok(t, s1.WaitSumMetricsWithOptions(e2emon.Equals(9), []string{"thanos_bucket_store_series_data_fetched"}, e2emon.WithLabelMatchers(matchers.MustNewMatcher(matchers.MatchEqual, tenancy.MetricLabel, "test-tenant-1"))))
testutil.Ok(t, s1.WaitSumMetricsWithOptions(e2emon.Equals(3), []string{"thanos_bucket_store_series_blocks_queried"}, e2emon.WithLabelMatchers(matchers.MustNewMatcher(matchers.MatchEqual, tenancy.MetricLabel, "test-tenant-1"))))

tenant2Header := make(http.Header)
tenant2Header.Add("thanos-tenant", "test-tenant-2")
queryAndAssertSeries(t, ctx, q.Endpoint("http"), func() string { return testQuery },
time.Now, promclient.QueryOptions{
Deduplicate: true,
HTTPHeaders: map[string]string{"thanos-tenant": "test-tenant-2"},
HTTPHeaders: tenant2Header,
},
[]model.Metric{
{
Expand Down

0 comments on commit 6065897

Please sign in to comment.