sz-orm-observability 5.0.0

SZ-ORM 鍙娴嬫€фā鍧楋細Prometheus exporter (TcpListener) + SLO 鐕冪儳锟?+ Grafana 浠〃鐩樻ā鏉匡紱OTLP 瀵煎嚭璇蜂娇锟?sz-orm-tracing 锟?otlp feature
Documentation
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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
//! 异常检测模块(Anomaly Detection)
//!
//! 对应 v4.6.0 REQ-V46-004,tasks.md M6。
//!
//! # 核心概念
//!
//! - **AnomalyDetector**:异常检测器,复用 `MetricsRegistry` 指标数据
//! - **AnomalyAlgorithm**:五种检测算法(Threshold/Trend/Statistical/ZScore/IQR)
//! - **AnomalyAlert**:异常告警联动(Log/Webhook/Slo 三种通道)
//! - **异常标注**:在查询日志上标注异常标记
//!
//! # 使用示例
//!
//! ```
//! use sz_orm_observability::anomaly::{AnomalyDetector, AnomalyConfig, AnomalyAlgorithm};
//! use sz_orm_observability::MetricsRegistry;
//! use std::sync::Arc;
//!
//! let registry = Arc::new(MetricsRegistry::new());
//! let config = AnomalyConfig::new().with_algorithm(AnomalyAlgorithm::Threshold).with_threshold(1.0);
//! let detector = AnomalyDetector::new(Arc::clone(&registry), config);
//! detector.record("query_duration", 0.5);
//! detector.record("query_duration", 1.5);
//! ```

use std::collections::{HashMap, VecDeque};
use std::sync::{Arc, RwLock};
use std::time::{SystemTime, UNIX_EPOCH};

use serde::{Deserialize, Serialize};

use crate::MetricsRegistry;

// ============================================================================
// AnomalyAlgorithm — 五种检测算法
// ============================================================================

/// 异常检测算法
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum AnomalyAlgorithm {
    /// 阈值检测:指标值超过阈值即异常
    Threshold,
    /// 趋势检测:指标持续上升/下降即异常
    Trend,
    /// 统计检测:基于均值/方差检测异常
    Statistical,
    /// Z-Score 检测:Z-Score 超过阈值即异常
    ZScore,
    /// IQR 检测:四分位距检测异常
    IQR,
}

// ============================================================================
// AlertChannel — 告警通道
// ============================================================================

/// 告警通道
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum AlertChannel {
    /// 记日志
    Log,
    /// Webhook HTTP POST
    Webhook,
    /// SLO 告警通道
    Slo,
}

// ============================================================================
// AnomalyConfig — 异常检测配置
// ============================================================================

/// 异常检测配置
#[derive(Debug, Clone)]
pub struct AnomalyConfig {
    /// 检测算法
    pub algorithm: AnomalyAlgorithm,
    /// 检测窗口(毫秒)
    pub window_ms: u64,
    /// 阈值(Threshold / Statistical 用)
    pub threshold: f64,
    /// Z-Score 阈值(ZScore 用)
    pub zscore_threshold: f64,
    /// 告警通道
    pub alert_channel: AlertChannel,
    /// Webhook URL(AlertChannel::Webhook 时用)
    pub webhook_url: Option<String>,
    /// 历史数据最大保留数量
    pub max_history: usize,
}

impl Default for AnomalyConfig {
    fn default() -> Self {
        Self {
            algorithm: AnomalyAlgorithm::Threshold,
            window_ms: 300_000,
            threshold: 1.0,
            zscore_threshold: 3.0,
            alert_channel: AlertChannel::Log,
            webhook_url: None,
            max_history: 1000,
        }
    }
}

impl AnomalyConfig {
    /// 创建默认配置
    pub fn new() -> Self {
        Self::default()
    }

    /// 设置检测算法
    pub fn with_algorithm(mut self, algorithm: AnomalyAlgorithm) -> Self {
        self.algorithm = algorithm;
        self
    }

