rusty_bin/monitor/
metrics.rs

1//! Metrics collection implementation
2//!
3//! This module provides the actual implementation of metrics collection.
4
5use crate::monitor::utils::time;
6use rusty_common::collections::FxHashMap;
7use serde::{Deserialize, Serialize};
8use sysinfo::{Disks, System};
9
10/// Application metrics structure
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct ApplicationMetrics {
13    /// Timestamp in nanoseconds
14    pub timestamp: u64,
15    /// Number of trades processed
16    pub trades_processed: u64,
17    /// Number of order books processed
18    pub orderbooks_processed: u64,
19    /// Number of files written
20    pub files_written: u64,
21    /// Total bytes written
22    pub bytes_written: u64,
23    /// Compression ratio
24    pub compression_ratio: f64,
25    /// Number of active connections
26    pub active_connections: u32,
27    /// Number of errors
28    pub error_count: u64,
29    /// 50th percentile latency
30    pub latency_p50: f64,
31    /// 95th percentile latency
32    pub latency_p95: f64,
33    /// 99th percentile latency
34    pub latency_p99: f64,
35}
36
37/// System metrics structure
38#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct SystemMetrics {
40    /// Timestamp in nanoseconds
41    pub timestamp: u64,
42    /// CPU usage percentage
43    pub cpu_usage: f32,
44    /// Memory usage percentage
45    pub memory_usage: f64,
46    /// Total memory in bytes
47    pub memory_total: u64,
48    /// Used memory in bytes
49    pub memory_used: u64,
50    /// Disk usage by mount point
51    pub disk_usage: FxHashMap<String, DiskMetrics>,
52    /// Network statistics
53    pub network_stats: NetworkMetrics,
54    /// System uptime in seconds
55    pub uptime: u64,
56}
57
58/// Disk metrics structure
59#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct DiskMetrics {
61    /// Total disk space in bytes
62    pub total_space: u64,
63    /// Available disk space in bytes
64    pub available_space: u64,
65    /// Used disk space in bytes
66    pub used_space: u64,
67    /// Usage percentage
68    pub usage_percent: f64,
69}
70
71/// Network metrics structure
72#[derive(Debug, Clone, Serialize, Deserialize)]
73pub struct NetworkMetrics {
74    /// Total bytes received
75    pub bytes_received: u64,
76    /// Total bytes transmitted
77    pub bytes_transmitted: u64,
78    /// Total packets received
79    pub packets_received: u64,
80    /// Total packets transmitted
81    pub packets_transmitted: u64,
82}
83
84/// Metrics collector implementation
85#[derive(Debug, Clone, Default)]
86pub struct MetricsCollector;
87
88impl MetricsCollector {
89    /// Create a new metrics collector
90    #[must_use]
91    pub const fn new() -> Self {
92        Self
93    }
94
95    /// Collect system metrics
96    #[must_use]
97    pub fn collect_system_metrics(&self) -> SystemMetrics {
98        let mut system = System::new_all();
99        system.refresh_all();
100
101        let timestamp = time::now_nanos();
102
103        // Get CPU usage (average across all cores)
104        let cpu_usage = if !system.cpus().is_empty() {
105            system.cpus().iter().map(|cpu| cpu.cpu_usage()).sum::<f32>()
106                / system.cpus().len() as f32
107        } else {
108            0.0
109        };
110
111        // Get memory info
112        let memory_total = system.total_memory();
113        let memory_used = system.used_memory();
114        let memory_usage = (memory_used as f64 / memory_total as f64) * 100.0;
115
116        // Get disk usage
117        let mut disk_usage = FxHashMap::default();
118        let disks = Disks::new_with_refreshed_list();
119        for disk in &disks {
120            let mount_point = disk.mount_point().to_string_lossy().to_string();
121            let total = disk.total_space();
122            let available = disk.available_space();
123            let used = total - available;
124            let usage_percent = (used as f64 / total as f64) * 100.0;
125
126            disk_usage.insert(
127                mount_point,
128                DiskMetrics {
129                    total_space: total,
130                    available_space: available,
131                    used_space: used,
132                    usage_percent,
133                },
134            );
135        }
136
137        // Get network stats (simplified for now)
138        let network_stats = NetworkMetrics {
139            bytes_received: 0,
140            bytes_transmitted: 0,
141            packets_received: 0,
142            packets_transmitted: 0,
143        };
144
145        SystemMetrics {
146            timestamp,
147            cpu_usage,
148            memory_usage,
149            memory_total,
150            memory_used,
151            disk_usage,
152            network_stats,
153            uptime: System::uptime(),
154        }
155    }
156}