ai-lib 0.4.0

A unified AI SDK for Rust providing a single interface for multiple AI providers with hybrid architecture
Documentation
//! Error monitoring and alerting

use crate::error_handling::ErrorContext;
use crate::metrics::Metrics;
use crate::types::AiLibError;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use std::time::Duration;

/// Error monitoring configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ErrorThresholds {
    /// Maximum error rate (errors per second)
    pub error_rate_threshold: f64,
    /// Maximum consecutive errors before alerting
    pub consecutive_errors: u32,
    /// Time window for error rate calculation
    pub time_window: Duration,
}

impl Default for ErrorThresholds {
    fn default() -> Self {
        Self {
            error_rate_threshold: 0.1, // 10% error rate
            consecutive_errors: 5,
            time_window: Duration::from_secs(60),
        }
    }
}

/// Error monitor for tracking and alerting
pub struct ErrorMonitor {
    metrics: Arc<dyn Metrics>,
    #[allow(dead_code)] // Reserved for future alerting functionality
    alert_thresholds: ErrorThresholds,
}

impl ErrorMonitor {
    /// Create a new error monitor
    pub fn new(metrics: Arc<dyn Metrics>, alert_thresholds: ErrorThresholds) -> Self {
        Self {
            metrics,
            alert_thresholds,
        }
    }

    /// Record an error and check for alerts
    pub async fn record_error(&self, error: &AiLibError, context: &ErrorContext) {
        // Record error metrics
        self.metrics.incr_counter("errors.total", 1).await;
        self.metrics
            .incr_counter(&format!("errors.{}", self.error_type_name(error)), 1)
            .await;

        // Check if we should send an alert
        if self.should_alert(error, context).await {
            self.send_alert(error, context).await;
        }
    }

    /// Check if an alert should be sent
    async fn should_alert(&self, error: &AiLibError, _context: &ErrorContext) -> bool {
        // This is a simplified implementation
        // In a real system, you would check error rates, consecutive errors, etc.
        matches!(
            error,
            AiLibError::RateLimitExceeded(_) | AiLibError::ProviderError(_)
        )
    }

    /// Send an alert (placeholder implementation)
    async fn send_alert(&self, error: &AiLibError, context: &ErrorContext) {
        // In a real implementation, this would send alerts via email, Slack, etc.
        // Error monitoring: In production, this would send alerts via email, Slack, etc.
        // For now, we just log the error context
        let _ = (error, context);
    }

    /// Get error type name for metrics
    fn error_type_name(&self, error: &AiLibError) -> String {
        match error {
            AiLibError::RateLimitExceeded(_) => "rate_limit".to_string(),
            AiLibError::NetworkError(_) => "network".to_string(),
            AiLibError::AuthenticationError(_) => "authentication".to_string(),
            AiLibError::ProviderError(_) => "provider".to_string(),
            AiLibError::TimeoutError(_) => "timeout".to_string(),
            _ => "unknown".to_string(),
        }
    }
}