Support sending grouped alerts on their own
This commit is contained in:
parent
46f4590d11
commit
8933ccd825
1 changed files with 72 additions and 5 deletions
77
main.go
77
main.go
|
@ -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 {
|
||||||
err := rcv.publish(notification)
|
notifications := rcv.singleAlertNotifications(&event)
|
||||||
if err != nil {
|
for _, n := range notifications {
|
||||||
rcv.logger.Error(err)
|
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)
|
||||||
|
if err != nil {
|
||||||
|
rcv.logger.Errorf("Failed to publish notification: %v", err)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue