Move notification creation into new function
This commit is contained in:
parent
5f187d1311
commit
db01ca2f2f
1 changed files with 69 additions and 57 deletions
126
main.go
126
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)
|
||||
|
|
Loading…
Reference in a new issue