feat(api): expose the number of matched alerts on /silences.json

This commit is contained in:
Łukasz Mierzwa
2019-10-30 14:18:47 +00:00
parent 1a7ec459dc
commit 67e473af54
2 changed files with 23 additions and 3 deletions

View File

@@ -475,6 +475,25 @@ func silences(c *gin.Context) {
return dedupedSilences[i].Silence.EndsAt.Before(dedupedSilences[j].Silence.EndsAt) == recentFirst
})
silenceCounters := make(map[string]int, len(dedupedSilences))
for _, silence := range dedupedSilences {
silenceCounters[silence.Silence.ID] = 0
}
for _, alertGroup := range alertmanager.DedupAlerts() {
for _, alert := range alertGroup.Alerts {
for _, sID := range alert.SilencedBy {
if _, ok := silenceCounters[sID]; ok {
silenceCounters[sID]++
}
}
}
}
for i := range dedupedSilences {
if counter, ok := silenceCounters[dedupedSilences[i].Silence.ID]; ok {
dedupedSilences[i].AlertCount = counter
}
}
data, err := json.Marshal(dedupedSilences)
if err != nil {
log.Error(err.Error())

View File

@@ -27,7 +27,8 @@ type Silence struct {
// ManagedSilence is a standalone silence detached from any alert
type ManagedSilence struct {
Cluster string `json:"cluster"`
IsExpired bool `json:"isExpired"`
Silence Silence `json:"silence"`
Cluster string `json:"cluster"`
IsExpired bool `json:"isExpired"`
AlertCount int `json:"alertCount"`
Silence Silence `json:"silence"`
}