somnytoo 2.0.0

Binary protocol server for secure communications
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
use std::sync::Arc;
use std::time::{Instant, Duration};
use std::collections::{VecDeque, HashMap};
use tracing::{info, debug, warn, error};
use dashmap::DashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use tokio::sync::RwLock;

/// Модель распределения метрик
#[derive(Debug, Clone)]
pub struct MetricDistributionModel {
    /// Среднее значение (μ)
    pub mean: f64,

    /// Медиана (50-й перцентиль)
    pub median: f64,

    /// Стандартное отклонение (σ)
    pub std_dev: f64,

    /// Дисперсия (σ²)
    pub variance: f64,

    /// Асимметрия (skewness)
    pub skewness: f64,

    /// Эксцесс (kurtosis)
    pub kurtosis: f64,

    /// Минимальное значение
    pub min: f64,

    /// Максимальное значение
    pub max: f64,

    /// Количество наблюдений
    pub count: u64,

    /// Сумма значений
    pub sum: f64,

    /// Сумма квадратов (для онлайн-вычисления дисперсии)
    pub sum_squares: f64,
}

impl MetricDistributionModel {
    pub fn new() -> Self {
        Self {
            mean: 0.0,
            median: 0.0,
            std_dev: 0.0,
            variance: 0.0,
            skewness: 0.0,
            kurtosis: 0.0,
            min: f64::INFINITY,
            max: f64::NEG_INFINITY,
            count: 0,
            sum: 0.0,
            sum_squares: 0.0,
        }
    }

    /// Обновление статистики онлайн (алгоритм Уэлфорда)
    pub fn update(&mut self, value: f64) {
        self.count += 1;
        self.sum += value;
        self.sum_squares += value * value;

        // Обновление минимума и максимума
        self.min = self.min.min(value);
        self.max = self.max.max(value);

        // Обновление среднего
        let delta = value - self.mean;
        self.mean += delta / self.count as f64;

        // Обновление суммы квадратов отклонений (для дисперсии)
        let delta2 = value - self.mean;
        self.sum_squares = self.sum_squares + delta * delta2;

        // Расчёт дисперсии и стандартного отклонения
        if self.count > 1 {
            self.variance = self.sum_squares / (self.count - 1) as f64;
            self.std_dev = self.variance.sqrt();
        }
    }

    /// Расчёт перцентиля (требует сохранения истории)
    pub fn percentile(&self, p: f64, sorted_values: &[f64]) -> f64 {
        if sorted_values.is_empty() {
            return 0.0;
        }

        let idx = (sorted_values.len() as f64 * p / 100.0) as usize;
        sorted_values[idx.min(sorted_values.len() - 1)]
    }
}

#[derive(Debug, Clone)]
pub struct TimeSeriesModel {
    /// Уровень (level)
    pub level: f64,

    /// Тренд (trend)
    pub trend: f64,

    /// Сезонные компоненты
    pub seasonal: Vec<f64>,

    /// Длина сезона
    pub season_length: usize,

    /// Коэффициент сглаживания уровня (α)
    pub alpha: f64,

    /// Коэффициент сглаживания тренда (β)
    pub beta: f64,

    /// Коэффициент сглаживания сезонности (γ)
    pub gamma: f64,

    /// Прогноз на следующий шаг
    pub forecast: f64,

    /// Ошибка прогноза
    pub forecast_error: f64,

    /// Доверительный интервал
    pub confidence_interval: f64,
}

impl TimeSeriesModel {
    pub fn new(season_length: usize) -> Self {
        Self {
            level: 0.0,
            trend: 0.0,
            seasonal: vec![0.0; season_length],
            season_length,
            alpha: 0.3,
            beta: 0.1,
            gamma: 0.2,
            forecast: 0.0,
            forecast_error: 0.0,
            confidence_interval: 0.0,
        }
    }

