llm_sentinel_alerting/
lib.rs

1//! # Sentinel Alerting
2//!
3//! Alert delivery and notification system for LLM-Sentinel.
4//!
5//! This crate provides:
6//! - Alert delivery via RabbitMQ
7//! - Webhook notifications
8//! - Alert deduplication
9//! - Retry logic with exponential backoff
10//! - Alert routing by severity
11
12#![warn(missing_debug_implementations, rust_2018_idioms, unreachable_pub)]
13
14pub mod deduplication;
15pub mod rabbitmq;
16pub mod webhook;
17
18use async_trait::async_trait;
19use llm_sentinel_core::{events::AnomalyEvent, Result};
20use serde::{Deserialize, Serialize};
21
22/// Trait for alert delivery systems
23#[async_trait]
24pub trait Alerter: Send + Sync {
25    /// Send a single alert
26    async fn send(&self, alert: &AnomalyEvent) -> Result<()>;
27
28    /// Send multiple alerts in batch
29    async fn send_batch(&self, alerts: &[AnomalyEvent]) -> Result<()> {
30        for alert in alerts {
31            self.send(alert).await?;
32        }
33        Ok(())
34    }
35
36    /// Health check
37    async fn health_check(&self) -> Result<()>;
38
39    /// Get alerter name for logging
40    fn name(&self) -> &str;
41}
42
43/// Alert metadata for tracking delivery
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct AlertMetadata {
46    /// Unique alert ID
47    pub alert_id: String,
48    /// Number of delivery attempts
49    pub attempts: u32,
50    /// Last delivery attempt timestamp
51    pub last_attempt: chrono::DateTime<chrono::Utc>,
52    /// Delivery status
53    pub status: AlertStatus,
54}
55
56/// Alert delivery status
57#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
58pub enum AlertStatus {
59    /// Pending delivery
60    Pending,
61    /// Successfully delivered
62    Delivered,
63    /// Failed after retries
64    Failed,
65    /// Deduplicated (not sent)
66    Deduplicated,
67}
68
69/// Alert delivery configuration
70#[derive(Debug, Clone, Serialize, Deserialize)]
71pub struct AlertConfig {
72    /// Enable alert deduplication
73    pub enable_deduplication: bool,
74    /// Deduplication window in seconds
75    pub deduplication_window_secs: u64,
76    /// Maximum retry attempts
77    pub max_retries: u32,
78    /// Initial retry delay in milliseconds
79    pub retry_delay_ms: u64,
80    /// Backoff multiplier for retries
81    pub backoff_multiplier: f64,
82}
83
84impl Default for AlertConfig {
85    fn default() -> Self {
86        Self {
87            enable_deduplication: true,
88            deduplication_window_secs: 300, // 5 minutes
89            max_retries: 3,
90            retry_delay_ms: 1000,
91            backoff_multiplier: 2.0,
92        }
93    }
94}
95
96/// Re-export commonly used types
97pub mod prelude {
98    pub use crate::deduplication::{AlertDeduplicator, DeduplicationConfig};
99    pub use crate::rabbitmq::{RabbitMqAlerter, RabbitMqConfig};
100    pub use crate::webhook::{WebhookAlerter, WebhookConfig};
101    pub use crate::{AlertConfig, AlertStatus, Alerter};
102}