dmsc 0.1.9

Ri - A high-performance Rust middleware framework with modular architecture
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
//! Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
//!
//! This file is part of Ri.
//! The Ri project belongs to the Dunimd Team.
//!
//! Licensed under the Apache License, Version 2.0 (the "License");
//! You may not use this file except in compliance with the License.
//! You may obtain a copy of the License at
//!
//!     http://www.apache.org/licenses/LICENSE-2.0
//!
//! Unless required by applicable law or agreed to in writing, software
//! distributed under the License is distributed on an "AS IS" BASIS,
//! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//! See the License for the specific language governing permissions and
//! limitations under the License.

#![allow(non_snake_case)]

//! # Metrics Collection and Aggregation Module
//! 
//! This module provides a comprehensive metrics collection and aggregation system for Ri.
//! It supports various metric types, sliding window aggregation, and Prometheus-compatible export.
//! 
//! ## Key Components
//! 
//! - **RiMetricType**: Enum defining supported metric types (Counter, Gauge, Histogram, Summary)
//! - **RiMetricSample**: Represents a single metric sample with timestamp, value, and labels
//! - **RiMetricConfig**: Configuration for creating metrics
//! - **RiSlidingWindow**: Internal sliding time window for metric aggregation
//! - **RiWindowStats**: Aggregated statistics from the sliding window
//! - **RiMetric**: Individual metric with sliding window aggregation
//! - **RiMetricsRegistry**: Registry for managing multiple metrics
//! 
//! ## Design Principles
//! 
//! 1. **Multiple Metric Types**: Supports Counter, Gauge, Histogram, and Summary metrics
//! 2. **Sliding Window Aggregation**: Efficiently aggregates metrics over configurable time windows
//! 3. **Thread Safety**: Uses Arc and RwLock for safe concurrent access
//! 4. **Prometheus Compatible**: Exports metrics in Prometheus format
//! 5. **Label Support**: Allows adding custom labels to metric samples
//! 6. **Configurable**: Supports custom window sizes, bucket sizes, and other parameters
//! 7. **Type Safety**: Strongly typed metrics with compile-time checks
//! 8. **Efficient Memory Usage**: Automatically rotates and prunes old metric data
//! 
//! ## Usage
//! 
//! ```rust
//! use ri::prelude::*;
//! use std::time::Duration;
//! 
//! fn example() -> RiResult<()> {
//!     // Create a metrics registry
//!     let registry = RiMetricsRegistry::new();
//!     
//!     // Configure a counter metric
//!     let counter_config = RiMetricConfig {
//!         metric_type: RiMetricType::Counter,
//!         name: "http_requests_total".to_string(),
//!         help: "Total number of HTTP requests".to_string(),
//!         buckets: Vec::new(),
//!         quantiles: Vec::new(),
//!         max_age: Duration::from_secs(300),
//!         age_buckets: 5,
//!     };
//!     
//!     // Create and register the metric
//!     let counter = Arc::new(RiMetric::new(counter_config));
//!     registry.register(counter.clone())?;
//!     
//!     // Record some metrics
//!     counter.record(1.0, vec![("method".to_string(), "GET".to_string())])?;
//!     counter.record(1.0, vec![("method".to_string(), "POST".to_string())])?;
//!     
//!     // Export metrics in Prometheus format
//!     let prometheus_output = registry.export_prometheus();
//!     println!("{}", prometheus_output);
//!     
//!     Ok(())
//! }
//! ```

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

#[cfg(feature = "pyo3")]
use pyo3::prelude::*;

use crate::core::RiResult;
use crate::core::lock::RwLockExtensions;

/// Metric types supported
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub enum RiMetricType {
    Counter,
    Gauge,
    Histogram,
    Summary,
}

/// A single metric sample
#[derive(Debug, Clone, Serialize, Deserialize)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub struct RiMetricSample {
    pub timestamp: u64, // seconds since epoch
    pub value: f64,
    pub labels: Vec<(String, String)>,
}

