@@ -8,21 +8,10 @@ type UintGauge = GenericGauge<AtomicU64>;
88
99type UintCounter = GenericGauge < AtomicU64 > ;
1010
11- /// A collector for process metrics.
11+ /// A collector for process (and some system) metrics.
1212///
1313/// # Metrics
14- /// - `process_threads`: The number of OS threads used by the process (Linux only).
15- /// - `process_cpu_cores`: The number of logical CPU cores available in the system.
16- /// - `process_cpu_usage`: The CPU usage of the process as a percentage.
17- /// - `process_max_cpu_freq`: The maximum CPU frequency of all cores in MHz.
18- /// - `process_min_cpu_freq`: The minimum CPU frequency of all cores in MHz.
19- /// - `process_resident_memory_bytes`: The resident memory of the process in bytes.
20- /// - `process_resident_memory_usage`: The resident memory usage of the process as a percentage of
21- /// the total memory available.
22- /// - `process_start_time_seconds`: The start time of the process in UNIX seconds.
23- /// - `process_open_fds`: The number of open file descriptors of the process.
24- /// - `process_max_fds`: The maximum number of open file descriptors of the process.
25- /// - `process_disk_written_bytes_total`: The total written bytes to disk by the process.
14+ /// See the documentation for the [`ProcessMetrics`] struct for the list of metrics.
2615///
2716/// # Example
2817/// ```rust
@@ -86,13 +75,17 @@ impl ProcessCollector {
8675 self . pid . as_u32 ( )
8776 }
8877
89- /// Collect the metrics for the process.
78+ /// Collect system and process metrics .
9079 pub fn collect ( & mut self ) {
80+ let start = std:: time:: Instant :: now ( ) ;
9181 self . sys . refresh_specifics ( self . specifics ) ;
9282
9383 let cpus = self . sys . cpus ( ) ;
9484 let min_cpu_freq = cpus. iter ( ) . map ( |cpu| cpu. frequency ( ) ) . min ( ) . unwrap ( ) ;
9585 let max_cpu_freq = cpus. iter ( ) . map ( |cpu| cpu. frequency ( ) ) . max ( ) . unwrap ( ) ;
86+ let system_cpu_usage = self . sys . global_cpu_usage ( ) ;
87+ let system_memory_usage =
88+ self . sys . used_memory ( ) as f64 / self . sys . total_memory ( ) as f64 * 100.0 ;
9689
9790 let process = self . sys . process ( self . pid ) . unwrap ( ) ;
9891 let threads = process. tasks ( ) . map ( |tasks| tasks. len ( ) ) . unwrap_or ( 0 ) ;
@@ -103,9 +96,11 @@ impl ProcessCollector {
10396 let resident_memory_usage = resident_memory as f64 / self . sys . total_memory ( ) as f64 ;
10497 let disk_usage = process. disk_usage ( ) . total_written_bytes ;
10598
106- self . metrics . cores . set ( self . cores ) ;
107- self . metrics . max_cpu_freq . set ( max_cpu_freq) ;
108- self . metrics . min_cpu_freq . set ( min_cpu_freq) ;
99+ self . metrics . system_cores . set ( self . cores ) ;
100+ self . metrics . system_max_cpu_freq . set ( max_cpu_freq) ;
101+ self . metrics . system_min_cpu_freq . set ( min_cpu_freq) ;
102+ self . metrics . system_cpu_usage . set ( system_cpu_usage as f64 ) ;
103+ self . metrics . system_memory_usage . set ( system_memory_usage) ;
109104
110105 self . metrics . threads . set ( threads as u64 ) ;
111106 self . metrics . cpu_usage . set ( cpu_usage as f64 ) ;
@@ -115,43 +110,69 @@ impl ProcessCollector {
115110 self . metrics . open_fds . set ( open_fds as u64 ) ;
116111 self . metrics . max_fds . set ( max_fds as u64 ) ;
117112 self . metrics . disk_written_bytes . set ( disk_usage) ;
113+
114+ // Record the duration of the collection routine
115+ self . metrics . collection_duration . set ( start. elapsed ( ) . as_secs_f64 ( ) ) ;
118116 }
119117}
120118
121- struct ProcessMetrics {
119+ /// A collection of metrics for a process, with some useful system metrics.
120+ pub struct ProcessMetrics {
122121 // System metrics
123- cores : UintGauge ,
124- max_cpu_freq : UintGauge ,
125- min_cpu_freq : UintGauge ,
122+ /// The number of logical CPU cores available in the system.
123+ system_cores : UintGauge ,
124+ /// The maximum CPU frequency of all cores in MHz.
125+ system_max_cpu_freq : UintGauge ,
126+ /// The minimum CPU frequency of all cores in MHz.
127+ system_min_cpu_freq : UintGauge ,
128+ /// The system-wide CPU usage percentage.
129+ system_cpu_usage : Gauge ,
130+ /// The system-wide memory usage percentage.
131+ system_memory_usage : Gauge ,
126132
127133 // Process metrics
134+ /// The number of OS threads used by the process (Linux only).
128135 threads : UintGauge ,
136+ /// The CPU usage of the process as a percentage.
129137 cpu_usage : Gauge ,
138+ /// The resident memory of the process in bytes. (RSS)
130139 resident_memory : UintGauge ,
140+ /// The resident memory usage of the process as a percentage of the total memory available.
131141 resident_memory_usage : Gauge ,
142+ /// The start time of the process in UNIX seconds.
132143 start_time : UintGauge ,
144+ /// The number of open file descriptors of the process.
133145 open_fds : UintGauge ,
146+ /// The maximum number of open file descriptors of the process.
134147 max_fds : UintGauge ,
148+ /// The total written bytes to disk by the process.
135149 disk_written_bytes : UintCounter ,
150+
151+ /// The duration of the associated collection routine in seconds.
152+ collection_duration : Gauge ,
136153}
137154
138155impl ProcessMetrics {
139156 pub fn new ( registry : & prometheus:: Registry ) -> Self {
140- let cores = UintGauge :: new (
157+ let system_cores = UintGauge :: new (
141158 "system_cpu_cores" ,
142159 "The number of logical CPU cores available in the system." ,
143160 )
144161 . unwrap ( ) ;
145- let max_cpu_freq = UintGauge :: new (
162+ let system_max_cpu_freq = UintGauge :: new (
146163 "system_max_cpu_frequency" ,
147164 "The maximum CPU frequency of all cores in MHz." ,
148165 )
149166 . unwrap ( ) ;
150- let min_cpu_freq = UintGauge :: new (
167+ let system_min_cpu_freq = UintGauge :: new (
151168 "system_min_cpu_frequency" ,
152169 "The minimum CPU frequency of all cores in MHz." ,
153170 )
154171 . unwrap ( ) ;
172+ let system_cpu_usage =
173+ Gauge :: new ( "system_cpu_usage" , "System-wide CPU usage percentage." ) . unwrap ( ) ;
174+ let system_memory_usage =
175+ Gauge :: new ( "system_memory_usage" , "System-wide memory usage percentage." ) . unwrap ( ) ;
155176
156177 let threads = UintGauge :: new (
157178 "process_threads" ,
@@ -192,10 +213,18 @@ impl ProcessMetrics {
192213 )
193214 . unwrap ( ) ;
194215
216+ let collection_duration = Gauge :: new (
217+ "process_collection_duration_seconds" ,
218+ "The duration of the associated collection routine in seconds." ,
219+ )
220+ . unwrap ( ) ;
221+
195222 // Register all metrics with the registry
196- registry. register ( Box :: new ( cores. clone ( ) ) ) . unwrap ( ) ;
197- registry. register ( Box :: new ( max_cpu_freq. clone ( ) ) ) . unwrap ( ) ;
198- registry. register ( Box :: new ( min_cpu_freq. clone ( ) ) ) . unwrap ( ) ;
223+ registry. register ( Box :: new ( system_cores. clone ( ) ) ) . unwrap ( ) ;
224+ registry. register ( Box :: new ( system_max_cpu_freq. clone ( ) ) ) . unwrap ( ) ;
225+ registry. register ( Box :: new ( system_min_cpu_freq. clone ( ) ) ) . unwrap ( ) ;
226+ registry. register ( Box :: new ( system_cpu_usage. clone ( ) ) ) . unwrap ( ) ;
227+ registry. register ( Box :: new ( system_memory_usage. clone ( ) ) ) . unwrap ( ) ;
199228
200229 registry. register ( Box :: new ( threads. clone ( ) ) ) . unwrap ( ) ;
201230 registry. register ( Box :: new ( cpu_usage. clone ( ) ) ) . unwrap ( ) ;
@@ -205,11 +234,14 @@ impl ProcessMetrics {
205234 registry. register ( Box :: new ( open_fds. clone ( ) ) ) . unwrap ( ) ;
206235 registry. register ( Box :: new ( max_fds. clone ( ) ) ) . unwrap ( ) ;
207236 registry. register ( Box :: new ( disk_written_bytes. clone ( ) ) ) . unwrap ( ) ;
237+ registry. register ( Box :: new ( collection_duration. clone ( ) ) ) . unwrap ( ) ;
208238
209239 Self {
210- cores,
211- max_cpu_freq,
212- min_cpu_freq,
240+ system_cores,
241+ system_max_cpu_freq,
242+ system_min_cpu_freq,
243+ system_cpu_usage,
244+ system_memory_usage,
213245 threads,
214246 cpu_usage,
215247 resident_memory,
@@ -218,6 +250,7 @@ impl ProcessMetrics {
218250 open_fds,
219251 max_fds,
220252 disk_written_bytes,
253+ collection_duration,
221254 }
222255 }
223256}
0 commit comments