Documentation
¶
Overview ¶
Package notify sends events to notification channels: webhooks, chat services, push services and email (#86, ADR-0034). Channels are configured by administrators and stored in the database; each receives the event kinds it subscribed to.
Index ¶
Constants ¶
const ( KindImageUpdate = "image.update" // a tag in use points to a new image (#96) KindAutoUpdateDone = "autoupdate.updated" // an automatic update succeeded (#54) KindAutoUpdateFailed = "autoupdate.failed" // an automatic update failed and was rolled back KindAutoUpdatePending = "autoupdate.pending" // automatic updates wait for approval (#56) KindContainerDied = "container.died" // a container exited with an error code KindContainerOOM = "container.oom" // a container ran out of memory KindContainerUnhealthy = "container.unhealthy" // a container's healthcheck started failing (#22) KindContainerAutoHeal = "container.autoheal" // nerdui restarted an unhealthy container, or gave up (#22) KindJobFailed = "job.failed" // a background operation failed KindGitUpdate = "git.update" // a Git stack has a new commit or was updated from it (#38, #39) KindHostAlert = "host.alert" // host CPU, memory or disk crossed a threshold, or recovered (#64) KindTest = "test" // sent by "Send test"; every channel receives it )
Event kinds. Keep in sync with api/openapi.yaml and the web UI.
const ( SeverityInfo = "info" SeverityWarning = "warning" SeverityError = "error" )
Severities.
const ( TypeWebhook = "webhook" TypeSlack = "slack" TypeDiscord = "discord" TypeTelegram = "telegram" TypeNtfy = "ntfy" TypeGotify = "gotify" TypeEmail = "email" )
Channel types.
const DedupeWindow = 15 * time.Minute
DedupeWindow suppresses repeats of an event (same kind, environment, namespace and object).
Variables ¶
var Kinds = []string{ KindImageUpdate, KindAutoUpdateDone, KindAutoUpdateFailed, KindAutoUpdatePending, KindContainerDied, KindContainerOOM, KindContainerUnhealthy, KindContainerAutoHeal, KindJobFailed, KindGitUpdate, KindHostAlert, }
Kinds are the event kinds a channel can subscribe to.
var Types = []string{TypeWebhook, TypeSlack, TypeDiscord, TypeTelegram, TypeNtfy, TypeGotify, TypeEmail}
Types are the supported channel types.
Functions ¶
This section is empty.
Types ¶
type Channel ¶
type Channel struct {
ID int64
Name string
Type string
Config Config
Events []string
Enabled bool
// LastSentAt and LastError describe the newest delivery attempt.
LastSentAt time.Time
LastError string
}
Channel is a configured notification channel.
type Config ¶
type Config struct {
// URL: webhook, Slack and Discord webhook URL, ntfy topic URL, Gotify server URL.
URL string `json:"url,omitempty"`
// Secret: HMAC key of webhook signatures. Write-only.
Secret string `json:"secret,omitempty"`
// Token: Telegram bot token, ntfy access token, Gotify application token. Write-only.
Token string `json:"token,omitempty"`
// ChatID: Telegram chat.
ChatID string `json:"chatId,omitempty"`
// Email.
Host string `json:"host,omitempty"`
Port int `json:"port,omitempty"`
TLS string `json:"tls,omitempty"` // starttls (default), tls or none
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"` // write-only
From string `json:"from,omitempty"`
To string `json:"to,omitempty"` // comma-separated
}
Config is a channel's settings; which fields apply depends on the type. Keep in sync with api/openapi.yaml.
func (*Config) KeepSecrets ¶
KeepSecrets fills secrets left empty in c from the stored config, when the type is unchanged.
type Event ¶
type Event struct {
Kind string `json:"kind"`
Severity string `json:"severity"`
Title string `json:"title"`
Message string `json:"message,omitempty"`
Host string `json:"host,omitempty"`
Environment string `json:"environment,omitempty"`
Namespace string `json:"namespace,omitempty"`
// Object names what the event is about (container, stack, image reference, job); events of
// the same kind and object are sent at most once per DedupeWindow.
Object string `json:"object,omitempty"`
Time time.Time `json:"time"`
}
Event is something to tell people about. It is the webhook payload: keep in sync with docs/notifications.md.
type Service ¶
type Service struct {
Store Store
Host string // shown in messages: which server the event comes from
Client *http.Client
Logger *slog.Logger
Now func() time.Time
// contains filtered or unexported fields
}
Service queues events and sends them to the channels subscribed to their kind.
func (*Service) Notify ¶
Notify queues an event without blocking; it is dropped when the queue is full or a repeat within DedupeWindow. A nil Service ignores events.
type Store ¶
type Store interface {
ListNotificationChannels(ctx context.Context) ([]db.NotificationChannel, error)
SetNotificationResult(ctx context.Context, arg db.SetNotificationResultParams) error
}
Store is the database of channels; *db.Queries of the write connection implements it.
type Watcher ¶
type Watcher struct {
Env *environment.Environment
Notifier *Service
Logger *slog.Logger
// Delay overrides settleDelay (tests).
Delay time.Duration
}
Watcher turns containerd events of an environment into notifications about containers that crashed or ran out of memory (#86).