    /// Обновление модели Хольта-Винтерса
    pub fn update(&mut self, observation: f64, t: usize) -> f64 {
        let season_idx = t % self.season_length;

        // Предыдущий уровень
        let last_level = self.level;

        // Прогноз
        self.forecast = self.level + self.trend + self.seasonal[season_idx];

        // Ошибка прогноза
        self.forecast_error = observation - self.forecast;

        // Обновление уровня
        self.level = self.alpha * (observation - self.seasonal[season_idx])
            + (1.0 - self.alpha) * (self.level + self.trend);

        // Обновление тренда
        self.trend = self.beta * (self.level - last_level)
            + (1.0 - self.beta) * self.trend;

        // Обновление сезонности
        self.seasonal[season_idx] = self.gamma * (observation - last_level - self.trend)
            + (1.0 - self.gamma) * self.seasonal[season_idx];

        // Расчёт доверительного интервала (95%)
        self.confidence_interval = 1.96 * self.forecast_error.abs();

        self.forecast
    }
}

#[derive(Debug, Clone)]
pub struct AggregatedMetric {
    pub name: String,
    pub distribution: MetricDistributionModel,
    pub time_series: TimeSeriesModel,
    pub values: VecDeque<f64>,
    pub timestamps: VecDeque<Instant>,
    pub max_history: usize,
    pub last_updated: Option<Instant>,
    pub labels: HashMap<String, String>,
}

impl AggregatedMetric {
    pub fn new(name: String, max_history: usize) -> Self {
        Self {
            name,
            distribution: MetricDistributionModel::new(),
            time_series: TimeSeriesModel::new(24), // Суточная сезонность
            values: VecDeque::with_capacity(max_history),
            timestamps: VecDeque::with_capacity(max_history),
            max_history,
            last_updated: None,
            labels: HashMap::new(),
        }
    }

    /// Добавление значения
    pub fn add_value(&mut self, value: f64) {
        let now = Instant::now();

        // Обновление распределения
        self.distribution.update(value);

        // Обновление временного ряда
        self.time_series.update(value, self.values.len());

        // Сохранение истории
        self.values.push_back(value);
        self.timestamps.push_back(now);

        if self.values.len() > self.max_history {
            self.values.pop_front();
            self.timestamps.pop_front();
        }

        self.last_updated = Some(now);
    }

    /// Получение отсортированных значений
    pub fn sorted_values(&self) -> Vec<f64> {
        let mut sorted: Vec<f64> = self.values.iter().copied().collect();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());
        sorted
    }

    /// Получение перцентиля
    pub fn percentile(&self, p: f64) -> f64 {
        let sorted = self.sorted_values();
        self.distribution.percentile(p, &sorted)
    }

    /// Получение скорости изменения (производной)
    pub fn derivative(&self) -> f64 {
        if self.values.len() < 2 {
            return 0.0;
        }

        let last = *self.values.back().unwrap_or(&0.0);
        let prev = *self.values.get(self.values.len() - 2).unwrap_or(&0.0);

        last - prev
    }
}

#[derive(Debug, Clone)]
pub struct MetricsConfig {
    pub enabled: bool,
    pub collection_interval: Duration,
    pub trace_sampling_rate: f64,
    pub service_name: String,
    pub service_version: String,
    pub environment: String,
    pub retention_period: Duration,
    pub max_metrics_per_service: usize,
    pub enable_histograms: bool,
    pub enable_time_series: bool,
    pub enable_anomaly_detection: bool,
    pub anomaly_threshold: f64,
}

impl Default for MetricsConfig {
    fn default() -> Self {
        Self {
            enabled: true,
            collection_interval: Duration::from_secs(5),
            trace_sampling_rate: 0.1,
            service_name: "batch-system".to_string(),
            service_version: "2.0.0".to_string(),
            environment: "production".to_string(),
            retention_period: Duration::from_secs(3600),
            max_metrics_per_service: 1000,
            enable_histograms: true,
            enable_time_series: true,
            enable_anomaly_detection: true,
            anomaly_threshold: 3.0, // 3 сигмы
        }
    }
}

