pd/metrics/cpu_worker.rs
1//! A metrics-focused worker for gathering CPU load and other system stats.
2//!
3//! ### Overview
4//!
5//! This submodule provides a worker that wraps the [metrics-process] logic to export OS-level
6//! runtime information about `pd`.
7
8use std::time::Duration;
9use tokio::time::sleep;
10
11use metrics_process::Collector;
12
13/// The time to sleep between polling the OS for process info about `pd`.
14const SLEEP_DURATION: Duration = Duration::from_secs(2);
15/// The string prepended to all metrics emitted by [metrics-process].
16const METRICS_PREFIX: &str = "pd_";
17
18pub fn register_metrics() {
19 // Call `describe()` method to register help string.
20 let collector = Collector::new(METRICS_PREFIX);
21 collector.describe();
22}
23
24/// Run the cpu worker.
25///
26/// This function will never return.
27pub async fn run() -> std::convert::Infallible {
28 let collector = Collector::new(METRICS_PREFIX);
29 loop {
30 // Periodically call `collect()` method to update information.
31 collector.collect();
32 sleep(SLEEP_DURATION).await;
33 }
34}