litellm-rs 0.1.1

A high-performance AI Gateway written in Rust, providing OpenAI-compatible APIs with intelligent routing, load balancing, and enterprise features
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
//! Alert management system
//!
//! This module provides comprehensive alerting functionality for monitoring events.

use crate::config::AlertingConfig;
use crate::monitoring::{Alert, AlertSeverity};
use crate::utils::error::{GatewayError, Result};
use std::collections::{HashMap, VecDeque};
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::{Mutex, RwLock};
use tracing::{debug, error, info, warn};

/// Alert manager for handling and dispatching alerts
#[derive(Debug)]
pub struct AlertManager {
    /// Configuration
    config: AlertingConfig,
    /// Pending alerts queue
    pending_alerts: Arc<Mutex<VecDeque<Alert>>>,
    /// Alert history
    alert_history: Arc<RwLock<VecDeque<Alert>>>,
    /// Alert rules
    alert_rules: Arc<RwLock<HashMap<String, AlertRule>>>,
    /// Notification channels
    notification_channels: Arc<RwLock<Vec<Box<dyn NotificationChannel>>>>,
    /// Whether the alert manager is active
    active: Arc<RwLock<bool>>,
    /// Alert statistics
    stats: Arc<RwLock<AlertStats>>,
}

/// Alert rule for automated alerting
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct AlertRule {
    /// Rule ID
    pub id: String,
    /// Rule name
    pub name: String,
    /// Rule description
    pub description: String,
    /// Metric to monitor
    pub metric: String,
    /// Threshold value
    pub threshold: f64,
    /// Comparison operator
    pub operator: ComparisonOperator,
    /// Alert severity
    pub severity: AlertSeverity,
    /// Evaluation interval
    pub interval: Duration,
    /// Whether the rule is enabled
    pub enabled: bool,
    /// Notification channels for this rule
    pub channels: Vec<String>,
}

/// Comparison operators for alert rules
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[allow(dead_code)]
pub enum ComparisonOperator {
    GreaterThan,
    LessThan,
    GreaterThanOrEqual,
    LessThanOrEqual,
    Equal,
    NotEqual,
}

/// Notification channel trait
#[async_trait::async_trait]
#[allow(dead_code)]
pub trait NotificationChannel: Send + Sync + std::fmt::Debug {
    /// Send a notification
    async fn send(&self, alert: &Alert) -> Result<()>;

    /// Get channel name
    fn name(&self) -> &str;

    /// Check if channel supports severity level
    fn supports_severity(&self, severity: AlertSeverity) -> bool;
}

/// Slack notification channel
#[derive(Debug)]
pub struct SlackChannel {
    webhook_url: String,
    channel: Option<String>,
    username: Option<String>,
    min_severity: AlertSeverity,
}

/// Email notification channel
#[derive(Debug)]
#[allow(dead_code)]
pub struct EmailChannel {
    smtp_config: SmtpConfig,
    recipients: Vec<String>,
    min_severity: AlertSeverity,
}

/// SMTP configuration
#[derive(Debug, Clone)]
#[allow(dead_code)]
pub struct SmtpConfig {
    pub server: String,
    pub port: u16,
    pub username: String,
    pub password: String,
    pub from_address: String,
}

/// Alert statistics
#[derive(Debug, Default, Clone, serde::Serialize)]
pub struct AlertStats {
    /// Total alerts sent
    pub total_alerts: u64,
    /// Alerts by severity
    pub alerts_by_severity: HashMap<String, u64>,
    /// Alerts by source
    pub alerts_by_source: HashMap<String, u64>,
    /// Failed notifications
    pub failed_notifications: u64,
    /// Last alert timestamp
    pub last_alert: Option<chrono::DateTime<chrono::Utc>>,
}

