levelalert

package
v1.148.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package levelalert evaluates conditions that have a warning and a critical level and routes them to notification channels (#2148, #2149).

An evaluator hands Apply every condition it found at warning or critical on this pass. Each is sent to the configured channels under a key carrying its level, every pass, so the channel's repeat window decides how often a standing condition is repeated and a move from warning to critical is a new key the warning's window cannot hold back. A condition that was alerting on the previous pass and is not in this one has cleared: a resolved notification is sent once, under its own key, and its record is removed. The records live in alert_conditions so a restart, or the next pass running on another replica, still knows what was alerting.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Locked

func Locked(ctx context.Context, db *sql.DB, key int64, fn func(ctx context.Context) error) error

Locked runs fn while holding the session advisory lock key on a dedicated connection, so one replica evaluates a family at a time. It returns bgloop.ErrSkipped when another replica holds the lock.

Types

type Engine

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

Engine applies a family's findings.

func New

func New(store Store, sender Sender) *Engine

New returns an engine, or nil when store is nil. A nil sender records conditions and sends nothing.

func (*Engine) Apply

func (e *Engine) Apply(ctx context.Context, family string, channels []string, findings []Finding, unknown ...string) (Result, error)

Apply sends each finding at its level to channels, sends a resolved notification for each condition of family that was alerting and is not among findings, and records the result.

unknown names key prefixes whose conditions this pass could not judge (a store that could not be read): what was recorded under them is left as it stands, neither resolved nor sent again, until a pass can judge them.

A send's key carries the condition, its level and when that level began, so the channel's repeat window holds back repeats of one episode while a condition that clears and returns, or changes level, is posted at once.

type Finding

type Finding struct {
	// Key names the condition and what it is about, unique within its
	// family: webhook:spool_age:replica-a. It never carries the level.
	Key   string
	Level Level
	// Title is the notification's one line: what, where, how bad.
	Title string
	// Body is markdown: the value, the threshold, and what to look at.
	Body string
	// Link is the admin page the condition is seen on.
	Link string
}

Finding is one condition at warning or critical.

type Level

type Level string

Level is how bad a condition is.

const (
	Warning  Level = "warning"
	Critical Level = "critical"
)

The levels a condition is reported at.

func Worse

func Worse(a, b Level) Level

Worse returns the more severe of a and b; empty when neither is a level.

type PostgresStore

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

PostgresStore is the alert_conditions table.

func NewPostgresStore

func NewPostgresStore(db *sql.DB) *PostgresStore

NewPostgresStore returns the store over db.

func (*PostgresStore) Delete

func (s *PostgresStore) Delete(ctx context.Context, key string) error

Delete removes a condition that cleared.

func (*PostgresStore) List

func (s *PostgresStore) List(ctx context.Context, family string) ([]Record, error)

List returns family's conditions.

func (*PostgresStore) Upsert

func (s *PostgresStore) Upsert(ctx context.Context, family string, f Finding, now time.Time) error

Upsert records f at now. A condition already recorded keeps when it began, unless its level changed, which starts it again.

type Record

type Record struct {
	Key   string
	Level Level
	Title string
	Since time.Time
}

Record is a condition as the previous pass left it.

type Result

type Result struct {
	Alerting int
	Resolved int
}

Result is what one Apply did.

type Sender

type Sender interface {
	Send(ctx context.Context, names []string, key string, p notification.Payload) int
}

Sender queues an alert for notification channels. notifyalert.Sender satisfies it.

type Store

type Store interface {
	List(ctx context.Context, family string) ([]Record, error)
	Upsert(ctx context.Context, family string, f Finding, now time.Time) error
	Delete(ctx context.Context, key string) error
}

Store keeps the conditions each family found on its last pass.

type Thresholds

type Thresholds struct {
	Warning  float64 `json:"warning"`
	Critical float64 `json:"critical"`
	Below    bool    `json:"-"`
}

Thresholds are the two values a reading is judged against. A zero threshold is off. Below marks a reading that is worse the lower it is (free space, days left); otherwise higher is worse.

func (Thresholds) Of

func (t Thresholds) Of(v float64) Level

Of returns the level v is at, or "" when it is under both thresholds.

Jump to

Keyboard shortcuts

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