#[derive(Debug, Clone)]
pub struct Anomaly {
    pub metric_name: String,
    pub timestamp: Instant,
    pub value: f64,
    pub expected_value: f64,
    pub deviation: f64,
    pub threshold: f64,
    pub severity: AnomalySeverity,
    pub description: String,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AnomalySeverity {
    Info,
    Warning,
    Critical,
}

#[derive(Debug, thiserror::Error)]
pub enum MetricsError {
    #[error("Tracing not initialized")]
    TracingNotInitialized,

    #[error("Metric not found: {0}")]
    MetricNotFound(String),

    #[error("Invalid configuration: {0}")]
    InvalidConfig(String),

    #[error("Storage error: {0}")]
    StorageError(String),
}

pub struct MetricsTracingSystem {
    metrics_store: Arc<DashMap<String, AggregatedMetric>>,
    system_health: Arc<RwLock<SystemHealthModel>>,
    anomaly_detector: Arc<RwLock<AnomalyDetector>>,
    config: MetricsConfig,
    total_metrics_recorded: Arc<AtomicU64>,
    total_anomalies_detected: Arc<AtomicU64>,
    start_time: Instant,
    anomalies: Arc<RwLock<VecDeque<Anomaly>>>,
    recent_anomalies: Arc<DashMap<String, Instant>>,
    anomaly_cooldown: Duration,
}

#[derive(Debug, Clone)]
pub struct SystemHealthModel {
    pub overall_health: f64,
    pub component_health: HashMap<String, f64>,
    pub latency_health: f64,
    pub throughput_health: f64,
    pub error_rate_health: f64,
    pub memory_health: f64,
    pub cpu_health: f64,
    pub last_updated: Instant,
}

impl SystemHealthModel {
    pub fn new() -> Self {
        Self {
            overall_health: 1.0,
            component_health: HashMap::new(),
            latency_health: 1.0,
            throughput_health: 1.0,
            error_rate_health: 1.0,
            memory_health: 1.0,
            cpu_health: 1.0,
            last_updated: Instant::now(),
        }
    }

    /// Обновление здоровья на основе метрик
    pub fn update(&mut self, metrics: &DashMap<String, AggregatedMetric>) {
        // Здоровье по задержке
        if let Some(metric) = metrics.get("latency.p95") {
            let p95 = metric.percentile(95.0);
            self.latency_health = 1.0 / (1.0 + (p95 / 100.0).min(10.0));
        }

        // Здоровье по пропускной способности
        if let Some(metric) = metrics.get("throughput") {
            let throughput = metric.distribution.mean;
            self.throughput_health = (throughput / 100000.0).min(1.0);
        }

        // Здоровье по ошибкам
        if let Some(metric) = metrics.get("system.errors") {
            let error_rate = metric.derivative();
            self.error_rate_health = 1.0 / (1.0 + error_rate * 100.0);
        }

        // Общее здоровье (среднее взвешенное)
        self.overall_health =
            self.latency_health * 0.4 +
            self.throughput_health * 0.3 +
            self.error_rate_health * 0.2 +
            self.memory_health * 0.05 +
            self.cpu_health * 0.05;

        self.last_updated = Instant::now();
    }
}

#[derive(Debug, Clone)]
pub struct AnomalyDetector {
    /// Порог для Z-score
    pub zscore_threshold: f64,

    /// Порог для MAD (Median Absolute Deviation)
    pub mad_threshold: f64,

    /// Порог для изменения тренда
    pub trend_threshold: f64,

    /// История аномалий
    pub history: VecDeque<Anomaly>,
    trend_history: DashMap<String, VecDeque<f64>>,
    _min_samples_for_trend: usize,
}

impl AnomalyDetector {
    pub fn new() -> Self {
        Self {
            zscore_threshold: 3.0,
            mad_threshold: 3.5,
            trend_threshold: 0.5,
            history: VecDeque::with_capacity(1000),
            trend_history: DashMap::new(),
            _min_samples_for_trend: 5,
        }
    }

