Skip to content

Commit ea0e02e

Browse files
authored
feat(process): add collection time metric, more system stats (#33)
2 parents 4eb1914 + b4dada1 commit ea0e02e

File tree

3 files changed

+107
-50
lines changed

3 files changed

+107
-50
lines changed

README.md

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -158,34 +158,46 @@ collector.collect();
158158
#### Sample Output
159159

160160
```text
161+
# HELP process_collection_duration_seconds The duration of the associated collection routine in seconds.
162+
# TYPE process_collection_duration_seconds gauge
163+
process_collection_duration_seconds 0.008691417
161164
# HELP process_cpu_usage The CPU usage of the process as a percentage.
162165
# TYPE process_cpu_usage gauge
163-
process_cpu_usage 0.7814099788665771
166+
process_cpu_usage 0.014495549723505974
164167
# HELP process_disk_written_bytes_total The total written bytes to disk by the process.
165168
# TYPE process_disk_written_bytes_total gauge
166169
process_disk_written_bytes_total 0
167-
# HELP process_max_cpu_freq The maximum CPU frequency of all cores in MHz.
168-
# TYPE process_max_cpu_freq gauge
169-
process_max_cpu_freq 4464
170170
# HELP process_max_fds The maximum number of open file descriptors of the process.
171171
# TYPE process_max_fds gauge
172172
process_max_fds 10240
173-
# HELP process_min_cpu_freq The minimum CPU frequency of all cores in MHz.
174-
# TYPE process_min_cpu_freq gauge
175-
process_min_cpu_freq 4464
176173
# HELP process_open_fds The number of open file descriptors of the process.
177174
# TYPE process_open_fds gauge
178175
process_open_fds 45
179176
# HELP process_resident_memory_bytes The resident memory of the process in bytes. (RSS)
180177
# TYPE process_resident_memory_bytes gauge
181-
process_resident_memory_bytes 4603904
178+
process_resident_memory_bytes 4915200
182179
# HELP process_resident_memory_usage The resident memory usage of the process as a percentage of the total memory available.
183180
# TYPE process_resident_memory_usage gauge
184-
process_resident_memory_usage 0.00013399124145507813
181+
process_resident_memory_usage 0.0001430511474609375
185182
# HELP process_start_time_seconds The start time of the process in UNIX seconds.
186183
# TYPE process_start_time_seconds gauge
187-
process_start_time_seconds 1762338704
184+
process_start_time_seconds 1762438572
188185
# HELP process_threads The number of OS threads used by the process (Linux only).
189186
# TYPE process_threads gauge
190187
process_threads 1
188+
# HELP system_cpu_cores The number of logical CPU cores available in the system.
189+
# TYPE system_cpu_cores gauge
190+
system_cpu_cores 10
191+
# HELP system_cpu_usage System-wide CPU usage percentage.
192+
# TYPE system_cpu_usage gauge
193+
system_cpu_usage 15.498806953430176
194+
# HELP system_max_cpu_frequency The maximum CPU frequency of all cores in MHz.
195+
# TYPE system_max_cpu_frequency gauge
196+
system_max_cpu_frequency 4464
197+
# HELP system_memory_usage System-wide memory usage percentage.
198+
# TYPE system_memory_usage gauge
199+
system_memory_usage 65.11974334716797
200+
# HELP system_min_cpu_frequency The minimum CPU frequency of all cores in MHz.
201+
# TYPE system_min_cpu_frequency gauge
202+
system_min_cpu_frequency 4464
191203
```

prometric-derive/src/lib.rs

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -162,36 +162,48 @@ mod utils;
162162
///
163163
/// #### Output
164164
/// ```text
165+
/// # HELP process_collection_duration_seconds The duration of the associated collection routine in
166+
/// seconds. # TYPE process_collection_duration_seconds gauge
167+
/// process_collection_duration_seconds 0.008691417
165168
/// # HELP process_cpu_usage The CPU usage of the process as a percentage.
166169
/// # TYPE process_cpu_usage gauge
167-
/// process_cpu_usage 0.7814099788665771
170+
/// process_cpu_usage 0.014495549723505974
168171
/// # HELP process_disk_written_bytes_total The total written bytes to disk by the process.
169172
/// # TYPE process_disk_written_bytes_total gauge
170173
/// process_disk_written_bytes_total 0
171-
/// # HELP process_max_cpu_freq The maximum CPU frequency of all cores in MHz.
172-
/// # TYPE process_max_cpu_freq gauge
173-
/// process_max_cpu_freq 4464
174174
/// # HELP process_max_fds The maximum number of open file descriptors of the process.
175175
/// # TYPE process_max_fds gauge
176176
/// process_max_fds 10240
177-
/// # HELP process_min_cpu_freq The minimum CPU frequency of all cores in MHz.
178-
/// # TYPE process_min_cpu_freq gauge
179-
/// process_min_cpu_freq 4464
180177
/// # HELP process_open_fds The number of open file descriptors of the process.
181178
/// # TYPE process_open_fds gauge
182179
/// process_open_fds 45
183180
/// # HELP process_resident_memory_bytes The resident memory of the process in bytes. (RSS)
184181
/// # TYPE process_resident_memory_bytes gauge
185-
/// process_resident_memory_bytes 4603904
182+
/// process_resident_memory_bytes 4915200
186183
/// # HELP process_resident_memory_usage The resident memory usage of the process as a percentage of
187184
/// the total memory available. # TYPE process_resident_memory_usage gauge
188-
/// process_resident_memory_usage 0.00013399124145507813
185+
/// process_resident_memory_usage 0.0001430511474609375
189186
/// # HELP process_start_time_seconds The start time of the process in UNIX seconds.
190187
/// # TYPE process_start_time_seconds gauge
191-
/// process_start_time_seconds 1762338704
188+
/// process_start_time_seconds 1762438572
192189
/// # HELP process_threads The number of OS threads used by the process (Linux only).
193190
/// # TYPE process_threads gauge
194191
/// process_threads 1
192+
/// # HELP system_cpu_cores The number of logical CPU cores available in the system.
193+
/// # TYPE system_cpu_cores gauge
194+
/// system_cpu_cores 10
195+
/// # HELP system_cpu_usage System-wide CPU usage percentage.
196+
/// # TYPE system_cpu_usage gauge
197+
/// system_cpu_usage 15.498806953430176
198+
/// # HELP system_max_cpu_frequency The maximum CPU frequency of all cores in MHz.
199+
/// # TYPE system_max_cpu_frequency gauge
200+
/// system_max_cpu_frequency 4464
201+
/// # HELP system_memory_usage System-wide memory usage percentage.
202+
/// # TYPE system_memory_usage gauge
203+
/// system_memory_usage 65.11974334716797
204+
/// # HELP system_min_cpu_frequency The minimum CPU frequency of all cores in MHz.
205+
/// # TYPE system_min_cpu_frequency gauge
206+
/// system_min_cpu_frequency 4464
195207
/// ```
196208
#[proc_macro_attribute]
197209
pub fn metrics(attr: TokenStream, item: TokenStream) -> TokenStream {

prometric/src/process.rs

Lines changed: 63 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,10 @@ type UintGauge = GenericGauge<AtomicU64>;
88

99
type 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

138155
impl 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

Comments
 (0)