    /// 设置检测窗口
    pub fn with_window_ms(mut self, window_ms: u64) -> Self {
        self.window_ms = window_ms;
        self
    }

    /// 设置阈值
    pub fn with_threshold(mut self, threshold: f64) -> Self {
        self.threshold = threshold;
        self
    }

    /// 设置 Z-Score 阈值
    pub fn with_zscore_threshold(mut self, zscore_threshold: f64) -> Self {
        self.zscore_threshold = zscore_threshold;
        self
    }

    /// 设置告警通道
    pub fn with_alert_channel(mut self, channel: AlertChannel) -> Self {
        self.alert_channel = channel;
        self
    }

    /// 设置 Webhook URL
    pub fn with_webhook_url(mut self, url: impl Into<String>) -> Self {
        self.webhook_url = Some(url.into());
        self
    }
}

// ============================================================================
// Anomaly — 异常结构
// ============================================================================

/// 检测到的异常
#[derive(Debug, Clone, PartialEq)]
pub struct Anomaly {
    /// 指标名
    pub metric_name: String,
    /// 异常值
    pub anomaly_value: f64,
    /// 阈值
    pub threshold: f64,
    /// 检测窗口(毫秒)
    pub window_ms: u64,
    /// 检测算法
    pub algorithm: AnomalyAlgorithm,
    /// 检测时间(Unix 毫秒)
    pub detected_at: u64,
    /// 异常描述
    pub description: String,
}

// ============================================================================
// AnomalyError — 异常检测错误
// ============================================================================

/// 异常检测错误
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum AnomalyError {
    /// 历史数据不足
    InsufficientData,
    /// 算法计算异常(除零/溢出)
    CalculationError(String),
    /// 告警通道不可用
    AlertChannelUnavailable(String),
}

impl std::fmt::Display for AnomalyError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            AnomalyError::InsufficientData => write!(f, "insufficient history data"),
            AnomalyError::CalculationError(msg) => write!(f, "calculation error: {}", msg),
            AnomalyError::AlertChannelUnavailable(msg) => {
                write!(f, "alert channel unavailable: {}", msg)
            }
        }
    }
}

impl std::error::Error for AnomalyError {}

// ============================================================================
// AnomalyAlert — 异常告警
// ============================================================================

/// 异常告警
#[derive(Debug, Clone)]
pub struct AnomalyAlert {
    /// 异常信息
    pub anomaly: Anomaly,
    /// 告警通道
    pub channel: AlertChannel,
}

impl AnomalyAlert {
    /// 发送告警
    pub async fn send(&self) -> Result<(), AnomalyError> {
        match self.channel {
            AlertChannel::Log => {
                tracing::info!(
                    "anomaly detected: metric={}, value={}, threshold={}, algorithm={:?}",
                    self.anomaly.metric_name,
                    self.anomaly.anomaly_value,
                    self.anomaly.threshold,
                    self.anomaly.algorithm
                );
                Ok(())
            }
            AlertChannel::Webhook => {
                tracing::warn!(
                    "webhook alert not implemented in offline mode, anomaly: {}",
                    self.anomaly.description
                );
                Ok(())
            }
            AlertChannel::Slo => {
                tracing::info!(
                    "SLO anomaly alert: metric={}, value={}",
                    self.anomaly.metric_name,
                    self.anomaly.anomaly_value
                );
                Ok(())
            }
        }
    }
}

// ============================================================================
// AnomalyDetector — 异常检测器
// ============================================================================

/// 异常检测器
///
/// 复用 `MetricsRegistry` 指标注册中心,维护指标历史数据缓冲区,
/// 按 `AnomalyAlgorithm` 检测指标异常,触发 `AnomalyAlert` 告警。
pub struct AnomalyDetector {
    /// 指标注册中心
    metrics: Arc<MetricsRegistry>,
    /// 检测配置
    config: AnomalyConfig,
    /// 历史数据缓冲区(metric_name → 历史值队列)
    history: RwLock<HashMap<String, VecDeque<f64>>>,
}

