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
import "sort"
func sliceContains(s []string, e string) bool {
for _, v := range s {
if e == v {
@ -9,3 +11,13 @@ func sliceContains(s []string, e string) bool {
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
n.body = "Labels:\n"
for key, value := range alert.Labels {
n.body = fmt.Sprintf("%s%s = %s\n", n.body, key, value)
sortedLabelKeys := sortKeys(alert.Labels)
for _, key := range sortedLabelKeys {
n.body = fmt.Sprintf("%s%s = %s\n", n.body, key, alert.Labels[key])
}
n.body += "\nAnnotations:\n"
@ -168,8 +169,10 @@ func (br *bridge) multiAlertNotification(p *payload) *notification {
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)
sortedLabelKeys := sortKeys(alert.Labels)
for _, key := range sortedLabelKeys {
alertBody = fmt.Sprintf("%s%s = %s\n", alertBody, key, alert.Labels[key])
}
alertBody += "Annotations:\n"