@@ -18,15 +18,22 @@ use crate::varz::*;
1818
1919const METRICS_CONNECTION_TIMEOUT_SECS : u64 = 10 ;
2020const METRICS_MAX_CONCURRENT_CONNECTIONS : u32 = 2 ;
21+ const HEALTH_PATH : & str = "/health" ;
2122
2223type BoxBody = http_body_util:: Full < hyper:: body:: Bytes > ;
2324
2425async fn handle_client_connection (
2526 req : Request < hyper:: body:: Incoming > ,
26- _varz : Varz ,
27+ varz : Varz ,
2728 path : Arc < String > ,
2829) -> Result < Response < BoxBody > , Error > {
29- if req. uri ( ) . path ( ) != path. as_str ( ) {
30+ let request_path = req. uri ( ) . path ( ) ;
31+
32+ if request_path == HEALTH_PATH {
33+ return handle_health_request ( & varz) ;
34+ }
35+
36+ if request_path != path. as_str ( ) {
3037 return Ok ( Response :: builder ( ) . status ( StatusCode :: NOT_FOUND ) . body (
3138 http_body_util:: Full :: new ( hyper:: body:: Bytes :: from ( "404 Not Found" ) ) ,
3239 ) ?) ;
@@ -41,6 +48,38 @@ async fn handle_client_connection(
4148 . body ( http_body_util:: Full :: new ( hyper:: body:: Bytes :: from ( buffer) ) ) ?)
4249}
4350
51+ fn handle_health_request ( varz : & Varz ) -> Result < Response < BoxBody > , Error > {
52+ let uptime_secs = varz. start_instant . 0 . elapsed ( ) . as_secs ( ) ;
53+
54+ let upstream_sent = varz. upstream_sent . get ( ) as u64 ;
55+ let upstream_received = varz. upstream_received . get ( ) as u64 ;
56+ let upstream_errors = varz. upstream_errors . get ( ) as u64 ;
57+
58+ let upstream_status = if upstream_sent == 0 {
59+ "unknown"
60+ } else if upstream_errors > upstream_received {
61+ "degraded"
62+ } else {
63+ "healthy"
64+ } ;
65+
66+ let status = if upstream_status == "degraded" {
67+ "degraded"
68+ } else {
69+ "healthy"
70+ } ;
71+
72+ let body = format ! (
73+ r#"{{"status":"{}","uptime_secs":{},"upstream":{{"status":"{}","sent":{},"received":{},"errors":{}}}}}"# ,
74+ status, uptime_secs, upstream_status, upstream_sent, upstream_received, upstream_errors
75+ ) ;
76+
77+ Ok ( Response :: builder ( )
78+ . status ( StatusCode :: OK )
79+ . header ( CONTENT_TYPE , "application/json" )
80+ . body ( http_body_util:: Full :: new ( hyper:: body:: Bytes :: from ( body) ) ) ?)
81+ }
82+
4483pub async fn prometheus_service (
4584 varz : Varz ,
4685 metrics_config : MetricsConfig ,
0 commit comments