feat(alert): summarize Grafana and Alertmanager payloads
Extract a readable title and severity from webhook bodies and keep the raw JSON available for the raw view.
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
package alert
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
const grafana = `{
|
||||
"status": "firing",
|
||||
"title": "DiskFull",
|
||||
"commonLabels": {"alertname": "DiskFull", "severity": "critical"},
|
||||
"commonAnnotations": {"summary": "disk > 90", "description": "host a"},
|
||||
"alerts": [
|
||||
{"status": "firing", "labels": {"alertname": "DiskFull", "severity": "critical"}, "annotations": {"summary": "disk > 90"}}
|
||||
]
|
||||
}`
|
||||
|
||||
func TestTitleSeverityPrefersNtfyTitle(t *testing.T) {
|
||||
title, sev := TitleSeverity("from ntfy", grafana)
|
||||
if title != "from ntfy" || sev != "firing" {
|
||||
t.Fatalf("%q %q", title, sev)
|
||||
}
|
||||
title, sev = TitleSeverity("", grafana)
|
||||
if title != "DiskFull" || sev != "firing" {
|
||||
t.Fatalf("%q %q", title, sev)
|
||||
}
|
||||
title, sev = TitleSeverity("", "plain body")
|
||||
if title != "plain body" || sev != "" {
|
||||
t.Fatalf("%q %q", title, sev)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTone(t *testing.T) {
|
||||
if Tone("firing", 3) != "bad" || Tone("resolved", 5) != "ok" || Tone("", 5) != "bad" || Tone("", 4) != "warn" || Tone("", 1) != "dim" {
|
||||
t.Fatal(Tone("firing", 3), Tone("resolved", 5), Tone("", 5), Tone("", 4), Tone("", 1))
|
||||
}
|
||||
}
|
||||
|
||||
func TestBodyView(t *testing.T) {
|
||||
view := BodyView(grafana, false)
|
||||
if !containsAll(view, "status: firing", "alert: DiskFull", "summary: disk > 90", "alerts:") {
|
||||
t.Fatalf("%s", view)
|
||||
}
|
||||
raw := BodyView(grafana, true)
|
||||
if !containsAll(raw, "\"commonLabels\"", "DiskFull") {
|
||||
t.Fatalf("%s", raw)
|
||||
}
|
||||
pretty := BodyView(`{"a":1}`, false)
|
||||
if !strings.Contains(pretty, "\"a\": 1") {
|
||||
t.Fatalf("%s", pretty)
|
||||
}
|
||||
}
|
||||
|
||||
func containsAll(s string, parts ...string) bool {
|
||||
for _, p := range parts {
|
||||
if !strings.Contains(s, p) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user