alert

package
v0.89.0 Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package alert defines threshold alerts on event metrics — "fire when <event> count over the last N hours is above/below a threshold". The Server evaluates them on a schedule and delivers via webhooks; this package is the persisted rules + their runtime state.

Index

Constants

View Source
const (
	// KindCount is the original: events in a window against a fixed threshold. Default when a
	// stored alert has no kind, so every alert that already exists keeps behaving identically.
	KindCount = "count"
	// KindRelative compares the window against the SAME-LENGTH window before it. "Tell me if
	// signups fall 30%" needs no knowledge of what signups usually are.
	KindRelative = "relative"
	// KindAnomaly defers entirely to the anomaly detector: no event, no threshold, no direction.
	// "Tell me when something is unusual."
	KindAnomaly = "anomaly"
	// KindRatio watches one event over another — the share, not the count.
	KindRatio = "ratio"
)

Alert shapes.

There was one: a raw event count against a number you typed in. That shape has a problem nobody names — it asks you to already know your own numbers. "Alert me if signups drop below 40" needs you to know that 40 is low, and to keep knowing it as the product grows, or the alert silently stops meaning anything. The fixed threshold is why most alerts are either never set up or muted within a month.

The other three ask nothing of you. A relative alert knows what normal was because it looks at the prior window. An anomaly alert uses the detector that has been sitting in internal/insight this whole time, wired to nothing — the dashboard could tell you "signups dropped 40% in the last 24h" and there was no way to be TOLD that without opening the page. And a ratio alert watches a derived metric, which is where most real health lives: not "how many errors" but "what share of sessions hit one".

Variables

This section is empty.

Functions

This section is empty.

Types

type Alert

type Alert struct {
	ID    string `json:"id"`
	Name  string `json:"name"`
	Event string `json:"event"`
	// KindName is the alert shape; empty means the original count alert, so every alert stored
	// before kinds existed keeps watching exactly what it watched. Read it through Kind().
	KindName string `json:"kind,omitempty"`
	// Against is the denominator for a ratio alert ("$exception per $pageview"). Ignored by
	// every other kind.
	Against     string    `json:"against,omitempty"`
	Op          string    `json:"op"` // "gt" | "lt"
	Threshold   float64   `json:"threshold"`
	WindowHours int       `json:"window_hours"`
	Enabled     bool      `json:"enabled"`
	Created     time.Time `json:"created"`
	LastChecked time.Time `json:"last_checked"`
	LastValue   float64   `json:"last_value"`
	LastFired   time.Time `json:"last_fired"`
}

Alert is one rule plus its last-evaluation state.

func (Alert) Describe added in v0.30.0

func (a Alert) Describe() string

Describe renders the rule in words. Alerts are read months after they are written, usually by someone deciding whether a page at 3am was worth it, and "gt 40" is not a rule anyone can judge.

func (Alert) Kind added in v0.30.0

func (a Alert) Kind() string

Kind returns the alert's shape, defaulting to the original.

A stored alert written before kinds existed has an empty string here, and it MUST mean count. Treating it as anything else would silently change what a live alert watches, which is the one thing an alert must never do.

type Store

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

func Open

func Open(path string) (*Store, error)

func (*Store) Add

func (s *Store) Add(a Alert) (Alert, error)

func (*Store) Delete

func (s *Store) Delete(id string) (found bool, err error)

Delete removes an alert by id. found is true only when an alert actually went away, so callers never claim a removal that did not occur. A miss is not an error.

func (*Store) List

func (s *Store) List() []Alert

func (*Store) SetChecked

func (s *Store) SetChecked(id string, value float64, fired bool, at time.Time)

SetChecked records an evaluation result (and a fire time, if it fired).

Jump to

Keyboard shortcuts

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