    /// Детекция аномалий методом Z-score
    pub fn detect_zscore(&self, value: f64, mean: f64, std_dev: f64) -> Option<f64> {
        if std_dev <= 0.0 {
            return None;
        }

        let zscore = (value - mean).abs() / std_dev;

        if zscore > self.zscore_threshold {
            Some(zscore)
        } else {
            None
        }
    }

    /// Детекция аномалий методом MAD
    pub fn detect_mad(&self, value: f64, median: f64, mad: f64) -> Option<f64> {
        if mad <= 0.0 {
            return None;
        }

        let modified_zscore = 0.6745 * (value - median).abs() / mad;

        if modified_zscore > self.mad_threshold {
            Some(modified_zscore)
        } else {
            None
        }
    }

    /// Детекция аномалий по изменению тренда
    pub fn detect_trend_change(&self, metric_name: &str, current_trend: f64) -> bool {
        let mut history = self.trend_history
            .entry(metric_name.to_string())
            .or_insert_with(|| VecDeque::with_capacity(10));

        history.push_back(current_trend);
        if history.len() > 10 {
            history.pop_front();
        }

        // Нужно минимум 5 измерений
        if history.len() < 5 {
            return false;
        }

        // БЕРЁМ ПРЕДЫДУЩЕЕ ЗНАЧЕНИЕ, НЕ ТЕКУЩЕЕ!
        let prev_trend = history[history.len() - 2];

        // ИСПРАВЛЕНО: Если тренд не изменился - НЕ АНОМАЛИЯ
        if (current_trend - prev_trend).abs() < 0.1 {
            return false;
        }

        // ИСПРАВЛЕНО: Игнорируем тренды около нуля при отсутствии нагрузки
        if history.iter().all(|&t| t.abs() < 0.1) {
            return false;
        }

        // Проверяем резкое изменение
        let change = (current_trend - prev_trend).abs();
        change > self.trend_threshold
    }

    /// Расчёт MAD (Median Absolute Deviation)
    pub fn calculate_mad(values: &[f64], median: f64) -> f64 {
        let deviations: Vec<f64> = values.iter()
            .map(|&x| (x - median).abs())
            .collect();

        let mut sorted = deviations.clone();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap());

        let mid = sorted.len() / 2;
        if sorted.len() % 2 == 0 {
            (sorted[mid - 1] + sorted[mid]) / 2.0
        } else {
            sorted[mid]
        }
    }
}

impl MetricsTracingSystem {
    pub fn new(config: MetricsConfig) -> Result<Self, MetricsError> {
        info!("📊 Initializing Mathematical Metrics & Tracing System v2.0");
        info!("  Service: {} v{}", config.service_name, config.service_version);
        info!("  Environment: {}", config.environment);
        info!("  Collection interval: {:?}", config.collection_interval);
        info!("  Sampling rate: {:.1}%", config.trace_sampling_rate * 100.0);
        info!("  Histograms: {}", if config.enable_histograms { "enabled" } else { "disabled" });
        info!("  Time series: {}", if config.enable_time_series { "enabled" } else { "disabled" });
        info!("  Anomaly detection: {}", if config.enable_anomaly_detection { "enabled" } else { "disabled" });

        if config.enabled && !tracing::dispatcher::has_been_set() {
            return Err(MetricsError::TracingNotInitialized);
        }

        Ok(Self {
            metrics_store: Arc::new(DashMap::with_capacity(1000)),
            system_health: Arc::new(RwLock::new(SystemHealthModel::new())),
            anomaly_detector: Arc::new(RwLock::new(AnomalyDetector::new())),
            config,
            total_metrics_recorded: Arc::new(AtomicU64::new(0)),
            total_anomalies_detected: Arc::new(AtomicU64::new(0)),
            start_time: Instant::now(),
            anomalies: Arc::new(RwLock::new(VecDeque::with_capacity(1000))),
            recent_anomalies: Arc::new(DashMap::new()),
            anomaly_cooldown: Duration::from_secs(300), // 5 минут
        })
    }