#[allow(dead_code)]
impl AlertManager {
    /// Create a new alert manager
    pub async fn new(config: &AlertingConfig) -> Result<Self> {
        let mut notification_channels: Vec<Box<dyn NotificationChannel>> = Vec::new();

        // Add Slack channel if configured
        if let Some(webhook_url) = &config.slack_webhook {
            notification_channels.push(Box::new(SlackChannel::new(
                webhook_url.clone(),
                None,
                Some("Gateway Alert".to_string()),
                AlertSeverity::Info,
            )));
        }

        // Add email channel if configured
        // TODO: Add email configuration support

        Ok(Self {
            config: config.clone(),
            pending_alerts: Arc::new(Mutex::new(VecDeque::new())),
            alert_history: Arc::new(RwLock::new(VecDeque::new())),
            alert_rules: Arc::new(RwLock::new(HashMap::new())),
            notification_channels: Arc::new(RwLock::new(notification_channels)),
            active: Arc::new(RwLock::new(false)),
            stats: Arc::new(RwLock::new(AlertStats::default())),
        })
    }

    /// Start the alert manager
    pub async fn start(&self) -> Result<()> {
        info!("Starting alert manager");

        *self.active.write().await = true;

        // Start alert processing task
        self.start_alert_processing().await;

        // Start rule evaluation task
        self.start_rule_evaluation().await;

        Ok(())
    }

    /// Stop the alert manager
    pub async fn stop(&self) -> Result<()> {
        info!("Stopping alert manager");
        *self.active.write().await = false;
        Ok(())
    }

    /// Send an alert
    pub async fn send_alert(&self, alert: Alert) -> Result<()> {
        debug!("Queuing alert: {} - {}", alert.severity, alert.title);

        // Add to pending queue
        {
            let mut pending = self.pending_alerts.lock().await;
            pending.push_back(alert.clone());
        }

        // Update statistics
        {
            let mut stats = self.stats.write().await;
            stats.total_alerts += 1;
            *stats
                .alerts_by_severity
                .entry(format!("{:?}", alert.severity))
                .or_insert(0) += 1;
            *stats
                .alerts_by_source
                .entry(alert.source.clone())
                .or_insert(0) += 1;
            stats.last_alert = Some(alert.timestamp);
        }

        // Add to history
        {
            let mut history = self.alert_history.write().await;
            history.push_back(alert);

            // Keep only recent alerts (last 1000)
            if history.len() > 1000 {
                history.pop_front();
            }
        }

        Ok(())
    }

    /// Process pending alerts
    pub async fn process_pending(&self) -> Result<()> {
        let mut alerts_to_process = Vec::new();

        // Get pending alerts
        {
            let mut pending = self.pending_alerts.lock().await;
            while let Some(alert) = pending.pop_front() {
                alerts_to_process.push(alert);
            }
        }

        // Process each alert
        for alert in alerts_to_process {
            if let Err(e) = self.process_alert(&alert).await {
                error!("Failed to process alert {}: {}", alert.id, e);

                // Update failed notification count
                let mut stats = self.stats.write().await;
                stats.failed_notifications += 1;
            }
        }

        Ok(())
    }

    /// Process a single alert
    async fn process_alert(&self, alert: &Alert) -> Result<()> {
        debug!("Processing alert: {}", alert.id);

        let channels = self.notification_channels.read().await;

        for channel in channels.iter() {
            if channel.supports_severity(alert.severity) {
                if let Err(e) = channel.send(alert).await {
                    warn!("Failed to send alert via {}: {}", channel.name(), e);
                } else {
                    debug!("Alert sent via {}", channel.name());
                }
            }
        }

        Ok(())
    }

    /// Add an alert rule
    pub async fn add_rule(&self, rule: AlertRule) -> Result<()> {
        info!("Adding alert rule: {}", rule.name);

        let mut rules = self.alert_rules.write().await;
        rules.insert(rule.id.clone(), rule);

        Ok(())
    }

    /// Remove an alert rule
    pub async fn remove_rule(&self, rule_id: &str) -> Result<()> {
        info!("Removing alert rule: {}", rule_id);

        let mut rules = self.alert_rules.write().await;
        rules.remove(rule_id);

        Ok(())
    }

    /// Get alert statistics
    pub async fn get_stats(&self) -> AlertStats {
        self.stats.read().await.clone()
    }

    /// Get alert history
    pub async fn get_history(&self, limit: Option<usize>) -> Vec<Alert> {
        let history = self.alert_history.read().await;
        let limit = limit.unwrap_or(100);

        history.iter().rev().take(limit).cloned().collect()
    }

