From db01ca2f2fe0a4a6dee34f0c7a0125312fe1d082 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thorben=20G=C3=BCnther?= Date: Wed, 2 Nov 2022 22:43:34 +0100 Subject: [PATCH] Move notification creation into new function --- main.go | 126 +++++++++++++++++++++++++++++++------------------------- 1 file changed, 69 insertions(+), 57 deletions(-) diff --git a/main.go b/main.go index 08412bd..7eab4a9 100644 --- a/main.go +++ b/main.go @@ -43,6 +43,74 @@ type notification struct { tags string } +func (rcv *receiver) newNotification(p *payload) *notification { + n := new(notification) + + // create title + count := len(p.Alerts) + title := fmt.Sprintf("[%s", strings.ToUpper(p.Status)) + if p.Status == "firing" { + title = fmt.Sprintf("%s:%d", title, count) + } + + title += "]" + for _, value := range p.GroupLabels { + title = fmt.Sprintf("%s %s", title, value) + } + n.title = title + + // create body + var body string + c := cases.Title(language.English) + + for _, alert := range p.Alerts { + alertBody := fmt.Sprintf("%s\nLabels:\n", c.String(alert.Status)) + for key, value := range alert.Labels { + alertBody = fmt.Sprintf("%s%s = %s\n", alertBody, key, value) + } + + alertBody += "Annotations:\n" + for key, value := range alert.Annotations { + alertBody = fmt.Sprintf("%s%s = %s\n", alertBody, key, value) + } + + alertBody += "\n" + + body += alertBody + } + n.body = body + + var priority string + var tags []string + for _, labelName := range rcv.cfg.labels.Order { + val, ok := p.CommonLabels[labelName] + if !ok { + continue + } + + labelConfig, ok := rcv.cfg.labels.Label[fmt.Sprintf("%s:%s", labelName, val)] + if !ok { + continue + } + + if priority == "" { + priority = labelConfig.Priority + } + + for _, val := range labelConfig.Tags { + if !sliceContains(tags, val) { + tags = append(tags, val) + } + } + } + + tagString := strings.Join(tags, ",") + n.priority = priority + n.tags = tagString + + return n +} + func (rcv *receiver) publish(n *notification) error { client := &http.Client{Timeout: time.Second * 3} req, err := http.NewRequest(http.MethodPost, rcv.cfg.ntfy.Topic, strings.NewReader(n.body)) @@ -101,63 +169,7 @@ func (rcv *receiver) handleWebhooks(w http.ResponseWriter, r *http.Request) { return } - count := len(event.Alerts) - title := fmt.Sprintf("[%s", strings.ToUpper(event.Status)) - if event.Status == "firing" { - title = fmt.Sprintf("%s:%d", title, count) - } - - title += "]" - for _, value := range event.GroupLabels { - title = fmt.Sprintf("%s %s", title, value) - } - - var body string - c := cases.Title(language.English) - - for _, alert := range event.Alerts { - alertBody := fmt.Sprintf("%s\nLabels:\n", c.String(alert.Status)) - for key, value := range alert.Labels { - alertBody = fmt.Sprintf("%s%s = %s\n", alertBody, key, value) - } - - alertBody += "Annotations:\n" - for key, value := range alert.Annotations { - alertBody = fmt.Sprintf("%s%s = %s\n", alertBody, key, value) - } - - alertBody += "\n" - - body += alertBody - } - - var priority string - var tags []string - for _, labelName := range rcv.cfg.labels.Order { - val, ok := event.CommonLabels[labelName] - if !ok { - continue - } - - labelConfig, ok := rcv.cfg.labels.Label[fmt.Sprintf("%s:%s", labelName, val)] - if !ok { - continue - } - - if priority == "" { - priority = labelConfig.Priority - } - - for _, val := range labelConfig.Tags { - if !sliceContains(tags, val) { - tags = append(tags, val) - } - } - } - - tagString := strings.Join(tags, ",") - - notification := ¬ification{title: title, body: body, priority: priority, tags: tagString} + notification := rcv.newNotification(&event) err := rcv.publish(notification) if err != nil { rcv.logger.Error(err)