Files
ez-api/internal/model/alert.go
zenfun ba54abd424 feat(alerts): add traffic spike detection with configurable thresholds
Introduce traffic_spike alert type for monitoring system and per-master
traffic levels with configurable thresholds stored in database.

- Add AlertThresholdConfig model for persistent threshold configuration
- Implement GET/PUT /admin/alerts/thresholds endpoints for threshold management
- Add traffic spike detection in alert detector cron job:
  - Global QPS monitoring across all masters
  - Per-master RPM/TPM checks with minimum sample thresholds
  - Per-master RPD/TPD checks for daily limits
- Use warning severity at threshold, critical at 2x threshold
- Include metric metadata (value, threshold, window) in alert details
- Update API documentation with new endpoints and alert type
2025-12-31 15:56:17 +08:00

59 lines
2.2 KiB
Go

package model
import (
"time"
"gorm.io/gorm"
)
// AlertType defines the type of alert
type AlertType string
const (
AlertTypeRateLimit AlertType = "rate_limit"
AlertTypeErrorSpike AlertType = "error_spike"
AlertTypeQuotaExceeded AlertType = "quota_exceeded"
AlertTypeKeyDisabled AlertType = "key_disabled"
AlertTypeKeyExpired AlertType = "key_expired"
AlertTypeProviderDown AlertType = "provider_down"
AlertTypeTrafficSpike AlertType = "traffic_spike"
)
// AlertSeverity defines the severity level of an alert
type AlertSeverity string
const (
AlertSeverityInfo AlertSeverity = "info"
AlertSeverityWarning AlertSeverity = "warning"
AlertSeverityCritical AlertSeverity = "critical"
)
// AlertStatus defines the status of an alert
type AlertStatus string
const (
AlertStatusActive AlertStatus = "active"
AlertStatusAcknowledged AlertStatus = "acknowledged"
AlertStatusResolved AlertStatus = "resolved"
AlertStatusDismissed AlertStatus = "dismissed"
)
// Alert represents a system alert or notification
type Alert struct {
gorm.Model
Type AlertType `gorm:"size:50;not null;index" json:"type"`
Severity AlertSeverity `gorm:"size:20;not null;index" json:"severity"`
Status AlertStatus `gorm:"size:20;not null;default:'active';index" json:"status"`
Title string `gorm:"size:255;not null" json:"title"`
Message string `gorm:"type:text" json:"message"`
RelatedID uint `gorm:"index" json:"related_id,omitempty"`
RelatedType string `gorm:"size:50" json:"related_type,omitempty"` // master, key, apikey, provider_group
RelatedName string `gorm:"size:255" json:"related_name,omitempty"`
Fingerprint string `gorm:"size:255;index" json:"fingerprint,omitempty"` // For deduplication: type:related_type:related_id
Metadata string `gorm:"type:text" json:"metadata,omitempty"` // JSON encoded additional data
AckedAt *time.Time `json:"acked_at,omitempty"`
AckedBy string `gorm:"size:100" json:"acked_by,omitempty"`
ResolvedAt *time.Time `json:"resolved_at,omitempty"`
ExpiresAt *time.Time `gorm:"index" json:"expires_at,omitempty"` // Auto-dismiss after this time
}