feat(api): store silences per cluster in the API response

This commit is contained in:
Łukasz Mierzwa
2018-12-01 09:53:47 +00:00
parent d668a64ffb
commit 0d68ea4c3c
5 changed files with 31 additions and 5 deletions
+1
View File
@@ -55,6 +55,7 @@ func getUpstreams() models.AlertmanagerAPISummary {
PublicURI: upstream.PublicURI(),
Error: upstream.Error(),
Version: upstream.Version(),
Cluster: upstream.ClusterID(),
ClusterMembers: members,
}
summary.Instances = append(summary.Instances, u)
+2 -2
View File
@@ -867,9 +867,9 @@ func TestVerifyAllGroups(t *testing.T) {
}
}
am, foundAM := ur.Silences["default"]
am, foundAM := ur.Silences["843c4a11660fe38ea61e6960a29d4f4796da6488"]
if !foundAM {
t.Errorf("[%s] Alertmanager 'default' missing from silences", version)
t.Errorf("[%s] Alertmanager cluster '843c4a11660fe38ea61e6960a29d4f4796da6488' (default) missing from silences", version)
} else if len(am) == 0 {
t.Errorf("[%s] Silences mismatch, expected >0 but got %d", version, len(am))
}
+13
View File
@@ -284,6 +284,7 @@ func (am *Alertmanager) pullAlerts(version string) error {
alert.Alertmanager = []models.AlertmanagerInstance{
models.AlertmanagerInstance{
Name: am.Name,
Cluster: am.ClusterID(),
State: alert.State,
StartsAt: alert.StartsAt,
EndsAt: alert.EndsAt,
@@ -503,3 +504,15 @@ func (am *Alertmanager) ClusterMemberNames() []string {
sort.Strings(members)
return members
}
// ClusterID returns the ID (sha1) of the cluster this Alertmanager instance
// belongs to
func (am *Alertmanager) ClusterID() string {
members := am.ClusterMemberNames()
id, err := slices.StringSliceToSHA1(members)
if err != nil {
log.Errorf("slices.StringSliceToSHA1 error: %s", err)
return am.Name
}
return id
}
+3 -1
View File
@@ -5,7 +5,8 @@ import "time"
// AlertmanagerInstance describes the Alertmanager instance alert was collected
// from
type AlertmanagerInstance struct {
Name string `json:"name"`
Name string `json:"name"`
Cluster string `json:"cluster"`
// per instance alert state
State string `json:"state"`
// timestamp collected from this instance, those on the alert itself
@@ -32,6 +33,7 @@ type AlertmanagerAPIStatus struct {
PublicURI string `json:"publicURI"`
Error string `json:"error"`
Version string `json:"version"`
Cluster string `json:"cluster"`
ClusterMembers []string `json:"clusterMembers"`
}
+12 -2
View File
@@ -111,9 +111,15 @@ func alerts(c *gin.Context) {
dedupedAlerts := alertmanager.DedupAlerts()
dedupedColors := alertmanager.DedupColors()
amNameToCluster := map[string]string{}
silences := map[string]map[string]models.Silence{}
for _, am := range alertmanager.GetAlertmanagers() {
silences[am.Name] = map[string]models.Silence{}
key := am.ClusterID()
amNameToCluster[am.Name] = key
_, found := silences[key]
if !found {
silences[key] = map[string]models.Silence{}
}
}
var matches int
@@ -189,8 +195,12 @@ func alerts(c *gin.Context) {
for _, alert := range agCopy.Alerts {
if alert.IsSilenced() {
for _, am := range alert.Alertmanager {
key := amNameToCluster[am.Name]
for _, silence := range am.Silences {
silences[am.Name][silence.ID] = *silence
_, found := silences[key][silence.ID]
if !found {
silences[key][silence.ID] = *silence
}
}
}
}