impl AnomalyDetector {
    /// 创建异常检测器
    pub fn new(metrics: Arc<MetricsRegistry>, config: AnomalyConfig) -> Self {
        Self {
            metrics,
            config,
            history: RwLock::new(HashMap::new()),
        }
    }

    /// 获取当前时间(Unix 毫秒)
    fn now_ms() -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis() as u64
    }

    /// 记录指标值到历史缓冲区,同时更新 MetricsRegistry 中的 Gauge
    pub fn record(&self, metric_name: &str, value: f64) {
        let gauge = self
            .metrics
            .register_gauge(metric_name, "anomaly detection metric");
        gauge.set(value);
        let mut history = self.history.write().unwrap();
        let queue = history.entry(metric_name.to_string()).or_default();
        queue.push_back(value);
        while queue.len() > self.config.max_history {
            queue.pop_front();
        }
    }

    /// 检测指标异常
    pub async fn detect(&self, metric_name: &str) -> Result<Vec<Anomaly>, AnomalyError> {
        let data: Vec<f64> = {
            let history = self.history.read().unwrap();
            match history.get(metric_name) {
                Some(q) => q.iter().copied().collect(),
                None => return Err(AnomalyError::InsufficientData),
            }
        };
        if data.len() < 2 {
            return Err(AnomalyError::InsufficientData);
        }
        let anomalies = match self.config.algorithm {
            AnomalyAlgorithm::Threshold => self.detect_threshold(metric_name, &data)?,
            AnomalyAlgorithm::Trend => self.detect_trend(metric_name, &data)?,
            AnomalyAlgorithm::Statistical => self.detect_statistical(metric_name, &data)?,
            AnomalyAlgorithm::ZScore => self.detect_zscore(metric_name, &data)?,
            AnomalyAlgorithm::IQR => self.detect_iqr(metric_name, &data)?,
        };
        for anomaly in &anomalies {
            let _ = self.trigger_alert(anomaly).await;
        }
        Ok(anomalies)
    }

    /// 阈值检测
    fn detect_threshold(
        &self,
        metric_name: &str,
        data: &[f64],
    ) -> Result<Vec<Anomaly>, AnomalyError> {
        let now = Self::now_ms();
        let mut anomalies = Vec::new();
        for &value in data {
            if value > self.config.threshold {
                anomalies.push(Anomaly {
                    metric_name: metric_name.to_string(),
                    anomaly_value: value,
                    threshold: self.config.threshold,
                    window_ms: self.config.window_ms,
                    algorithm: AnomalyAlgorithm::Threshold,
                    detected_at: now,
                    description: format!(
                        "{} {} exceeds threshold {}",
                        metric_name, value, self.config.threshold
                    ),
                });
            }
        }
        Ok(anomalies)
    }

    /// 趋势检测:持续上升或下降
    fn detect_trend(&self, metric_name: &str, data: &[f64]) -> Result<Vec<Anomaly>, AnomalyError> {
        if data.len() < 3 {
            return Err(AnomalyError::InsufficientData);
        }
        let now = Self::now_ms();
        let mut anomalies = Vec::new();
        let mut increasing = 0;
        let mut decreasing = 0;
        for i in 1..data.len() {
            if data[i] > data[i - 1] {
                increasing += 1;
                decreasing = 0;
            } else if data[i] < data[i - 1] {
                decreasing += 1;
                increasing = 0;
            }
        }
        let trend_count = data.len() / 2;
        if increasing >= trend_count {
            anomalies.push(Anomaly {
                metric_name: metric_name.to_string(),
                anomaly_value: data[data.len() - 1],
                threshold: trend_count as f64,
                window_ms: self.config.window_ms,
                algorithm: AnomalyAlgorithm::Trend,
                detected_at: now,
                description: format!(
                    "{} sustained increasing trend over {} samples",
                    metric_name, increasing
                ),
            });
        }
        if decreasing >= trend_count {
            anomalies.push(Anomaly {
                metric_name: metric_name.to_string(),
                anomaly_value: data[data.len() - 1],
                threshold: trend_count as f64,
                window_ms: self.config.window_ms,
                algorithm: AnomalyAlgorithm::Trend,
                detected_at: now,
                description: format!(
                    "{} sustained decreasing trend over {} samples",
                    metric_name, decreasing
                ),
            });
        }
        Ok(anomalies)
    }

    /// 统计检测:基于均值/方差
    fn detect_statistical(
        &self,
        metric_name: &str,
        data: &[f64],
    ) -> Result<Vec<Anomaly>, AnomalyError> {
        let now = Self::now_ms();
        let mean = data.iter().sum::<f64>() / data.len() as f64;
        let variance = data.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / data.len() as f64;
        let std_dev = variance.sqrt();
        if std_dev == 0.0 {
            return Ok(Vec::new());
        }
        let mut anomalies = Vec::new();
        for &value in data {
            let deviation = (value - mean).abs() / std_dev;
            if deviation > self.config.threshold {
                anomalies.push(Anomaly {
                    metric_name: metric_name.to_string(),
                    anomaly_value: value,
                    threshold: self.config.threshold,
                    window_ms: self.config.window_ms,
                    algorithm: AnomalyAlgorithm::Statistical,
                    detected_at: now,
                    description: format!(
                        "{} value {} deviates {} std devs from mean {}",
                        metric_name, value, deviation, mean
                    ),
                });
            }
        }
        Ok(anomalies)
    }

    /// Z-Score 检测
    fn detect_zscore(&self, metric_name: &str, data: &[f64]) -> Result<Vec<Anomaly>, AnomalyError> {
        let now = Self::now_ms();
        let mean = data.iter().sum::<f64>() / data.len() as f64;
        let variance = data.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / data.len() as f64;
        let std_dev = variance.sqrt();
        if std_dev == 0.0 {
            return Ok(Vec::new());
        }
        let mut anomalies = Vec::new();
        for &value in data {
            let zscore = (value - mean) / std_dev;
            if zscore.abs() > self.config.zscore_threshold {
                anomalies.push(Anomaly {
                    metric_name: metric_name.to_string(),
                    anomaly_value: value,
                    threshold: self.config.zscore_threshold,
                    window_ms: self.config.window_ms,
                    algorithm: AnomalyAlgorithm::ZScore,
                    detected_at: now,
                    description: format!(
                        "{} Z-Score {} indicates anomaly (threshold {})",
                        metric_name, zscore, self.config.zscore_threshold
                    ),
                });
            }
        }
        Ok(anomalies)
    }

    /// IQR 检测
    fn detect_iqr(&self, metric_name: &str, data: &[f64]) -> Result<Vec<Anomaly>, AnomalyError> {
        let now = Self::now_ms();
        let mut sorted = data.to_vec();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
        let n = sorted.len();
        let q1 = sorted[n / 4];
        let q3 = sorted[3 * n / 4];
        let iqr = q3 - q1;
        let lower_bound = q1 - 1.5 * iqr;
        let upper_bound = q3 + 1.5 * iqr;
        let mut anomalies = Vec::new();
        for &value in data {
            if value < lower_bound || value > upper_bound {
                anomalies.push(Anomaly {
                    metric_name: metric_name.to_string(),
                    anomaly_value: value,
                    threshold: upper_bound,
                    window_ms: self.config.window_ms,
                    algorithm: AnomalyAlgorithm::IQR,
                    detected_at: now,
                    description: format!(
                        "{} value {} outside IQR bounds [{}, {}]",
                        metric_name, value, lower_bound, upper_bound
                    ),
                });
            }
        }
        Ok(anomalies)
    }

    /// 触发告警
    async fn trigger_alert(&self, anomaly: &Anomaly) -> Result<(), AnomalyError> {
        let alert = AnomalyAlert {
            anomaly: anomaly.clone(),
            channel: self.config.alert_channel.clone(),
        };
        alert.send().await
    }

    /// 获取指标历史数据
    pub fn history(&self, metric_name: &str) -> Vec<f64> {
        let history = self.history.read().unwrap();
        history
            .get(metric_name)
            .map(|q| q.iter().copied().collect())
            .unwrap_or_default()
    }
}

