|
| 1 | +use std::{ |
| 2 | + fmt::Write as _, |
| 3 | + net::SocketAddr, |
| 4 | + sync::Arc, |
| 5 | + thread, |
| 6 | +}; |
| 7 | + |
| 8 | +use anyhow::Context as _; |
| 9 | +use tiny_http::{ |
| 10 | + Header, |
| 11 | + Method, |
| 12 | + Response, |
| 13 | + Server, |
| 14 | + StatusCode, |
| 15 | +}; |
| 16 | +use tokio::sync::RwLock; |
| 17 | + |
| 18 | +use crate::{ |
| 19 | + config, |
| 20 | + profile, |
| 21 | + system::DaemonState, |
| 22 | +}; |
| 23 | + |
| 24 | +pub fn start( |
| 25 | + config: &config::MetricsConfig, |
| 26 | + state: Arc<RwLock<DaemonState>>, |
| 27 | +) -> anyhow::Result<()> { |
| 28 | + let address = SocketAddr::new(config.listen_addr, config.port); |
| 29 | + let server = Server::http(address) |
| 30 | + .map_err(|err| anyhow::anyhow!(err)) |
| 31 | + .with_context(|| { |
| 32 | + format!("failed to bind metrics HTTP server to {address}") |
| 33 | + })?; |
| 34 | + |
| 35 | + thread::Builder::new() |
| 36 | + .name("watt-metrics".to_owned()) |
| 37 | + .spawn(move || serve(server, state)) |
| 38 | + .context("failed to spawn metrics server thread")?; |
| 39 | + |
| 40 | + log::info!("serving Prometheus metrics at http://{address}/metrics",); |
| 41 | + |
| 42 | + Ok(()) |
| 43 | +} |
| 44 | + |
| 45 | +fn serve(server: Server, state: Arc<RwLock<DaemonState>>) { |
| 46 | + for request in server.incoming_requests() { |
| 47 | + let response = |
| 48 | + if request.method() == &Method::Get && request.url() == "/metrics" { |
| 49 | + let metrics = render_metrics(&state); |
| 50 | + let content_type = Header::from_bytes( |
| 51 | + b"Content-Type", |
| 52 | + b"text/plain; version=0.0.4; charset=utf-8", |
| 53 | + ) |
| 54 | + .expect("static metrics content type header is valid"); |
| 55 | + |
| 56 | + Response::from_string(metrics).with_header(content_type) |
| 57 | + } else { |
| 58 | + Response::from_string("not found\n").with_status_code(StatusCode(404)) |
| 59 | + }; |
| 60 | + |
| 61 | + if let Err(error) = request.respond(response) { |
| 62 | + log::warn!("failed to respond to metrics request: {error}"); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + log::error!("metrics HTTP server loop exited unexpectedly"); |
| 67 | +} |
| 68 | + |
| 69 | +fn render_metrics(state: &RwLock<DaemonState>) -> String { |
| 70 | + let state = state.blocking_read(); |
| 71 | + let mut metrics = String::new(); |
| 72 | + |
| 73 | + metric_help( |
| 74 | + &mut metrics, |
| 75 | + "watt_rule_count", |
| 76 | + "Configured daemon rule count.", |
| 77 | + ); |
| 78 | + metric_type(&mut metrics, "watt_rule_count", "gauge"); |
| 79 | + metric(&mut metrics, "watt_rule_count", state.rule_count() as f64); |
| 80 | + |
| 81 | + metric_help(&mut metrics, "watt_cpu_count", "Detected CPU count."); |
| 82 | + metric_type(&mut metrics, "watt_cpu_count", "gauge"); |
| 83 | + metric(&mut metrics, "watt_cpu_count", state.cpu_count() as f64); |
| 84 | + |
| 85 | + if let Some(cpu_log) = state.latest_cpu_log() { |
| 86 | + metric_help( |
| 87 | + &mut metrics, |
| 88 | + "watt_cpu_usage_ratio", |
| 89 | + "CPU usage ratio from 0 to 1.", |
| 90 | + ); |
| 91 | + metric_type(&mut metrics, "watt_cpu_usage_ratio", "gauge"); |
| 92 | + metric(&mut metrics, "watt_cpu_usage_ratio", cpu_log.usage); |
| 93 | + |
| 94 | + metric_help( |
| 95 | + &mut metrics, |
| 96 | + "watt_cpu_load_average_1m", |
| 97 | + "One minute CPU load average.", |
| 98 | + ); |
| 99 | + metric_type(&mut metrics, "watt_cpu_load_average_1m", "gauge"); |
| 100 | + metric( |
| 101 | + &mut metrics, |
| 102 | + "watt_cpu_load_average_1m", |
| 103 | + cpu_log.load_average, |
| 104 | + ); |
| 105 | + |
| 106 | + if let Some(temperature) = cpu_log.temperature { |
| 107 | + metric_help( |
| 108 | + &mut metrics, |
| 109 | + "watt_cpu_temperature_celsius", |
| 110 | + "CPU temperature in degrees Celsius.", |
| 111 | + ); |
| 112 | + metric_type(&mut metrics, "watt_cpu_temperature_celsius", "gauge"); |
| 113 | + metric(&mut metrics, "watt_cpu_temperature_celsius", temperature); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + metric_help( |
| 118 | + &mut metrics, |
| 119 | + "watt_power_supply_discharging", |
| 120 | + "Whether the system is currently discharging.", |
| 121 | + ); |
| 122 | + metric_type(&mut metrics, "watt_power_supply_discharging", "gauge"); |
| 123 | + metric( |
| 124 | + &mut metrics, |
| 125 | + "watt_power_supply_discharging", |
| 126 | + u8::from(state.is_discharging()) as f64, |
| 127 | + ); |
| 128 | + |
| 129 | + metric_help( |
| 130 | + &mut metrics, |
| 131 | + "watt_active_profile", |
| 132 | + "Active power profile labelled by profile name.", |
| 133 | + ); |
| 134 | + metric_type(&mut metrics, "watt_active_profile", "gauge"); |
| 135 | + |
| 136 | + let active_profile = state.active_profile(); |
| 137 | + for profile in profile::PowerProfile::all() { |
| 138 | + let value = u8::from(profile == active_profile) as f64; |
| 139 | + labelled_metric( |
| 140 | + &mut metrics, |
| 141 | + "watt_active_profile", |
| 142 | + "profile", |
| 143 | + profile.as_str(), |
| 144 | + value, |
| 145 | + ); |
| 146 | + } |
| 147 | + |
| 148 | + metrics |
| 149 | +} |
| 150 | + |
| 151 | +fn metric_help(metrics: &mut String, name: &str, help: &str) { |
| 152 | + let _ = writeln!(metrics, "# HELP {name} {help}"); |
| 153 | +} |
| 154 | + |
| 155 | +fn metric_type(metrics: &mut String, name: &str, ty: &str) { |
| 156 | + let _ = writeln!(metrics, "# TYPE {name} {ty}"); |
| 157 | +} |
| 158 | + |
| 159 | +fn metric(metrics: &mut String, name: &str, value: f64) { |
| 160 | + let _ = writeln!(metrics, "{name} {value}"); |
| 161 | +} |
| 162 | + |
| 163 | +fn labelled_metric( |
| 164 | + metrics: &mut String, |
| 165 | + name: &str, |
| 166 | + label: &str, |
| 167 | + label_value: &str, |
| 168 | + value: f64, |
| 169 | +) { |
| 170 | + let _ = writeln!(metrics, r#"{name}{{{label}="{label_value}"}} {value}"#); |
| 171 | +} |
0 commit comments