feat(alerting): add quiet hours to alert groups
This commit is contained in:
1 parent
bf529a7711
commit
ee3769f578
21 files changed
+1310
-130
No files matched your search
@@ -152,3 +152,137 @@ func TestAlertGroups(t *testing.T) {
|
||||
t.Fatalf("explicit empty creation = %d %v", st, body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAlertGroupQuietHours(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
owner := e.browser(t)
|
||||
owner.signup("q@"+uniq("agq")+".test", uniq("agq-"))
|
||||
channel := func(name string) string {
|
||||
t.Helper()
|
||||
st, body := owner.do("POST", "/api/v1/org/channels", map[string]any{"name": name, "type": "webhook", "url": "https://example.test/hook", "enabled": true})
|
||||
if st != http.StatusCreated {
|
||||
t.Fatalf("create channel = %d %v", st, body)
|
||||
}
|
||||
return body["channel"].(map[string]any)["id"].(string)
|
||||
}
|
||||
quietCh := channel("QuietOnly")
|
||||
sharedCh := channel("Shared")
|
||||
activeCh := channel("ActiveOnly")
|
||||
|
||||
// Invalid quiet hours are rejected.
|
||||
st, body := owner.do("POST", "/api/v1/org/alert-groups", map[string]any{
|
||||
"name": "BadTZ",
|
||||
"channel_ids": []string{quietCh},
|
||||
"quiet_hours": []map[string]any{{"start": "22:00", "end": "07:00", "time_zone": "Not/AZone"}},
|
||||
})
|
||||
expect(t, "invalid timezone", st, body, http.StatusUnprocessableEntity, "invalid_time_zone")
|
||||
|
||||
st, body = owner.do("POST", "/api/v1/org/alert-groups", map[string]any{
|
||||
"name": "BadRange",
|
||||
"channel_ids": []string{quietCh},
|
||||
"quiet_hours": []map[string]any{{"start": "12:00", "end": "12:00", "time_zone": "UTC"}},
|
||||
})
|
||||
expect(t, "equal non-zero times", st, body, http.StatusUnprocessableEntity, "invalid_quiet_hours")
|
||||
|
||||
// Create a group that is muted 24/7 (00:00-00:00 every day) and an active group.
|
||||
st, body = owner.do("POST", "/api/v1/org/alert-groups", map[string]any{
|
||||
"name": "Muted",
|
||||
"channel_ids": []string{quietCh, sharedCh},
|
||||
"quiet_hours": []map[string]any{{"weekdays": []string{}, "start": "00:00", "end": "00:00", "time_zone": "UTC"}},
|
||||
})
|
||||
if st != http.StatusCreated {
|
||||
t.Fatalf("create muted group = %d %v", st, body)
|
||||
}
|
||||
mutedGroup := body["id"].(string)
|
||||
if qh := body["quiet_hours"].([]any); len(qh) != 1 {
|
||||
t.Fatalf("quiet_hours = %v", qh)
|
||||
}
|
||||
|
||||
// Updating without quiet_hours preserves existing quiet hours.
|
||||
st, body = owner.do("PUT", "/api/v1/org/alert-groups/"+mutedGroup, map[string]any{
|
||||
"name": "Muted Renamed",
|
||||
"channel_ids": []string{quietCh, sharedCh},
|
||||
})
|
||||
if st != http.StatusOK || len(body["quiet_hours"].([]any)) != 1 {
|
||||
t.Fatalf("preserve quiet_hours = %d %v", st, body)
|
||||
}
|
||||
|
||||
// Settings page shows the quiet hours inputs and active badge.
|
||||
st, _, html := owner.page("/settings/channels")
|
||||
if st != http.StatusOK || !strings.Contains(html, `name="quiet_hours.0.start"`) || !strings.Contains(html, "Quiet hours active") {
|
||||
t.Fatalf("settings page = %d, lacks quiet hours UI", st)
|
||||
}
|
||||
|
||||
st, body = owner.do("POST", "/api/v1/org/alert-groups", map[string]any{
|
||||
"name": "AlwaysOn",
|
||||
"channel_ids": []string{activeCh, sharedCh},
|
||||
"quiet_hours": []map[string]any{},
|
||||
})
|
||||
if st != http.StatusCreated {
|
||||
t.Fatalf("create active group = %d %v", st, body)
|
||||
}
|
||||
activeGroup := body["id"].(string)
|
||||
|
||||
// Monitor assigned only to Muted: incident opens & resolves, 0 notifications sent.
|
||||
st, body = owner.do("POST", "/api/v1/org/monitors", map[string]any{
|
||||
"name": "SilentBackup", "type": "heartbeat", "interval_seconds": 3600,
|
||||
"alert_group_ids": []string{mutedGroup},
|
||||
})
|
||||
if st != http.StatusCreated {
|
||||
t.Fatalf("create monitor = %d %v", st, body)
|
||||
}
|
||||
monID := body["id"].(string)
|
||||
token := body["target"].(string)
|
||||
ping := func(suffix string) {
|
||||
t.Helper()
|
||||
resp, err := e.srv.Client().Post(e.srv.URL+"/hb/"+token+suffix, "text/plain", strings.NewReader(""))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
_ = resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
t.Fatalf("heartbeat %q = %d", suffix, resp.StatusCode)
|
||||
}
|
||||
}
|
||||
count := func(ch string) int {
|
||||
return e.count(t, `SELECT count(*) FROM notifications n JOIN incidents i ON i.id=n.incident_id WHERE i.monitor_id=$1 AND n.channel_id=$2`, monID, ch)
|
||||
}
|
||||
ping("/fail")
|
||||
ping("")
|
||||
if inc := e.count(t, `SELECT count(*) FROM incidents WHERE monitor_id=$1`, monID); inc != 1 {
|
||||
t.Fatalf("incidents = %d, want 1", inc)
|
||||
}
|
||||
if count(quietCh) != 0 || count(sharedCh) != 0 {
|
||||
t.Fatalf("muted group sent notifications: quiet=%d shared=%d", count(quietCh), count(sharedCh))
|
||||
}
|
||||
|
||||
// Assign both Muted and AlwaysOn: only AlwaysOn's channels (activeCh + sharedCh) notify,
|
||||
// while quietCh stays silent.
|
||||
st, body = owner.do("PUT", "/api/v1/org/monitors/"+monID, map[string]any{
|
||||
"name": "SilentBackup", "type": "heartbeat", "interval_seconds": 3600,
|
||||
"alert_group_ids": []string{mutedGroup, activeGroup},
|
||||
})
|
||||
if st != http.StatusOK {
|
||||
t.Fatalf("update monitor = %d %v", st, body)
|
||||
}
|
||||
ping("/fail")
|
||||
ping("")
|
||||
if count(quietCh) != 0 || count(activeCh) != 2 || count(sharedCh) != 2 {
|
||||
t.Fatalf("mixed quiet/active deliveries quiet/active/shared = %d/%d/%d, want 0/2/2", count(quietCh), count(activeCh), count(sharedCh))
|
||||
}
|
||||
|
||||
// Explicitly clearing quiet_hours re-enables notifications on quietCh.
|
||||
st, body = owner.do("PUT", "/api/v1/org/alert-groups/"+mutedGroup, map[string]any{
|
||||
"name": "Muted Renamed",
|
||||
"channel_ids": []string{quietCh, sharedCh},
|
||||
"quiet_hours": []map[string]any{},
|
||||
})
|
||||
if st != http.StatusOK || len(body["quiet_hours"].([]any)) != 0 {
|
||||
t.Fatalf("clear quiet_hours = %d %v", st, body)
|
||||
}
|
||||
ping("/fail")
|
||||
ping("")
|
||||
if count(quietCh) != 2 || count(activeCh) != 4 || count(sharedCh) != 4 {
|
||||
t.Fatalf("after clearing quiet hours quiet/active/shared = %d/%d/%d, want 2/4/4", count(quietCh), count(activeCh), count(sharedCh))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user