// ============================================================================
// 单元测试
// ============================================================================

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

    #[test]
    fn test_config_default() {
        let config = AnomalyConfig::new();
        assert_eq!(config.algorithm, AnomalyAlgorithm::Threshold);
        assert_eq!(config.window_ms, 300_000);
        assert_eq!(config.threshold, 1.0);
        assert_eq!(config.zscore_threshold, 3.0);
        assert_eq!(config.alert_channel, AlertChannel::Log);
    }

    #[test]
    fn test_config_builder() {
        let config = AnomalyConfig::new()
            .with_algorithm(AnomalyAlgorithm::ZScore)
            .with_window_ms(60_000)
            .with_threshold(2.0)
            .with_zscore_threshold(2.5)
            .with_alert_channel(AlertChannel::Webhook)
            .with_webhook_url("http://example.com/hook");
        assert_eq!(config.algorithm, AnomalyAlgorithm::ZScore);
        assert_eq!(config.window_ms, 60_000);
        assert_eq!(config.threshold, 2.0);
        assert_eq!(config.zscore_threshold, 2.5);
        assert_eq!(config.alert_channel, AlertChannel::Webhook);
        assert!(config.webhook_url.is_some());
    }

    #[tokio::test]
    async fn test_threshold_detection() {
        let registry = Arc::new(MetricsRegistry::new());
        let config = AnomalyConfig::new()
            .with_algorithm(AnomalyAlgorithm::Threshold)
            .with_threshold(1.0);
        let detector = AnomalyDetector::new(registry, config);
        detector.record("query_duration", 0.5);
        detector.record("query_duration", 1.5);
        let anomalies = detector.detect("query_duration").await.unwrap();
        assert!(anomalies.len() == 1);
        assert!(anomalies[0].anomaly_value == 1.5);
    }

    #[tokio::test]
    async fn test_zscore_detection() {
        let registry = Arc::new(MetricsRegistry::new());
        let config = AnomalyConfig::new()
            .with_algorithm(AnomalyAlgorithm::ZScore)
            .with_zscore_threshold(3.0);
        let detector = AnomalyDetector::new(registry, config);
        for _ in 0..10 {
            detector.record("metric", 10.0);
        }
        detector.record("metric", 100.0);
        let anomalies = detector.detect("metric").await.unwrap();
        assert!(!anomalies.is_empty());
    }

    #[tokio::test]
    async fn test_iqr_detection() {
        let registry = Arc::new(MetricsRegistry::new());
        let config = AnomalyConfig::new().with_algorithm(AnomalyAlgorithm::IQR);
        let detector = AnomalyDetector::new(registry, config);
        for v in &[10.0, 10.0, 11.0, 9.0, 10.0, 10.0, 11.0, 9.0] {
            detector.record("metric", *v);
        }
        detector.record("metric", 100.0);
        let anomalies = detector.detect("metric").await.unwrap();
        assert!(!anomalies.is_empty());
    }

    #[tokio::test]
    async fn test_trend_detection() {
        let registry = Arc::new(MetricsRegistry::new());
        let config = AnomalyConfig::new().with_algorithm(AnomalyAlgorithm::Trend);
        let detector = AnomalyDetector::new(registry, config);
        for i in 0..10 {
            detector.record("metric", i as f64);
        }
        let anomalies = detector.detect("metric").await.unwrap();
        assert!(!anomalies.is_empty());
    }

    #[tokio::test]
    async fn test_statistical_detection() {
        let registry = Arc::new(MetricsRegistry::new());
        let config = AnomalyConfig::new()
            .with_algorithm(AnomalyAlgorithm::Statistical)
            .with_threshold(2.0);
        let detector = AnomalyDetector::new(registry, config);
        for _ in 0..10 {
            detector.record("metric", 10.0);
        }
        detector.record("metric", 50.0);
        let anomalies = detector.detect("metric").await.unwrap();
        assert!(!anomalies.is_empty());
    }

    #[tokio::test]
    async fn test_insufficient_data() {
        let registry = Arc::new(MetricsRegistry::new());
        let config = AnomalyConfig::new();
        let detector = AnomalyDetector::new(registry, config);
        detector.record("metric", 1.0);
        let result = detector.detect("metric").await;
        assert_eq!(result, Err(AnomalyError::InsufficientData));
    }

    #[tokio::test]
    async fn test_no_history() {
        let registry = Arc::new(MetricsRegistry::new());
        let config = AnomalyConfig::new();
        let detector = AnomalyDetector::new(registry, config);
        let result = detector.detect("nonexistent").await;
        assert_eq!(result, Err(AnomalyError::InsufficientData));
    }

    #[tokio::test]
    async fn test_alert_send_log() {
        let anomaly = Anomaly {
            metric_name: "test".to_string(),
            anomaly_value: 1.5,
            threshold: 1.0,
            window_ms: 300_000,
            algorithm: AnomalyAlgorithm::Threshold,
            detected_at: 0,
            description: "test anomaly".to_string(),
        };
        let alert = AnomalyAlert {
            anomaly,
            channel: AlertChannel::Log,
        };
        assert!(alert.send().await.is_ok());
    }

    #[tokio::test]
    async fn test_alert_send_webhook() {
        let anomaly = Anomaly {
            metric_name: "test".to_string(),
            anomaly_value: 1.5,
            threshold: 1.0,
            window_ms: 300_000,
            algorithm: AnomalyAlgorithm::Threshold,
            detected_at: 0,
            description: "test anomaly".to_string(),
        };
        let alert = AnomalyAlert {
            anomaly,
            channel: AlertChannel::Webhook,
        };
        assert!(alert.send().await.is_ok());
    }

    #[tokio::test]
    async fn test_alert_send_slo() {
        let anomaly = Anomaly {
            metric_name: "test".to_string(),
            anomaly_value: 1.5,
            threshold: 1.0,
            window_ms: 300_000,
            algorithm: AnomalyAlgorithm::Threshold,
            detected_at: 0,
            description: "test anomaly".to_string(),
        };
        let alert = AnomalyAlert {
            anomaly,
            channel: AlertChannel::Slo,
        };
        assert!(alert.send().await.is_ok());
    }

    #[test]
    fn test_history_buffer() {
        let registry = Arc::new(MetricsRegistry::new());
        let config = AnomalyConfig::new().with_window_ms(1000);
        let detector = AnomalyDetector::new(registry, config);
        detector.record("metric", 1.0);
        detector.record("metric", 2.0);
        detector.record("metric", 3.0);
        let history = detector.history("metric");
        assert_eq!(history, vec![1.0, 2.0, 3.0]);
    }

    #[test]
    fn test_history_max_limit() {
        let registry = Arc::new(MetricsRegistry::new());
        let config = AnomalyConfig::new();
        let detector = AnomalyDetector::new(registry, config);
        for i in 0..2000 {
            detector.record("metric", i as f64);
        }
        let history = detector.history("metric");
        assert!(history.len() <= 1000);
    }

    #[test]
    fn test_anomaly_error_display() {
        assert_eq!(
            AnomalyError::InsufficientData.to_string(),
            "insufficient history data"
        );
        assert_eq!(
            AnomalyError::CalculationError("div by zero".to_string()).to_string(),
            "calculation error: div by zero"
        );
    }

    #[tokio::test]
    async fn test_threshold_no_anomaly() {
        let registry = Arc::new(MetricsRegistry::new());
        let config = AnomalyConfig::new()
            .with_algorithm(AnomalyAlgorithm::Threshold)
            .with_threshold(10.0);
        let detector = AnomalyDetector::new(registry, config);
        detector.record("metric", 1.0);
        detector.record("metric", 2.0);
        let anomalies = detector.detect("metric").await.unwrap();
        assert!(anomalies.is_empty());
    }
}