|
| 1 | +use opentelemetry::KeyValue; |
| 2 | +use sysinfo::{CpuRefreshKind, ProcessRefreshKind, RefreshKind, System}; |
| 3 | +use tokio::time::{sleep, Duration}; |
| 4 | + |
| 5 | +fn find_main_dockerd_pid(system: &System) -> Option<u32> { |
| 6 | + // Prefer dockerd whose parent PID is 1 (your original heuristic) |
| 7 | + for (pid, process) in system.processes() { |
| 8 | + if process.name() == "dockerd" |
| 9 | + && process.parent().map(|pp| pp.as_u32()).unwrap_or(0) == 1 |
| 10 | + { |
| 11 | + return Some(pid.as_u32()); |
| 12 | + } |
| 13 | + } |
| 14 | + // Fallback: first dockerd found |
| 15 | + for (pid, process) in system.processes() { |
| 16 | + if process.name() == "dockerd" { |
| 17 | + return Some(pid.as_u32()); |
| 18 | + } |
| 19 | + } |
| 20 | + None |
| 21 | +} |
| 22 | + |
| 23 | +#[tokio::main] |
| 24 | +async fn main() -> anyhow::Result<()> { |
| 25 | + // Config path: env override + default |
| 26 | + let cfg_path = |
| 27 | + std::env::var("OTEL_CONFIG_FILE").unwrap_or_else(|_| "otel.toml".to_string()); |
| 28 | + |
| 29 | + let cfg = otel_lib_rs::OtelConfig::from_toml_file(&cfg_path)?; |
| 30 | + let otel = otel_lib_rs::OtelRuntime::init(cfg)?; |
| 31 | + |
| 32 | + let meter = otel.meter(); |
| 33 | + |
| 34 | + let gauge = meter |
| 35 | + .f64_observable_gauge("dockerd.cpu.usage.percent") |
| 36 | + .with_description("Total CPU usage of dockerd process and its children (%)") |
| 37 | + .init(); |
| 38 | + |
| 39 | + meter |
| 40 | + .register_callback(&[gauge.as_any()], move |observer| { |
| 41 | + let mut system = System::new_with_specifics( |
| 42 | + RefreshKind::new() |
| 43 | + .with_processes(ProcessRefreshKind::everything()) |
| 44 | + .with_cpu(CpuRefreshKind::everything()), |
| 45 | + ); |
| 46 | + |
| 47 | + // sysinfo CPU% is based on two samples |
| 48 | + system.refresh_processes(); |
| 49 | + system.refresh_cpu(); |
| 50 | + |
| 51 | + std::thread::sleep(std::time::Duration::from_millis(500)); |
| 52 | + |
| 53 | + system.refresh_processes(); |
| 54 | + system.refresh_cpu(); |
| 55 | + |
| 56 | + let mut total_cpu = 0.0; |
| 57 | + |
| 58 | + if let Some(main_pid) = find_main_dockerd_pid(&system) { |
| 59 | + for (pid, process) in system.processes() { |
| 60 | + if process.name() == "dockerd" { |
| 61 | + let p = pid.as_u32(); |
| 62 | + let parent = process.parent().map(|pp| pp.as_u32()).unwrap_or(0); |
| 63 | + if p == main_pid || parent == main_pid { |
| 64 | + total_cpu += process.cpu_usage() as f64; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + observer.observe_f64( |
| 70 | + &gauge, |
| 71 | + total_cpu, |
| 72 | + &[ |
| 73 | + KeyValue::new("process", "dockerd"), |
| 74 | + KeyValue::new("scope", "main+children"), |
| 75 | + ], |
| 76 | + ); |
| 77 | + } else { |
| 78 | + observer.observe_f64( |
| 79 | + &gauge, |
| 80 | + 0.0, |
| 81 | + &[ |
| 82 | + KeyValue::new("process", "dockerd"), |
| 83 | + KeyValue::new("status", "not_found"), |
| 84 | + ], |
| 85 | + ); |
| 86 | + } |
| 87 | + }) |
| 88 | + .expect("Failed to register callback"); |
| 89 | + |
| 90 | + println!("Loaded OTEL config from: {}", cfg_path); |
| 91 | + println!("Exporting dockerd CPU usage..."); |
| 92 | + |
| 93 | + loop { |
| 94 | + sleep(Duration::from_secs(600)).await; |
| 95 | + } |
| 96 | +} |
0 commit comments