alert

package
v0.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 11, 2026 License: Apache-2.0 Imports: 33 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package alert batches and delivers watch alerts.

Delivery is best effort: a slow or failing channel never blocks the watchers, and alerts that do not fit are counted as dropped.

Index

Constants

View Source
const (
	Warning = "warning"
	Info    = "info"
	// HostCluster is the cluster and namespace of alerts about the host
	// itself (systemd units and Docker containers), so groups can match them.
	HostCluster = "host"
)

Severities.

Variables

This section is empty.

Functions

func HostKind added in v0.4.0

func HostKind(a Alert) string

HostKind returns "unit" or "container" for a host alert, or "".

func ReadSecret added in v0.4.0

func ReadSecret(path string) ([]byte, error)

ReadSecret loads the same trimmed key on both ends, with bounded file reads.

func Sign

func Sign(secret []byte, body []byte) string

Sign returns the X-Opsgate-Signature value for a body: "sha256=" + hex(HMAC-SHA256(secret, body)). Receivers check freshness with the signed sent_at field.

Types

type ActionLink struct {
	Label string `json:"label"`
	URL   string `json:"url"`
}

type Alert

type Alert struct {
	Display   *Display  `json:"display,omitempty"`
	Time      time.Time `json:"time"`
	Cluster   string    `json:"cluster"`
	Severity  string    `json:"severity"`
	Kind      string    `json:"kind"`
	Namespace string    `json:"namespace,omitempty"`
	Name      string    `json:"name"`
	// Reason is a short machine-readable cause, e.g. OOMKilled or ImageChanged.
	Reason  string `json:"reason"`
	Summary string `json:"summary"`
	// Hint is fixed advice chosen by the watcher. It never contains cluster text.
	Hint string `json:"hint,omitempty"`
	// Restart names a workload a person may choose to restart. Nothing acts on
	// it automatically.
	Restart *Workload `json:"restart,omitempty"`
	// OptionsURL navigates to live choices; Workload is an untrusted owner hint.
	OptionsURL string    `json:"options_url,omitempty"`
	Workload   *Workload `json:"workload,omitempty"`
	// Suppressed counts repeats folded into this alert during the cooldown.
	Suppressed int `json:"suppressed,omitempty"`
	// Key identifies repeats; it includes the container for pod alerts.
	Key string `json:"-"`
}

type Batch

type Batch struct {
	Group    string       `json:"group,omitempty"`
	Language string       `json:"language,omitempty"`
	Actions  []ActionLink `json:"actions,omitempty"`
	Source   string       `json:"source"`
	SentAt   time.Time    `json:"sent_at"`
	Alerts   []Alert      `json:"alerts"`
	// Dropped counts alerts lost to the queue or batch limit.
	Dropped int `json:"dropped,omitempty"`
	// Text renders the batch for chat forwarding without further code.
	Text string `json:"text"`
}

Batch is one delivery, also the JSON body of a webhook POST.

type Dispatcher

type Dispatcher struct {
	// contains filtered or unexported fields
}

Dispatcher gathers and delivers alerts in batches.

func New

func New(opts Options) *Dispatcher

New returns a Dispatcher; call Run to deliver.

func (*Dispatcher) Emit

func (d *Dispatcher) Emit(a Alert)

Emit queues a without blocking.

func (*Dispatcher) Run

func (d *Dispatcher) Run(ctx context.Context)

Run delivers batches until ctx ends, then sends what is pending.

type Display

type Display struct {
	Reason  string `json:"reason"`
	Summary string `json:"summary"`
	Hint    string `json:"hint,omitempty"`
}

Alert is one observed change. Text fields must already be redacted. Display holds human-facing localization while source fields stay unchanged.

type EmailOptions

type EmailOptions struct {
	Host     string
	Port     int
	Username string
	Password string
	From     string
	To       []string
}

EmailOptions configures TLS-only SMTP delivery.

type Group added in v0.4.0

