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
  • Loading branch information
rajagopalanand committed Oct 9, 2023
1 parent ea3ad74 commit 4d7141f
Show file tree
Hide file tree
Showing 2 changed files with 279 additions and 26 deletions.
20 changes: 19 additions & 1 deletion web/api/v1/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1373,6 +1373,18 @@ 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 +1426,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 +1440,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 4d7141f

Please sign in to comment.