alert

package
v1.1.0 Latest Latest
Warning

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

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

Documentation

Overview

Package alert opens an incident when a check keeps failing or an edge goes quiet, resolves it when it stops, and tells the channels of each event.

Index

Constants

View Source
const (
	DefaultFailures = 3
	DefaultSilence  = 5 * time.Minute
	DefaultInterval = 30 * time.Second
)

Defaults of the settings.

View Source
const EventTest = "test"

Events a webhook is called for, besides the two of an incident.

Variables

This section is empty.

Functions

func SendTest

func SendTest(ctx context.Context, s Sender, ch store.Channel) error

SendTest sends a test notification, to check that a channel works.

Types

type Config

type Config struct {
	// Failures is how many failures in a row open an incident; zero means 3.
	Failures int
	// Silence is how long an edge may go without a result before it is
	// reported; zero means 5 minutes.
	Silence time.Duration
	// Interval is how often the results are looked at; zero means 30 seconds.
	Interval time.Duration
	// Sender delivers the notifications; nil posts to the channels' webhooks.
	Sender Sender
	// Policy is where those webhooks may not connect; nil means the default
	// ranges, a pointer to the zero value nowhere.
	Policy *probe.Policy
	// Now is the clock; nil means time.Now.
	Now func() time.Time
}

Config sets when an incident opens.

type Engine

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

Engine evaluates the results and notifies the channels.

func New

func New(log *slog.Logger, st *store.Store, cfg Config) (*Engine, error)

New builds an engine on top of a store.

func (*Engine) Run

func (e *Engine) Run(ctx context.Context)

Run evaluates until the context ends.

func (*Engine) Tick

func (e *Engine) Tick(ctx context.Context) error

Tick looks at the results once and tells the channels what is new, unless another central is doing it.

type IncidentJSON

type IncidentJSON struct {
	ID         int64      `json:"id"`
	Kind       string     `json:"kind"`
	CheckID    string     `json:"check_id,omitempty"`
	EdgeID     string     `json:"edge_id"`
	Edge       string     `json:"edge"`
	StartedAt  time.Time  `json:"started_at"`
	ResolvedAt *time.Time `json:"resolved_at,omitempty"`
	Detail     string     `json:"detail,omitempty"`
	Resolution string     `json:"resolution,omitempty"`
}

IncidentJSON is an incident as a webhook sees it.

func ToJSON

func ToJSON(i store.Incident) IncidentJSON

ToJSON describes an incident for the UI API and for webhooks alike.

type Payload

type Payload struct {
	Event    string        `json:"event"`
	Text     string        `json:"text"`
	Incident *IncidentJSON `json:"incident,omitempty"`
}

Payload is the JSON body posted to a channel. Text reads as a sentence, which is all that chat tools with incoming webhooks ask for.

func NewPayload

func NewPayload(n store.Notification) Payload

NewPayload says what happened in a notification.

type Sender

type Sender interface {
	Send(ctx context.Context, ch store.Channel, p Payload) error
}

Sender delivers a payload to a channel.

type Webhook

type Webhook struct {
	Client *http.Client
}

Webhook posts payloads as JSON. Redirects are not followed: a channel is where the administrator said, nowhere else.

func NewWebhook

func NewWebhook(policy probe.Policy) *Webhook

NewWebhook returns a sender with a timeout and no redirects. It never connects to an address the policy denies, whatever name led to it.

func (*Webhook) Send

func (w *Webhook) Send(ctx context.Context, ch store.Channel, p Payload) error

Send posts the payload, signed when the channel has a secret (see the README). An error never holds the address, which may hold a secret of its own.

Jump to

Keyboard shortcuts

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