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
|
||||
}
|
||||
|
||||
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)
|
||||
|
||||
// create title
|
||||
|
@ -175,10 +232,20 @@ func (rcv *receiver) handleWebhooks(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
notification := rcv.newNotification(&event)
|
||||
err := rcv.publish(notification)
|
||||
if err != nil {
|
||||
rcv.logger.Error(err)
|
||||
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)
|
||||
if err != nil {
|
||||
rcv.logger.Errorf("Failed to publish notification: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue