Skip to content

Commit

Permalink
Add filtering by health and state in list rules (#6040)
Browse files Browse the repository at this point in the history
* Add filtering by health and state in list rules

Signed-off-by: Eunice Kim <[email protected]>

* Update CHANGELOG

Signed-off-by: Eunice Kim <[email protected]>

---------

Signed-off-by: Eunice Kim <[email protected]>
  • Loading branch information
euniceek authored Jul 9, 2024
1 parent 176df21 commit 6f5fab7
Show file tree
Hide file tree
Showing 6 changed files with 252 additions and 50 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* [BUGFIX] Ingester: Fix `user` and `type` labels for the `cortex_ingester_tsdb_head_samples_appended_total` TSDB metric. #5952
* [BUGFIX] Querier: Enforce max query length check for `/api/v1/series` API even though `ignoreMaxQueryLength` is set to true. #6018
* [BUGFIX] Ingester: Fix issue with the minimize token generator where it was not taking in consideration the current ownerhip of an instance when generating extra tokens. #6062
* [ENHANCEMENT] Ruler: Add support for filtering by `state` and `health` field on Rules API. #6040

## 1.17.1 2024-05-20

Expand Down
14 changes: 14 additions & 0 deletions pkg/ruler/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,11 +135,25 @@ func (a *API) PrometheusRules(w http.ResponseWriter, req *http.Request) {
return
}

state := strings.ToLower(req.URL.Query().Get("state"))
if state != "" && state != firingStateFilter && state != pendingStateFilter && state != inactiveStateFilter {
util_api.RespondError(logger, w, v1.ErrBadData, fmt.Sprintf("unsupported state value %q", state), http.StatusBadRequest)
return
}

health := strings.ToLower(req.URL.Query().Get("health"))
if health != "" && health != unknownHealthFilter && health != okHealthFilter && health != errHealthFilter {
util_api.RespondError(logger, w, v1.ErrBadData, fmt.Sprintf("unsupported health value %q", health), http.StatusBadRequest)
return
}

rulesRequest := RulesRequest{
RuleNames: req.Form["rule_name[]"],
RuleGroupNames: req.Form["rule_group[]"],
Files: req.Form["file[]"],
Type: typ,
State: state,
Health: health,
}

w.Header().Set("Content-Type", "application/json")
Expand Down
26 changes: 25 additions & 1 deletion pkg/ruler/ruler.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,14 @@ const (

alertingRuleFilter string = "alert"
recordingRuleFilter string = "record"

firingStateFilter string = "firing"
pendingStateFilter string = "pending"
inactiveStateFilter string = "inactive"

unknownHealthFilter string = "unknown"
okHealthFilter string = "ok"
errHealthFilter string = "err"
)

type DisabledRuleGroupErr struct {
Expand Down Expand Up @@ -874,9 +882,11 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB
ruleGroupNameSet := sliceToSet(rulesRequest.RuleGroupNames)
fileSet := sliceToSet(rulesRequest.Files)
ruleType := rulesRequest.Type
alertState := rulesRequest.State
health := rulesRequest.Health

returnAlerts := ruleType == "" || ruleType == alertingRuleFilter
returnRecording := ruleType == "" || ruleType == recordingRuleFilter
returnRecording := (ruleType == "" || ruleType == recordingRuleFilter) && alertState == ""

for _, group := range groups {
// The mapped filename is url path escaped encoded to make handling `/` characters easier
Expand Down Expand Up @@ -915,6 +925,9 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB
continue
}
}
if !returnByHealth(health, string(r.Health())) {
continue
}
lastError := ""
if r.LastError() != nil {
lastError = r.LastError().Error()
Expand All @@ -926,6 +939,9 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB
if !returnAlerts {
continue
}
if !returnByState(alertState, rule.State().String()) {
continue
}
alerts := []*AlertStateDesc{}
for _, a := range rule.ActiveAlerts() {
alerts = append(alerts, &AlertStateDesc{
Expand Down Expand Up @@ -1296,3 +1312,11 @@ func (r *Ruler) ListAllRules(w http.ResponseWriter, req *http.Request) {
close(iter)
<-done
}

func returnByState(requestState string, alertState string) bool {
return requestState == "" || requestState == alertState
}

func returnByHealth(requestHealth string, ruleHealth string) bool {
return requestHealth == "" || requestHealth == ruleHealth
}
211 changes: 162 additions & 49 deletions pkg/ruler/ruler.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/ruler/ruler.proto
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ message RulesRequest {
repeated string ruleGroupNames = 2;
repeated string files = 3;
string type = 4;
string state = 5;
string health = 6;
}

message RulesResponse {
Expand Down
Loading

0 comments on commit 6f5fab7

Please sign in to comment.