Help us shape the future of Go! Take the 2026 Go Developer Survey

alerting

package
v0.35.4 Latest Latest
Warning

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

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

Documentation

Overview

Package alerting provides a unified notification dispatcher for Prism.

All budget alerts, shortfall warnings, and cushion triggers route through AlertDispatcher so the delivery backend can be swapped without changing callers. The default backend (LogDispatcher) always writes to stdout/stderr — no external dependencies required for basic operation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Alert

type Alert struct {
	ID          string            `json:"id"`
	Severity    AlertSeverity     `json:"severity"`
	Title       string            `json:"title"`
	Body        string            `json:"body"`
	ProjectID   string            `json:"project_id,omitempty"`
	ProjectName string            `json:"project_name,omitempty"`
	Tags        map[string]string `json:"tags,omitempty"`
	Channels    []Channel         `json:"channels"`
	CreatedAt   time.Time         `json:"created_at"`
}

Alert is the canonical notification payload.

func FormatAutoActionAlert

func FormatAutoActionAlert(projectID, projectName string, action types.BudgetActionType, spentAmount, totalBudget float64) Alert

FormatAutoActionAlert creates an Alert when an automatic budget action executes.

func FormatBudgetThresholdAlert

func FormatBudgetThresholdAlert(projectID, projectName string, threshold, spentAmount, totalBudget float64) Alert

FormatBudgetThresholdAlert creates an Alert when a budget threshold is crossed.

func FormatCushionAlert

func FormatCushionAlert(projectID, projectName string, headroomPct, remaining, total float64, mode string) Alert

FormatCushionAlert creates an Alert when the budget cushion threshold is crossed.

func FormatShortfallAlert

func FormatShortfallAlert(projectID, projectName string, dailyRate float64, daysUntil int, exhaustionDate time.Time) Alert

FormatShortfallAlert creates an Alert from a predictive shortfall warning.

type AlertDispatcher

type AlertDispatcher interface {
	Send(ctx context.Context, alert Alert) error
	SendBatch(ctx context.Context, alerts []Alert) error
	// History returns the most recent sent alerts (up to limit).
	History(limit int) []SentAlert
}

AlertDispatcher is the unified notification interface.

type AlertSeverity

type AlertSeverity string

AlertSeverity classifies the urgency of an alert.

const (
	AlertSeverityInfo     AlertSeverity = "info"
	AlertSeverityWarning  AlertSeverity = "warning"
	AlertSeverityCritical AlertSeverity = "critical"
)

type Channel

type Channel struct {
	Type    ChannelType       `json:"type"`
	Target  string            `json:"target"` // email addr, webhook URL, etc.
	Options map[string]string `json:"options,omitempty"`
}

Channel defines a single notification destination.

type ChannelType

type ChannelType string

ChannelType identifies a notification delivery mechanism.

const (
	ChannelLog     ChannelType = "log" // always active — stdout/stderr
	ChannelEmail   ChannelType = "email"
	ChannelSlack   ChannelType = "slack"
	ChannelWebhook ChannelType = "webhook"
	ChannelDesktop ChannelType = "desktop" // OS desktop notification
)

type LogDispatcher

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

LogDispatcher is the default no-dependency AlertDispatcher. It writes every alert to stdout/stderr and keeps an in-memory history. Used when no external notification channels are configured.

func NewLogDispatcher

func NewLogDispatcher() *LogDispatcher

NewLogDispatcher creates a LogDispatcher.

func (*LogDispatcher) History

func (d *LogDispatcher) History(limit int) []SentAlert

History returns the most recent sent alerts up to limit.

func (*LogDispatcher) Send

func (d *LogDispatcher) Send(_ context.Context, alert Alert) error

Send writes the alert to stdout and records it in history.

func (*LogDispatcher) SendBatch

func (d *LogDispatcher) SendBatch(ctx context.Context, alerts []Alert) error

SendBatch sends multiple alerts sequentially.

type SentAlert

type SentAlert struct {
	Alert     Alert     `json:"alert"`
	SentAt    time.Time `json:"sent_at"`
	Succeeded bool      `json:"succeeded"`
	Error     string    `json:"error,omitempty"`
}

SentAlert records a delivery attempt.

type WebhookConfig

type WebhookConfig struct {
	Channels   []Channel
	RetryCount int           // default 2
	Timeout    time.Duration // default 10s
}

WebhookConfig configures the WebhookDispatcher.

type WebhookDispatcher

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

WebhookDispatcher sends alerts to one or more HTTP endpoints (Slack, generic webhook). Falls back to LogDispatcher for any channel that fails after retries.

func NewWebhookDispatcher

func NewWebhookDispatcher(cfg WebhookConfig) *WebhookDispatcher

NewWebhookDispatcher creates a dispatcher that delivers to all configured channels.

func (*WebhookDispatcher) History

func (d *WebhookDispatcher) History(limit int) []SentAlert

History returns the most recent sent alerts.

func (*WebhookDispatcher) Send

func (d *WebhookDispatcher) Send(ctx context.Context, alert Alert) error

Send delivers the alert to all configured channels.

func (*WebhookDispatcher) SendBatch

func (d *WebhookDispatcher) SendBatch(ctx context.Context, alerts []Alert) error

SendBatch sends multiple alerts.

Jump to

Keyboard shortcuts

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