    /// Start alert processing task
    async fn start_alert_processing(&self) {
        let alert_manager = self.clone();

        tokio::spawn(async move {
            let mut interval = tokio::time::interval(Duration::from_secs(5));

            loop {
                interval.tick().await;

                if !*alert_manager.active.read().await {
                    break;
                }

                if let Err(e) = alert_manager.process_pending().await {
                    error!("Failed to process pending alerts: {}", e);
                }
            }
        });
    }

    /// Start rule evaluation task
    async fn start_rule_evaluation(&self) {
        let alert_manager = self.clone();

        tokio::spawn(async move {
            let mut interval = tokio::time::interval(Duration::from_secs(60));

            loop {
                interval.tick().await;

                if !*alert_manager.active.read().await {
                    break;
                }

                if let Err(e) = alert_manager.evaluate_rules().await {
                    error!("Failed to evaluate alert rules: {}", e);
                }
            }
        });
    }

    /// Evaluate alert rules
    async fn evaluate_rules(&self) -> Result<()> {
        debug!("Evaluating alert rules");

        let rules = self.alert_rules.read().await.clone();

        for rule in rules.values() {
            if rule.enabled {
                if let Err(e) = self.evaluate_rule(rule).await {
                    warn!("Failed to evaluate rule {}: {}", rule.name, e);
                }
            }
        }

        Ok(())
    }

    /// Evaluate a single alert rule
    async fn evaluate_rule(&self, rule: &AlertRule) -> Result<()> {
        // TODO: Implement metric evaluation
        // This would involve getting the current metric value and comparing it to the threshold

        debug!("Evaluating rule: {}", rule.name);

        // Placeholder implementation
        let metric_value = 0.0; // Get actual metric value
        let threshold_exceeded = match rule.operator {
            ComparisonOperator::GreaterThan => metric_value > rule.threshold,
            ComparisonOperator::LessThan => metric_value < rule.threshold,
            ComparisonOperator::GreaterThanOrEqual => metric_value >= rule.threshold,
            ComparisonOperator::LessThanOrEqual => metric_value <= rule.threshold,
            ComparisonOperator::Equal => (metric_value - rule.threshold).abs() < f64::EPSILON,
            ComparisonOperator::NotEqual => (metric_value - rule.threshold).abs() >= f64::EPSILON,
        };

        if threshold_exceeded {
            let alert = Alert {
                id: uuid::Uuid::new_v4().to_string(),
                severity: rule.severity,
                title: format!("Alert Rule Triggered: {}", rule.name),
                description: format!(
                    "Rule '{}' triggered: {} {} {} (current value: {})",
                    rule.name,
                    rule.metric,
                    format!("{:?}", rule.operator).to_lowercase(),
                    rule.threshold,
                    metric_value
                ),
                timestamp: chrono::Utc::now(),
                source: "alert_rule".to_string(),
                metadata: serde_json::json!({
                    "rule_id": rule.id,
                    "metric": rule.metric,
                    "threshold": rule.threshold,
                    "current_value": metric_value,
                    "operator": format!("{:?}", rule.operator)
                }),
                resolved: false,
            };

            self.send_alert(alert).await?;
        }

        Ok(())
    }
}

impl Clone for AlertManager {
    fn clone(&self) -> Self {
        Self {
            config: self.config.clone(),
            pending_alerts: self.pending_alerts.clone(),
            alert_history: self.alert_history.clone(),
            alert_rules: self.alert_rules.clone(),
            notification_channels: self.notification_channels.clone(),
            active: self.active.clone(),
            stats: self.stats.clone(),
        }
    }
}

#[allow(dead_code)]
impl SlackChannel {
    /// Create a new Slack notification channel
    pub fn new(
        webhook_url: String,
        channel: Option<String>,
        username: Option<String>,
        min_severity: AlertSeverity,
    ) -> Self {
        Self {
            webhook_url,
            channel,
            username,
            min_severity,
        }
    }
}

