Skip to content

Commit 3a39004

Browse files
committed
Add filtering by health and state in list rules
1 parent 2866917 commit 3a39004

File tree

5 files changed

+272
-50
lines changed

5 files changed

+272
-50
lines changed

pkg/ruler/api.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,25 @@ func (a *API) PrometheusRules(w http.ResponseWriter, req *http.Request) {
135135
return
136136
}
137137

138+
state := strings.TrimSpace(strings.ToLower(req.URL.Query().Get("state")))
139+
if state != "" && !AlertState(state).Valid() {
140+
util_api.RespondError(logger, w, v1.ErrBadData, fmt.Sprintf("unsupported state value %q", state), http.StatusBadRequest)
141+
return
142+
}
143+
144+
health := strings.TrimSpace(strings.ToLower(req.URL.Query().Get("health")))
145+
if health != "" && !RuleHealth(health).Valid() {
146+
util_api.RespondError(logger, w, v1.ErrBadData, fmt.Sprintf("unsupported health value %q", health), http.StatusBadRequest)
147+
return
148+
}
149+
138150
rulesRequest := RulesRequest{
139151
RuleNames: req.Form["rule_name[]"],
140152
RuleGroupNames: req.Form["rule_group[]"],
141153
Files: req.Form["file[]"],
142154
Type: typ,
155+
State: state,
156+
Health: health,
143157
}
144158

145159
w.Header().Set("Content-Type", "application/json")

pkg/ruler/ruler.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,19 @@ const (
7575
recordingRuleFilter string = "record"
7676
)
7777

78+
type AlertState string
79+
type RuleHealth string
80+
81+
const (
82+
firing AlertState = "firing"
83+
pending AlertState = "pending"
84+
inactive AlertState = "inactive"
85+
86+
unknown RuleHealth = "unknown"
87+
ok RuleHealth = "ok"
88+
err RuleHealth = "err"
89+
)
90+
7891
type DisabledRuleGroupErr struct {
7992
Message string
8093
}
@@ -874,9 +887,11 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB
874887
ruleGroupNameSet := sliceToSet(rulesRequest.RuleGroupNames)
875888
fileSet := sliceToSet(rulesRequest.Files)
876889
ruleType := rulesRequest.Type
890+
alertState := rulesRequest.State
891+
health := rulesRequest.Health
877892

878893
returnAlerts := ruleType == "" || ruleType == alertingRuleFilter
879-
returnRecording := ruleType == "" || ruleType == recordingRuleFilter
894+
returnRecording := (ruleType == "" || ruleType == recordingRuleFilter) && alertState == ""
880895

881896
for _, group := range groups {
882897
// The mapped filename is url path escaped encoded to make handling `/` characters easier
@@ -915,6 +930,9 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB
915930
continue
916931
}
917932
}
933+
if !returnByHealth(health, string(r.Health())) {
934+
continue
935+
}
918936
lastError := ""
919937
if r.LastError() != nil {
920938
lastError = r.LastError().Error()
@@ -926,6 +944,9 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB
926944
if !returnAlerts {
927945
continue
928946
}
947+
if !returnByState(alertState, rule.State().String()) {
948+
continue
949+
}
929950
alerts := []*AlertStateDesc{}
930951
for _, a := range rule.ActiveAlerts() {
931952
alerts = append(alerts, &AlertStateDesc{
@@ -1296,3 +1317,27 @@ func (r *Ruler) ListAllRules(w http.ResponseWriter, req *http.Request) {
12961317
close(iter)
12971318
<-done
12981319
}
1320+
1321+
func returnByState(requestState string, alertState string) bool {
1322+
return requestState == "" || requestState == alertState
1323+
}
1324+
1325+
func returnByHealth(requestHealth string, ruleHealth string) bool {
1326+
return requestHealth == "" || requestHealth == ruleHealth
1327+
}
1328+
1329+
func (r AlertState) Valid() bool {
1330+
switch r {
1331+
case firing, pending, inactive:
1332+
return true
1333+
}
1334+
return false
1335+
}
1336+
1337+
func (r RuleHealth) Valid() bool {
1338+
switch r {
1339+
case unknown, ok, err:
1340+
return true
1341+
}
1342+
return false
1343+
}

pkg/ruler/ruler.pb.go

Lines changed: 162 additions & 49 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/ruler/ruler.proto

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ message RulesRequest {
2424
repeated string ruleGroupNames = 2;
2525
repeated string files = 3;
2626
string type = 4;
27+
string state = 5;
28+
string health = 6;
2729
}
2830

2931
message RulesResponse {

0 commit comments

Comments
 (0)