rusty_bin/monitor/
metrics.rs1use crate::monitor::utils::time;
6use rusty_common::collections::FxHashMap;
7use serde::{Deserialize, Serialize};
8use sysinfo::{Disks, System};
9
10#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct ApplicationMetrics {
13 pub timestamp: u64,
15 pub trades_processed: u64,
17 pub orderbooks_processed: u64,
19 pub files_written: u64,
21 pub bytes_written: u64,
23 pub compression_ratio: f64,
25 pub active_connections: u32,
27 pub error_count: u64,
29 pub latency_p50: f64,
31 pub latency_p95: f64,
33 pub latency_p99: f64,
35}
36
37#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct SystemMetrics {
40 pub timestamp: u64,
42 pub cpu_usage: f32,
44 pub memory_usage: f64,
46 pub memory_total: u64,
48 pub memory_used: u64,
50 pub disk_usage: FxHashMap<String, DiskMetrics>,
52 pub network_stats: NetworkMetrics,
54 pub uptime: u64,
56}
57
58#[derive(Debug, Clone, Serialize, Deserialize)]
60pub struct DiskMetrics {
61 pub total_space: u64,
63 pub available_space: u64,
65 pub used_space: u64,
67 pub usage_percent: f64,
69}
70
71#[derive(Debug, Clone, Serialize, Deserialize)]
73pub struct NetworkMetrics {
74 pub bytes_received: u64,
76 pub bytes_transmitted: u64,
78 pub packets_received: u64,
80 pub packets_transmitted: u64,
82}
83
84#[derive(Debug, Clone, Default)]
86pub struct MetricsCollector;
87
88impl MetricsCollector {
89 #[must_use]
91 pub const fn new() -> Self {
92 Self
93 }
94
95 #[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 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 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 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 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}