@@ -273,13 +273,12 @@ impl Component {
273273 /// Add Prometheus metrics for this component's NATS service stats.
274274 ///
275275 /// Starts a background task that periodically requests service statistics from NATS
276- /// and updates the corresponding Prometheus metrics. The scraping interval is set to
277- /// approximately 873ms (MAX_DELAY_MS), which is arbitrary but any value less than a second
278- /// is fair game. This frequent scraping provides real-time service statistics updates.
276+ /// and updates the corresponding Prometheus metrics. The first scrape happens immediately,
277+ /// then subsequent scrapes occur at a fixed interval of 9.8 seconds (MAX_WAIT_MS),
278+ /// which should be near or smaller than typical Prometheus scraping intervals to ensure
279+ /// metrics are fresh when Prometheus collects them.
279280 pub fn start_scraping_nats_service_component_metrics ( & self ) -> Result < ( ) > {
280- const NATS_TIMEOUT_AND_INITIAL_DELAY_MS : std:: time:: Duration =
281- std:: time:: Duration :: from_millis ( 300 ) ;
282- const MAX_DELAY_MS : std:: time:: Duration = std:: time:: Duration :: from_millis ( 873 ) ;
281+ const MAX_WAIT_MS : std:: time:: Duration = std:: time:: Duration :: from_millis ( 9800 ) ; // Should be <= Prometheus scrape interval
283282
284283 // If there is another component with the same service name, this will fail.
285284 let component_metrics = ComponentNatsServerPrometheusMetrics :: new ( self ) ?;
@@ -308,8 +307,8 @@ impl Component {
308307 // By using the DRT's own runtime handle, we ensure the task runs in the
309308 // correct runtime that will persist for the lifetime of the component.
310309 c. drt ( ) . runtime ( ) . secondary ( ) . spawn ( async move {
311- let timeout = NATS_TIMEOUT_AND_INITIAL_DELAY_MS ;
312- let mut interval = tokio:: time:: interval ( MAX_DELAY_MS ) ;
310+ let timeout = std :: time :: Duration :: from_millis ( 500 ) ;
311+ let mut interval = tokio:: time:: interval ( MAX_WAIT_MS ) ;
313312 interval. set_missed_tick_behavior ( tokio:: time:: MissedTickBehavior :: Skip ) ;
314313
315314 loop {
@@ -326,6 +325,7 @@ impl Component {
326325 m. reset_to_zeros ( ) ;
327326 }
328327 }
328+
329329 interval. tick ( ) . await ;
330330 }
331331 } ) ;
0 commit comments