Documentation
¶
Overview ¶
Package alerts evaluates alert rules (§9) every 30 seconds and notifies channels when an alert fires and when it resolves. Rules watch metrics (resource use, request rates, errors, latency, any PromQL), log lines, service health and nodes; failed deployments, builds and jobs notify once each.
Index ¶
- Constants
- Variables
- func Send(ctx context.Context, typ string, c ChannelConfig, n Notification) error
- type Channel
- type ChannelConfig
- type ErrInvalid
- type HealthSource
- type LogsSource
- type Manager
- func (m *Manager) Active(ctx context.Context) ([]store.AlertState, error)
- func (m *Manager) Channels(ctx context.Context) ([]Channel, error)
- func (m *Manager) DeleteChannel(ctx context.Context, id string) error
- func (m *Manager) DeleteRule(ctx context.Context, id string) error
- func (m *Manager) Evaluate(ctx context.Context)
- func (m *Manager) PutChannel(ctx context.Context, id, name, typ string, cfg ChannelConfig) (Channel, error)
- func (m *Manager) PutRule(ctx context.Context, r Rule) (Rule, error)
- func (m *Manager) Rules(ctx context.Context) ([]Rule, error)
- func (m *Manager) Run(ctx context.Context)
- func (m *Manager) TestChannel(ctx context.Context, id string) error
- type MetricsSource
- type Notification
- type Rule
Constants ¶
const ( // Interval between evaluations. Interval = 30 * time.Second // TopicAlert carries a store.AlertEvent for every notification. TopicAlert = "alert.event" )
Variables ¶
var ( RuleTypes = map[string]string{ "metric": "a service metric crosses a threshold", "promql": "a PromQL expression crosses a threshold", "log": "log lines matching a pattern", "health": "a service is degraded or down", "node": "a node stops reporting", "deployment": "a deployment fails", "build": "a build fails", "job": "a job run fails", } RuleMetrics = map[string]string{ "cpu": "CPU % of one core (average per task)", "memory": "memory % of the limit (busiest task)", "rps": "requests per second", "error_rate": "5xx responses, % of requests", "latency": "p95 latency, ms", } )
Types and metrics, with what they measure.
var ChannelTypes = []string{"webhook", "slack", "discord", "telegram", "email"}
ChannelTypes lists the supported channels.
var TelegramAPI = "https://api.telegram.org"
TelegramAPI is the Bot API base URL (a variable for tests).
Functions ¶
func Send ¶
func Send(ctx context.Context, typ string, c ChannelConfig, n Notification) error
Send delivers n through one channel.
Types ¶
type Channel ¶
type Channel struct {
ID string `json:"id"`
Name string `json:"name"`
Type string `json:"type"`
Summary string `json:"summary"`
CreatedAt time.Time `json:"createdAt"`
}
Channel is a notification channel as the API shows it (no secrets).
type ChannelConfig ¶
type ChannelConfig struct {
// webhook, slack, discord
URL string `json:"url,omitempty"`
// telegram
BotToken string `json:"botToken,omitempty"`
ChatID string `json:"chatId,omitempty"`
// email
SMTPHost string `json:"smtpHost,omitempty"`
SMTPPort int `json:"smtpPort,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
From string `json:"from,omitempty"`
To []string `json:"to,omitempty"`
}
ChannelConfig holds every channel type's settings; only the type's fields are used. URLs and tokens are secrets: they are sealed at rest and never returned by the API.
type ErrInvalid ¶
type ErrInvalid struct{ Err error }
ErrInvalid wraps validation errors.
func (ErrInvalid) Error ¶
func (e ErrInvalid) Error() string
type HealthSource ¶
type HealthSource interface {
List(ctx context.Context) ([]health.ServiceHealth, error)
}
HealthSource lists service health (health.Monitor).
type LogsSource ¶
type LogsSource interface {
Count(ctx context.Context, f logs.Filter, level string, window time.Duration) ([]logs.Count, error)
}
LogsSource counts log lines (logs.Store).
type Manager ¶
type Manager struct {
// DashboardURL links notifications to the dashboard.
DashboardURL func() string
// contains filtered or unexported fields
}
func New ¶
func New(st *store.Store, box *secrets.Box, m MetricsSource, l LogsSource, h HealthSource, bus *events.Bus, log *slog.Logger) *Manager
func (*Manager) DeleteChannel ¶
func (*Manager) PutChannel ¶
func (m *Manager) PutChannel(ctx context.Context, id, name, typ string, cfg ChannelConfig) (Channel, error)
PutChannel creates (empty id) or replaces a channel.
type MetricsSource ¶
MetricsSource reads metrics (metrics.Store).
type Notification ¶
type Notification struct {
Kind string `json:"status"` // firing | resolved | event | test
Rule string `json:"rule"`
Severity string `json:"severity"`
Instance string `json:"instance"` // e.g. shop/production/web or node w2
Message string `json:"message"`
Value *float64 `json:"value,omitempty"`
At time.Time `json:"at"`
URL string `json:"url,omitempty"` // the dashboard
}
Notification is what a channel delivers.
func (Notification) Text ¶
func (n Notification) Text() string
Text is the plain-text body used by chat channels and email.
type Rule ¶
type Rule struct {
ID string `json:"id"`
Name string `json:"name"`
Enabled bool `json:"enabled"`
Type string `json:"type"` // metric | promql | log | health | node | deployment | build | job
Severity string `json:"severity"` // info | warning | critical
// Scope: empty matches every project, environment or service.
Project string `json:"project,omitempty"`
Environment string `json:"environment,omitempty"`
Service string `json:"service,omitempty"`
// metric: cpu | memory | rps | error_rate | latency.
Metric string `json:"metric,omitempty"`
// promql: any query; each series is an instance.
Query string `json:"query,omitempty"`
// log: lines containing Text (case-insensitive) and/or with Level.
Text string `json:"text,omitempty"`
Level string `json:"level,omitempty"`
// metric, promql, log: value Op Threshold over WindowSeconds.
Op string `json:"op,omitempty"`
Threshold float64 `json:"threshold"`
WindowSeconds int `json:"windowSeconds,omitempty"`
// ForSeconds is how long the condition must hold before firing.
ForSeconds int `json:"forSeconds"`
Channels []string `json:"channels"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}
Rule is an alert rule as the API shows and accepts it.