@@ -117,6 +117,13 @@ impl DistributedRuntime {
117117 } ) ;
118118 distributed_runtime. register_metrics_callback ( drt_hierarchies, nats_client_callback) ;
119119
120+ // Initialize the uptime gauge in SystemHealth
121+ distributed_runtime
122+ . system_health
123+ . lock ( )
124+ . unwrap ( )
125+ . initialize_uptime_gauge ( & distributed_runtime) ?;
126+
120127 // Handle system status server initialization
121128 if let Some ( cancel_token) = cancel_token {
122129 // System server is enabled - start both the state and HTTP server
@@ -153,17 +160,7 @@ impl DistributedRuntime {
153160 }
154161 }
155162 } else {
156- // System server HTTP is disabled, but still create the state for metrics
157- // This ensures uptime_seconds metric is always registered
158- let system_status_state = crate :: system_status_server:: SystemStatusState :: new (
159- Arc :: new ( distributed_runtime. clone ( ) ) ,
160- ) ?;
161-
162- // Initialize the start time for uptime tracking
163- if let Err ( e) = system_status_state. initialize_start_time ( ) {
164- tracing:: warn!( "Failed to initialize system status start time: {}" , e) ;
165- }
166-
163+ // System server HTTP is disabled, but uptime metrics are still being tracked via SystemHealth
167164 tracing:: debug!(
168165 "System status server HTTP endpoints disabled, but uptime metrics are being tracked"
169166 ) ;
@@ -349,7 +346,7 @@ impl DistributedConfig {
349346}
350347
351348#[ cfg( test) ]
352- pub mod test_helpers {
349+ pub mod distributed_test_utils {
353350 //! Common test helper functions for DistributedRuntime tests
354351 // TODO: Use in-memory DistributedRuntime for tests instead of full runtime when available.
355352
@@ -364,3 +361,61 @@ pub mod test_helpers {
364361 . unwrap ( )
365362 }
366363}
364+
365+ #[ cfg( feature = "integration" ) ]
366+ #[ cfg( test) ]
367+ mod tests {
368+ use super :: distributed_test_utils:: create_test_drt_async;
369+
370+ #[ tokio:: test]
371+ async fn test_drt_uptime_after_delay_system_disabled ( ) {
372+ // Test uptime with system status server disabled
373+ temp_env:: async_with_vars ( [ ( "DYN_SYSTEM_ENABLED" , Some ( "false" ) ) ] , async {
374+ // Start a DRT
375+ let drt = create_test_drt_async ( ) . await ;
376+
377+ // Wait 50ms
378+ tokio:: time:: sleep ( tokio:: time:: Duration :: from_millis ( 50 ) ) . await ;
379+
380+ // Check that uptime is 50+ ms
381+ let uptime = drt. system_health . lock ( ) . unwrap ( ) . uptime ( ) ;
382+ assert ! (
383+ uptime >= std:: time:: Duration :: from_millis( 50 ) ,
384+ "Expected uptime to be at least 50ms, but got {:?}" ,
385+ uptime
386+ ) ;
387+
388+ println ! (
389+ "✓ DRT uptime test passed (system disabled): uptime = {:?}" ,
390+ uptime
391+ ) ;
392+ } )
393+ . await ;
394+ }
395+
396+ #[ tokio:: test]
397+ async fn test_drt_uptime_after_delay_system_enabled ( ) {
398+ // Test uptime with system status server enabled
399+ temp_env:: async_with_vars ( [ ( "DYN_SYSTEM_ENABLED" , Some ( "true" ) ) ] , async {
400+ // Start a DRT
401+ let drt = create_test_drt_async ( ) . await ;
402+
403+ // Wait 50ms
404+ tokio:: time:: sleep ( tokio:: time:: Duration :: from_millis ( 50 ) ) . await ;
405+
406+ // Check that uptime is 50+ ms
407+ let uptime = drt. system_health . lock ( ) . unwrap ( ) . uptime ( ) ;
408+ assert ! (
409+ uptime >= std:: time:: Duration :: from_millis( 50 ) ,
410+ "Expected uptime to be at least 50ms, but got {:?}" ,
411+ uptime
412+ ) ;
413+
414+ println ! (
415+ "✓ DRT uptime test passed (system enabled): uptime = {:?}" ,
416+ uptime
417+ ) ;
418+ } )
419+ . await ;
420+ }
421+ }
0 commit comments