@@ -315,6 +315,7 @@ async fn main() -> Result<()> {
315315 server,
316316 forward_unsupported_queries,
317317 fallback_timeout_secs,
318+ ..
318319 } => AdapterConfig :: prometheus_promql (
319320 server. clone ( ) ,
320321 * forward_unsupported_queries,
@@ -353,41 +354,11 @@ async fn main() -> Result<()> {
353354
354355 if config. backend . forward_unsupported_queries ( ) {
355356 let client = reqwest:: Client :: new ( ) ;
356- let ( health_url, backend_label) = match & config. backend {
357- BackendConfig :: Prometheus { server, .. } => (
358- format ! ( "{}/api/v1/status/runtimeinfo" , server. trim_end_matches( '/' ) ) ,
359- server. clone ( ) ,
360- ) ,
361- BackendConfig :: Clickhouse { url, .. } => {
362- ( format ! ( "{}/ping" , url. trim_end_matches( '/' ) ) , url. clone ( ) )
363- }
364- BackendConfig :: ElasticQuerydsl { url, .. } | BackendConfig :: ElasticSql { url, .. } => (
365- format ! ( "{}/_cluster/health" , url. trim_end_matches( '/' ) ) ,
366- url. clone ( ) ,
367- ) ,
368- } ;
369- match client
370- . get ( & health_url)
371- . timeout ( std:: time:: Duration :: from_secs ( 5 ) )
372- . send ( )
373- . await
374- {
375- Ok ( resp) if resp. status ( ) . is_success ( ) => {
376- info ! ( "Backend reachable at {}" , backend_label) ;
377- }
378- Ok ( resp) => {
379- error ! (
380- "Backend at {} returned HTTP {} — cannot start" ,
381- backend_label,
382- resp. status( )
383- ) ;
384- std:: process:: exit ( 1 ) ;
385- }
386- Err ( e) => {
387- error ! ( "Cannot reach backend at {}: {}" , backend_label, e) ;
388- std:: process:: exit ( 1 ) ;
389- }
357+ if let Err ( message) = check_backend_health ( & client, & config. backend ) . await {
358+ error ! ( "{}" , message) ;
359+ std:: process:: exit ( 1 ) ;
390360 }
361+ info ! ( "Backend reachable at {}" , config. backend. server_url( ) ) ;
391362 }
392363
393364 let query_tracker = if config. query_tracker . enabled {
@@ -503,6 +474,29 @@ async fn main() -> Result<()> {
503474 Ok ( ( ) )
504475}
505476
477+ async fn check_backend_health (
478+ client : & reqwest:: Client ,
479+ backend : & BackendConfig ,
480+ ) -> std:: result:: Result < ( ) , String > {
481+ let health_url = backend. health_check_url ( ) ;
482+ let backend_label = backend. server_url ( ) ;
483+
484+ match client
485+ . get ( & health_url)
486+ . timeout ( std:: time:: Duration :: from_secs ( 5 ) )
487+ . send ( )
488+ . await
489+ {
490+ Ok ( resp) if resp. status ( ) . is_success ( ) => Ok ( ( ) ) ,
491+ Ok ( resp) => Err ( format ! (
492+ "Backend at {} returned HTTP {} — cannot start" ,
493+ backend_label,
494+ resp. status( )
495+ ) ) ,
496+ Err ( e) => Err ( format ! ( "Cannot reach backend at {}: {}" , backend_label, e) ) ,
497+ }
498+ }
499+
506500/// Periodic memory diagnostics logger — runs every 30 seconds.
507501async fn spawn_memory_diagnostics (
508502 store : Arc < SimpleMapStore > ,
@@ -596,3 +590,35 @@ fn setup_logging(
596590 info ! ( "Logs will be written to: {}/query_engine.log" , output_dir) ;
597591 Ok ( guard)
598592}
593+
594+ #[ cfg( test) ]
595+ mod tests {
596+ use super :: { check_backend_health, BackendConfig } ;
597+ use axum:: { routing:: get, Router } ;
598+ use tokio:: net:: TcpListener ;
599+
600+ #[ tokio:: test]
601+ async fn backend_health_check_uses_configured_endpoint_and_reports_failure ( ) {
602+ let app = Router :: new ( ) . route (
603+ "/health" ,
604+ get ( || async { axum:: http:: StatusCode :: SERVICE_UNAVAILABLE } ) ,
605+ ) ;
606+ let listener = TcpListener :: bind ( "127.0.0.1:0" ) . await . unwrap ( ) ;
607+ let address = listener. local_addr ( ) . unwrap ( ) ;
608+ tokio:: spawn ( async move {
609+ axum:: serve ( listener, app) . await . unwrap ( ) ;
610+ } ) ;
611+
612+ let backend = BackendConfig :: Prometheus {
613+ server : format ! ( "http://{}" , address) ,
614+ health_endpoint : "/health" . to_string ( ) ,
615+ forward_unsupported_queries : true ,
616+ fallback_timeout_secs : 30 ,
617+ } ;
618+ let error = check_backend_health ( & reqwest:: Client :: new ( ) , & backend)
619+ . await
620+ . unwrap_err ( ) ;
621+
622+ assert ! ( error. contains( "returned HTTP 503" ) ) ;
623+ }
624+ }
0 commit comments