/// Metric configuration
#[derive(Debug, Clone)]
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub struct RiMetricConfig {
    pub metric_type: RiMetricType,
    pub name: String,
    pub help: String,
    pub buckets: Vec<f64>, // for histogram
    pub quantiles: Vec<f64>, // for summary
    pub max_age: Duration, // for summary
    pub age_buckets: usize, // for summary
}

/// Sliding time window for metric aggregation
#[allow(dead_code)]
struct RiSlidingWindow {
    #[allow(dead_code)]
    window_size: Duration,
    #[allow(dead_code)]
    bucket_size: Duration,
    buckets: VecDeque<Vec<RiMetricSample>>,
    current_bucket: Vec<RiMetricSample>,
    #[allow(dead_code)]
    last_rotation: u64,
}

impl RiSlidingWindow {
    fn new(window_size: Duration, bucket_size: Duration) -> Self {
        let bucket_count = window_size.as_secs().div_ceil(bucket_size.as_secs());
        
        Self {
            window_size,
            bucket_size,
            buckets: VecDeque::with_capacity(bucket_count as usize),
            current_bucket: Vec::new(),
            last_rotation: Self::current_timestamp(),
        }
    }
    
    #[allow(dead_code)]
    fn current_timestamp() -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or(Duration::from_secs(0))
            .as_secs()
    }
    
    #[allow(dead_code)]
    fn rotate_if_needed(&mut self) {
        let now = Self::current_timestamp();
        let elapsed = now.saturating_sub(self.last_rotation);
        
        if elapsed >= self.bucket_size.as_secs() {
            let rotations = elapsed / self.bucket_size.as_secs();
            
            for _ in 0..rotations {
                self.buckets.push_back(std::mem::take(&mut self.current_bucket));
                
                // Remove old buckets outside window
                let max_buckets = self.window_size.as_secs().div_ceil(self.bucket_size.as_secs());
                while self.buckets.len() > max_buckets as usize {
                    self.buckets.pop_front();
                }
            }
            
            self.last_rotation = now;
        }
    }
    
    #[allow(dead_code)]
    fn add_sample(&mut self, sample: RiMetricSample) {
        self.rotate_if_needed();
        self.current_bucket.push(sample);
    }
    
    #[allow(dead_code)]
    fn get_samples(&self) -> Vec<RiMetricSample> {
        let mut all_samples = Vec::with_capacity(8);
        
        for bucket in &self.buckets {
            all_samples.extend(bucket.iter().cloned());
        }
        all_samples.extend(self.current_bucket.iter().cloned());
        
        all_samples
    }
    
    #[allow(dead_code)]
    fn get_window_stats(&self) -> RiWindowStats {
        let samples = self.get_samples();
        
        if samples.is_empty() {
            return RiWindowStats::default();
        }
        
        let mut sorted_values: Vec<f64> = samples.iter().map(|s| s.value).collect();
        sorted_values.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
        
        let count = sorted_values.len();
        let sum: f64 = sorted_values.iter().sum();
        let min = sorted_values[0];
        let max = sorted_values[count - 1];
        let mean = sum / count as f64;
        
        // Calculate variance and standard deviation
        let variance: f64 = sorted_values
            .iter()
            .map(|x| (x - mean).powi(2))
            .sum::<f64>() / count as f64;
        let stddev = variance.sqrt();
        
        // Calculate quantiles
        let p50 = Self::quantile(&sorted_values, 0.50);
        let p90 = Self::quantile(&sorted_values, 0.90);
        let p95 = Self::quantile(&sorted_values, 0.95);
        let p99 = Self::quantile(&sorted_values, 0.99);
        
        RiWindowStats {
            count: count as u64,
            sum,
            min,
            max,
            mean,
            stddev,
            p50,
            p90,
            p95,
            p99,
        }
    }
    
    #[allow(dead_code)]
    fn quantile(sorted_values: &[f64], q: f64) -> f64 {
        if sorted_values.is_empty() {
            return 0.0;
        }
        
        let index = q * (sorted_values.len() - 1) as f64;
        let lower = index.floor() as usize;
        let upper = index.ceil() as usize;
        
        if lower == upper {
            sorted_values[lower]
        } else {
            let weight = index - lower as f64;
            sorted_values[lower] * (1.0 - weight) + sorted_values[upper] * weight
        }
    }
}

