monitor

package
v1.10.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("monitor: not found")

ErrNotFound is returned when a monitor or alert does not exist.

Functions

This section is empty.

Types

type AlertEvent

type AlertEvent struct {
	ID          string        `json:"id"`
	MonitorID   string        `json:"monitor_id"`
	MonitorName string        `json:"monitor_name"`
	Status      MonitorStatus `json:"status"`
	Value       float64       `json:"value"`
	Threshold   float64       `json:"threshold"`
	Service     string        `json:"service"`
	Action      string        `json:"action,omitempty"`
	Message     string        `json:"message,omitempty"`
	CreatedAt   time.Time     `json:"created_at"`
}

AlertEvent records a single alert transition (trigger or recovery).

type AlertFilter

type AlertFilter struct {
	MonitorID string
	Status    MonitorStatus
	Service   string
	Limit     int
}

AlertFilter controls which alert events are returned by ListAlerts.

type AlertStore

type AlertStore interface {
	SaveAlert(ctx context.Context, a *AlertEvent) error
	GetAlert(ctx context.Context, id string) (*AlertEvent, error)
	ListAlerts(ctx context.Context, filter AlertFilter) ([]AlertEvent, error)
}

AlertStore persists and retrieves alert events.

type Evaluator

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

Evaluator periodically checks all enabled monitors against live metrics, creating alert events when thresholds are breached and resolving them on recovery.

func NewEvaluator

func NewEvaluator(
	monitors MonitorStore,
	alerts AlertStore,
	provider MetricsProvider,
	interval time.Duration,
) *Evaluator

NewEvaluator creates an evaluator that ticks at the given interval.

func (*Evaluator) SetIncidentCreator

func (e *Evaluator) SetIncidentCreator(ic IncidentCreator)

SetIncidentCreator configures the optional incident integration.

func (*Evaluator) SetWebhookDispatcher

func (e *Evaluator) SetWebhookDispatcher(wd WebhookDispatcher)

SetWebhookDispatcher configures the optional webhook integration.

func (*Evaluator) Start

func (e *Evaluator) Start(ctx context.Context)

Start begins the evaluation loop in a background goroutine.

func (*Evaluator) Stop

func (e *Evaluator) Stop()

Stop cancels the background evaluation loop.

type GatewayProvider

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

GatewayProvider reads live metrics from the gateway SLO engine and request stats to produce ServiceSnapshots for the evaluator.

func NewGatewayProvider

func NewGatewayProvider(slo *gateway.SLOEngine, stats *gateway.RequestStats) *GatewayProvider

NewGatewayProvider creates a MetricsProvider backed by gateway components.

func (*GatewayProvider) Snapshot

func (g *GatewayProvider) Snapshot(_ context.Context) ([]ServiceSnapshot, error)

Snapshot collects the current metrics from the SLO engine windows and request stats.

type IncidentCreator

type IncidentCreator interface {
	CreateFromMonitor(ctx context.Context, alert AlertEvent) error
}

IncidentCreator is the interface the monitor system uses to create incidents when a critical threshold is breached.

type MetricsProvider

type MetricsProvider interface {
	Snapshot(ctx context.Context) ([]ServiceSnapshot, error)
}

MetricsProvider is the interface the evaluator calls each tick to collect current metric values. Implementations may read from the gateway stats, DataPlane, or Prometheus.

type Monitor

type Monitor struct {
	ID          string        `json:"id"`
	Name        string        `json:"name"`
	Type        MonitorType   `json:"type"`
	Service     string        `json:"service"`          // target service (e.g. "dynamodb"), "*" for all
	Action      string        `json:"action,omitempty"` // target action (e.g. "Query"), "*" or empty for all
	Operator    string        `json:"operator"`         // "gt", "lt", "gte", "lte"
	Warning     float64       `json:"warning"`          // warning threshold
	Critical    float64       `json:"critical"`         // critical threshold
	Enabled     bool          `json:"enabled"`
	Status      MonitorStatus `json:"status"`
	LastValue   float64       `json:"last_value"`
	MutedUntil  *time.Time    `json:"muted_until,omitempty"`
	Tags        []string      `json:"tags,omitempty"`
	Message     string        `json:"message,omitempty"` // optional description / runbook
	CreatedAt   time.Time     `json:"created_at"`
	UpdatedAt   time.Time     `json:"updated_at"`
	LastChecked *time.Time    `json:"last_checked,omitempty"`
}

