Skip to content

Commit 26f7228

Browse files
rajagopalanandCharlieTLe
authored andcommitted
Pagination support for ListRules (cortexproject#6299)
* 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]>
1 parent 73cb98d commit 26f7228

File tree

11 files changed

+1050
-179
lines changed

11 files changed

+1050
-179
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* [CHANGE] Change all max async concurrency default values `50` to `3` #6268
1010
* [CHANGE] Change default value of `-blocks-storage.bucket-store.index-cache.multilevel.max-async-concurrency` from `50` to `3` #6265
1111
* [CHANGE] Enable Compactor and Alertmanager in target all. #6204
12+
* [FEATURE] Ruler: Pagination support for List Rules API. #6299
1213
* [FEATURE] Query Frontend/Querier: Add protobuf codec `-api.querier-default-codec` and the option to choose response compression type `-querier.response-compression`. #5527
1314
* [FEATURE] Ruler: Experimental: Add `ruler.frontend-address` to allow query to query frontends instead of ingesters. #6151
1415
* [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

integration/e2ecortex/client.go

+15-7
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,8 @@ type RuleFilter struct {
603603
RuleNames []string
604604
RuleType string
605605
ExcludeAlerts string
606+
MaxRuleGroup int
607+
NextToken string
606608
}
607609

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

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

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

@@ -629,6 +631,12 @@ func (c *Client) GetPrometheusRules(filter RuleFilter) ([]*ruler.RuleGroup, erro
629631
addQueryParams(urlValues, "rule_group[]", filter.RuleGroupNames...)
630632
addQueryParams(urlValues, "type", filter.RuleType)
631633
addQueryParams(urlValues, "exclude_alerts", filter.ExcludeAlerts)
634+
if filter.MaxRuleGroup > 0 {
635+
addQueryParams(urlValues, "group_limit", strconv.Itoa(filter.MaxRuleGroup))
636+
}
637+
if filter.NextToken != "" {
638+
addQueryParams(urlValues, "group_next_token", filter.NextToken)
639+
}
632640
req.URL.RawQuery = urlValues.Encode()
633641

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

644652
body, err := io.ReadAll(res.Body)
645653
if err != nil {
646-
return nil, err
654+
return nil, "", err
647655
}
648656

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

655663
decoded := &response{}
656664
if err := json.Unmarshal(body, decoded); err != nil {
657-
return nil, err
665+
return nil, "", err
658666
}
659667

660668
if decoded.Status != "success" {
661-
return nil, fmt.Errorf("unexpected response status '%s'", decoded.Status)
669+
return nil, "", fmt.Errorf("unexpected response status '%s'", decoded.Status)
662670
}
663671

664-
return decoded.Data.RuleGroups, nil
672+
return decoded.Data.RuleGroups, decoded.Data.GroupNextToken, nil
665673
}
666674

667675
// GetRuleGroups gets the configured rule groups from the ruler.

0 commit comments

Comments
 (0)