From 530a715bdfcade022ae8ceb5d5067f037bc7c2f2 Mon Sep 17 00:00:00 2001 From: Nils Andreas Svee Date: Mon, 13 Apr 2026 16:32:30 +0700 Subject: [PATCH] fix(observability): refresh metric timestamps on each loop iteration Move clock initialization inside processing loops to ensure exported metrics reflect the current time rather than a stale timestamp. --- src/baseliner.rs | 5 ++--- src/pinger.rs | 5 ++--- src/reflector_selector.rs | 4 ++-- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/baseliner.rs b/src/baseliner.rs index 31c9726..005793e 100644 --- a/src/baseliner.rs +++ b/src/baseliner.rs @@ -46,10 +46,9 @@ impl Baseliner { let slow_factor = ewma_factor(self.config.tick_interval, 135.0); let fast_factor = ewma_factor(self.config.tick_interval, 0.4); - // We need a realtime clock when exporting metrics - let clock = Time::new(ClockId::Realtime); - loop { + // Capture current time at the start of each iteration for metric timestamps + let clock = Time::new(ClockId::Realtime); let time_data = self.stats_rx.recv()?; let mut owd_baseline_map = self.owd_baseline.lock_anyhow()?; diff --git a/src/pinger.rs b/src/pinger.rs index 745120f..3a5aac0 100644 --- a/src/pinger.rs +++ b/src/pinger.rs @@ -59,10 +59,9 @@ pub trait PingListener { ) -> anyhow::Result<()> { let socket = &mut open_socket(type_)?; - // We need a realtime clock when exporting metrics - let clock = Time::new(ClockId::Realtime); - loop { + // Capture current time at the start of each iteration for metric timestamps + let clock = Time::new(ClockId::Realtime); let (pkt, sender) = match socket.rcv_from() { Ok(val) => val, Err(_) => continue, diff --git a/src/reflector_selector.rs b/src/reflector_selector.rs index 2ca4f56..11b259e 100644 --- a/src/reflector_selector.rs +++ b/src/reflector_selector.rs @@ -26,12 +26,12 @@ impl ReflectorSelector { let mut reselection_count = 0; let baseline_sleep_time = Duration::from_secs_f64(self.config.tick_interval * std::f64::consts::PI); - let clock = Time::new(ClockId::Realtime); - // Initial wait of several seconds to allow some OWD data to build up sleep(baseline_sleep_time); loop { + // Capture current time at the start of each iteration for metric timestamps + let clock = Time::new(ClockId::Realtime); /* * Selection is triggered either by some other thread triggering it through the channel, * or it passes the timeout. In any case we don't care about the result of this function,