@@ -2,6 +2,8 @@ use anyhow::{anyhow, Result};
22use prometheus:: { Encoder , Registry , TextEncoder } ;
33use std:: net:: SocketAddr ;
44use std:: sync:: Arc ;
5+ use std:: time:: Instant ;
6+ use sysinfo:: { get_current_pid, ProcessExt , System , SystemExt } ;
57use tokio:: sync:: RwLock ;
68use tokio:: task:: JoinHandle ;
79use tracing:: { debug, error, info, warn} ;
@@ -54,6 +56,7 @@ pub struct Server {
5456 runtime : Option < Arc < Runtime > > ,
5557 shard_manager : Option < Arc < ShardManager > > ,
5658 server_handle : RwLock < Option < JoinHandle < Result < ( ) > > > > ,
59+ start_time : Instant ,
5760}
5861
5962impl Server {
@@ -70,6 +73,7 @@ impl Server {
7073 runtime,
7174 shard_manager,
7275 server_handle : RwLock :: new ( None ) ,
76+ start_time : Instant :: now ( ) ,
7377 }
7478 }
7579
@@ -78,12 +82,7 @@ impl Server {
7882 let addr = format ! ( "{}:{}" , self . config. address, self . config. port) ;
7983 let addr: SocketAddr = addr. parse ( ) ?;
8084
81- let metrics = self . metrics . clone ( ) ;
82- let config = self . config . clone ( ) ;
83- let runtime = self . runtime . clone ( ) ;
84- let shard_manager = self . shard_manager . clone ( ) ;
85-
86- let server = warp:: serve ( self . routes ( metrics, config, runtime, shard_manager) ) ;
85+ let server = warp:: serve ( self . filter ( ) ) ;
8786
8887 info ! ( "Starting server on {}" , addr) ;
8988
@@ -122,13 +121,27 @@ impl Server {
122121 Ok ( ( ) )
123122 }
124123
124+ /// Get the Warp filter for this server
125+ pub fn filter (
126+ & self ,
127+ ) -> impl warp:: Filter < Extract = impl warp:: Reply , Error = warp:: Rejection > + Clone {
128+ self . routes (
129+ self . metrics . clone ( ) ,
130+ self . config . clone ( ) ,
131+ self . runtime . clone ( ) ,
132+ self . shard_manager . clone ( ) ,
133+ self . start_time ,
134+ )
135+ }
136+
125137 /// Create the server routes
126138 fn routes (
127139 & self ,
128140 metrics : Arc < MetricsCollector > ,
129141 config : ServerConfig ,
130142 runtime : Option < Arc < Runtime > > ,
131143 shard_manager : Option < Arc < ShardManager > > ,
144+ start_time : Instant ,
132145 ) -> impl warp:: Filter < Extract = impl warp:: Reply , Error = warp:: Rejection > + Clone {
133146 let health_route = warp:: path ( "health" ) . map ( move || {
134147 debug ! ( "Health check request received" ) ;
@@ -182,10 +195,14 @@ impl Server {
182195 let stats_route = warp:: path ( api_path)
183196 . and ( warp:: path ( "stats" ) )
184197 . map ( move || {
198+ let mut sys = System :: new ( ) ;
199+ let pid = get_current_pid ( ) . unwrap ( ) ;
200+ sys. refresh_process ( pid) ;
201+ let mem_mb = sys. process ( pid) . map ( |p| p. memory ( ) / 1024 ) . unwrap_or ( 0 ) ;
185202 let stats = serde_json:: json!( {
186203 "version" : crate :: VERSION ,
187- "uptime_seconds" : 0 , // TODO: Add actual uptime
188- "memory_usage_mb" : 0 , // TODO: Add actual memory usage
204+ "uptime_seconds" : start_time . elapsed ( ) . as_secs ( ) ,
205+ "memory_usage_mb" : mem_mb ,
189206 } ) ;
190207
191208 warp:: reply:: json ( & stats) . into_response ( )
0 commit comments