anomaly

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: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Anomaly

type Anomaly struct {
	ID          string    `json:"id"`
	Service     string    `json:"service"`
	Metric      string    `json:"metric"`
	Observed    float64   `json:"observed"`
	Expected    float64   `json:"expected"`  // baseline mean
	Deviation   float64   `json:"deviation"` // how many std devs away
	Severity    string    `json:"severity"`  // "info" (<2sigma), "warning" (2-3sigma), "critical" (>3sigma)
	DetectedAt  time.Time `json:"detected_at"`
	Description string    `json:"description"`
}

Anomaly represents a detected deviation from baseline behaviour.

type Baseline

type Baseline struct {
	Service     string    `json:"service"`
	Metric      string    `json:"metric"` // "latency_p50", "error_rate", "throughput"
	Mean        float64   `json:"mean"`
	StdDev      float64   `json:"std_dev"`
	SampleCount int       `json:"sample_count"`
	WindowStart time.Time `json:"window_start"`
	WindowEnd   time.Time `json:"window_end"`
}

Baseline represents learned normal behaviour for a service metric.

type Detector

type Detector struct {
	PersistFunc func(baselines []Baseline, anomalies []Anomaly) // called after mutations, if non-nil
	// contains filtered or unexported fields
}

Detector maintains rolling baselines and detects anomalies.

func NewDetector

func NewDetector(window time.Duration, threshold float64) *Detector

NewDetector creates an anomaly detector with the given window and threshold.

func NewDetectorWithData

func NewDetectorWithData(window time.Duration, threshold float64, baselines []Baseline, anomalies []Anomaly) *Detector

NewDetectorWithData creates an anomaly detector pre-loaded with baselines and anomalies.

func (*Detector) Check

func (d *Detector) Check(service, metric string, value float64) *Anomaly

Check evaluates whether the given value is anomalous for the service+metric. Returns nil if the value is within normal bounds or if there's insufficient data.

func (*Detector) GetAnomalies

func (d *Detector) GetAnomalies(minutes int) []Anomaly

GetAnomalies returns anomalies detected within the last N minutes.

func (*Detector) GetBaselines

func (d *Detector) GetBaselines() []Baseline

GetBaselines returns a copy of all current baselines.

func (*Detector) UpdateBaseline

func (d *Detector) UpdateBaseline(service, metric string, value float64)

UpdateBaseline performs a rolling update of baseline statistics using Welford's online algorithm for numerically stable mean and variance.

func (*Detector) WhatChanged

func (d *Detector) WhatChanged(anom Anomaly) string

WhatChanged returns a human-readable explanation of what might have caused an anomaly, based on recent baseline changes across related metrics.

Jump to

Keyboard shortcuts

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