feat(alerting): add monitor alert groups with default routing

This commit is contained in:
Sebastian Unterschütz committed 2026-11-22 18:25:36 +01:00
1 parent 52819e64bb
commit 8544e5a585
26 files changed
+2938 -1473

No files matched your search

+44 -2
View File
@@ -36,6 +36,9 @@ type monitorRequest struct {
Confirmations int `json:"confirmations" binding:"min=0,max=10"`
// GroupID places the monitor in a group; nil keeps it at the top level.
GroupID *uuid.UUID `json:"group_id"`
// AlertGroupIDs defaults on creation and preserves assignments on update
// when omitted. An explicit empty list disables notifications.
AlertGroupIDs []uuid.UUID `json:"alert_group_ids" binding:"max=100"`
}
type latestView struct {
@@ -48,6 +51,7 @@ type latestView struct {
}
type monitorView struct {
AlertGroupIDs []uuid.UUID `json:"alert_group_ids"`
ID uuid.UUID `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
@@ -192,6 +196,14 @@ func (s *Server) listMonitors(c *gin.Context) {
if err != nil {
return err
}
assignments, err := q.AllMonitorAlertGroups(ctx)
if err != nil {
return err
}
alertGroups := map[uuid.UUID][]uuid.UUID{}
for _, a := range assignments {
alertGroups[a.MonitorID] = append(alertGroups[a.MonitorID], a.GroupID)
}
regions, err := q.AllMonitorRegions(ctx)
if err != nil {
return err
@@ -215,6 +227,10 @@ func (s *Server) listMonitors(c *gin.Context) {
continue
}
v := viewMonitor(m, byMonitor[m.ID])
v.AlertGroupIDs = alertGroups[m.ID]
if v.AlertGroupIDs == nil {
v.AlertGroupIDs = []uuid.UUID{}
}
if !canInGroup(p, authz.MonitorsWrite, t, m.GroupID) {
v = redactMonitor(v)
}
@@ -266,8 +282,19 @@ func (s *Server) createMonitor(c *gin.Context) {
if err != nil {
return err
}
if err := setMonitorAlertGroups(ctx, q, m, req.AlertGroupIDs); err != nil {
return err
}
req.AlertGroupIDs, err = q.MonitorAlertGroups(ctx, m.ID)
if err != nil {
return err
}
return setRegions(ctx, q, m, req.Regions)
})
if errors.Is(err, errInvalidAlertGroup) {
fail(c, http.StatusUnprocessableEntity, "invalid_alert_group")
return
}
if err != nil {
internalError(c, err)
return
@@ -276,7 +303,9 @@ func (s *Server) createMonitor(c *gin.Context) {
internalError(c, err)
return
}
c.JSON(http.StatusCreated, s.withPingURL(viewMonitor(m, req.Regions)))
v := viewMonitor(m, req.Regions)
v.AlertGroupIDs = req.AlertGroupIDs
c.JSON(http.StatusCreated, s.withPingURL(v))
}
func setRegions(ctx context.Context, q *store.Queries, m store.Monitor, regions []uuid.UUID) error {
@@ -372,8 +401,19 @@ func (s *Server) updateMonitor(c *gin.Context) {
if err != nil {
return err
}
if err := setMonitorAlertGroups(ctx, q, m, req.AlertGroupIDs); err != nil {
return err
}
req.AlertGroupIDs, err = q.MonitorAlertGroups(ctx, m.ID)
if err != nil {
return err
}
return setRegions(ctx, q, m, req.Regions)
})
if errors.Is(err, errInvalidAlertGroup) {
fail(c, http.StatusUnprocessableEntity, "invalid_alert_group")
return
}
if errors.Is(err, errCheckTypeDisabled) {
fail(c, http.StatusUnprocessableEntity, "check_type_disabled")
return
@@ -390,7 +430,9 @@ func (s *Server) updateMonitor(c *gin.Context) {
internalError(c, err)
return
}
c.JSON(http.StatusOK, s.withPingURL(viewMonitor(m, req.Regions)))
v := viewMonitor(m, req.Regions)
v.AlertGroupIDs = req.AlertGroupIDs
c.JSON(http.StatusOK, s.withPingURL(v))
}
func (s *Server) deleteMonitor(c *gin.Context) {