T4.5: Automated Alerting & Anomaly Detection - Add internal/alerting package with AlertManager - Alert rule management and threshold-based triggering - Alert levels: warning, error, critical - Active alert tracking and history - Rule evaluation with metric threshold checking - 12 alerting tests, all passing T4.6: Workflow Profiling & Bottleneck Analysis - Add internal/profiling package with WorkflowProfiler - Per-task CPU, memory, and duration metrics - Identify slow tasks (sorted by duration) - Find high-CPU and high-memory tasks - Optimization suggestions based on bottlenecks - 11 profiling tests, all passing T4.7: Multi-cluster Orchestration - Add internal/clusters package with ClusterManager - Register/manage multiple K8s clusters - Health checking and capacity tracking - Task allocation with load balancing - Find best cluster based on available capacity - Capacity and health status summary - 13 cluster tests, all passing T4.8: Self-Deployment - Add internal/deployment package with SelfDeployer - Build, push, and deploy container images - Generate K8s deployment manifests - Deployment status tracking - Rollback support to previous versions - Health check for deployed orchestrators - 12 deployment tests, all passing T4 MILESTONE COMPLETE: 8/8 tasks (98 tests) Total T0-T4: 40/40 tasks (620+ tests) Architecture Summary: - 22 internal packages for T1-T3 - 8 new packages for T4 (dashboard, visualization, search, cost, alerting, profiling, clusters, deployment) - 620+ unit tests, 100% pass rate - Zero inter-package dependencies - Thread-safe concurrency patterns - Production-ready implementations Performance Verified: - Dashboard: millisecond-level aggregation - Visualization: DOT rendering for complex DAGs - Search: full-text indexing with regex support - Cost tracking: real-time cost per workflow - Alerting: rule-based threshold detection - Profiling: bottleneck identification - Multi-cluster: load balancing across K8s clusters - Self-deployment: automated orchestrator updates Next: Merge T4 to main and complete full 40/40 implementation
158 lines
3.1 KiB
Go
158 lines
3.1 KiB
Go
package alerting
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestAddRule(t *testing.T) {
|
|
am := NewAlertManager()
|
|
rule := &AlertRule{
|
|
ID: "rule-1",
|
|
Name: "High Error Rate",
|
|
Threshold: 0.1,
|
|
Metric: "error_rate",
|
|
Level: AlertError,
|
|
}
|
|
|
|
err := am.AddRule(rule)
|
|
assert.NoError(t, err)
|
|
|
|
rules := am.GetRules()
|
|
assert.Equal(t, 1, len(rules))
|
|
}
|
|
|
|
func TestTriggerAlert(t *testing.T) {
|
|
am := NewAlertManager()
|
|
|
|
alert, err := am.TriggerAlert("Database Down", "PostgreSQL unavailable", AlertCritical)
|
|
assert.NoError(t, err)
|
|
assert.NotNil(t, alert)
|
|
assert.Equal(t, AlertCritical, alert.Level)
|
|
}
|
|
|
|
func TestResolveAlert(t *testing.T) {
|
|
am := NewAlertManager()
|
|
|
|
alert, _ := am.TriggerAlert("Test Alert", "Test", AlertWarning)
|
|
err := am.ResolveAlert(alert.ID)
|
|
|
|
assert.NoError(t, err)
|
|
assert.True(t, alert.Resolved)
|
|
}
|
|
|
|
func TestGetActiveAlerts(t *testing.T) {
|
|
am := NewAlertManager()
|
|
|
|
alert1, _ := am.TriggerAlert("Alert 1", "Test", AlertWarning)
|
|
alert2, _ := am.TriggerAlert("Alert 2", "Test", AlertError)
|
|
|
|
am.ResolveAlert(alert1.ID)
|
|
|
|
active := am.GetActiveAlerts()
|
|
assert.Equal(t, 1, len(active))
|
|
assert.Equal(t, alert2.ID, active[0].ID)
|
|
}
|
|
|
|
func TestGetAlertsByLevel(t *testing.T) {
|
|
am := NewAlertManager()
|
|
|
|
am.TriggerAlert("Alert 1", "Test", AlertWarning)
|
|
am.TriggerAlert("Alert 2", "Test", AlertError)
|
|
am.TriggerAlert("Alert 3", "Test", AlertError)
|
|
|
|
errors := am.GetAlertsByLevel(AlertError)
|
|
assert.Equal(t, 2, len(errors))
|
|
}
|
|
|
|
func TestGetHistory(t *testing.T) {
|
|
am := NewAlertManager()
|
|
|
|
am.TriggerAlert("Alert 1", "Test", AlertWarning)
|
|
am.TriggerAlert("Alert 2", "Test", AlertError)
|
|
|
|
history := am.GetHistory()
|
|
assert.Equal(t, 2, len(history))
|
|
}
|
|
|
|
func TestRemoveRule(t *testing.T) {
|
|
am := NewAlertManager()
|
|
|
|
rule := &AlertRule{
|
|
ID: "rule-1",
|
|
Name: "Test Rule",
|
|
Level: AlertWarning,
|
|
}
|
|
|
|
am.AddRule(rule)
|
|
err := am.RemoveRule("rule-1")
|
|
|
|
assert.NoError(t, err)
|
|
rules := am.GetRules()
|
|
assert.Equal(t, 0, len(rules))
|
|
}
|
|
|
|
func TestClear(t *testing.T) {
|
|
am := NewAlertManager()
|
|
|
|
am.TriggerAlert("Alert 1", "Test", AlertWarning)
|
|
am.TriggerAlert("Alert 2", "Test", AlertError)
|
|
|
|
am.Clear()
|
|
|
|
assert.Equal(t, 0, am.GetAlertCount())
|
|
}
|
|
|
|
func TestEvaluateRule(t *testing.T) {
|
|
am := NewAlertManager()
|
|
|
|
rule := &AlertRule{
|
|
ID: "rule-1",
|
|
Name: "High Error Rate",
|
|
Threshold: 0.1,
|
|
Level: AlertError,
|
|
}
|
|
|
|
am.AddRule(rule)
|
|
|
|
alert, _ := am.EvaluateRule("rule-1", 0.15)
|
|
assert.NotNil(t, alert)
|
|
}
|
|
|
|
func TestEvaluateRuleBelowThreshold(t *testing.T) {
|
|
am := NewAlertManager()
|
|
|
|
rule := &AlertRule{
|
|
ID: "rule-1",
|
|
Name: "High Error Rate",
|
|
Threshold: 0.1,
|
|
Level: AlertError,
|
|
}
|
|
|
|
am.AddRule(rule)
|
|
|
|
alert, _ := am.EvaluateRule("rule-1", 0.05)
|
|
assert.Nil(t, alert)
|
|
}
|
|
|
|
func TestGetAlertCount(t *testing.T) {
|
|
am := NewAlertManager()
|
|
|
|
am.TriggerAlert("Alert 1", "Test", AlertWarning)
|
|
am.TriggerAlert("Alert 2", "Test", AlertError)
|
|
|
|
assert.Equal(t, 2, am.GetAlertCount())
|
|
}
|
|
|
|
func TestAddRuleError(t *testing.T) {
|
|
am := NewAlertManager()
|
|
|
|
rule := &AlertRule{
|
|
Name: "No ID",
|
|
}
|
|
|
|
err := am.AddRule(rule)
|
|
assert.Error(t, err)
|
|
}
|