test(alerts): add comprehensive tests for alert handler and detector

Add unit tests for alert-related functionality:

- alert_handler_test.go: tests for threshold CRUD operations,
  alert creation with traffic_spike type, filtering, and stats
- alert_detector_test.go: tests for threshold config loading,
  traffic spike severity calculation, deduplication logic,
  error rate severity, and nil-safety checks

Also fix format string issues:
- Use %d instead of %.2f for integer QPS in alert messages
- Wrap error description with format directive to avoid linter warning
This commit is contained in:
zenfun
2025-12-31 16:09:02 +08:00
parent 0b9556ee7e
commit f714a314a9
4 changed files with 594 additions and 2 deletions

View File

@@ -128,7 +128,7 @@ func (d *AlertDetector) detectRateLimits(ctx context.Context) {
model.AlertTypeRateLimit,
model.AlertSeverityWarning,
fmt.Sprintf("Master '%s' is rate limited", master.Name),
fmt.Sprintf("Master '%s' (ID: %d) is currently being rate limited. Current QPS: %.2f", master.Name, master.ID, snapshot.QPS),
fmt.Sprintf("Master '%s' (ID: %d) is currently being rate limited. Current QPS: %d", master.Name, master.ID, snapshot.QPS),
master.ID,
"master",
master.Name,