    pub async fn record_metric(&self, name: &str, value: f64) {
        if !self.config.enabled {
            return;
        }

        let key = name.to_string();

        // Получение или создание метрики
        let mut metric = self.metrics_store
            .entry(key.clone())
            .or_insert_with(|| AggregatedMetric::new(key, 1000));

        // Добавление значения
        metric.add_value(value);

        // Обновление счётчика
        self.total_metrics_recorded.fetch_add(1, Ordering::Relaxed);

        // Детекция аномалий
        if self.config.enable_anomaly_detection {
            self.detect_anomalies(&metric, value).await;
        }
    }

    pub fn record_metric_with_labels(&self, name: &str, value: f64, labels: HashMap<String, String>) {
        if !self.config.enabled {
            return;
        }

        let key = name.to_string();

        let mut metric = self.metrics_store
            .entry(key.clone())
            .or_insert_with(|| AggregatedMetric::new(key, 1000));

        metric.labels = labels;
        metric.add_value(value);

        self.total_metrics_recorded.fetch_add(1, Ordering::Relaxed);
    }

    async fn detect_anomalies(&self, metric: &AggregatedMetric, value: f64) {
        if metric.distribution.count < 10 {
            return;
        }

        if metric.distribution.std_dev < 0.001 {
            return;
        }

        let detector = self.anomaly_detector.read().await;
        let dist = &metric.distribution;

        // Z-score детекция
        if let Some(zscore) = detector.detect_zscore(value, dist.mean, dist.std_dev) {
            let severity = if zscore > 5.0 {
                AnomalySeverity::Critical
            } else if zscore > 4.0 {
                AnomalySeverity::Warning
            } else {
                AnomalySeverity::Info
            };

            let anomaly = Anomaly {
                metric_name: metric.name.clone(),
                timestamp: Instant::now(),
                value,
                expected_value: dist.mean,
                deviation: zscore,
                threshold: detector.zscore_threshold,
                severity,
                description: format!("Z-score anomaly: {:.2}σ", zscore),
            };

            self.report_anomaly(anomaly);
        }

        // MAD детекция
        let sorted = metric.sorted_values();
        if !sorted.is_empty() {
            let median = sorted[sorted.len() / 2];
            let mad = AnomalyDetector::calculate_mad(&sorted, median);

            if let Some(mad_score) = detector.detect_mad(value, median, mad) {
                let anomaly = Anomaly {
                    metric_name: metric.name.clone(),
                    timestamp: Instant::now(),
                    value,
                    expected_value: median,
                    deviation: mad_score,
                    threshold: detector.mad_threshold,
                    severity: AnomalySeverity::Warning,
                    description: format!("MAD anomaly: {:.2}", mad_score),
                };

                self.report_anomaly(anomaly);
            }
        }

        // Тренд-детекция
        if metric.values.len() >= 10 {
            let recent_trend = metric.derivative();

            if detector.detect_trend_change(&metric.name, recent_trend) {
                let anomaly = Anomaly {
                    metric_name: metric.name.clone(),
                    timestamp: Instant::now(),
                    value: recent_trend,
                    expected_value: recent_trend,
                    deviation: (recent_trend - recent_trend).abs(),
                    threshold: detector.trend_threshold,
                    severity: AnomalySeverity::Warning,
                    description: format!("Trend change: {:.2}{:.2}", recent_trend, recent_trend),
                };

                self.report_anomaly(anomaly);
            }
        }
    }