/// Window statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RiWindowStats {
    pub count: u64,
    pub sum: f64,
    pub min: f64,
    pub max: f64,
    pub mean: f64,
    pub stddev: f64,
    pub p50: f64,
    pub p90: f64,
    pub p95: f64,
    pub p99: f64,
}

impl Default for RiWindowStats {
    fn default() -> Self {
        Self {
            count: 0,
            sum: 0.0,
            min: 0.0,
            max: 0.0,
            mean: 0.0,
            stddev: 0.0,
            p50: 0.0,
            p90: 0.0,
            p95: 0.0,
            p99: 0.0,
        }
    }
}

/// A single metric with sliding window aggregation
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub struct RiMetric {
    config: RiMetricConfig,
    sliding_window: RwLock<RiSlidingWindow>,
    total_count: RwLock<u64>,
    #[allow(dead_code)]
    total_sum: RwLock<f64>,
}

impl RiMetric {
    pub fn new(config: RiMetricConfig) -> Self {
        let sliding_window = RiSlidingWindow::new(
            Duration::from_secs(300), // 5 minute window
            Duration::from_secs(10),  // 10 second buckets
        );
        
        Self {
            config,
            sliding_window: RwLock::new(sliding_window),
            total_count: RwLock::new(0),
            total_sum: RwLock::new(0.0),
        }
    }
    
    #[allow(dead_code)]
    fn record(&self, value: f64, labels: Vec<(String, String)>) -> RiResult<()> {
        let sample = RiMetricSample {
            timestamp: Self::current_timestamp(),
            value,
            labels,
        };
        
        {
            let mut window = self.sliding_window.write_safe("sliding window")?;
            window.add_sample(sample);
        }
        
        {
            let mut count = self.total_count.write_safe("total count")?;
            *count += 1;
        }
        
        {
            let mut sum = self.total_sum.write_safe("total sum")?;
            *sum += value;
        }
        
        Ok(())
    }
    
    #[allow(dead_code)]
    fn get_stats(&self) -> RiWindowStats {
        match self.sliding_window.read_safe("sliding window stats") {
            Ok(window) => window.get_window_stats(),
            Err(_) => RiWindowStats::default(),
        }
    }
    
    #[allow(dead_code)]
    fn get_total_count(&self) -> u64 {
        match self.total_count.read_safe("total count") {
            Ok(count) => *count,
            Err(_) => 0,
        }
    }
    
    #[allow(dead_code)]
    fn get_total_sum(&self) -> f64 {
        match self.total_sum.read_safe("total sum") {
            Ok(sum) => *sum,
            Err(_) => 0.0,
        }
    }
    
    fn get_config(&self) -> &RiMetricConfig {
        &self.config
    }

    pub fn get_value(&self) -> f64 {
        match self.total_count.read_safe("total count value") {
            Ok(count) => *count as f64,
            Err(_) => 0.0,
        }
    }

    #[allow(dead_code)]
    fn current_timestamp() -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs()
    }
}

/// Metrics registry to manage multiple metrics
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
#[derive(Clone)]
pub struct RiMetricsRegistry {
    metrics: Arc<RwLock<HashMap<String, Arc<RiMetric>>>>,
}

impl RiMetricsRegistry {
    /// Maximum number of metrics that can be registered
    const MAX_METRICS: usize = 10000;
    
    /// Maximum metric name length
    const MAX_NAME_LENGTH: usize = 256;
    
