Support sending grouped alerts on their own

This commit is contained in:
Thorben Günther 2023-02-03 17:35:38 +01:00
parent 46f4590d11
commit 8933ccd825
No known key found for this signature in database
GPG key ID: 415CD778D8C5AFED

73
main.go
View file

@ -49,7 +49,64 @@ type notification struct {
tags string tags string
} }
func (rcv *receiver) newNotification(p *payload) *notification { func (rcv *receiver) singleAlertNotifications(p *payload) []*notification {
var notifications []*notification
for _, alert := range p.Alerts {
n := new(notification)
// create title
n.title = fmt.Sprintf("[%s]", strings.ToUpper(alert.Status))
if name, ok := alert.Labels["alertname"]; ok {
n.title = fmt.Sprintf("%s %s", n.title, name)
}
for _, value := range p.GroupLabels {
n.title = fmt.Sprintf("%s %s", n.title, value)
}
// create body
n.body = "Labels:\n"
for key, value := range alert.Labels {
n.body = fmt.Sprintf("%s%s = %s\n", n.body, key, value)
}
n.body += "\nAnnotations:\n"
for key, value := range alert.Annotations {
n.body = fmt.Sprintf("%s%s = %s\n", n.body, key, value)
}
var tags []string
for _, labelName := range rcv.cfg.labels.Order {
val, ok := alert.Labels[labelName]
if !ok {
continue
}
labelConfig, ok := rcv.cfg.labels.Label[fmt.Sprintf("%s:%s", labelName, val)]
if !ok {
continue
}
if n.priority == "" {
n.priority = labelConfig.Priority
}
for _, val := range labelConfig.Tags {
if !sliceContains(tags, val) {
tags = append(tags, val)
}
}
}
n.tags = strings.Join(tags, ",")
notifications = append(notifications, n)
}
return notifications
}
func (rcv *receiver) multiAlertNotification(p *payload) *notification {
n := new(notification) n := new(notification)
// create title // create title
@ -175,10 +232,20 @@ func (rcv *receiver) handleWebhooks(w http.ResponseWriter, r *http.Request) {
return return
} }
notification := rcv.newNotification(&event) if rcv.cfg.alertMode == single {
notifications := rcv.singleAlertNotifications(&event)
for _, n := range notifications {
err := rcv.publish(n)
if err != nil {
rcv.logger.Errorf("Failed to publish notification: %v", err)
}
}
} else {
notification := rcv.multiAlertNotification(&event)
err := rcv.publish(notification) err := rcv.publish(notification)
if err != nil { if err != nil {
rcv.logger.Error(err) rcv.logger.Errorf("Failed to publish notification: %v", err)
}
} }
} }