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

+240
View File
@@ -0,0 +1,240 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.31.1
// source: alert_groups.sql
package store
import (
"context"
"github.com/google/uuid"
)
const addAlertGroupChannel = `-- name: AddAlertGroupChannel :execrows
INSERT INTO alert_group_channels (org_id, group_id, channel_id)
SELECT c.org_id, $1, c.id FROM notification_channels c
WHERE c.id = $2 AND c.org_id = $3
ON CONFLICT DO NOTHING
`
type AddAlertGroupChannelParams struct {
GroupID uuid.UUID
ChannelID uuid.UUID
OrgID uuid.UUID
}
func (q *Queries) AddAlertGroupChannel(ctx context.Context, arg AddAlertGroupChannelParams) (int64, error) {
result, err := q.db.Exec(ctx, addAlertGroupChannel, arg.GroupID, arg.ChannelID, arg.OrgID)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const addMonitorAlertGroup = `-- name: AddMonitorAlertGroup :execrows
INSERT INTO monitor_alert_groups (org_id, monitor_id, group_id)
SELECT g.org_id, $1, g.id FROM alert_groups g
WHERE g.id = $2 AND g.org_id = $3
ON CONFLICT DO NOTHING
`
type AddMonitorAlertGroupParams struct {
MonitorID uuid.UUID
GroupID uuid.UUID
OrgID uuid.UUID
}
func (q *Queries) AddMonitorAlertGroup(ctx context.Context, arg AddMonitorAlertGroupParams) (int64, error) {
result, err := q.db.Exec(ctx, addMonitorAlertGroup, arg.MonitorID, arg.GroupID, arg.OrgID)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const allMonitorAlertGroups = `-- name: AllMonitorAlertGroups :many
SELECT org_id, monitor_id, group_id FROM monitor_alert_groups ORDER BY monitor_id, group_id
`
func (q *Queries) AllMonitorAlertGroups(ctx context.Context) ([]MonitorAlertGroup, error) {
rows, err := q.db.Query(ctx, allMonitorAlertGroups)
if err != nil {
return nil, err
}
defer rows.Close()
items := []MonitorAlertGroup{}
for rows.Next() {
var i MonitorAlertGroup
if err := rows.Scan(&i.OrgID, &i.MonitorID, &i.GroupID); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const createAlertGroup = `-- name: CreateAlertGroup :one
INSERT INTO alert_groups (org_id, name) VALUES ($1, $2) RETURNING id, org_id, name, is_default
`
type CreateAlertGroupParams struct {
OrgID uuid.UUID
Name string
}
func (q *Queries) CreateAlertGroup(ctx context.Context, arg CreateAlertGroupParams) (AlertGroup, error) {
row := q.db.QueryRow(ctx, createAlertGroup, arg.OrgID, arg.Name)
var i AlertGroup
err := row.Scan(
&i.ID,
&i.OrgID,
&i.Name,
&i.IsDefault,
)
return i, err
}
const deleteAlertGroup = `-- name: DeleteAlertGroup :execrows
DELETE FROM alert_groups WHERE id = $1 AND NOT is_default
`
func (q *Queries) DeleteAlertGroup(ctx context.Context, id uuid.UUID) (int64, error) {
result, err := q.db.Exec(ctx, deleteAlertGroup, id)
if err != nil {
return 0, err
}
return result.RowsAffected(), nil
}
const deleteAlertGroupChannels = `-- name: DeleteAlertGroupChannels :exec
DELETE FROM alert_group_channels WHERE group_id = $1
`
func (q *Queries) DeleteAlertGroupChannels(ctx context.Context, groupID uuid.UUID) error {
_, err := q.db.Exec(ctx, deleteAlertGroupChannels, groupID)
return err
}
const deleteMonitorAlertGroups = `-- name: DeleteMonitorAlertGroups :exec
DELETE FROM monitor_alert_groups WHERE monitor_id = $1
`
func (q *Queries) DeleteMonitorAlertGroups(ctx context.Context, monitorID uuid.UUID) error {
_, err := q.db.Exec(ctx, deleteMonitorAlertGroups, monitorID)
return err
}
const listAlertGroups = `-- name: ListAlertGroups :many
SELECT g.id, g.org_id, g.name, g.is_default, ARRAY(SELECT c.channel_id FROM alert_group_channels c WHERE c.group_id = g.id ORDER BY c.channel_id)::uuid[] AS channel_ids
FROM alert_groups g ORDER BY g.is_default DESC, g.name, g.id
`
type ListAlertGroupsRow struct {
ID uuid.UUID
OrgID uuid.UUID
Name string
IsDefault bool
ChannelIds []uuid.UUID
}
func (q *Queries) ListAlertGroups(ctx context.Context) ([]ListAlertGroupsRow, error) {
rows, err := q.db.Query(ctx, listAlertGroups)
if err != nil {
return nil, err
}
defer rows.Close()
items := []ListAlertGroupsRow{}
for rows.Next() {
var i ListAlertGroupsRow
if err := rows.Scan(
&i.ID,
&i.OrgID,
&i.Name,
&i.IsDefault,
&i.ChannelIds,
); err != nil {
return nil, err
}
items = append(items, i)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const monitorAlertGroups = `-- name: MonitorAlertGroups :many
SELECT group_id FROM monitor_alert_groups WHERE monitor_id = $1 ORDER BY group_id
`
func (q *Queries) MonitorAlertGroups(ctx context.Context, monitorID uuid.UUID) ([]uuid.UUID, error) {
rows, err := q.db.Query(ctx, monitorAlertGroups, monitorID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []uuid.UUID{}
for rows.Next() {
var group_id uuid.UUID
if err := rows.Scan(&group_id); err != nil {
return nil, err
}
items = append(items, group_id)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const monitorChannelIDs = `-- name: MonitorChannelIDs :many
SELECT DISTINCT c.id FROM notification_channels c
JOIN alert_group_channels gc ON gc.channel_id = c.id AND gc.org_id = c.org_id
JOIN monitor_alert_groups mg ON mg.group_id = gc.group_id AND mg.org_id = gc.org_id
WHERE mg.monitor_id = $1 AND c.enabled
`
func (q *Queries) MonitorChannelIDs(ctx context.Context, monitorID uuid.UUID) ([]uuid.UUID, error) {
rows, err := q.db.Query(ctx, monitorChannelIDs, monitorID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []uuid.UUID{}
for rows.Next() {
var id uuid.UUID
if err := rows.Scan(&id); err != nil {
return nil, err
}
items = append(items, id)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const updateAlertGroup = `-- name: UpdateAlertGroup :one
UPDATE alert_groups SET name = $2 WHERE id = $1 RETURNING id, org_id, name, is_default
`
type UpdateAlertGroupParams struct {
ID uuid.UUID
Name string
}
func (q *Queries) UpdateAlertGroup(ctx context.Context, arg UpdateAlertGroupParams) (AlertGroup, error) {
row := q.db.QueryRow(ctx, updateAlertGroup, arg.ID, arg.Name)
var i AlertGroup
err := row.Scan(
&i.ID,
&i.OrgID,
&i.Name,
&i.IsDefault,
)
return i, err
}
-24
View File
@@ -241,30 +241,6 @@ func (q *Queries) ListChannels(ctx context.Context) ([]NotificationChannel, erro
return items, nil
}
const listEnabledChannelIDs = `-- name: ListEnabledChannelIDs :many
SELECT id FROM notification_channels WHERE org_id = $1 AND enabled
`
func (q *Queries) ListEnabledChannelIDs(ctx context.Context, orgID uuid.UUID) ([]uuid.UUID, error) {
rows, err := q.db.Query(ctx, listEnabledChannelIDs, orgID)
if err != nil {
return nil, err
}
defer rows.Close()
items := []uuid.UUID{}
for rows.Next() {
var id uuid.UUID
if err := rows.Scan(&id); err != nil {
return nil, err
}
items = append(items, id)
}
if err := rows.Err(); err != nil {
return nil, err
}
return items, nil
}
const listIncidents = `-- name: ListIncidents :many
SELECT i.id, i.org_id, i.monitor_id, i.started_at, i.resolved_at, i.cause, m.name AS monitor_name
FROM incidents i
+19
View File
@@ -41,6 +41,19 @@ type AgentStorage struct {
Modified time.Time
}
type AlertGroup struct {
ID uuid.UUID
OrgID uuid.UUID
Name string
IsDefault bool
}
type AlertGroupChannel struct {
OrgID uuid.UUID
GroupID uuid.UUID
ChannelID uuid.UUID
}
type AlertmanagerAlert struct {
OrgID uuid.UUID
Fingerprint string
@@ -214,6 +227,12 @@ type Monitor struct {
GroupID *uuid.UUID
}
type MonitorAlertGroup struct {
OrgID uuid.UUID
MonitorID uuid.UUID
GroupID uuid.UUID
}
type MonitorGroup struct {
ID uuid.UUID
OrgID uuid.UUID
+11 -1
View File
@@ -13,8 +13,10 @@ import (
type Querier interface {
APITokenOrg(ctx context.Context, hash []byte) (uuid.UUID, error)
AddAlertGroupChannel(ctx context.Context, arg AddAlertGroupChannelParams) (int64, error)
AddMaintenanceTarget(ctx context.Context, arg AddMaintenanceTargetParams) error
AddMembership(ctx context.Context, arg AddMembershipParams) error
AddMonitorAlertGroup(ctx context.Context, arg AddMonitorAlertGroupParams) (int64, error)
AddMonitorRegion(ctx context.Context, arg AddMonitorRegionParams) error
AddStatusPageMonitor(ctx context.Context, arg AddStatusPageMonitorParams) error
AgentInstancesByID(ctx context.Context, ids []uuid.UUID) ([]AgentInstancesByIDRow, error)
@@ -23,6 +25,7 @@ type Querier interface {
AlertmanagerMonitors(ctx context.Context, orgID *uuid.UUID) ([]AlertmanagerMonitorsRow, error)
// System scope: the receiver a webhook token belongs to.
AlertmanagerReceiverByHash(ctx context.Context, tokenHash []byte) (AlertmanagerReceiverByHashRow, error)
AllMonitorAlertGroups(ctx context.Context) ([]MonitorAlertGroup, error)
AllMonitorRegions(ctx context.Context) ([]AllMonitorRegionsRow, error)
AnyUserExists(ctx context.Context) (bool, error)
// How much one instance checked for the organization in the last hour.
@@ -38,6 +41,7 @@ type Querier interface {
CountUnusedRecoveryCodes(ctx context.Context, userID uuid.UUID) (int64, error)
CreateAPIToken(ctx context.Context, arg CreateAPITokenParams) (ApiToken, error)
CreateAgent(ctx context.Context, arg CreateAgentParams) (Agent, error)
CreateAlertGroup(ctx context.Context, arg CreateAlertGroupParams) (AlertGroup, error)
CreateAlertmanagerReceiver(ctx context.Context, arg CreateAlertmanagerReceiverParams) (CreateAlertmanagerReceiverRow, error)
CreateAssignment(ctx context.Context, arg CreateAssignmentParams) (RoleAssignment, error)
CreateChannel(ctx context.Context, arg CreateChannelParams) (NotificationChannel, error)
@@ -57,6 +61,8 @@ type Querier interface {
DeleteAPIToken(ctx context.Context, id uuid.UUID) (int64, error)
DeleteAccountAudit(ctx context.Context, actorUserID *uuid.UUID) error
DeleteAgent(ctx context.Context, arg DeleteAgentParams) (int64, error)
DeleteAlertGroup(ctx context.Context, id uuid.UUID) (int64, error)
DeleteAlertGroupChannels(ctx context.Context, groupID uuid.UUID) error
DeleteAlertmanagerReceiver(ctx context.Context, id uuid.UUID) (int64, error)
DeleteAssignment(ctx context.Context, id uuid.UUID) (int64, error)
DeleteAssignmentsForResource(ctx context.Context, arg DeleteAssignmentsForResourceParams) error
@@ -67,6 +73,7 @@ type Querier interface {
DeleteLegalDocument(ctx context.Context, arg DeleteLegalDocumentParams) error
DeleteMaintenance(ctx context.Context, id uuid.UUID) (int64, error)
DeleteMonitor(ctx context.Context, id uuid.UUID) (int64, error)
DeleteMonitorAlertGroups(ctx context.Context, monitorID uuid.UUID) error
DeleteMonitorRegions(ctx context.Context, monitorID uuid.UUID) error
DeleteOrg(ctx context.Context, id uuid.UUID) (int64, error)
DeleteOrgStorage(ctx context.Context, orgID uuid.UUID) error
@@ -149,13 +156,13 @@ type Querier interface {
ListAPITokens(ctx context.Context, arg ListAPITokensParams) ([]ListAPITokensRow, error)
// Agents owned by org, or platform agents when org is NULL.
ListAgents(ctx context.Context, orgID *uuid.UUID) ([]ListAgentsRow, error)
ListAlertGroups(ctx context.Context) ([]ListAlertGroupsRow, error)
ListAlertmanagerReceivers(ctx context.Context) ([]ListAlertmanagerReceiversRow, error)
ListAssignments(ctx context.Context, orgID uuid.UUID) ([]ListAssignmentsRow, error)
// Newest first, paged by time. kind narrows to one kind; the row policy
// already limits what the scope can see.
ListAuditEvents(ctx context.Context, arg ListAuditEventsParams) ([]ListAuditEventsRow, error)
ListChannels(ctx context.Context) ([]NotificationChannel, error)
ListEnabledChannelIDs(ctx context.Context, orgID uuid.UUID) ([]uuid.UUID, error)
// System scope: every enabled monitor with its regions, for reconciling the
// schedule.
ListEnabledMonitorsForSchedule(ctx context.Context) ([]ListEnabledMonitorsForScheduleRow, error)
@@ -188,6 +195,8 @@ type Querier interface {
MarkNotificationSent(ctx context.Context, id uuid.UUID) error
// Every role a member holds, with the scope it applies to.
MemberGrants(ctx context.Context, arg MemberGrantsParams) ([]MemberGrantsRow, error)
MonitorAlertGroups(ctx context.Context, monitorID uuid.UUID) ([]uuid.UUID, error)
MonitorChannelIDs(ctx context.Context, monitorID uuid.UUID) ([]uuid.UUID, error)
MonitorIncidents(ctx context.Context, monitorID uuid.UUID) ([]Incident, error)
MonitorRegions(ctx context.Context, monitorID uuid.UUID) ([]uuid.UUID, error)
// System scope: monitors to hand to a checker of region, skipping ones that
@@ -260,6 +269,7 @@ type Querier interface {
// Writes at most once a minute per instance; agents poll far more often.
TouchAgentInstance(ctx context.Context, arg TouchAgentInstanceParams) error
TouchIdentity(ctx context.Context, arg TouchIdentityParams) error
UpdateAlertGroup(ctx context.Context, arg UpdateAlertGroupParams) (AlertGroup, error)
UpdateChannel(ctx context.Context, arg UpdateChannelParams) (NotificationChannel, error)
UpdateGroup(ctx context.Context, arg UpdateGroupParams) (MonitorGroup, error)
UpdateMaintenance(ctx context.Context, arg UpdateMaintenanceParams) (MaintenanceWindow, error)
+42
View File
@@ -0,0 +1,42 @@
-- name: ListAlertGroups :many
SELECT g.*, ARRAY(SELECT c.channel_id FROM alert_group_channels c WHERE c.group_id = g.id ORDER BY c.channel_id)::uuid[] AS channel_ids
FROM alert_groups g ORDER BY g.is_default DESC, g.name, g.id;
-- name: CreateAlertGroup :one
INSERT INTO alert_groups (org_id, name) VALUES ($1, $2) RETURNING *;
-- name: UpdateAlertGroup :one
UPDATE alert_groups SET name = $2 WHERE id = $1 RETURNING *;
-- name: DeleteAlertGroup :execrows
DELETE FROM alert_groups WHERE id = $1 AND NOT is_default;
-- name: DeleteAlertGroupChannels :exec
DELETE FROM alert_group_channels WHERE group_id = $1;
-- name: AddAlertGroupChannel :execrows
INSERT INTO alert_group_channels (org_id, group_id, channel_id)
SELECT c.org_id, sqlc.arg(group_id), c.id FROM notification_channels c
WHERE c.id = sqlc.arg(channel_id) AND c.org_id = sqlc.arg(org_id)
ON CONFLICT DO NOTHING;
-- name: AllMonitorAlertGroups :many
SELECT * FROM monitor_alert_groups ORDER BY monitor_id, group_id;
-- name: MonitorAlertGroups :many
SELECT group_id FROM monitor_alert_groups WHERE monitor_id = $1 ORDER BY group_id;
-- name: DeleteMonitorAlertGroups :exec
DELETE FROM monitor_alert_groups WHERE monitor_id = $1;
-- name: AddMonitorAlertGroup :execrows
INSERT INTO monitor_alert_groups (org_id, monitor_id, group_id)
SELECT g.org_id, sqlc.arg(monitor_id), g.id FROM alert_groups g
WHERE g.id = sqlc.arg(group_id) AND g.org_id = sqlc.arg(org_id)
ON CONFLICT DO NOTHING;
-- name: MonitorChannelIDs :many
SELECT DISTINCT c.id FROM notification_channels c
JOIN alert_group_channels gc ON gc.channel_id = c.id AND gc.org_id = c.org_id
JOIN monitor_alert_groups mg ON mg.group_id = gc.group_id AND mg.org_id = gc.org_id
WHERE mg.monitor_id = $1 AND c.enabled;
-3
View File
@@ -16,9 +16,6 @@ JOIN monitors m ON m.id = i.monitor_id
ORDER BY i.started_at DESC
LIMIT 200;
-- name: ListEnabledChannelIDs :many
SELECT id FROM notification_channels WHERE org_id = $1 AND enabled;
-- name: EnqueueNotification :exec
INSERT INTO notifications (org_id, channel_id, incident_id, event)
VALUES ($1, $2, $3, $4)