Sort labels alphabetically

Closes: https://todo.xenrox.net/~xenrox/ntfy-alertmanager/13
This commit is contained in:
Thorben Günther 2023-02-21 14:37:26 +01:00
parent 82a22c8959
commit 5a8a85f0f9
No known key found for this signature in database
GPG key ID: 415CD778D8C5AFED
2 changed files with 19 additions and 4 deletions

View file

@ -1,5 +1,7 @@
package main package main
import "sort"
func sliceContains(s []string, e string) bool { func sliceContains(s []string, e string) bool {
for _, v := range s { for _, v := range s {
if e == v { if e == v {
@ -9,3 +11,13 @@ func sliceContains(s []string, e string) bool {
return false return false
} }
func sortKeys(m map[string]string) []string {
var s []string
for key := range m {
s = append(s, key)
}
sort.Strings(s)
return s
}

11
main.go
View file

@ -79,8 +79,9 @@ func (br *bridge) singleAlertNotifications(p *payload) []*notification {
// create body // create body
n.body = "Labels:\n" n.body = "Labels:\n"
for key, value := range alert.Labels { sortedLabelKeys := sortKeys(alert.Labels)
n.body = fmt.Sprintf("%s%s = %s\n", n.body, key, value) for _, key := range sortedLabelKeys {
n.body = fmt.Sprintf("%s%s = %s\n", n.body, key, alert.Labels[key])
} }
n.body += "\nAnnotations:\n" n.body += "\nAnnotations:\n"
@ -168,8 +169,10 @@ func (br *bridge) multiAlertNotification(p *payload) *notification {
for _, alert := range p.Alerts { for _, alert := range p.Alerts {
alertBody := fmt.Sprintf("%s\nLabels:\n", c.String(alert.Status)) 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) sortedLabelKeys := sortKeys(alert.Labels)
for _, key := range sortedLabelKeys {
alertBody = fmt.Sprintf("%s%s = %s\n", alertBody, key, alert.Labels[key])
} }
alertBody += "Annotations:\n" alertBody += "Annotations:\n"