    pub fn new() -> Self {
        Self {
            metrics: Arc::new(RwLock::new(HashMap::new())),
        }
    }
    
    /// Validates a metric name to prevent injection attacks.
    ///
    /// # Security
    ///
    /// Metric names must:
    /// - Be 1-256 characters long
    /// - Contain only alphanumeric characters, underscores, and colons
    /// - Start with a letter
    fn validate_metric_name(name: &str) -> RiResult<()> {
        if name.is_empty() || name.len() > Self::MAX_NAME_LENGTH {
            return Err(crate::core::RiError::Other(format!(
                "Metric name must be 1-{} characters",
                Self::MAX_NAME_LENGTH
            )));
        }
        
        let chars: Vec<char> = name.chars().collect();
        
        // First character must be a letter
        if !chars[0].is_ascii_alphabetic() {
            return Err(crate::core::RiError::Other(
                "Metric name must start with a letter".to_string()
            ));
        }
        
        for c in &chars {
            if !c.is_ascii_alphanumeric() && *c != '_' && *c != ':' {
                return Err(crate::core::RiError::Other(
                    "Metric name can only contain alphanumeric characters, underscores, and colons".to_string()
                ));
            }
        }
        
        Ok(())
    }
    
    pub fn register(&self, metric: Arc<RiMetric>) -> RiResult<()> {
        let name = metric.get_config().name.clone();
        
        // Security: Validate metric name
        Self::validate_metric_name(&name)?;
        
        let mut metrics = self.metrics.write_safe("metrics registry")?;
        
        // Security: Check maximum metrics limit
        if metrics.len() >= Self::MAX_METRICS && !metrics.contains_key(&name) {
            return Err(crate::core::RiError::Other(format!(
                "Maximum metrics limit reached: {} metrics",
                Self::MAX_METRICS
            )));
        }
        
        metrics.insert(name, metric);
        Ok(())
    }
    
    pub fn get_metric(&self, name: &str) -> Option<Arc<RiMetric>> {
        match self.metrics.read_safe("metrics registry") {
            Ok(metrics) => metrics.get(name).cloned(),
            Err(_) => None,
        }
    }
    
    pub fn get_all_metrics(&self) -> HashMap<String, Arc<RiMetric>> {
        match self.metrics.read_safe("metrics registry") {
            Ok(metrics) => metrics.clone(),
            Err(_) => HashMap::new(),
        }
    }
    
    /// Export metrics in Prometheus format
    #[cfg(feature = "observability")]
    pub fn export_prometheus(&self) -> String {
        let mut output = String::new();
        let metrics = match self.metrics.read_safe("metrics registry for export") {
            Ok(m) => m,
            Err(_) => return "# Error: Failed to acquire metrics registry lock".to_string(),
        };
        
        for (name, metric) in metrics.iter() {
            let config = metric.get_config();
            
            // Write help and type
            output.push_str(&format!("# HELP {} {}\n", name, config.help));
            output.push_str(&format!("# TYPE {} {:?}\n", name, config.metric_type));
            
            // Write metric value
            let stats = metric.get_stats();
            match config.metric_type {
                RiMetricType::Counter => {
                    output.push_str(&format!("{} {}\n", name, metric.get_total_count()));
                }
                RiMetricType::Gauge => {
                    output.push_str(&format!("{} {}\n", name, stats.mean));
                }
                RiMetricType::Histogram => {
                    output.push_str(&format!("{}_count {}\n", name, stats.count));
                    output.push_str(&format!("{}_sum {}\n", name, stats.sum));
                    output.push_str(&format!("{}_min {}\n", name, stats.min));
                    output.push_str(&format!("{}_max {}\n", name, stats.max));
                    output.push_str(&format!("{}_avg {}\n", name, stats.mean));
                    output.push_str(&format!("{}_p50 {}\n", name, stats.p50));
                    output.push_str(&format!("{}_p90 {}\n", name, stats.p90));
                    output.push_str(&format!("{}_p95 {}\n", name, stats.p95));
                    output.push_str(&format!("{}_p99 {}\n", name, stats.p99));
                }
                RiMetricType::Summary => {
                    output.push_str(&format!("{} {}\n", name, stats.mean));
                }
            }
            
            output.push('\n');
        }
        
        output
    }
}