    fn report_anomaly(&self, anomaly: Anomaly) {
        // ИСПРАВЛЕНО: Более уникальный ключ
        let key = format!("{}:{}:{:.2}",
                          anomaly.metric_name,
                          anomaly.description,
                          anomaly.timestamp.elapsed().as_secs() / 60  // меняется каждую минуту
        );

        // Проверяем, не было ли такой аномалии в последние 5 минут
        if let Some(last_time) = self.recent_anomalies.get(&key) {
            if last_time.elapsed() < self.anomaly_cooldown {
                debug!("⏸️ Anomaly suppressed (cooldown): {}", key);
                return;
            }
        }

        // ИСПРАВЛЕНО: Очищаем старые записи ПЕРЕД вставкой
        self.recent_anomalies.retain(|_, time| {
            time.elapsed() < Duration::from_secs(3600)
        });

        self.recent_anomalies.insert(key, Instant::now());

        // ИСПРАВЛЕНО: Логируем ТОЛЬКО ЗДЕСЬ, убираем лог из integration.rs
        match anomaly.severity {
            AnomalySeverity::Info => debug!("ℹ️ {}: {}", anomaly.metric_name, anomaly.description),
            AnomalySeverity::Warning => warn!("⚠️ {}: {}", anomaly.metric_name, anomaly.description),
            AnomalySeverity::Critical => error!("🚨 {}: {}", anomaly.metric_name, anomaly.description),
        }

        self.total_anomalies_detected.fetch_add(1, Ordering::Relaxed);
    }

    pub fn get_aggregated_metric(&self, name: &str) -> Option<AggregatedMetric> {
        self.metrics_store.get(name).map(|m| m.clone())  // ← ЭТО ДОЛЖНО РАБОТАТЬ!
    }

    pub fn get_all_metrics(&self) -> Vec<AggregatedMetric> {
        self.metrics_store
            .iter()
            .map(|entry| entry.value().clone())
            .collect()
    }

    pub async fn get_system_health(&self) -> f64 {
        let mut health = self.system_health.write().await;
        health.update(&self.metrics_store);
        health.overall_health
    }

    pub async fn get_anomalies(&self, limit: usize) -> Vec<Anomaly> {
        let anomalies = self.anomalies.read().await;
        let start = if anomalies.len() > limit {
            anomalies.len() - limit
        } else {
            0
        };
        anomalies.iter().skip(start).cloned().collect()
    }

    pub fn get_stats(&self) -> MetricsStats {
        MetricsStats {
            total_metrics: self.metrics_store.len() as u64,
            total_recorded: self.total_metrics_recorded.load(Ordering::Relaxed),
            total_anomalies: self.total_anomalies_detected.load(Ordering::Relaxed),
            uptime: self.start_time.elapsed(),
            collection_interval: self.config.collection_interval,
            sampling_rate: self.config.trace_sampling_rate,
        }
    }

    pub fn reset_metrics(&self) {
        self.metrics_store.clear();
        self.total_metrics_recorded.store(0, Ordering::Relaxed);
        self.total_anomalies_detected.store(0, Ordering::Relaxed);

        if let Ok(mut anomalies) = self.anomalies.try_write() {
            anomalies.clear();
        }

        info!("📊 All metrics reset");
    }
}

#[derive(Debug, Clone)]
pub struct MetricsStats {
    pub total_metrics: u64,
    pub total_recorded: u64,
    pub total_anomalies: u64,
    pub uptime: Duration,
    pub collection_interval: Duration,
    pub sampling_rate: f64,
}

impl Clone for MetricsTracingSystem {
    fn clone(&self) -> Self {
        Self {
            metrics_store: self.metrics_store.clone(),
            system_health: self.system_health.clone(),
            anomaly_detector: self.anomaly_detector.clone(),
            config: self.config.clone(),
            total_metrics_recorded: self.total_metrics_recorded.clone(),
            total_anomalies_detected: self.total_anomalies_detected.clone(),
            start_time: self.start_time,
            anomalies: self.anomalies.clone(),
            anomaly_cooldown: self.anomaly_cooldown.clone(),
            recent_anomalies: self.recent_anomalies.clone(),
        }
    }
}