Skip to content

Commit

Permalink
Adding a query parameter to filter out active alerts
Browse files Browse the repository at this point in the history
Signed-off-by: Anand Rajagopal <[email protected]>
  • Loading branch information
rajagopalanand committed Oct 9, 2023
1 parent ea3ad74 commit de9efc6
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 24 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 2.47.1 / 2023-10-04

* [BUGFIX] Fix duplicate sample detection at chunk size limit #12874
* [ENHANCEMENT] Add ability to filter out active alerts in `/rules` API

## 2.47.0 / 2023-09-06

Expand Down
3 changes: 2 additions & 1 deletion docs/querying/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ URL query parameters:
- `rule_name[]=<string>`: only return rules with the given rule name. If the parameter is repeated, rules with any of the provided names are returned. If we've filtered out all the rules of a group, the group is not returned. When the parameter is absent or empty, no filtering is done.
- `rule_group[]=<string>`: only return rules with the given rule group name. If the parameter is repeated, rules with any of the provided rule group names are returned. When the parameter is absent or empty, no filtering is done.
- `file[]=<string>`: only return rules with the given filepath. If the parameter is repeated, rules with any of the provided filepaths are returned. When the parameter is absent or empty, no filtering is done.
- `exclude_active_alerts`: only return rules without active alerts. When this parameter is absent or empty, no filtering is done.

```json
$ curl http://localhost:9090/api/v1/rules
Expand Down Expand Up @@ -1307,4 +1308,4 @@ Enable the OTLP receiver by the feature flag
`--enable-feature=otlp-write-receiver`. When enabled, the OTLP receiver
endpoint is `/api/v1/otlp/v1/metrics`.

*New in v2.47*
*New in v2.47*
19 changes: 18 additions & 1 deletion web/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,17 @@ func (api *API) rules(r *http.Request) apiFuncResult {
returnAlerts := typ == "" || typ == "alert"
returnRecording := typ == "" || typ == "record"

excludeAlertsParam := strings.ToLower(r.URL.Query().Get("exclude_active_alerts"))

if excludeAlertsParam == "" {
excludeAlertsParam = "false"
}

excludeAlerts, err := strconv.ParseBool(excludeAlertsParam)
if err != nil {
excludeAlerts = false
}

rgs := make([]*RuleGroup, 0, len(ruleGroups))
for _, grp := range ruleGroups {
if len(rgSet) > 0 {
Expand Down Expand Up @@ -1414,6 +1425,12 @@ func (api *API) rules(r *http.Request) apiFuncResult {
if !returnAlerts {
break
}
var activeAlerts []*Alert
if excludeAlerts {
activeAlerts = []*Alert{}
} else {
activeAlerts = rulesAlertsToAPIAlerts(rule.ActiveAlerts())
}
enrichedRule = AlertingRule{
State: rule.State().String(),
Name: rule.Name(),
Expand All @@ -1422,7 +1439,7 @@ func (api *API) rules(r *http.Request) apiFuncResult {
KeepFiringFor: rule.KeepFiringFor().Seconds(),
Labels: rule.Labels(),
Annotations: rule.Annotations(),
Alerts: rulesAlertsToAPIAlerts(rule.ActiveAlerts()),
Alerts: activeAlerts,
Health: rule.Health(),
LastError: lastError,
EvaluationTime: rule.GetEvaluationDuration().Seconds(),
Expand Down
Loading

0 comments on commit de9efc6

Please sign in to comment.