#[cfg(feature = "pyo3")]
/// Python methods for RiMetricsRegistry
#[pyo3::prelude::pymethods]
impl RiMetricsRegistry {
    /// Create a new metrics registry from Python
    #[new]
    fn py_new() -> Self {
        Self::new()
    }
    
    /// Register a metric from Python
    #[pyo3(name = "register")]
    fn register_py(&self, metric: &RiMetric) -> PyResult<()> {
        let name = metric.config.name.clone();
        let mut metrics = self.metrics.write_safe("metrics registry")
            .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(e.to_string()))?;
        metrics.insert(name, Arc::new(RiMetric::new(metric.config.clone())));
        Ok(())
    }
    
    /// Get a metric's current value by name from Python
    #[pyo3(name = "get_metric_value")]
    fn get_metric_value_impl(&self, name: &str) -> Option<f64> {
        self.get_metric(name).map(|m| m.get_value())
    }

    /// Get all metric names from Python
    #[pyo3(name = "get_all_metric_names")]
    fn get_all_metric_names_impl(&self) -> Vec<String> {
        let metrics = match self.metrics.read_safe("metrics registry for names") {
            Ok(m) => m,
            Err(_) => return Vec::new(),
        };
        metrics.keys().cloned().collect()
    }
    
    /// Export metrics in Prometheus format from Python
    #[pyo3(name = "export_prometheus")]
    fn export_prometheus_impl(&self) -> String {
        #[cfg(feature = "observability")]
        {
            self.export_prometheus()
        }
        #[cfg(not(feature = "observability"))]
        {
            "# Observability feature not enabled".to_string()
        }
    }
    
    /// Get metric count from Python
    #[pyo3(name = "get_metric_count")]
    fn get_metric_count_impl(&self) -> usize {
        let metrics = match self.metrics.read_safe("metrics registry for count") {
            Ok(m) => m,
            Err(_) => return 0,
        };
        metrics.len()
    }
}

#[cfg(feature = "pyo3")]
#[pyo3::prelude::pymethods]
impl RiMetricConfig {
    #[new]
    #[pyo3(signature = (name, metric_type, help="", buckets=None, quantiles=None))]
    fn py_new(
        name: String,
        metric_type: RiMetricType,
        help: &str,
        buckets: Option<Vec<f64>>,
        quantiles: Option<Vec<f64>>,
    ) -> Self {
        Self {
            name,
            metric_type,
            help: help.to_string(),
            buckets: buckets.unwrap_or_else(|| vec![0.005, 0.01, 0.025, 0.05, 0.1, 0.25, 0.5, 1.0, 2.5, 5.0, 10.0]),
            quantiles: quantiles.unwrap_or_else(|| vec![0.5, 0.9, 0.95, 0.99]),
            max_age: Duration::from_secs(600),
            age_buckets: 5,
        }
    }
}

#[cfg(feature = "pyo3")]
#[pyo3::prelude::pymethods]
impl RiMetric {
    #[new]
    fn py_new(config: RiMetricConfig) -> Self {
        Self::new(config)
    }
    
    #[pyo3(name = "record")]
    fn record_py(&self, value: f64) -> PyResult<()> {
        self.record(value, vec![])
            .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(e.to_string()))
    }
    
    #[pyo3(name = "get_value")]
    fn get_value_py(&self) -> f64 {
        self.get_value()
    }
    
    #[pyo3(name = "get_total_count")]
    fn get_total_count_py(&self) -> u64 {
        self.get_total_count()
    }
}