Monitor defines a threshold-based alert rule over a service metric.

type MonitorFilter

type MonitorFilter struct {
	Service string
	Type    MonitorType
	Status  MonitorStatus
	Enabled *bool
	Limit   int
}

MonitorFilter controls which monitors are returned by List.

type MonitorStatus

type MonitorStatus string

MonitorStatus represents the current evaluation state of a monitor.

const (
	MonitorStatusOK       MonitorStatus = "ok"
	MonitorStatusWarning  MonitorStatus = "warning"
	MonitorStatusCritical MonitorStatus = "critical"
	MonitorStatusMuted    MonitorStatus = "muted"
	MonitorStatusNoData   MonitorStatus = "no_data"
)

type MonitorStore

type MonitorStore interface {
	Save(ctx context.Context, m *Monitor) error
	Get(ctx context.Context, id string) (*Monitor, error)
	List(ctx context.Context, filter MonitorFilter) ([]Monitor, error)
	Update(ctx context.Context, m *Monitor) error
	Delete(ctx context.Context, id string) error
	ListEnabled(ctx context.Context) ([]Monitor, error)
}

MonitorStore persists and retrieves monitors.

type MonitorType

type MonitorType string

MonitorType identifies the kind of metric a monitor watches.

const (
	MonitorTypeErrorRate  MonitorType = "error_rate"
	MonitorTypeLatencyP50 MonitorType = "latency_p50"
	MonitorTypeLatencyP95 MonitorType = "latency_p95"
	MonitorTypeLatencyP99 MonitorType = "latency_p99"
	MonitorTypeThroughput MonitorType = "throughput"
	MonitorTypeCustom     MonitorType = "custom"
)

type Service

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

Service owns the monitor and alert stores, the evaluator loop, and provides the high-level API used by the admin handlers.

func NewService

func NewService(
	monitors MonitorStore,
	alerts AlertStore,
	provider MetricsProvider,
	interval time.Duration,
) *Service

NewService creates a monitoring service. Pass nil for provider to skip automatic evaluation (useful for testing or when metrics are not available).

func (*Service) Alerts

func (s *Service) Alerts() AlertStore

Alerts returns the underlying alert store.

func (*Service) Evaluator

func (s *Service) Evaluator() *Evaluator

Evaluator returns the evaluator, which may be nil if no provider was given.

func (*Service) Monitors

func (s *Service) Monitors() MonitorStore

Monitors returns the underlying monitor store.

func (*Service) SetIncidentCreator

func (s *Service) SetIncidentCreator(ic IncidentCreator)

SetIncidentCreator wires the incident integration into the evaluator.

func (*Service) SetWebhookDispatcher

func (s *Service) SetWebhookDispatcher(wd WebhookDispatcher)

SetWebhookDispatcher wires the webhook integration into the evaluator.

func (*Service) Start

func (s *Service) Start(ctx context.Context)

Start begins the background evaluation loop. No-op if no evaluator is configured.

func (*Service) Stop

func (s *Service) Stop()

Stop terminates the background evaluation loop.

type ServiceSnapshot

type ServiceSnapshot struct {
	Service    string
	ErrorRate  float64 // 0..1
	LatencyP50 float64 // milliseconds
	LatencyP95 float64 // milliseconds
	LatencyP99 float64 // milliseconds
	Throughput float64 // requests per second
}

ServiceSnapshot holds the current metric values for a single service, collected by the MetricsProvider during each evaluation tick.

type WebhookDispatcher

type WebhookDispatcher interface {
	Fire(ctx context.Context, event string, payload any) error
}

WebhookDispatcher is the interface for firing outbound webhooks.

Directories

Path Synopsis
Package dynamostore implements monitor.MonitorStore and monitor.AlertStore backed by DynamoDB via the generic dynamostore package.
Package dynamostore implements monitor.MonitorStore and monitor.AlertStore backed by DynamoDB via the generic dynamostore package.
Package filestore implements monitor.MonitorStore and monitor.AlertStore backed by JSON files on disk via the generic filestore package.
Package filestore implements monitor.MonitorStore and monitor.AlertStore backed by JSON files on disk via the generic filestore package.

Jump to

Keyboard shortcuts

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