Skip to content

Commit

Permalink
Pagination support for ListRules (#6299)
Browse files Browse the repository at this point in the history
* Pagination support for ListRules

Signed-off-by: Anand Rajagopal <[email protected]>

* Addressing comments

Signed-off-by: Anand Rajagopal <[email protected]>

* Fixed a typo

Signed-off-by: Anand Rajagopal <[email protected]>

---------

Signed-off-by: Anand Rajagopal <[email protected]>
  • Loading branch information
rajagopalanand authored Nov 6, 2024
1 parent f658039 commit 9acd3f7
Show file tree
Hide file tree
Showing 11 changed files with 1,050 additions and 179 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [CHANGE] Change all max async concurrency default values `50` to `3` #6268
* [CHANGE] Change default value of `-blocks-storage.bucket-store.index-cache.multilevel.max-async-concurrency` from `50` to `3` #6265
* [CHANGE] Enable Compactor and Alertmanager in target all. #6204
* [FEATURE] Ruler: Pagination support for List Rules API. #6299
* [FEATURE] Query Frontend/Querier: Add protobuf codec `-api.querier-default-codec` and the option to choose response compression type `-querier.response-compression`. #5527
* [FEATURE] Ruler: Experimental: Add `ruler.frontend-address` to allow query to query frontends instead of ingesters. #6151
* [FEATURE] Ruler: Minimize chances of missed rule group evaluations that can occur due to OOM kills, bad underlying nodes, or due to an unhealthy ruler that appears in the ring as healthy. This feature is enabled via `-ruler.enable-ha-evaluation` flag. #6129
Expand Down
22 changes: 15 additions & 7 deletions integration/e2ecortex/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,8 @@ type RuleFilter struct {
RuleNames []string
RuleType string
ExcludeAlerts string
MaxRuleGroup int
NextToken string
}

func addQueryParams(urlValues url.Values, paramName string, params ...string) {
Expand All @@ -614,12 +616,12 @@ func addQueryParams(urlValues url.Values, paramName string, params ...string) {
}

// GetPrometheusRules fetches the rules from the Prometheus endpoint /api/v1/rules.
func (c *Client) GetPrometheusRules(filter RuleFilter) ([]*ruler.RuleGroup, error) {
func (c *Client) GetPrometheusRules(filter RuleFilter) ([]*ruler.RuleGroup, string, error) {
// Create HTTP request

req, err := http.NewRequest("GET", fmt.Sprintf("http://%s/api/prom/api/v1/rules", c.rulerAddress), nil)
if err != nil {
return nil, err
return nil, "", err
}
req.Header.Set("X-Scope-OrgID", c.orgID)

Expand All @@ -629,6 +631,12 @@ func (c *Client) GetPrometheusRules(filter RuleFilter) ([]*ruler.RuleGroup, erro
addQueryParams(urlValues, "rule_group[]", filter.RuleGroupNames...)
addQueryParams(urlValues, "type", filter.RuleType)
addQueryParams(urlValues, "exclude_alerts", filter.ExcludeAlerts)
if filter.MaxRuleGroup > 0 {
addQueryParams(urlValues, "group_limit", strconv.Itoa(filter.MaxRuleGroup))
}
if filter.NextToken != "" {
addQueryParams(urlValues, "group_next_token", filter.NextToken)
}
req.URL.RawQuery = urlValues.Encode()

ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
Expand All @@ -637,13 +645,13 @@ func (c *Client) GetPrometheusRules(filter RuleFilter) ([]*ruler.RuleGroup, erro
// Execute HTTP request
res, err := c.httpClient.Do(req.WithContext(ctx))
if err != nil {
return nil, err
return nil, "", err
}
defer res.Body.Close()

body, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
return nil, "", err
}

// Decode the response.
Expand All @@ -654,14 +662,14 @@ func (c *Client) GetPrometheusRules(filter RuleFilter) ([]*ruler.RuleGroup, erro

decoded := &response{}
if err := json.Unmarshal(body, decoded); err != nil {
return nil, err
return nil, "", err
}

if decoded.Status != "success" {
return nil, fmt.Errorf("unexpected response status '%s'", decoded.Status)
return nil, "", fmt.Errorf("unexpected response status '%s'", decoded.Status)
}

return decoded.Data.RuleGroups, nil
return decoded.Data.RuleGroups, decoded.Data.GroupNextToken, nil
}

// GetRuleGroups gets the configured rule groups from the ruler.
Expand Down
Loading

0 comments on commit 9acd3f7

Please sign in to comment.