#[async_trait::async_trait]
impl NotificationChannel for SlackChannel {
    async fn send(&self, alert: &Alert) -> Result<()> {
        let color = match alert.severity {
            AlertSeverity::Info => "#36a64f",      // Green
            AlertSeverity::Warning => "#ff9500",   // Orange
            AlertSeverity::Critical => "#ff0000",  // Red
            AlertSeverity::Emergency => "#8b0000", // Dark Red
        };

        let payload = serde_json::json!({
            "username": self.username.as_deref().unwrap_or("Gateway Alert"),
            "channel": self.channel,
            "attachments": [{
                "color": color,
                "title": alert.title,
                "text": alert.description,
                "fields": [
                    {
                        "title": "Severity",
                        "value": format!("{:?}", alert.severity),
                        "short": true
                    },
                    {
                        "title": "Source",
                        "value": alert.source,
                        "short": true
                    },
                    {
                        "title": "Time",
                        "value": alert.timestamp.format("%Y-%m-%d %H:%M:%S UTC").to_string(),
                        "short": true
                    }
                ],
                "footer": "Gateway Monitoring",
                "ts": alert.timestamp.timestamp()
            }]
        });

        let client = reqwest::Client::new();
        let response = client
            .post(&self.webhook_url)
            .json(&payload)
            .send()
            .await
            .map_err(|e| {
                GatewayError::Alert(format!("Failed to send Slack notification: {}", e))
            })?;

        if !response.status().is_success() {
            return Err(GatewayError::Alert(format!(
                "Slack webhook returned status: {}",
                response.status()
            )));
        }

        Ok(())
    }

    fn name(&self) -> &str {
        "slack"
    }

    fn supports_severity(&self, severity: AlertSeverity) -> bool {
        severity as u8 >= self.min_severity as u8
    }
}

#[allow(dead_code)]
impl EmailChannel {
    /// Create a new email notification channel
    pub fn new(
        smtp_config: SmtpConfig,
        recipients: Vec<String>,
        min_severity: AlertSeverity,
    ) -> Self {
        Self {
            smtp_config,
            recipients,
            min_severity,
        }
    }
}

#[async_trait::async_trait]
impl NotificationChannel for EmailChannel {
    async fn send(&self, _alert: &Alert) -> Result<()> {
        // TODO: Implement email sending
        // This would use an SMTP library to send emails
        warn!("Email notifications not implemented yet");
        Ok(())
    }

    fn name(&self) -> &str {
        "email"
    }

    fn supports_severity(&self, severity: AlertSeverity) -> bool {
        severity as u8 >= self.min_severity as u8
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_alert_rule_creation() {
        let rule = AlertRule {
            id: "test-rule".to_string(),
            name: "High CPU Usage".to_string(),
            description: "Alert when CPU usage exceeds 80%".to_string(),
            metric: "cpu_usage".to_string(),
            threshold: 80.0,
            operator: ComparisonOperator::GreaterThan,
            severity: AlertSeverity::Warning,
            interval: Duration::from_secs(60),
            enabled: true,
            channels: vec!["slack".to_string()],
        };

        assert_eq!(rule.name, "High CPU Usage");
        assert_eq!(rule.threshold, 80.0);
        assert!(rule.enabled);
    }

    #[test]
    fn test_comparison_operators() {
        assert_eq!(
            ComparisonOperator::GreaterThan,
            ComparisonOperator::GreaterThan
        );
        assert_ne!(
            ComparisonOperator::GreaterThan,
            ComparisonOperator::LessThan
        );
    }

    #[test]
    fn test_slack_channel_creation() {
        let channel = SlackChannel::new(
            "https://hooks.slack.com/test".to_string(),
            Some("#alerts".to_string()),
            Some("Gateway".to_string()),
            AlertSeverity::Warning,
        );

        assert_eq!(channel.name(), "slack");
        assert!(channel.supports_severity(AlertSeverity::Critical));
        assert!(!channel.supports_severity(AlertSeverity::Info));
    }

    #[test]
    fn test_alert_stats() {
        let mut stats = AlertStats {
            total_alerts: 10,
            ..Default::default()
        };
        stats.alerts_by_severity.insert("Warning".to_string(), 5);
        stats.alerts_by_severity.insert("Critical".to_string(), 3);

        assert_eq!(stats.total_alerts, 10);
        assert_eq!(stats.alerts_by_severity.get("Warning"), Some(&5));
    }
}