1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
//! Health checking for model endpoints
//!
//! Provides periodic health checks for model endpoints with state tracking.
//! Endpoints that fail consecutive checks are marked unhealthy and excluded from selection.
use crate::config::{Config, ModelEndpoint};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant};
use thiserror::Error;
use tokio::sync::{Mutex, RwLock};
// Health check configuration constants
const CONSECUTIVE_FAILURES_THRESHOLD: u32 = 3;
const HEALTH_CHECK_INTERVAL_SECS: u64 = 30;
const HEALTH_CHECK_STALE_THRESHOLD_SECS: u64 = 60;
const MAX_BACKGROUND_TASK_RESTARTS: u32 = 5;
/// Status of the background health checking task
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BackgroundTaskStatus {
/// Task is running normally
Running,
/// Task has failed and is restarting
Restarting,
/// Task has exhausted all restart attempts and is permanently stopped
PermanentlyFailed,
}
/// Per-endpoint health tracking failure information
///
/// Tracks persistent failures in health tracking operations (mark_success, mark_failure)
/// to provide operators with visibility into observability system degradation.
#[derive(Debug, Clone)]
pub struct HealthTrackingFailure {
/// Name of the endpoint experiencing tracking failures
pub endpoint_name: String,
/// Number of consecutive tracking failures for this endpoint
pub consecutive_failures: u32,
/// The last error message encountered
pub last_error: String,
/// When the last tracking failure occurred
pub last_failure_time: Instant,
}
const MAX_TRACKED_FAILURES: usize = 10; // Limit to prevent unbounded growth
/// Metrics for monitoring the health checking system itself
///
/// Tracks the background task's health to enable external monitoring
/// and alerting when the health checking system fails.
pub struct HealthMetrics {
state: Arc<RwLock<HealthMetricsState>>,
}
/// Internal state for HealthMetrics
struct HealthMetricsState {
/// When the background task last completed a health check cycle
last_successful_check: Option<Instant>,
/// Current status of the background task
background_task_status: BackgroundTaskStatus,
/// Number of times the background task has restarted
restart_count: u32,
/// When the background task last failed (if applicable)
last_failure_time: Option<Instant>,
/// Per-endpoint health tracking failures
/// Key: endpoint name, Value: tracking failure info
/// Limited to MAX_TRACKED_FAILURES entries (LRU eviction)
tracking_failures: HashMap<String, HealthTrackingFailure>,
}
impl Default for HealthMetrics {
fn default() -> Self {
Self::new()
}
}
impl HealthMetrics {
/// Create a new HealthMetrics instance
pub fn new() -> Self {
Self {
state: Arc::new(RwLock::new(HealthMetricsState {
last_successful_check: None,
background_task_status: BackgroundTaskStatus::Running,
restart_count: 0,
last_failure_time: None,
tracking_failures: HashMap::new(),
})),
}
}
/// Record a successful health check cycle completion
pub async fn record_successful_check(&self) {
let mut state = self.state.write().await;
state.last_successful_check = Some(Instant::now());
state.background_task_status = BackgroundTaskStatus::Running;
}
/// Record a background task restart attempt
pub async fn record_restart(&self, attempt: u32) {
let mut state = self.state.write().await;
state.restart_count = attempt;
state.background_task_status = BackgroundTaskStatus::Restarting;
state.last_failure_time = Some(Instant::now());
}
/// Mark the background task as permanently failed
pub async fn mark_permanently_failed(&self) {
let mut state = self.state.write().await;
state.background_task_status = BackgroundTaskStatus::PermanentlyFailed;
state.last_failure_time = Some(Instant::now());
}
/// Get the current status for monitoring
pub async fn status(&self) -> BackgroundTaskStatus {
let state = self.state.read().await;
state.background_task_status
}
/// Get the last successful check time
pub async fn last_successful_check(&self) -> Option<Instant> {
let state = self.state.read().await;
state.last_successful_check
}
/// Get the number of restart attempts
pub async fn restart_count(&self) -> u32 {
let state = self.state.read().await;
state.restart_count
}
/// Get the last failure time
pub async fn last_failure_time(&self) -> Option<Instant> {
let state = self.state.read().await;
state.last_failure_time
}
/// Check if the background task is healthy
///
/// Returns false if:
/// - Task is permanently failed
/// - No successful health checks have completed yet (prevents false positive)
/// - Last successful check is stale (>60 seconds old, 2x the 30s check interval)
///
/// ## Staleness Detection
///
/// Health data is considered stale if more than `HEALTH_CHECK_STALE_THRESHOLD_SECS`
/// (60 seconds) have elapsed since the last successful check. This is 2x the check
/// interval (`HEALTH_CHECK_INTERVAL_SECS` = 30s), allowing one missed check before
/// marking the background task as unhealthy.
pub async fn is_background_task_healthy(&self) -> bool {
let state = self.state.read().await;
if state.background_task_status == BackgroundTaskStatus::PermanentlyFailed {
return false;
}
// Check if we've had a recent successful check
if let Some(last_check) = state.last_successful_check {
let elapsed = Instant::now().duration_since(last_check);
if elapsed > Duration::from_secs(HEALTH_CHECK_STALE_THRESHOLD_SECS) {
tracing::warn!(
elapsed_seconds = elapsed.as_secs(),
threshold_seconds = HEALTH_CHECK_STALE_THRESHOLD_SECS,
last_check = ?last_check,
"Background health check data is stale (no successful check in {}s, threshold: {}s). \
Health checker may be stalled or overloaded.",
elapsed.as_secs(),
HEALTH_CHECK_STALE_THRESHOLD_SECS
);
return false; // No successful check in threshold time (2x the interval)
}
true // Recent successful check - system is healthy
} else {
// No successful check yet - system starting up
// If background task is Running, assume healthy during warmup period
// This prevents false negatives during startup before first health check completes
state.background_task_status == BackgroundTaskStatus::Running
}
}
/// Record a health tracking failure for an endpoint
///
/// Tracks consecutive failures to provide operators with visibility into
/// observability system degradation. Limited to MAX_TRACKED_FAILURES entries.
pub async fn record_tracking_failure(&self, endpoint_name: &str, error_message: &str) {
let mut state = self.state.write().await;
state
.tracking_failures
.entry(endpoint_name.to_string())
.and_modify(|f| {
f.consecutive_failures += 1;
f.last_error = error_message.to_string();
f.last_failure_time = Instant::now();
})
.or_insert(HealthTrackingFailure {
endpoint_name: endpoint_name.to_string(),
consecutive_failures: 1,
last_error: error_message.to_string(),
last_failure_time: Instant::now(),
});
// Enforce MAX_TRACKED_FAILURES limit using LRU eviction
// Remove oldest entry if we exceed the limit
if state.tracking_failures.len() > MAX_TRACKED_FAILURES
&& let Some(oldest_key) = state
.tracking_failures
.iter()
.min_by_key(|(_, v)| v.last_failure_time)
.map(|(k, _)| k.clone())
{
state.tracking_failures.remove(&oldest_key);
}
}
/// Clear tracking failures for an endpoint (called on successful tracking operation)
pub async fn clear_tracking_failure(&self, endpoint_name: &str) {
let mut state = self.state.write().await;
state.tracking_failures.remove(endpoint_name);
}
/// Get all current health tracking failures
///
/// Returns a snapshot of all endpoints currently experiencing health tracking issues.
/// Used by /health endpoint to expose tracking failures to operators.
pub async fn get_tracking_failures(&self) -> Vec<HealthTrackingFailure> {
let state = self.state.read().await;
state.tracking_failures.values().cloned().collect()
}
/// Check if there are any health tracking failures
///
/// Returns true if any endpoints are experiencing health tracking issues.
pub async fn has_tracking_failures(&self) -> bool {
let state = self.state.read().await;
!state.tracking_failures.is_empty()
}
}
/// Errors that can occur during health checking operations
#[derive(Error, Debug)]
pub enum HealthError {
/// Endpoint name not found in configuration
///
/// Indicates a programming error (endpoint name mismatch) or race condition
/// (config reloaded during request). Should never occur under normal operation.
#[error("Unknown endpoint: {0}")]
UnknownEndpoint(String),
/// Failed to create HTTP client for health checks
///
/// This indicates a systemic issue (TLS configuration error, resource exhaustion,
/// library bug) rather than an individual endpoint failure. When this error occurs,
/// ALL subsequent health checks will fail.
#[error(
"Failed to create HTTP client: {0}. This indicates a systemic issue (TLS config, resource exhaustion)."
)]
HttpClientCreationFailed(String),
/// Invalid endpoint URL configuration
///
/// The base_url is malformed or contains invalid components (query parameters,
/// fragments) that would cause health check failures. This is a configuration error.
#[error("Invalid endpoint URL for '{endpoint}': {base_url}. {details}")]
InvalidEndpointUrl {
endpoint: String,
base_url: String,
details: String,
},
}
impl HealthError {
/// Get the error type as a string label for metrics
///
/// Returns a consistent snake_case string matching the error variant:
/// - `UnknownEndpoint` → "unknown_endpoint"
/// - `HttpClientCreationFailed` → "http_client_failed"
/// - `InvalidEndpointUrl` → "invalid_url"
///
/// Used for Prometheus metric labels to categorize health tracking failures.
pub fn error_type(&self) -> &'static str {
match self {
HealthError::UnknownEndpoint(_) => "unknown_endpoint",
HealthError::HttpClientCreationFailed(_) => "http_client_failed",
HealthError::InvalidEndpointUrl { .. } => "invalid_url",
}
}
}
/// Health status for a single endpoint
///
/// Encapsulates health state to prevent invalid state transitions.
/// All fields are private to ensure state invariants are maintained.
#[derive(Clone, Debug)]
pub struct EndpointHealth {
name: String,
base_url: String,
healthy: bool,
last_check: Instant,
consecutive_failures: u32,
}
impl EndpointHealth {
/// Create a new EndpointHealth starting in healthy state
pub fn new(name: String, base_url: String) -> Self {
Self {
name,
base_url,
healthy: true,
last_check: Instant::now(),
consecutive_failures: 0,
}
}
/// Get the endpoint name
pub fn name(&self) -> &str {
&self.name
}
/// Get the endpoint base URL
pub fn base_url(&self) -> &str {
&self.base_url
}
/// Check if the endpoint is currently healthy
pub fn is_healthy(&self) -> bool {
self.healthy
}
/// Get the last health check time
pub fn last_check(&self) -> Instant {
self.last_check
}
/// Get the consecutive failure count
pub fn consecutive_failures(&self) -> u32 {
self.consecutive_failures
}
}
/// Health checker for model endpoints
///
/// Tracks health status of all endpoints and provides background checking.
/// - 3 consecutive failures → mark unhealthy
/// - 1 successful check → recover to healthy
pub struct HealthChecker {
health_status: Arc<RwLock<HashMap<String, EndpointHealth>>>,
config: Arc<Config>,
metrics: Arc<HealthMetrics>,
/// Reference to application-wide Prometheus metrics for surfacing health tracking failures
app_metrics: Option<Arc<crate::metrics::Metrics>>,
/// Background health checking task handle for graceful shutdown
background_task: Arc<Mutex<Option<tokio::task::JoinHandle<()>>>>,
}
impl std::fmt::Debug for HealthChecker {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("HealthChecker")
.field("health_status", &"<RwLock<HashMap>>")
.field("config", &"<Config>")
.field("metrics", &"<HealthMetrics>")
.field(
"app_metrics",
&if self.app_metrics.is_some() {
"<Some(Metrics)>"
} else {
"<None>"
},
)
.field("background_task", &"<Mutex<JoinHandle>>")
.finish()
}
}
impl HealthChecker {
/// Create a new HealthChecker with all endpoints starting as healthy
pub fn new(config: Arc<Config>) -> Self {
let mut health_status = HashMap::new();
// Initialize all fast endpoints
for endpoint in &config.models.fast {
health_status.insert(
endpoint.name().to_string(),
EndpointHealth::new(endpoint.name().to_string(), endpoint.base_url().to_string()),
);
}
// Initialize all balanced endpoints
for endpoint in &config.models.balanced {
health_status.insert(
endpoint.name().to_string(),
EndpointHealth::new(endpoint.name().to_string(), endpoint.base_url().to_string()),
);
}
// Initialize all deep endpoints
for endpoint in &config.models.deep {
health_status.insert(
endpoint.name().to_string(),
EndpointHealth::new(endpoint.name().to_string(), endpoint.base_url().to_string()),
);
}
tracing::info!(
total_endpoints = health_status.len(),
"HealthChecker initialized with all endpoints starting as healthy"
);
Self {
health_status: Arc::new(RwLock::new(health_status)),
config,
metrics: Arc::new(HealthMetrics::new()),
app_metrics: None,
background_task: Arc::new(Mutex::new(None)),
}
}
/// Create a new HealthChecker with Prometheus metrics integration
///
/// This constructor enables surfacing of health tracking failures to operators
/// via the `/health` endpoint and Prometheus metrics. When mark_success or
/// mark_failure operations fail in the background health checking task, the
/// failure is incremented in `health_tracking_failures_total`.
///
/// # Arguments
/// * `config` - Application configuration with model endpoints
/// * `app_metrics` - Prometheus metrics collector for surfacing failures
///
/// # Example
/// ```ignore
/// let config = Arc::new(Config::load()?);
/// let metrics = Arc::new(Metrics::new()?);
/// let checker = Arc::new(HealthChecker::new_with_metrics(config, metrics));
/// checker.start_background_checks();
/// ```
pub fn new_with_metrics(
config: Arc<Config>,
app_metrics: Arc<crate::metrics::Metrics>,
) -> Self {
let mut health_status = HashMap::new();
// Initialize all fast endpoints
for endpoint in &config.models.fast {
health_status.insert(
endpoint.name().to_string(),
EndpointHealth::new(endpoint.name().to_string(), endpoint.base_url().to_string()),
);
}
// Initialize all balanced endpoints
for endpoint in &config.models.balanced {
health_status.insert(
endpoint.name().to_string(),
EndpointHealth::new(endpoint.name().to_string(), endpoint.base_url().to_string()),
);
}
// Initialize all deep endpoints
for endpoint in &config.models.deep {
health_status.insert(
endpoint.name().to_string(),
EndpointHealth::new(endpoint.name().to_string(), endpoint.base_url().to_string()),
);
}
tracing::info!(
total_endpoints = health_status.len(),
has_metrics = true,
"HealthChecker initialized with Prometheus metrics integration"
);
Self {
health_status: Arc::new(RwLock::new(health_status)),
config,
metrics: Arc::new(HealthMetrics::new()),
app_metrics: Some(app_metrics),
background_task: Arc::new(Mutex::new(None)),
}
}
/// Get reference to health metrics for monitoring
pub fn metrics(&self) -> &Arc<HealthMetrics> {
&self.metrics
}
/// Check if an endpoint is currently healthy
///
/// Returns `false` for unknown endpoints (with warning logged).
///
/// # Performance
/// - **Time complexity**: O(1) HashMap lookup
/// - **Space complexity**: O(1)
/// - **Async**: RwLock read (shared, non-blocking with other readers)
/// - **Expected latency**: <1μs
pub async fn is_healthy(&self, endpoint_name: &str) -> bool {
let status = self.health_status.read().await;
match status.get(endpoint_name) {
Some(h) => h.healthy,
None => {
// DEFENSIVE: Log unknown endpoint checks
// This catches typos, race conditions (config reload mid-request),
// or stale endpoint references from calling code.
tracing::warn!(
endpoint_name = %endpoint_name,
available_endpoints = ?status.keys().collect::<Vec<_>>(),
"Unknown endpoint checked for health - returning false. \
This may indicate a typo in endpoint name, config reload race condition, \
or stale endpoint reference."
);
false
}
}
}
/// Mark an endpoint as having failed
///
/// Increments consecutive failure count.
/// After 3 consecutive failures, marks endpoint as unhealthy.
///
/// # Performance
/// - **Time complexity**: O(1) HashMap lookup + mutation
/// - **Space complexity**: O(1)
/// - **Async**: RwLock write (exclusive lock, blocks other readers/writers)
/// - **Expected latency**: <10μs (depends on lock contention)
///
/// Returns an error if the endpoint name is unknown.
pub async fn mark_failure(&self, endpoint_name: &str) -> Result<(), HealthError> {
let mut status = self.health_status.write().await;
// Defense-in-depth: Validate endpoint name exists in health_status
//
// This check catches programming errors where routing logic passes an endpoint
// name that wasn't registered during HealthChecker initialization. Possible causes:
// - Config was modified after HealthChecker creation (shouldn't happen)
// - Routing logic has a typo or name mismatch
// - Endpoint was removed from config but routing code wasn't updated
//
// Without this check, we'd panic on unwrap or silently do nothing, making
// bugs harder to diagnose. Explicit error allows observability via metrics.
let health = match status.get_mut(endpoint_name) {
Some(h) => h,
None => {
let available: Vec<_> = status.keys().collect();
tracing::error!(
endpoint_name = %endpoint_name,
available_endpoints = ?available,
"Unknown endpoint '{}' in mark_failure - available: {:?}",
endpoint_name, available
);
return Err(HealthError::UnknownEndpoint(endpoint_name.to_string()));
}
};
health.consecutive_failures += 1;
health.last_check = Instant::now();
// After 3 consecutive failures, mark as unhealthy
if health.consecutive_failures >= CONSECUTIVE_FAILURES_THRESHOLD {
if health.healthy {
// Log only on transition to unhealthy
tracing::warn!(
endpoint_name = %health.name,
endpoint_url = %health.base_url,
consecutive_failures = health.consecutive_failures,
"Endpoint marked as unhealthy after 3 consecutive failures"
);
}
health.healthy = false;
} else {
tracing::debug!(
endpoint_name = %health.name,
endpoint_url = %health.base_url,
consecutive_failures = health.consecutive_failures,
"Endpoint failure recorded (still healthy)"
);
}
Ok(())
}
/// Mark an endpoint as having succeeded
///
/// Resets consecutive failure count and marks endpoint as healthy.
///
/// # Performance
/// - **Time complexity**: O(1) HashMap lookup + mutation
/// - **Space complexity**: O(1)
/// - **Async**: RwLock write (exclusive lock, blocks other readers/writers)
/// - **Expected latency**: <10μs (depends on lock contention)
///
/// Returns an error if the endpoint name is unknown.
pub async fn mark_success(&self, endpoint_name: &str) -> Result<(), HealthError> {
let mut status = self.health_status.write().await;
// Defense-in-depth: Validate endpoint name exists in health_status
//
// This check catches programming errors where routing logic passes an endpoint
// name that wasn't registered during HealthChecker initialization. Possible causes:
// - Config was modified after HealthChecker creation (shouldn't happen)
// - Routing logic has a typo or name mismatch
// - Endpoint was removed from config but routing code wasn't updated
//
// Without this check, we'd panic on unwrap or silently do nothing, making
// bugs harder to diagnose. Explicit error allows observability via metrics.
let health = match status.get_mut(endpoint_name) {
Some(h) => h,
None => {
let available: Vec<_> = status.keys().collect();
tracing::error!(
endpoint_name = %endpoint_name,
available_endpoints = ?available,
"Unknown endpoint '{}' in mark_success - available: {:?}",
endpoint_name, available
);
return Err(HealthError::UnknownEndpoint(endpoint_name.to_string()));
}
};
let was_unhealthy = !health.healthy;
health.consecutive_failures = 0;
health.healthy = true;
health.last_check = Instant::now();
if was_unhealthy {
// Log recovery
tracing::info!(
endpoint_name = %health.name,
endpoint_url = %health.base_url,
"Endpoint recovered to healthy state"
);
} else {
tracing::debug!(
endpoint_name = %health.name,
endpoint_url = %health.base_url,
"Endpoint health check succeeded"
);
}
Ok(())
}
/// Get all health statuses for display/debugging
pub async fn get_all_statuses(&self) -> Vec<EndpointHealth> {
let status = self.health_status.read().await;
status.values().cloned().collect()
}
/// Check a single endpoint's health via HTTP HEAD request
///
/// Returns:
/// - `Ok(true)` if endpoint is healthy (2xx response)
/// - `Ok(false)` if endpoint is unhealthy (non-2xx, timeout, connection error)
/// - `Err(HealthError::HttpClientCreationFailed)` if HTTP client creation fails
/// (indicates systemic issue, not endpoint-specific problem)
async fn check_endpoint(&self, endpoint: &ModelEndpoint) -> Result<bool, HealthError> {
let client = reqwest::Client::builder()
.timeout(Duration::from_secs(5))
.build()
.map_err(|e| {
tracing::error!(
error = %e,
"FATAL: Failed to create HTTP client for health checks. \
This indicates a systemic issue (TLS config, resource exhaustion, library bug), \
not an endpoint failure. All health checks will fail."
);
HealthError::HttpClientCreationFailed(e.to_string())
})?;
// IMPORTANT: Health check URL construction
// Config validation (config.rs:523-534) ENFORCES that base_url ends with "/v1"
// (e.g., "http://host:port/v1"). We append "/models" to get "http://host:port/v1/models"
// DO NOT append "/v1/models" - config validation prevents this but historically
// this bug caused all endpoints to fail after 90 seconds.
//
// HISTORICAL BUG (Jan 2025): base_url without /v1 suffix caused "/models" → 404
// FIX: Config validation now enforces base_url ends with /v1 (config.rs:527-534)
// This prevents double-/v1 bug that caused endpoints to fail after 90 seconds.
// Validate base_url is a properly formatted URL
// This catches malformed URLs and URLs with unwanted components
let base_url = endpoint.base_url();
let parsed_url =
reqwest::Url::parse(base_url).map_err(|e| HealthError::InvalidEndpointUrl {
endpoint: endpoint.name().to_string(),
base_url: base_url.to_string(),
details: format!("Invalid URL format: {}", e),
})?;
// Check for unwanted URL components (query params, fragments)
// These would create malformed health check URLs and cause cryptic failures
if parsed_url.query().is_some() || parsed_url.fragment().is_some() {
return Err(HealthError::InvalidEndpointUrl {
endpoint: endpoint.name().to_string(),
base_url: base_url.to_string(),
details: "base_url should not contain query parameters or fragments".to_string(),
});
}
let url = format!("{}/models", base_url);
match client.head(&url).send().await {
Ok(response) => {
let is_success = response.status().is_success();
tracing::debug!(
endpoint_name = %endpoint.name(),
url = %url,
status = %response.status(),
healthy = is_success,
"Health check completed"
);
Ok(is_success)
}
Err(e) => {
tracing::debug!(
endpoint_name = %endpoint.name(),
url = %url,
error = %e,
"Health check failed"
);
Ok(false)
}
}
}
/// Run health checks on all endpoints once
async fn run_health_checks(&self) {
let endpoints: Vec<ModelEndpoint> = {
let config = &self.config;
let mut all = Vec::new();
all.extend(config.models.fast.clone());
all.extend(config.models.balanced.clone());
all.extend(config.models.deep.clone());
all
};
for endpoint in endpoints {
match self.check_endpoint(&endpoint).await {
Ok(true) => {
// Endpoint is healthy
if let Err(e) = self.mark_success(endpoint.name()).await {
// Surface the failure via Prometheus metrics if available with labels
if let Some(ref app_metrics) = self.app_metrics {
app_metrics.health_tracking_failure(endpoint.name(), e.error_type());
}
match &e {
HealthError::HttpClientCreationFailed(msg) => {
// SYSTEMIC ERROR: HTTP client creation failed (TLS config, resource exhaustion).
// This error originates from check_endpoint() when reqwest::Client::builder().build()
// fails. It indicates a system-wide issue affecting ALL endpoints.
//
// Response: Log error, surface via metrics, continue checking other endpoints
// (they may also fail, but we want complete failure visibility).
tracing::error!(
endpoint_name = %endpoint.name(),
error = %msg,
"SYSTEMIC: HTTP client creation failed during health check. \
This indicates TLS configuration issues or resource exhaustion. \
Continuing to check other endpoints for complete visibility."
);
continue;
}
HealthError::UnknownEndpoint(name) => {
// TRANSIENT: Log and continue (may be config reload race)
tracing::error!(
endpoint_name = %endpoint.name(),
unknown_name = %name,
"Health tracking failed: unknown endpoint (may be config reload race)"
);
continue; // Skip to next endpoint instead of panicking
}
HealthError::InvalidEndpointUrl {
endpoint,
base_url,
details,
} => {
// CONFIGURATION ERROR: Log and continue
tracing::error!(
endpoint = %endpoint,
base_url = %base_url,
details = %details,
"Health tracking failed: invalid endpoint URL configuration"
);
continue; // Skip to next endpoint
}
}
}
}
Ok(false) => {
// Endpoint is unhealthy
if let Err(e) = self.mark_failure(endpoint.name()).await {
// Surface the failure via Prometheus metrics if available with labels
if let Some(ref app_metrics) = self.app_metrics {
app_metrics.health_tracking_failure(endpoint.name(), e.error_type());
}
match &e {
HealthError::HttpClientCreationFailed(msg) => {
// SYSTEMIC ERROR: HTTP client creation failed (TLS config, resource exhaustion).
// This error originates from check_endpoint() when reqwest::Client::builder().build()
// fails. It indicates a system-wide issue affecting ALL endpoints.
//
// Response: Log error, surface via metrics, continue checking other endpoints
// (they may also fail, but we want complete failure visibility).
tracing::error!(
endpoint_name = %endpoint.name(),
error = %msg,
"SYSTEMIC: HTTP client creation failed during health check. \
This indicates TLS configuration issues or resource exhaustion. \
Continuing to check other endpoints for complete visibility."
);
continue;
}
HealthError::UnknownEndpoint(name) => {
// TRANSIENT: Log and continue (may be config reload race)
tracing::error!(
endpoint_name = %endpoint.name(),
unknown_name = %name,
"Health tracking failed: unknown endpoint (may be config reload race)"
);
continue; // Skip to next endpoint instead of panicking
}
HealthError::InvalidEndpointUrl {
endpoint,
base_url,
details,
} => {
// CONFIGURATION ERROR: Log and continue
tracing::error!(
endpoint = %endpoint,
base_url = %base_url,
details = %details,
"Health tracking failed: invalid endpoint URL configuration"
);
continue; // Skip to next endpoint
}
}
}
}
Err(HealthError::HttpClientCreationFailed(msg)) => {
// SYSTEMIC ERROR: HTTP client creation failed
// This indicates TLS config error or resource exhaustion, but we should
// mark the endpoint unhealthy and continue checking other endpoints
// instead of panicking and crashing the server.
tracing::error!(
endpoint_name = %endpoint.name(),
error = %msg,
"HTTP client creation failed during endpoint health check. \
Marking endpoint unhealthy until resolved. This indicates a systemic issue \
(TLS config error, resource exhaustion). Continuing to check other endpoints."
);
// Mark endpoint as unhealthy
if let Err(e) = self.mark_failure(endpoint.name()).await {
tracing::error!(
endpoint_name = %endpoint.name(),
error = %e,
"Failed to mark endpoint unhealthy after HTTP client creation failure"
);
}
// Continue checking other endpoints
continue;
}
Err(e) => {
// Other errors (currently none, but future-proofing)
tracing::error!(
endpoint_name = %endpoint.name(),
error = %e,
"Unexpected error during health check"
);
}
}
}
// Record successful completion of health check cycle
self.metrics.record_successful_check().await;
}
/// Start background health checking task
///
/// Spawns a tokio task that runs health checks every 30 seconds.
/// Includes automatic restart logic with exponential backoff (max 5 attempts).
/// Updates HealthMetrics to enable external monitoring of the background task health.
///
/// The background task handle is stored internally and can be cancelled via `shutdown()`.
///
/// # Panics
///
/// Panics after 5 failed restart attempts, causing the server process to shut down.
/// This is intentional fail-fast behavior to prevent the server from continuing
/// to run without health monitoring, which would lead to endpoints never recovering
/// from failures. The server cannot operate safely in a degraded state without
/// health checks. Operator intervention is required to investigate the root cause
/// (typically TLS misconfiguration, resource exhaustion, or a critical bug in the
/// health check logic).
pub fn start_background_checks(self: Arc<Self>) {
let background_task = Arc::clone(&self.background_task);
let handle = tokio::spawn(async move {
let mut restart_count = 0;
loop {
let checker = Arc::clone(&self);
let handle = tokio::spawn(async move {
tracing::info!(
attempt = restart_count + 1,
"Starting background health checks (30s interval)"
);
loop {
tokio::time::sleep(Duration::from_secs(HEALTH_CHECK_INTERVAL_SECS)).await;
tracing::debug!("Running scheduled health checks");
checker.run_health_checks().await;
}
});
// Monitor the health check task to detect failures
match handle.await {
Ok(_) => {
// Task terminated normally (shouldn't happen - it's an infinite loop)
tracing::error!(
restart_count = restart_count,
"Background health check task terminated unexpectedly"
);
// Record failure in metrics for operator visibility
if let Some(ref app_metrics) = self.app_metrics {
app_metrics.background_task_failure("unexpected_termination");
}
}
Err(e) => {
// Task panicked
tracing::error!(
error = %e,
restart_count = restart_count,
"Background health check task panicked"
);
// Record failure in metrics for operator visibility
if let Some(ref app_metrics) = self.app_metrics {
app_metrics.background_task_failure("panic");
}
}
}
restart_count += 1;
if restart_count >= MAX_BACKGROUND_TASK_RESTARTS {
// Mark as permanently failed in metrics
self.metrics.mark_permanently_failed().await;
tracing::error!(
max_attempts = MAX_BACKGROUND_TASK_RESTARTS,
"FATAL: Background health check task failed {} times. \
Cannot operate safely without health monitoring. \
Panicking to shut down server and force operator intervention. \
Check TLS configuration, resource limits, DNS resolution, and logs. \
Process supervisor (systemd/Docker) should restart the server.",
MAX_BACKGROUND_TASK_RESTARTS
);
// FAIL FAST: Server cannot operate safely without health checks.
// The process supervisor (systemd/Docker/Kubernetes) should restart the server.
// This prevents silent degradation where the server appears healthy but
// cannot recover failed endpoints, leading to poor user experience.
panic!(
"Background health checking permanently failed after {} attempts. \
Server cannot operate without health monitoring. \
Operator intervention required - check TLS config, resource limits, DNS resolution.",
MAX_BACKGROUND_TASK_RESTARTS
);
}
// Record restart attempt in metrics
self.metrics.record_restart(restart_count).await;
// Exponential backoff: 1s, 2s, 4s, 8s, 16s
let backoff_seconds = 2_u64.pow(restart_count - 1);
tracing::warn!(
restart_count = restart_count,
backoff_seconds = backoff_seconds,
max_attempts = MAX_BACKGROUND_TASK_RESTARTS,
"Restarting background health check task after {}s backoff",
backoff_seconds
);
tokio::time::sleep(Duration::from_secs(backoff_seconds)).await;
}
});
// Store the background task handle for graceful shutdown
tokio::spawn(async move {
*background_task.lock().await = Some(handle);
});
}
/// Shutdown the background health checking task
///
/// Cancels the background health check task, allowing for graceful server shutdown.
/// This method should be called during server shutdown to prevent the background task
/// from preventing clean process termination.
///
/// # Example
/// ```no_run
/// # use std::sync::Arc;
/// # use octoroute::config::Config;
/// # use octoroute::models::HealthChecker;
/// # async fn example() {
/// # let config = Arc::new(Config::from_file("config.toml").unwrap());
/// let health_checker = Arc::new(HealthChecker::new(config));
/// health_checker.clone().start_background_checks();
/// // ... server runs ...
/// health_checker.shutdown().await;
/// # }
/// ```
pub async fn shutdown(&self) {
let mut task = self.background_task.lock().await;
if let Some(handle) = task.take() {
tracing::info!("Cancelling background health check task for graceful shutdown");
handle.abort();
// Wait for the task to finish aborting
let _ = handle.await;
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn create_test_config() -> Config {
// ModelEndpoint fields are private - use TOML deserialization
let toml = r#"
[server]
host = "127.0.0.1"
port = 3000
request_timeout_seconds = 30
[[models.fast]]
name = "fast-1"
base_url = "http://localhost:1234/v1"
max_tokens = 2048
temperature = 0.7
weight = 1.0
priority = 1
[[models.fast]]
name = "fast-2"
base_url = "http://localhost:1235/v1"
max_tokens = 2048
temperature = 0.7
weight = 1.0
priority = 1
[[models.balanced]]
name = "balanced-1"
base_url = "http://localhost:1236/v1"
max_tokens = 4096
temperature = 0.7
weight = 1.0
priority = 1
[[models.deep]]
name = "deep-1"
base_url = "http://localhost:1237/v1"
max_tokens = 8192
temperature = 0.7
weight = 1.0
priority = 1
[routing]
strategy = "rule"
default_importance = "normal"
router_tier = "balanced"
"#;
toml::from_str(toml).expect("should parse TOML config")
}
#[tokio::test]
async fn test_health_checker_new_initializes_all_healthy() {
let config = Arc::new(create_test_config());
let checker = HealthChecker::new(config);
// All endpoints should start as healthy
assert!(checker.is_healthy("fast-1").await);
assert!(checker.is_healthy("fast-2").await);
assert!(checker.is_healthy("balanced-1").await);
assert!(checker.is_healthy("deep-1").await);
}
#[tokio::test]
async fn test_health_checker_unknown_endpoint_is_unhealthy() {
let config = Arc::new(create_test_config());
let checker = HealthChecker::new(config);
// Unknown endpoint should be considered unhealthy
assert!(!checker.is_healthy("unknown-endpoint").await);
}
#[tokio::test]
async fn test_health_checker_mark_failure_tracks_consecutive() {
let config = Arc::new(create_test_config());
let checker = HealthChecker::new(config);
// Should still be healthy after 1-2 failures
checker.mark_failure("fast-1").await.unwrap();
assert!(checker.is_healthy("fast-1").await);
checker.mark_failure("fast-1").await.unwrap();
assert!(checker.is_healthy("fast-1").await);
// After 3rd consecutive failure, should be unhealthy
checker.mark_failure("fast-1").await.unwrap();
assert!(!checker.is_healthy("fast-1").await);
}
#[tokio::test]
async fn test_health_checker_mark_success_recovers() {
let config = Arc::new(create_test_config());
let checker = HealthChecker::new(config);
// Mark unhealthy with 3 failures
checker.mark_failure("fast-1").await.unwrap();
checker.mark_failure("fast-1").await.unwrap();
checker.mark_failure("fast-1").await.unwrap();
assert!(!checker.is_healthy("fast-1").await);
// One success should recover
checker.mark_success("fast-1").await.unwrap();
assert!(checker.is_healthy("fast-1").await);
// Consecutive failure count should be reset
let statuses = checker.get_all_statuses().await;
let fast1_status = statuses.iter().find(|s| s.name == "fast-1").unwrap();
assert_eq!(fast1_status.consecutive_failures, 0);
}
#[tokio::test]
async fn test_health_checker_get_all_statuses_returns_all_endpoints() {
let config = Arc::new(create_test_config());
let checker = HealthChecker::new(config);
let statuses = checker.get_all_statuses().await;
// Should have 4 endpoints total (2 fast + 1 balanced + 1 deep)
assert_eq!(statuses.len(), 4);
// Verify all endpoint names present
let names: Vec<String> = statuses.iter().map(|s| s.name.clone()).collect();
assert!(names.contains(&"fast-1".to_string()));
assert!(names.contains(&"fast-2".to_string()));
assert!(names.contains(&"balanced-1".to_string()));
assert!(names.contains(&"deep-1".to_string()));
}
#[tokio::test]
async fn test_health_checker_success_resets_partial_failures() {
let config = Arc::new(create_test_config());
let checker = HealthChecker::new(config);
// 2 failures (not enough to mark unhealthy)
checker.mark_failure("fast-1").await.unwrap();
checker.mark_failure("fast-1").await.unwrap();
// Success should reset counter
checker.mark_success("fast-1").await.unwrap();
// Should still be healthy and counter reset
assert!(checker.is_healthy("fast-1").await);
// Verify counter is actually reset by checking we need 3 more failures
checker.mark_failure("fast-1").await.unwrap();
checker.mark_failure("fast-1").await.unwrap();
assert!(checker.is_healthy("fast-1").await); // Still healthy after 2
checker.mark_failure("fast-1").await.unwrap();
assert!(!checker.is_healthy("fast-1").await); // Now unhealthy after 3rd
}
#[tokio::test]
async fn test_health_metrics_starts_in_running_state() {
let metrics = HealthMetrics::new();
assert_eq!(metrics.status().await, BackgroundTaskStatus::Running);
assert_eq!(metrics.restart_count().await, 0);
assert!(metrics.last_successful_check().await.is_none());
assert!(metrics.last_failure_time().await.is_none());
}
#[tokio::test]
async fn test_health_metrics_record_successful_check() {
let metrics = HealthMetrics::new();
metrics.record_successful_check().await;
assert_eq!(metrics.status().await, BackgroundTaskStatus::Running);
assert!(metrics.last_successful_check().await.is_some());
// Verify the timestamp is recent (within last second)
let last_check = metrics.last_successful_check().await.unwrap();
let elapsed = Instant::now().duration_since(last_check);
assert!(
elapsed < Duration::from_secs(1),
"Last check should be very recent"
);
}
#[tokio::test]
async fn test_health_metrics_record_restart() {
let metrics = HealthMetrics::new();
metrics.record_restart(1).await;
assert_eq!(metrics.status().await, BackgroundTaskStatus::Restarting);
assert_eq!(metrics.restart_count().await, 1);
assert!(metrics.last_failure_time().await.is_some());
// Record second restart
metrics.record_restart(2).await;
assert_eq!(metrics.restart_count().await, 2);
}
#[tokio::test]
async fn test_health_metrics_mark_permanently_failed() {
let metrics = HealthMetrics::new();
metrics.mark_permanently_failed().await;
assert_eq!(
metrics.status().await,
BackgroundTaskStatus::PermanentlyFailed
);
assert!(metrics.last_failure_time().await.is_some());
}
#[tokio::test]
async fn test_health_metrics_is_healthy_when_running() {
let metrics = HealthMetrics::new();
// Should be HEALTHY during startup warmup (background task is Running)
// This prevents false negatives that would cause load balancers to mark
// the server as DOWN during normal startup
assert!(metrics.is_background_task_healthy().await);
// Should remain healthy after successful check
metrics.record_successful_check().await;
assert!(metrics.is_background_task_healthy().await);
}
#[tokio::test]
async fn test_health_metrics_is_unhealthy_when_permanently_failed() {
let metrics = HealthMetrics::new();
metrics.mark_permanently_failed().await;
assert!(!metrics.is_background_task_healthy().await);
}
#[tokio::test]
async fn test_health_metrics_is_unhealthy_when_no_recent_checks() {
let metrics = HealthMetrics::new();
// Record a successful check
metrics.record_successful_check().await;
assert!(metrics.is_background_task_healthy().await);
}
#[tokio::test]
async fn test_health_metrics_restart_then_recover() {
let metrics = HealthMetrics::new();
// Simulate a restart
metrics.record_restart(1).await;
assert_eq!(metrics.status().await, BackgroundTaskStatus::Restarting);
// Simulate recovery with successful check
metrics.record_successful_check().await;
assert_eq!(metrics.status().await, BackgroundTaskStatus::Running);
assert!(metrics.is_background_task_healthy().await);
}
/// Test that health returns true during startup warmup when background task is Running
///
/// During startup, before the first health check completes, we should return true
/// if the background task is Running. This prevents false negatives that would cause:
/// - Load balancers marking server as DOWN during startup
/// - K8s readiness probes failing and preventing traffic
/// - Spurious monitoring alerts during deployments
///
/// Once the first check completes, we switch to staleness-based health verification.
#[tokio::test]
async fn test_health_returns_true_during_startup_warmup() {
let metrics = HealthMetrics::new();
// Initially, task is Running but no checks have completed yet
assert_eq!(metrics.status().await, BackgroundTaskStatus::Running);
// Should return true during startup warmup (background task is Running)
// This prevents false negatives before first health check completes
assert!(
metrics.is_background_task_healthy().await,
"Should return true during startup warmup when background task is Running"
);
}
}