Skip to content

Commit 81601e9

Browse files
fix for #5989
Signed-off-by: Anand Rajagopal <[email protected]>
1 parent d664dd7 commit 81601e9

File tree

6 files changed

+250
-154
lines changed

6 files changed

+250
-154
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
* [ENHANCEMENT] Store Gateway: Log gRPC requests together with headers configured in `http_request_headers_to_log`. #5958
1717
* [BUGFIX] Configsdb: Fix endline issue in db password. #5920
1818
* [BUGFIX] Ingester: Fix `user` and `type` labels for the `cortex_ingester_tsdb_head_samples_appended_total` TSDB metric. #5952
19+
* [BUGFIX] Ruler: Fix ListRules API not honoring `exclude_alerts` flag. #6011
1920

2021
## 1.17.1 2024-05-20
2122

pkg/ruler/api.go

+21
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,18 @@ func (a *API) PrometheusRules(w http.ResponseWriter, req *http.Request) {
135135
return
136136
}
137137

138+
excludeAlerts, err := parseExcludeAlerts(req)
139+
if err != nil {
140+
util_api.RespondError(logger, w, v1.ErrBadData, "invalid boolean value for exclude_alerts", http.StatusBadRequest)
141+
return
142+
}
143+
138144
rulesRequest := RulesRequest{
139145
RuleNames: req.Form["rule_name[]"],
140146
RuleGroupNames: req.Form["rule_group[]"],
141147
Files: req.Form["file[]"],
142148
Type: typ,
149+
ExcludeAlerts: excludeAlerts,
143150
}
144151

145152
w.Header().Set("Content-Type", "application/json")
@@ -234,6 +241,20 @@ func (a *API) PrometheusRules(w http.ResponseWriter, req *http.Request) {
234241
}
235242
}
236243

244+
func parseExcludeAlerts(r *http.Request) (bool, error) {
245+
excludeAlertsParam := strings.ToLower(r.URL.Query().Get("exclude_alerts"))
246+
247+
if excludeAlertsParam == "" {
248+
return false, nil
249+
}
250+
251+
excludeAlerts, err := strconv.ParseBool(excludeAlertsParam)
252+
if err != nil {
253+
return false, fmt.Errorf("error converting exclude_alerts: %w", err)
254+
}
255+
return excludeAlerts, nil
256+
}
257+
237258
func (a *API) PrometheusAlerts(w http.ResponseWriter, req *http.Request) {
238259
logger := util_log.WithContext(req.Context(), a.logger)
239260
userID, err := tenant.TenantID(req.Context())

pkg/ruler/ruler.go

+16-13
Original file line numberDiff line numberDiff line change
@@ -927,19 +927,21 @@ func (r *Ruler) getLocalRules(userID string, rulesRequest RulesRequest, includeB
927927
continue
928928
}
929929
alerts := []*AlertStateDesc{}
930-
for _, a := range rule.ActiveAlerts() {
931-
alerts = append(alerts, &AlertStateDesc{
932-
State: a.State.String(),
933-
Labels: cortexpb.FromLabelsToLabelAdapters(a.Labels),
934-
Annotations: cortexpb.FromLabelsToLabelAdapters(a.Annotations),
935-
Value: a.Value,
936-
ActiveAt: a.ActiveAt,
937-
FiredAt: a.FiredAt,
938-
ResolvedAt: a.ResolvedAt,
939-
LastSentAt: a.LastSentAt,
940-
ValidUntil: a.ValidUntil,
941-
KeepFiringSince: a.KeepFiringSince,
942-
})
930+
if !rulesRequest.ExcludeAlerts {
931+
for _, a := range rule.ActiveAlerts() {
932+
alerts = append(alerts, &AlertStateDesc{
933+
State: a.State.String(),
934+
Labels: cortexpb.FromLabelsToLabelAdapters(a.Labels),
935+
Annotations: cortexpb.FromLabelsToLabelAdapters(a.Annotations),
936+
Value: a.Value,
937+
ActiveAt: a.ActiveAt,
938+
FiredAt: a.FiredAt,
939+
ResolvedAt: a.ResolvedAt,
940+
LastSentAt: a.LastSentAt,
941+
ValidUntil: a.ValidUntil,
942+
KeepFiringSince: a.KeepFiringSince,
943+
})
944+
}
943945
}
944946
ruleDesc = &RuleStateDesc{
945947
Rule: &rulespb.RuleDesc{
@@ -1146,6 +1148,7 @@ func (r *Ruler) getShardedRules(ctx context.Context, userID string, rulesRequest
11461148
RuleGroupNames: rulesRequest.GetRuleGroupNames(),
11471149
Files: rulesRequest.GetFiles(),
11481150
Type: rulesRequest.GetType(),
1151+
ExcludeAlerts: rulesRequest.GetExcludeAlerts(),
11491152
})
11501153

11511154
if err != nil {

0 commit comments

Comments
 (0)