feat(notify): name regions and link the console in alerts; consent to subscribe
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m8s
ci / test (pull_request) Successful in 13m45s
ci / build (pull_request) Successful in 19m0s
ci / image (pull_request) Successful in 6m5s
ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m8s
ci / test (pull_request) Successful in 13m45s
ci / build (pull_request) Successful in 19m0s
ci / image (pull_request) Successful in 6m5s
Alert mails and chat messages named regions by id. They now show the region's name and mails link the monitor in the console. Visitors of a status page accept the platform's terms and the page's privacy policy, where set, before they can subscribe to it; the status page server and the API both check it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
cca4e80e54
commit
2d4bd10c9b
16 files changed
+660
-479
No files matched your search
@@ -15,6 +15,7 @@ import (
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/evaluate"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/notify"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
||||
@@ -197,9 +198,40 @@ func eventOf(n store.NotificationDetailsRow) notify.Event {
|
||||
Monitor: notify.Monitor{ID: n.MonitorID, Name: n.MonitorName, Type: n.MonitorType, Target: n.MonitorTarget},
|
||||
}
|
||||
_ = json.Unmarshal(n.Cause, &ev.Cause)
|
||||
_ = json.Unmarshal(n.RegionNames, &ev.RegionNames)
|
||||
return ev
|
||||
}
|
||||
|
||||
// causeLines describes the errors of ev per region, sorted, naming each
|
||||
// region as people know it.
|
||||
func causeLines(ev notify.Event, l *i18n.Localizer) []string {
|
||||
var out []string
|
||||
for region, msg := range ev.Cause {
|
||||
name, ok := ev.RegionNames[region]
|
||||
switch {
|
||||
case ok:
|
||||
case region == heartbeatRegion:
|
||||
name = l.T("monitors.type_heartbeat", nil)
|
||||
case region == alertmanagerRegion:
|
||||
name = "Alertmanager"
|
||||
default:
|
||||
name = region
|
||||
}
|
||||
out = append(out, name+": "+msg)
|
||||
}
|
||||
slices.Sort(out)
|
||||
return out
|
||||
}
|
||||
|
||||
// monitorLink is the address of ev's monitor in the console, or "" if
|
||||
// the platform's own address is unknown.
|
||||
func (s *Server) monitorLink(ev notify.Event) string {
|
||||
if ev.Monitor.ID == (uuid.UUID{}) || !absoluteURL(s.cfg.PublicURL) {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSuffix(s.cfg.PublicURL, "/") + "/monitors/" + ev.Monitor.ID.String()
|
||||
}
|
||||
|
||||
// webhookConfig is stored for webhook channels; Secret is sealed.
|
||||
type webhookConfig struct {
|
||||
URL string `json:"url"`
|
||||
@@ -257,18 +289,17 @@ func (s *Server) mailFor(ev notify.Event, to []string, locale string) notify.Mai
|
||||
"Target": ev.Monitor.Target,
|
||||
"Since": ev.StartedAt.UTC().Format("2006-01-02 15:04 MST"),
|
||||
}
|
||||
var cause []string
|
||||
for region, msg := range ev.Cause {
|
||||
cause = append(cause, region+": "+msg)
|
||||
}
|
||||
slices.Sort(cause)
|
||||
data["Cause"] = strings.Join(cause, "\n")
|
||||
data["Cause"] = strings.Join(causeLines(ev, l), "\n")
|
||||
if ev.ResolvedAt != nil {
|
||||
data["Duration"] = ev.ResolvedAt.Sub(ev.StartedAt).Round(time.Second).String()
|
||||
}
|
||||
body := l.T("email."+ev.Kind+".body", data)
|
||||
if link := s.monitorLink(ev); link != "" {
|
||||
body += "\n\n" + l.T("email.monitor_link", map[string]any{"Link": link})
|
||||
}
|
||||
return notify.Mail{
|
||||
To: to,
|
||||
Subject: l.T("email."+ev.Kind+".subject", data),
|
||||
Body: l.T("email."+ev.Kind+".body", data),
|
||||
Body: body,
|
||||
}
|
||||
}
|
||||
@@ -128,6 +128,8 @@ func TestAlerting(t *testing.T) {
|
||||
}
|
||||
if msg := mail.Messages()[0].Data; !strings.Contains(msg, "Subject: [DOWN] Shop") || !strings.Contains(msg, "ist in allen Regionen nicht erreichbar") {
|
||||
t.Errorf("down mail = %q, want German down notice", msg)
|
||||
} else if strings.Contains(msg, eu) || strings.Contains(msg, us) || !strings.Contains(msg, "/monitors/"+monitorID) {
|
||||
t.Errorf("down mail = %q, want region names instead of ids and a link to the monitor", msg)
|
||||
}
|
||||
|
||||
healthy.Store(true)
|
||||
|
||||
@@ -134,10 +134,7 @@ func (s *Server) messageFor(ev notify.Event, locale string) notify.Message {
|
||||
switch ev.Kind {
|
||||
case notify.Opened:
|
||||
m.Title = l.T("notify.title_opened", data)
|
||||
for region, msg := range ev.Cause {
|
||||
lines = append(lines, region+": "+msg)
|
||||
}
|
||||
slices.Sort(lines)
|
||||
lines = causeLines(ev, l)
|
||||
case notify.Resolved:
|
||||
m.Title = l.T("notify.title_resolved", data)
|
||||
if ev.ResolvedAt != nil {
|
||||
@@ -151,8 +148,6 @@ func (s *Server) messageFor(ev notify.Event, locale string) notify.Message {
|
||||
lines = append([]string{ev.Monitor.Type + " · " + ev.Monitor.Target}, lines...)
|
||||
}
|
||||
m.Body = strings.Join(lines, "\n")
|
||||
if ev.Monitor.ID != [16]byte{} {
|
||||
m.URL = strings.TrimSuffix(s.cfg.PublicURL, "/") + "/monitors/" + ev.Monitor.ID.String()
|
||||
}
|
||||
m.URL = s.monitorLink(ev)
|
||||
return m
|
||||
}
|
||||
@@ -128,8 +128,9 @@ func (s *Server) buildSnapshot(ctx context.Context, page store.StatusPage) (agen
|
||||
return err
|
||||
}
|
||||
docs, err := q.LegalDocuments(ctx, &page.OrgID)
|
||||
// Visitors of status pages accept no terms.
|
||||
snap.Legal = slices.DeleteFunc(mergeLegal(docs), func(d agentapi.LegalDoc) bool { return d.Kind == "terms" })
|
||||
// Visitors of status pages accept the platform's terms only when
|
||||
// they subscribe to it.
|
||||
snap.Legal = slices.DeleteFunc(mergeLegal(docs), func(d agentapi.LegalDoc) bool { return d.Kind == "terms" && !snap.Subscribe })
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
|
||||
@@ -134,6 +134,10 @@ func (s *Server) agentSubscribe(c *gin.Context) {
|
||||
fail(c, http.StatusUnprocessableEntity, "invalid_email")
|
||||
return
|
||||
}
|
||||
if !req.Accepted {
|
||||
fail(c, http.StatusUnprocessableEntity, "consent_required")
|
||||
return
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
a := agentOf(c)
|
||||
var host, slug *string
|
||||
|
||||
@@ -95,13 +95,28 @@ func TestStatusPageSubscriptions(t *testing.T) {
|
||||
return out
|
||||
}
|
||||
|
||||
if code, html, _ := visit("GET", "/"+slug, ""); code != 200 || !strings.Contains(html, `action="/`+slug+`/subscribe"`) || !strings.Contains(html, "feed.rss") {
|
||||
// Subscribers accept the page's privacy policy.
|
||||
if st, body := owner.do("PUT", "/api/v1/org/legal/privacy", map[string]any{"url": "https://shop.example/privacy"}); st != http.StatusNoContent {
|
||||
t.Fatalf("set privacy policy = %d %v", st, body)
|
||||
}
|
||||
t.Cleanup(func() { owner.do("PUT", "/api/v1/org/legal/privacy", map[string]any{}) })
|
||||
pages.Sync(ctx)
|
||||
code, html, _ := visit("GET", "/"+slug, "")
|
||||
if code != 200 || !strings.Contains(html, `action="/`+slug+`/subscribe"`) || !strings.Contains(html, "feed.rss") {
|
||||
t.Fatalf("page = %d, want the subscription form and the feed link", code)
|
||||
}
|
||||
if code, html, _ := visit("POST", "/"+slug+"/subscribe", "email=not-an-address"); code != http.StatusUnprocessableEntity {
|
||||
if !strings.Contains(html, `name="accept"`) || !strings.Contains(html, `href="https://shop.example/privacy"`) {
|
||||
t.Error("the subscription form does not ask to accept the privacy policy")
|
||||
}
|
||||
if code, html, _ := visit("POST", "/"+slug+"/subscribe", "email=not-an-address&accept=1"); code != http.StatusUnprocessableEntity {
|
||||
t.Errorf("subscribing an invalid address = %d %.200s", code, html)
|
||||
}
|
||||
if code, _, _ := visit("POST", "/"+slug+"/subscribe", "email=Visitor@Example.test"); code != 200 {
|
||||
visitor = "198.51.100.9:1234" // (not to use up the subscription requests of the first)
|
||||
if code, _, _ := visit("POST", "/"+slug+"/subscribe", "email=visitor@example.test"); code != http.StatusUnprocessableEntity {
|
||||
t.Errorf("subscribing without accepting the privacy policy = %d, want 422", code)
|
||||
}
|
||||
visitor = "198.51.100.7:1234"
|
||||
if code, _, _ := visit("POST", "/"+slug+"/subscribe", "email=Visitor@Example.test&accept=1"); code != 200 {
|
||||
t.Fatalf("subscribe = %d", code)
|
||||
}
|
||||
if c, p := subscribers(); c != 0 || p != 1 {
|
||||
@@ -134,7 +149,7 @@ func TestStatusPageSubscriptions(t *testing.T) {
|
||||
t.Errorf("subscribers after confirming = %d, %d; want 1, 0", c, p)
|
||||
}
|
||||
// Asking again for a confirmed address mails nothing.
|
||||
visit("POST", "/"+slug+"/subscribe", "email=visitor@example.test")
|
||||
visit("POST", "/"+slug+"/subscribe", "email=visitor@example.test&accept=1")
|
||||
if n := len(mailsTo("visitor@example.test")); n != 1 {
|
||||
t.Errorf("mails after subscribing a confirmed address again = %d, want 1", n)
|
||||
}
|
||||
@@ -199,10 +214,10 @@ func TestStatusPageSubscriptions(t *testing.T) {
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if code, _, _ := visit("POST", "/"+slug+"/subscribe", "email=second@example.test"); code != 200 {
|
||||
if code, _, _ := visit("POST", "/"+slug+"/subscribe", "email=second@example.test&accept=1"); code != 200 {
|
||||
t.Errorf("subscription within the weekly limit = %d", code)
|
||||
}
|
||||
if code, _, _ := visit("POST", "/"+slug+"/subscribe", "email=third@example.test"); code != http.StatusTooManyRequests {
|
||||
if code, _, _ := visit("POST", "/"+slug+"/subscribe", "email=third@example.test&accept=1"); code != http.StatusTooManyRequests {
|
||||
t.Errorf("subscription beyond the weekly limit = %d, want 429", code)
|
||||
}
|
||||
if n := len(mailsTo("third@example.test")); n != 0 {
|
||||
@@ -218,13 +233,13 @@ func TestStatusPageSubscriptions(t *testing.T) {
|
||||
if code, html, _ := visit("GET", "/"+slug, ""); code != 200 || strings.Contains(html, "/subscribe\"") {
|
||||
t.Errorf("page without subscriptions = %d, still offers the form", code)
|
||||
}
|
||||
if code, _, _ := visit("POST", "/"+slug+"/subscribe", "email=fourth@example.test"); code != http.StatusNotFound {
|
||||
if code, _, _ := visit("POST", "/"+slug+"/subscribe", "email=fourth@example.test&accept=1"); code != http.StatusNotFound {
|
||||
t.Errorf("subscribing where it is off = %d, want 404", code)
|
||||
}
|
||||
|
||||
// This visitor used up its subscription requests on the server.
|
||||
visitor = "198.51.100.7:1234"
|
||||
if code, _, _ := visit("POST", "/"+slug+"/subscribe", "email=fifth@example.test"); code != http.StatusTooManyRequests {
|
||||
if code, _, _ := visit("POST", "/"+slug+"/subscribe", "email=fifth@example.test&accept=1"); code != http.StatusTooManyRequests {
|
||||
t.Errorf("sixth subscription request of a visitor = %d, want 429", code)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user