From c81ef16e59cb59c08f5c83b0e0d50bd444550c01 Mon Sep 17 00:00:00 2001 From: jonathanrainer Date: Wed, 16 Oct 2024 10:21:32 +0100 Subject: [PATCH] FLEET-19 Change gears to make metrics less spammy --- apollo-router/src/plugins/fleet_detector.rs | 34 ++++++++++++++++----- 1 file changed, 27 insertions(+), 7 deletions(-) diff --git a/apollo-router/src/plugins/fleet_detector.rs b/apollo-router/src/plugins/fleet_detector.rs index ea11ea57a29..78b5d06c998 100644 --- a/apollo-router/src/plugins/fleet_detector.rs +++ b/apollo-router/src/plugins/fleet_detector.rs @@ -1,6 +1,7 @@ use schemars::JsonSchema; use serde::Deserialize; use sysinfo::System; +use tokio::task::JoinHandle; use tower::BoxError; use tracing::debug; use tracing::info; @@ -9,7 +10,9 @@ use crate::plugin::Plugin; use crate::plugin::PluginInit; #[derive(Debug)] -struct FleetDetector {} +struct FleetDetector { + handle: JoinHandle<()>, +} #[derive(Debug, Default, Deserialize, JsonSchema)] struct Conf {} @@ -20,17 +23,34 @@ impl Plugin for FleetDetector { async fn new(_: PluginInit) -> Result { debug!("beginning environment detection"); - let sys = &System::new_all(); - detect_cpu_values(sys); - detect_memory_values(sys); - Ok(FleetDetector {}) + debug!("spawning continuous detector task"); + let handle = tokio::task::spawn(async { + let mut sys = System::new_all(); + let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(60)); + loop { + interval.tick().await; + sys.refresh_cpu_all(); + sys.refresh_memory(); + detect_cpu_values(&sys); + detect_memory_values(&sys); + } + }); + + Ok(FleetDetector { handle }) } } + +impl Drop for FleetDetector { + fn drop(&mut self) { + self.handle.abort(); + } +} + fn detect_cpu_values(system: &System) { let cpus = system.cpus(); - let cpu_freq = cpus.iter().map(|cpu| cpu.frequency()).sum::() / cpus.len() as u64; - info!(counter.apollo.router.instance.cpu_freq = cpu_freq); let cpu_count = detect_cpu_count(system); + let cpu_freq = cpus.iter().map(|cpu| cpu.frequency()).sum::() / cpus.len() as u64; + info!(value.apollo.router.instance.cpu_freq = cpu_freq); info!(counter.apollo.router.instance.cpu_count = cpu_count); }