Skip to content

Commit

Permalink
query rejection - address comments.
Browse files Browse the repository at this point in the history
Signed-off-by: Erlan Zholdubai uulu <[email protected]>
  • Loading branch information
erlan-z committed Jun 26, 2024
1 parent 4a1cf8d commit c7ad0a0
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 62 deletions.
6 changes: 3 additions & 3 deletions docs/configuration/config-file-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -5396,9 +5396,9 @@ time_range_limit:
# 0, it won't be checked.
[max: <int> | default = 0]
# For range query, step should be within this limit to match. For instant query,
# subQuery step should be within this limit to match. If not set, it won't be
# checked. This property won't be applied to metadata queries.
# If query step provided should be within this limit to match. If query contains
# subquery and it has steps provided, then it will be checked as well. If not
# set, it won't be checked. This property won't be applied to metadata queries.
query_step_limit:
# Query step should be above or equal to this value to match. If set to 0, it
# won't be checked.
Expand Down
29 changes: 13 additions & 16 deletions integration/e2ecortex/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ type Client struct {
httpClient *http.Client
querierClient promv1.API
orgID string
headers []string
}

// NewClient makes a new Cortex client
Expand All @@ -60,7 +59,6 @@ func NewClient(
alertmanagerAddress string,
rulerAddress string,
orgID string,
headers ...string,
) (*Client, error) {
// Create querier API client
querierAPIClient, err := promapi.NewClient(promapi.Config{
Expand All @@ -80,7 +78,6 @@ func NewClient(
httpClient: &http.Client{},
querierClient: promv1.NewAPI(querierAPIClient),
orgID: orgID,
headers: headers,
}

if alertmanagerAddress != "" {
Expand Down Expand Up @@ -237,7 +234,7 @@ func (c *Client) QueryRange(query string, start, end time.Time, step time.Durati
}

// QueryRangeRaw runs a ranged query directly against the querier API.
func (c *Client) QueryRangeRaw(query string, start, end time.Time, step time.Duration) (*http.Response, []byte, error) {
func (c *Client) QueryRangeRaw(query string, start, end time.Time, step time.Duration, headers map[string]string) (*http.Response, []byte, error) {
addr := fmt.Sprintf(
"http://%s/api/prom/api/v1/query_range?query=%s&start=%s&end=%s&step=%s",
c.querierAddress,
Expand All @@ -247,11 +244,11 @@ func (c *Client) QueryRangeRaw(query string, start, end time.Time, step time.Dur
strconv.FormatFloat(step.Seconds(), 'f', -1, 64),
)

return c.query(addr)
return c.query(addr, headers)
}

// QueryRaw runs a query directly against the querier API.
func (c *Client) QueryRaw(query string, ts time.Time) (*http.Response, []byte, error) {
func (c *Client) QueryRaw(query string, ts time.Time, headers map[string]string) (*http.Response, []byte, error) {
u := &url.URL{
Scheme: "http",
Path: fmt.Sprintf("%s/api/prom/api/v1/query", c.querierAddress),
Expand All @@ -263,11 +260,11 @@ func (c *Client) QueryRaw(query string, ts time.Time) (*http.Response, []byte, e
q.Set("time", FormatTime(ts))
}
u.RawQuery = q.Encode()
return c.query(u.String())
return c.query(u.String(), headers)
}

// SeriesRaw runs a series request directly against the querier API.
func (c *Client) SeriesRaw(matches []string, startTime, endTime time.Time) (*http.Response, []byte, error) {
func (c *Client) SeriesRaw(matches []string, startTime, endTime time.Time, headers map[string]string) (*http.Response, []byte, error) {
u := &url.URL{
Scheme: "http",
Path: fmt.Sprintf("%s/api/prom/api/v1/series", c.querierAddress),
Expand All @@ -286,11 +283,11 @@ func (c *Client) SeriesRaw(matches []string, startTime, endTime time.Time) (*htt
}

u.RawQuery = q.Encode()
return c.query(u.String())
return c.query(u.String(), headers)
}

// LabelNamesRaw runs a label names request directly against the querier API.
func (c *Client) LabelNamesRaw(matches []string, startTime, endTime time.Time) (*http.Response, []byte, error) {
func (c *Client) LabelNamesRaw(matches []string, startTime, endTime time.Time, headers map[string]string) (*http.Response, []byte, error) {
u := &url.URL{
Scheme: "http",
Path: fmt.Sprintf("%s/api/prom/api/v1/labels", c.querierAddress),
Expand All @@ -309,11 +306,11 @@ func (c *Client) LabelNamesRaw(matches []string, startTime, endTime time.Time) (
}

u.RawQuery = q.Encode()
return c.query(u.String())
return c.query(u.String(), headers)
}

// LabelValuesRaw runs a label values request directly against the querier API.
func (c *Client) LabelValuesRaw(label string, matches []string, startTime, endTime time.Time) (*http.Response, []byte, error) {
func (c *Client) LabelValuesRaw(label string, matches []string, startTime, endTime time.Time, headers map[string]string) (*http.Response, []byte, error) {
u := &url.URL{
Scheme: "http",
Path: fmt.Sprintf("%s/api/prom/api/v1/label/%s/values", c.querierAddress, label),
Expand All @@ -332,7 +329,7 @@ func (c *Client) LabelValuesRaw(label string, matches []string, startTime, endTi
}

u.RawQuery = q.Encode()
return c.query(u.String())
return c.query(u.String(), headers)
}

// RemoteRead runs a remote read query.
Expand Down Expand Up @@ -401,7 +398,7 @@ func (c *Client) RemoteRead(matchers []*labels.Matcher, start, end time.Time, st
return &resp, nil
}

func (c *Client) query(addr string) (*http.Response, []byte, error) {
func (c *Client) query(addr string, headers map[string]string) (*http.Response, []byte, error) {
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
defer cancel()

Expand All @@ -412,8 +409,8 @@ func (c *Client) query(addr string) (*http.Response, []byte, error) {

req.Header.Set("X-Scope-OrgID", c.orgID)

for i := 0; i < len(c.headers); i += 2 {
req.Header.Set(c.headers[i], c.headers[i+1])
for key, value := range headers {
req.Header.Set(key, value)
}

retries := backoff.New(ctx, backoff.Config{
Expand Down
12 changes: 6 additions & 6 deletions integration/querier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -901,22 +901,22 @@ func TestQuerierWithBlocksStorageLimits(t *testing.T) {
require.NoError(t, err)

// We expect all queries hitting 422 exceeded series limit on store gateway.
resp, body, err := c.QueryRangeRaw(`{job="test"}`, seriesTimestamp.Add(-time.Second), seriesTimestamp, time.Second)
resp, body, err := c.QueryRangeRaw(`{job="test"}`, seriesTimestamp.Add(-time.Second), seriesTimestamp, time.Second, map[string]string{})
require.NoError(t, err)
require.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
require.Contains(t, string(body), "exceeded series limit")

resp, body, err = c.SeriesRaw([]string{`{job="test"}`}, seriesTimestamp.Add(-time.Second), seriesTimestamp)
resp, body, err = c.SeriesRaw([]string{`{job="test"}`}, seriesTimestamp.Add(-time.Second), seriesTimestamp, map[string]string{})
require.NoError(t, err)
require.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
require.Contains(t, string(body), "exceeded series limit")

resp, body, err = c.LabelNamesRaw([]string{`{job="test"}`}, seriesTimestamp.Add(-time.Second), seriesTimestamp)
resp, body, err = c.LabelNamesRaw([]string{`{job="test"}`}, seriesTimestamp.Add(-time.Second), seriesTimestamp, map[string]string{})
require.NoError(t, err)
require.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
require.Contains(t, string(body), "exceeded series limit")

resp, body, err = c.LabelValuesRaw("job", []string{`{job="test"}`}, seriesTimestamp.Add(-time.Second), seriesTimestamp)
resp, body, err = c.LabelValuesRaw("job", []string{`{job="test"}`}, seriesTimestamp.Add(-time.Second), seriesTimestamp, map[string]string{})
require.NoError(t, err)
require.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
require.Contains(t, string(body), "exceeded series limit")
Expand Down Expand Up @@ -994,7 +994,7 @@ func TestQuerierWithStoreGatewayDataBytesLimits(t *testing.T) {
require.NoError(t, err)

// We expect all queries hitting 422 exceeded series limit
resp, body, err := c.QueryRaw(`{job="test"}`, series2Timestamp)
resp, body, err := c.QueryRaw(`{job="test"}`, series2Timestamp, map[string]string{})
require.NoError(t, err)
require.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
require.Contains(t, string(body), "exceeded bytes limit")
Expand Down Expand Up @@ -1245,7 +1245,7 @@ func TestQuerierMaxSamplesLimit(t *testing.T) {
var body []byte
for retries.Ongoing() {
// We expect request to hit max samples limit.
res, body, err = c.QueryRaw(`sum({job="test"})`, series1Timestamp)
res, body, err = c.QueryRaw(`sum({job="test"})`, series1Timestamp, map[string]string{})
if err == nil {
break
}
Expand Down
Loading

0 comments on commit c7ad0a0

Please sign in to comment.