@@ -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 starts at
277- /// 500ms (INITIAL_WAIT_MS) and doubles after each scrape (regardless of success or failure)
278- /// up to 9.8 seconds (MAX_WAIT_MS).
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_MS : std:: time:: Duration = std:: time:: Duration :: from_millis ( 300 ) ;
281- const INITIAL_WAIT_MS : std:: time:: Duration = std:: time:: Duration :: from_millis ( 500 ) ;
282- const MAX_WAIT_MS : std:: time:: Duration = std:: time:: Duration :: from_millis ( 9800 ) ; // Arbitrary value
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,9 +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_MS ;
312- let mut current_wait = INITIAL_WAIT_MS ;
313- let mut interval = tokio:: time:: interval ( current_wait) ;
310+ let timeout = std:: time:: Duration :: from_millis ( 500 ) ;
311+ let mut interval = tokio:: time:: interval ( MAX_WAIT_MS ) ;
314312 interval. set_missed_tick_behavior ( tokio:: time:: MissedTickBehavior :: Skip ) ;
315313
316314 loop {
@@ -328,14 +326,6 @@ impl Component {
328326 }
329327 }
330328
331- // Always double the wait time up to MAX_WAIT_MS
332- let new_wait = std:: cmp:: min ( current_wait * 2 , MAX_WAIT_MS ) ;
333- if new_wait != current_wait {
334- current_wait = new_wait;
335- interval = tokio:: time:: interval ( current_wait) ;
336- interval. set_missed_tick_behavior ( tokio:: time:: MissedTickBehavior :: Skip ) ;
337- }
338-
339329 interval. tick ( ) . await ;
340330 }
341331 } ) ;
0 commit comments