Skip to content

Pagination support for ListRules #6299

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

* [CHANGE] Change default value of `-blocks-storage.bucket-store.index-cache.memcached.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 @@ -601,6 +601,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 @@ -612,12 +614,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 @@ -627,6 +629,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)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: If we just add group_next_token="" what will happen? It should still be a non paginated request?
Maybe we can just remove the condition check here

Copy link
Contributor

@rapphil rapphil Nov 4, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To keep consistency with upstream, we should fail if group_next_token was provided but group_limit was not.

https://github.com/prometheus/prometheus/blob/main/web/api/v1/api.go#L1609

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never mind, I see the validation in the API.

req.URL.RawQuery = urlValues.Encode()

ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
Expand All @@ -635,13 +643,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 @@ -652,14 +660,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
Loading