type Group struct {
	Name         string
	Namespaces   []string
	Outputs      []string
	ProblemsOnly bool
	Batch        time.Duration
}

Group selects namespaces in configuration order. Outputs name loaded senders. Settings must be validated by the caller before construction.

type Notice added in v0.4.0

type Notice struct {
	Cluster      string    `json:"cluster"`
	Namespace    string    `json:"namespace"`
	Pod          string    `json:"pod"`
	WorkloadKind string    `json:"workload_kind"`
	WorkloadName string    `json:"workload_name"`
	Reason       string    `json:"reason"`
	Severity     string    `json:"severity"`
	OccurredAt   time.Time `json:"occurred_at"`
	// HostKind is "unit" or "container" for a host target named by Pod; then
	// Cluster, Namespace and the workload fields are empty.
	HostKind string `json:"host_kind,omitempty"`
}

func (Notice) Valid added in v0.4.0

func (n Notice) Valid() bool

Valid bounds all display and navigation fields and excludes free text.

type NoticeBatch added in v0.4.0

type NoticeBatch struct {
	SentAt  time.Time `json:"sent_at"`
	Notices []Notice  `json:"notices"`
}

NoticeBatch is the signed, identifier-only watch-to-inbox protocol.

type Options

type Options struct {
	// Group labels batches; empty preserves ungrouped delivery.
	Group string
	// Sender delivers batches; nil posts them to URL as a webhook.
	Sender Sender
	// ProblemsOnly drops every alert that is not a warning.
	ProblemsOnly bool
	// ChoicesOnly admits only pod warnings with live choices navigation.
	ChoicesOnly bool
	Language    string
	URL         string
	Secret      []byte
	Batch       time.Duration
	Cooldown    time.Duration
	MaxAlerts   int
	// PrivateOnly refuses connections to addresses that are neither
	// private nor loopback, checked after DNS resolution. Use it for
	// plain http, where a host name could otherwise lead anywhere.
	PrivateOnly bool
	Client      *http.Client
}

Options configures a Dispatcher.

type Router added in v0.4.0

type Router struct {
	// contains filtered or unexported fields
}

Router chooses one group before applying any severity or output filters. With no groups it preserves the original broadcast dispatchers unchanged.

func NewRouter added in v0.4.0

func NewRouter(groups []Group, outputs []*Dispatcher) *Router

NewRouter gives every (group, output) independent queues, cooldown and retries. Senders are immutable after construction and may be shared by dispatchers.

func (*Router) Emit added in v0.4.0

func (r *Router) Emit(a Alert)

Emit never blocks watchers. A matched group that filters an alert does not fall through to later groups, including catch-all groups.

func (*Router) Run added in v0.4.0

func (r *Router) Run(ctx context.Context)

Run stops and flushes all deliveries when ctx ends.

type Sender

type Sender interface {
	Name() string
	Send(context.Context, *Batch) (retry bool, err error)
}

Sender delivers one batch.

func NewEmail

func NewEmail(opts EmailOptions) Sender

NewEmail returns an SMTP sender. Settings must already be validated.

func NewNoticeSender added in v0.4.0

func NewNoticeSender(opts Options) Sender

NewNoticeSender reuses webhook signing, transport policy and retry decisions.

func NewWebhook

func NewWebhook(o WebhookOptions) (Sender, error)

NewWebhook builds a sender before watching starts. Templates receive Batch and a json helper; values from alerts are never evaluated as templates.

type WebhookOptions

type WebhookOptions struct {
	Options
	Name         string
	Format       string
	Language     string
	Headers      map[string]string
	BodyTemplate string
}

WebhookOptions configures a named JSON POST destination.

type Workload

type Workload struct {
	// Kind is deployment, statefulset or daemonset.
	Kind      string `json:"kind"`
	Namespace string `json:"namespace"`
	Name      string `json:"name"`
	// URL opens the approval page for this restart, if the watcher knows it.
	URL string `json:"url,omitempty"`
}

Workload identifies the target of a suggested k8s_rollout_restart call.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL