dmsc 0.1.9

Ri - A high-performance Rust middleware framework with modular architecture
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
//! Copyright © 2025-2026 Wenze Wei. All Rights Reserved.
//!
//! This file is part of Ri.
//! The Ri project belongs to the Dunimd Team.
//!
//! Licensed under the Apache License, Version 2.0 (the "License");
//! You may not use this file except in compliance with the License.
//! You may obtain a copy of the License at
//!
//!     http://www.apache.org/licenses/LICENSE-2.0
//!
//! Unless required by applicable law or agreed to in writing, software
//! distributed under the License is distributed on an "AS IS" BASIS,
//! WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//! See the License for the specific language governing permissions and
//! limitations under the License.

//! # Health Check Module
//! 
//! This module provides health checking functionality for the Ri service mesh. It allows
//! monitoring the health of services using various protocols and provides comprehensive
//! health status information.
//! 
//! ## Key Components
//! 
//! - **RiHealthCheckConfig**: Configuration for health checks
//! - **RiHealthCheckResult**: Result of a health check
//! - **RiHealthCheckType**: Supported health check types
//! - **RiHealthCheckProvider**: Trait for implementing health check providers
//! - **RiHttpHealthCheckProvider**: HTTP health check implementation
//! - **RiTcpHealthCheckProvider**: TCP health check implementation
//! - **RiHealthChecker**: Main health checking service
//! - **RiHealthStatus**: Health status enum
//! - **RiHealthSummary**: Summary of health check results
//! 
//! ## Design Principles
//! 
//! 1. **Protocol Agnostic**: Supports multiple health check protocols (HTTP, TCP, gRPC, custom)
//! 2. **Async-First**: All health check operations are asynchronous
//! 3. **Extensible**: Easy to implement new health check providers
//! 4. **Configurable**: Highly configurable health check parameters
//! 5. **Real-time Monitoring**: Background tasks for continuous health monitoring
//! 6. **Comprehensive Results**: Detailed health check results with response times and error messages
//! 7. **Health Summary**: Aggregated health status with success rates and average response times
//! 8. **Thread-safe**: Uses Arc and RwLock for safe concurrent access
//! 9. **Graceful Shutdown**: Proper cleanup of background tasks
//! 10. **Error Handling**: Comprehensive error handling with RiResult
//! 
//! ## Usage
//! 
//! ```rust
//! use ri::prelude::*;
//! use std::time::Duration;
//! 
//! async fn example() -> RiResult<()> {
//!     // Create a health checker with 30-second intervals
//!     let health_checker = RiHealthChecker::new(Duration::from_secs(30));
//!     
//!     // Register a health check for a service
//!     let config = RiHealthCheckConfig {
//!         endpoint: "/health".to_string(),
//!         method: "GET".to_string(),
//!         timeout: Duration::from_secs(5),
//!         expected_status_code: 200,
//!         expected_response_body: None,
//!         headers: FxHashMap::default(),
//!     };
//!     
//!     health_checker.register_health_check(
//!         "example-service",
//!         "http://localhost:8080",
//!         RiHealthCheckType::Http,
//!         config
//!     ).await?;
//!     
//!     // Start background health checks
//!     health_checker.start_health_check("example-service", "http://localhost:8080").await?;
//!     
//!     // Get health summary
//!     let summary = health_checker.get_service_health_summary("example-service").await?;
//!     println!("Service health: {:?}", summary.overall_status);
//!     println!("Success rate: {:.2}%", summary.success_rate);
//!     
//!     Ok(())
//! }
//! ```

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap as FxHashMap;
use std::sync::Arc;
use std::time::{Duration, SystemTime};
use tokio::sync::RwLock;
use tokio::task::JoinHandle;

#[cfg(feature = "pyo3")]
use pyo3::PyResult;
#[cfg(feature = "service_mesh")]
use hyper;

use crate::core::{RiResult, RiError};
use crate::observability::{RiTracer, RiSpanKind, RiSpanStatus};

/// Configuration for health checks.
///
/// This struct defines the parameters for performing health checks, including
/// endpoint, HTTP method, timeout, expected status code, and custom headers.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RiHealthCheckConfig {
    /// Health check endpoint path
    pub endpoint: String,
    /// HTTP method to use for health checks
    pub method: String,
    /// Timeout for health check requests
    pub timeout: Duration,
    /// Expected HTTP status code for a healthy service
    pub expected_status_code: u16,
    /// Optional expected response body for validation
    pub expected_response_body: Option<String>,
    /// Custom headers to include in health check requests
    pub headers: FxHashMap<String, String>,
}

impl Default for RiHealthCheckConfig {
    /// Creates a default health check configuration.
    ///
    /// # Returns
    ///
    /// A `RiHealthCheckConfig` instance with default values
    fn default() -> Self {
        Self {
            endpoint: "/health".to_string(),
            method: "GET".to_string(),
            timeout: Duration::from_secs(5),
            expected_status_code: 200,
            expected_response_body: None,
            headers: FxHashMap::default(),
        }
    }
}

/// Result of a health check operation.
///
/// This struct contains detailed information about the result of a health check,
/// including whether the service is healthy, response time, and error messages if any.
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
#[derive(Debug, Clone)]
pub struct RiHealthCheckResult {
    /// Name of the service being checked
    pub service_name: String,
    /// Endpoint used for the health check
    pub endpoint: String,
    /// Whether the service is considered healthy
    pub is_healthy: bool,
    /// HTTP status code received (if applicable)
    pub status_code: Option<u16>,
    /// Time taken to perform the health check
    pub response_time: Duration,
    /// Error message if the health check failed
    pub error_message: Option<String>,
    /// Timestamp when the health check was performed
    pub timestamp: SystemTime,
}

#[cfg(feature = "pyo3")]
#[pyo3::prelude::pymethods]
impl RiHealthCheckResult {
    fn get_service_name(&self) -> String {
        self.service_name.clone()
    }
    
    fn get_endpoint(&self) -> String {
        self.endpoint.clone()
    }
    
    fn get_is_healthy(&self) -> bool {
        self.is_healthy
    }
    
    fn get_status_code(&self) -> Option<u16> {
        self.status_code
    }
    
    fn get_response_time_ms(&self) -> u64 {
        self.response_time.as_millis() as u64
    }
    
    fn get_error_message(&self) -> Option<String> {
        self.error_message.clone()
    }
}

/// Types of health checks supported.
///
/// This enum defines the different protocols that can be used for health checking.
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum RiHealthCheckType {
    /// HTTP health check
    Http,
    /// TCP health check
    Tcp,
    /// gRPC health check
    Grpc,
    /// Custom health check implementation
    Custom,
}

/// Trait for implementing health check providers.
///
/// This trait defines the interface for health check providers, allowing for
/// different health check implementations based on protocol.
#[async_trait]
pub trait RiHealthCheckProvider: Send + Sync {
    /// Performs a health check on the specified endpoint.
    ///
    /// # Parameters
    ///
    /// - `endpoint`: The endpoint to check
    /// - `config`: Health check configuration
    ///
    /// # Returns
    ///
    /// A `RiResult<RiHealthCheckResult>` containing the health check result
    async fn check_health(&self, endpoint: &str, config: &RiHealthCheckConfig) -> RiResult<RiHealthCheckResult>;
}

/// HTTP health check provider.
///
/// This struct implements the `RiHealthCheckProvider` trait for HTTP health checks.
pub struct RiHttpHealthCheckProvider;

#[async_trait]
impl RiHealthCheckProvider for RiHttpHealthCheckProvider {
    /// Performs an HTTP health check on the specified endpoint.
    ///
    /// # Parameters
    ///
    /// - `endpoint`: The HTTP endpoint to check
    /// - `config`: Health check configuration
    ///
    /// # Returns
    ///
    /// A `RiResult<RiHealthCheckResult>` containing the health check result
    #[cfg(feature = "service_mesh")]
    async fn check_health(&self, endpoint: &str, _config: &RiHealthCheckConfig) -> RiResult<RiHealthCheckResult> {
        let start_time = SystemTime::now();
        
        let client = hyper::Client::new();

        let uri: hyper::Uri = endpoint.parse()
            .map_err(|e| RiError::ServiceMesh(format!("Invalid URI: {e}")))?;

        let req = hyper::Request::builder()
            .method(_config.method.as_str())
            .uri(uri)
            .body(hyper::Body::empty())
            .map_err(|e| RiError::ServiceMesh(format!("Failed to build request: {e}")))?;

        match client.request(req).await {
            Ok(response) => {
                let status_code = response.status().as_u16();
                let is_healthy = status_code == _config.expected_status_code;
                let response_time = SystemTime::now().duration_since(start_time)
                    .unwrap_or(Duration::from_secs(0));

                let error_message = if !is_healthy {
                    Some(format!("Expected status code {}, got {}", _config.expected_status_code, status_code))
                } else {
                    None
                };

                Ok(RiHealthCheckResult {
                    service_name: "unknown".to_string(),
                    endpoint: endpoint.to_string(),
                    is_healthy,
                    status_code: Some(status_code),
                    response_time,
                    error_message,
                    timestamp: SystemTime::now(),
                })
            }
            Err(e) => {
                let response_time = SystemTime::now().duration_since(start_time)
                    .unwrap_or(Duration::from_secs(0));

                Ok(RiHealthCheckResult {
                    service_name: "unknown".to_string(),
                    endpoint: endpoint.to_string(),
                    is_healthy: false,
                    status_code: None,
                    response_time,
                    error_message: Some(e.to_string()),
                    timestamp: SystemTime::now(),
                })
            }
        }
    }
    
    #[cfg(not(feature = "service_mesh"))]
    async fn check_health(&self, endpoint: &str, _config: &RiHealthCheckConfig) -> RiResult<RiHealthCheckResult> {
        // If service_mesh feature is not enabled, assume all endpoints are healthy
        Ok(RiHealthCheckResult {
            service_name: "unknown".to_string(),
            endpoint: endpoint.to_string(),
            is_healthy: true,
            status_code: Some(_config.expected_status_code),
            response_time: Duration::from_secs(0),
            error_message: None,
            timestamp: SystemTime::now(),
        })
    }
}

/// TCP health check provider.
///
/// This struct implements the `RiHealthCheckProvider` trait for TCP health checks.
pub struct RiTcpHealthCheckProvider;

#[async_trait]
impl RiHealthCheckProvider for RiTcpHealthCheckProvider {
    /// Performs a TCP health check on the specified endpoint.
    ///
    /// # Parameters
    ///
    /// - `endpoint`: The TCP endpoint to check (format: "host:port")
    /// - `config`: Health check configuration
    ///
    /// # Returns
    ///
    /// A `RiResult<RiHealthCheckResult>` containing the health check result
    async fn check_health(&self, endpoint: &str, _config: &RiHealthCheckConfig) -> RiResult<RiHealthCheckResult> {
        let start_time = SystemTime::now();
        
        match tokio::net::TcpStream::connect(endpoint).await {
            Ok(_) => {
                let response_time = SystemTime::now().duration_since(start_time)
                    .unwrap_or(Duration::from_secs(0));

                Ok(RiHealthCheckResult {
                    service_name: "unknown".to_string(),
                    endpoint: endpoint.to_string(),
                    is_healthy: true,
                    status_code: None,
                    response_time,
                    error_message: None,
                    timestamp: SystemTime::now(),
                })
            }
            Err(e) => {
                let response_time = SystemTime::now().duration_since(start_time)
                    .unwrap_or(Duration::from_secs(0));

                Ok(RiHealthCheckResult {
                    service_name: "unknown".to_string(),
                    endpoint: endpoint.to_string(),
                    is_healthy: false,
                    status_code: None,
                    response_time,
                    error_message: Some(e.to_string()),
                    timestamp: SystemTime::now(),
                })
            }
        }
    }
}

/// gRPC health check provider.
///
/// This struct implements the `RiHealthCheckProvider` trait for gRPC health checks.
pub struct RiGrpcHealthCheckProvider;

#[async_trait]
impl RiHealthCheckProvider for RiGrpcHealthCheckProvider {
    /// Performs a gRPC health check on the specified endpoint.
    ///
    /// # Parameters
    ///
    /// - `endpoint`: The gRPC endpoint to check (format: "host:port")
    /// - `config`: Health check configuration
    ///
    /// # Returns
    ///
    /// A `RiResult<RiHealthCheckResult>` containing the health check result
    async fn check_health(&self, endpoint: &str, _config: &RiHealthCheckConfig) -> RiResult<RiHealthCheckResult> {
        let start_time = SystemTime::now();
        
        // Simple gRPC health check implementation using TCP connection
        // In a full implementation, this would use the gRPC health check service
        match tokio::net::TcpStream::connect(endpoint).await {
            Ok(_) => {
                let response_time = SystemTime::now().duration_since(start_time)
                    .unwrap_or(Duration::from_secs(0));

                Ok(RiHealthCheckResult {
                    service_name: "unknown".to_string(),
                    endpoint: endpoint.to_string(),
                    is_healthy: true,
                    status_code: None,
                    response_time,
                    error_message: None,
                    timestamp: SystemTime::now(),
                })
            }
            Err(e) => {
                let response_time = SystemTime::now().duration_since(start_time)
                    .unwrap_or(Duration::from_secs(0));

                Ok(RiHealthCheckResult {
                    service_name: "unknown".to_string(),
                    endpoint: endpoint.to_string(),
                    is_healthy: false,
                    status_code: None,
                    response_time,
                    error_message: Some(e.to_string()),
                    timestamp: SystemTime::now(),
                })
            }
        }
    }
}

/// Main health checker service.
///
/// This struct provides the core functionality for managing health checks, including
/// registering health checks, starting background monitoring, and retrieving health status.
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
pub struct RiHealthChecker {
    check_interval: Duration,
    providers: Arc<RwLock<FxHashMap<RiHealthCheckType, Box<dyn RiHealthCheckProvider>>>>,
    check_results: Arc<RwLock<FxHashMap<String, Vec<RiHealthCheckResult>>>>,
    background_tasks: Arc<RwLock<Vec<JoinHandle<()>>>>,
    tracer: Option<Arc<RiTracer>>,
}

impl RiHealthChecker {
    pub fn new(check_interval: Duration) -> Self {
        let mut providers: FxHashMap<RiHealthCheckType, Box<dyn RiHealthCheckProvider>> = FxHashMap::default();
        providers.insert(RiHealthCheckType::Http, Box::new(RiHttpHealthCheckProvider));
        providers.insert(RiHealthCheckType::Tcp, Box::new(RiTcpHealthCheckProvider));
        providers.insert(RiHealthCheckType::Grpc, Box::new(RiGrpcHealthCheckProvider));

        Self {
            check_interval,
            providers: Arc::new(RwLock::new(providers)),
            check_results: Arc::new(RwLock::new(FxHashMap::default())),
            background_tasks: Arc::new(RwLock::new(Vec::new())),
            tracer: None,
        }
    }
    
    pub fn with_tracer(mut self, tracer: Arc<RiTracer>) -> Self {
        self.tracer = Some(tracer);
        self
    }
    
    pub fn set_tracer(&mut self, tracer: Arc<RiTracer>) {
        self.tracer = Some(tracer);
    }
    
    /// Validates an endpoint URL to prevent SSRF attacks.
    ///
    /// # Security
    ///
    /// This method validates:
    /// 1. URL scheme must be HTTP or HTTPS
    /// 2. URL must be well-formed
    /// 3. Warns if URL points to private IP address
    /// 4. Blocks file://, gopher://, and other dangerous schemes
    fn validate_endpoint_url(endpoint: &str) -> RiResult<()> {
        // Check URL length
        if endpoint.is_empty() || endpoint.len() > 2048 {
            return Err(RiError::ServiceMesh(
                "Endpoint URL must be 1-2048 characters".to_string()
            ));
        }

        // Parse and validate URL
        let parsed_url = url::Url::parse(endpoint)
            .map_err(|e| RiError::ServiceMesh(format!("Invalid endpoint URL: {}", e)))?;

        // Only allow HTTP and HTTPS schemes
        let scheme = parsed_url.scheme();
        if scheme != "http" && scheme != "https" {
            log::warn!(
                "[Ri.HealthCheck] Blocked non-HTTP(S) endpoint: scheme={} url={}",
                scheme, endpoint
            );
            return Err(RiError::ServiceMesh(
                format!("Invalid URL scheme '{}'. Only HTTP and HTTPS are allowed for health checks.", scheme)
            ));
        }

        // Check for private IP addresses (informational warning)
        if let Some(host) = parsed_url.host_str() {
            // Check for localhost
            if host == "localhost" || host == "127.0.0.1" || host == "::1" {
                log::warn!(
                    "[Ri.HealthCheck] Health check endpoint points to localhost: {}",
                    endpoint
                );
            }
            
            // Check for private IP ranges
            if let Ok(ip) = host.parse::<std::net::IpAddr>() {
                let is_private_or_link_local = match ip {
                    std::net::IpAddr::V4(ipv4) => ipv4.is_private() || ipv4.is_link_local(),
                    std::net::IpAddr::V6(ipv6) => ipv6.is_unicast_link_local(),
                };
                if ip.is_loopback() || is_private_or_link_local {
                    log::warn!(
                        "[Ri.HealthCheck] Health check endpoint points to private IP {}: {}",
                        ip, endpoint
                    );
                }
            }
        }

        // Check for credentials in URL
        if parsed_url.username() != "" || parsed_url.password().is_some() {
            log::warn!(
                "[Ri.HealthCheck] Health check endpoint URL contains credentials: {}",
                endpoint.split('@').last().unwrap_or(endpoint)
            );
        }

        Ok(())
    }


    /// Registers a health check for a service.
    ///
    /// This method registers a health check for a service and performs an immediate check.
    ///
    /// # Parameters
    ///
    /// - `service_name`: Name of the service to check
    /// - `endpoint`: Endpoint URL for health checks
    /// - `check_type`: Type of health check to perform
    /// - `config`: Health check configuration
    ///
    /// # Returns
    ///
    /// A `RiResult<()>` indicating success or failure
    ///
    /// # Security
    ///
    /// This method validates the endpoint URL to prevent SSRF attacks:
    /// - Only HTTP and HTTPS schemes are allowed
    /// - Private IP addresses are logged as warnings
    /// - File://, gopher://, and other schemes are blocked
    pub async fn register_health_check(
        &self,
        service_name: &str,
        endpoint: &str,
        check_type: RiHealthCheckType,
        config: RiHealthCheckConfig,
    ) -> RiResult<()> {
        // Security: Validate endpoint URL to prevent SSRF
        Self::validate_endpoint_url(endpoint)?;
        
        let span_id = if let Some(tracer) = &self.tracer {
            let span_id = tracer.start_span_from_context(
                format!("health_check:{}", service_name),
                RiSpanKind::Internal,
            );
            if let Some(ref sid) = span_id {
                let _ = tracer.span_mut(sid, |span| {
                    span.set_attribute("service_name".to_string(), service_name.to_string());
                    span.set_attribute("endpoint".to_string(), endpoint.to_string());
                    span.set_attribute("check_type".to_string(), format!("{:?}", check_type));
                });
            }
            span_id
        } else {
            None
        };

        let result = self.register_health_check_internal(service_name, endpoint, check_type, config).await;

        if let (Some(tracer), Some(sid)) = (&self.tracer, span_id) {
            let status = match &result {
                Ok(_) => RiSpanStatus::Ok,
                Err(e) => RiSpanStatus::Error(e.to_string()),
            };
            let _ = tracer.end_span(&sid, status);
        }

        result
    }
    
    async fn register_health_check_internal(
        &self,
        service_name: &str,
        endpoint: &str,
        check_type: RiHealthCheckType,
        config: RiHealthCheckConfig,
    ) -> RiResult<()> {
        let providers = self.providers.read().await;
        let provider = providers.get(&check_type)
            .ok_or_else(|| RiError::ServiceMesh(format!("Health check provider for {check_type:?} not found")))?;

        let result = provider.check_health(endpoint, &config).await?;
        
        let mut check_results = self.check_results.write().await;
        let service_results = check_results.entry(service_name.to_string())
            .or_insert_with(Vec::new);
        service_results.push(result);

        Ok(())
    }

    /// Starts background health checks for a service.
    /// 
    /// This method creates a background task that periodically checks the health of a service.
    /// 
    /// # Parameters
    /// 
    /// - `service_name`: Name of the service to check
    /// - `endpoint`: Endpoint URL for health checks
    /// 
    /// # Returns
    /// 
    /// A `RiResult<()>` indicating success or failure
    pub async fn start_health_check(&self, service_name: &str, endpoint: &str) -> RiResult<()> {
        let mut tasks = self.background_tasks.write().await;
        
        let service_name_clone = service_name.to_string();
        let endpoint_clone = endpoint.to_string();
        let check_interval = self.check_interval;
        let providers = Arc::clone(&self.providers);
        let check_results = Arc::clone(&self.check_results);

        // Determine health check type based on endpoint URL scheme
        let check_type = if endpoint.starts_with("grpc://") || endpoint.starts_with("grpcs://") {
            RiHealthCheckType::Grpc
        } else if endpoint.starts_with("http://") || endpoint.starts_with("https://") {
            RiHealthCheckType::Http
        } else {
            // Assume TCP for other protocols
            RiHealthCheckType::Tcp
        };

        let task = tokio::spawn(async move {
            let mut interval = tokio::time::interval(check_interval);
            let config = RiHealthCheckConfig::default();
            
            loop {
                interval.tick().await;
                
                let providers_guard = providers.read().await;
                if let Some(provider) = providers_guard.get(&check_type) {
                    match provider.check_health(&endpoint_clone, &config).await {
                        Ok(result) => {
                            let mut results = check_results.write().await;
                            let service_results = results.entry(service_name_clone.clone())
                                .or_insert_with(Vec::new);
                            
                            // Add new result to the end
                            service_results.push(result);
                            
                            // Keep only the most recent 100 results per service to avoid memory issues
                            if service_results.len() > 100 {
                                service_results.drain(0..service_results.len() - 100);
                            }
                        }
                        Err(e) => {
                            log::warn!("Health check failed for {endpoint_clone}: {e}");
                        }
                    }
                }
            }
        });

        tasks.push(task);
        Ok(())
    }
    
    /// Stops health checks for a specific service endpoint.
    /// 
    /// This method clears the health check results for the specified service.
    /// The background task will continue running but will no longer record results.
    /// 
    /// # Parameters
    /// 
    /// - `service_name`: Name of the service
    /// - `endpoint`: Endpoint URL
    /// 
    /// # Returns
    /// 
    /// A `RiResult<()>` indicating success or failure
    pub async fn stop_health_check(&self, service_name: &str, _endpoint: &str) -> RiResult<()> {
        let mut results = self.check_results.write().await;
        results.remove(service_name);
        Ok(())
    }
    
    /// Starts background health checks for a service with a specific health check type.
    /// 
    /// This method creates a background task that periodically checks the health of a service
    /// using the specified health check type.
    /// 
    /// # Parameters
    /// 
    /// - `service_name`: Name of the service to check
    /// - `endpoint`: Endpoint URL for health checks
    /// - `check_type`: Type of health check to perform
    /// 
    /// # Returns
    /// 
    /// A `RiResult<()>` indicating success or failure
    pub async fn start_health_check_with_type(
        &self, 
        service_name: &str, 
        endpoint: &str,
        check_type: RiHealthCheckType
    ) -> RiResult<()> {
        let mut tasks = self.background_tasks.write().await;
        
        let service_name_clone = service_name.to_string();
        let endpoint_clone = endpoint.to_string();
        let check_interval = self.check_interval;
        let providers = Arc::clone(&self.providers);
        let check_results = Arc::clone(&self.check_results);
        let check_type_clone = check_type;

        let task = tokio::spawn(async move {
            let mut interval = tokio::time::interval(check_interval);
            let config = RiHealthCheckConfig::default();
            
            loop {
                interval.tick().await;
                
                let providers_guard = providers.read().await;
                if let Some(provider) = providers_guard.get(&check_type_clone) {
                    match provider.check_health(&endpoint_clone, &config).await {
                        Ok(result) => {
                            let mut results = check_results.write().await;
                            let service_results = results.entry(service_name_clone.clone())
                                .or_insert_with(Vec::new);
                            
                            // Add new result to the end
                            service_results.push(result);
                            
                            // Keep only the most recent 100 results per service to avoid memory issues
                            if service_results.len() > 100 {
                                service_results.drain(0..service_results.len() - 100);
                            }
                        }
                        Err(e) => {
                            log::warn!("Health check failed for {endpoint_clone}: {e}");
                        }
                    }
                }
            }
        });

        tasks.push(task);
        Ok(())
    }

    /// Gets the health check results for a service.
    ///
    /// # Parameters
    ///
    /// - `service_name`: Name of the service to get results for
    ///
    /// # Returns
    ///
    /// A `RiResult<Vec<RiHealthCheckResult>>` containing the health check results
    pub async fn get_health_status(&self, service_name: &str) -> RiResult<Vec<RiHealthCheckResult>> {
        let check_results = self.check_results.read().await;
        let results = check_results.get(service_name)
            .cloned()
            .unwrap_or_default();

        Ok(results)
    }
    
    /// Gets the latest health check result for a service.
    ///
    /// # Parameters
    ///
    /// - `service_name`: Name of the service to get the latest result for
    ///
    /// # Returns
    ///
    /// A `RiResult<Option<RiHealthCheckResult>>` containing the latest health check result if available
    pub async fn get_latest_health_status(&self, service_name: &str) -> RiResult<Option<RiHealthCheckResult>> {
        let check_results = self.check_results.read().await;
        let latest_result = check_results.get(service_name)
            .and_then(|results| results.last().cloned());

        Ok(latest_result)
    }
    
    /// Gets the health check results for a service within a specified time window.
    ///
    /// # Parameters
    ///
    /// - `service_name`: Name of the service to get results for
    /// - `time_window`: Time window to filter results by
    ///
    /// # Returns
    ///
    /// A `RiResult<Vec<RiHealthCheckResult>>` containing the filtered health check results
    pub async fn get_health_status_within(&self, service_name: &str, time_window: Duration) -> RiResult<Vec<RiHealthCheckResult>> {
        let check_results = self.check_results.read().await;
        let now = SystemTime::now();
        
        let results = check_results.get(service_name)
            .map(|results| {
                results.iter()
                    .filter(|r| {
                        if let Ok(elapsed) = now.duration_since(r.timestamp) {
                            elapsed <= time_window
                        } else {
                            false
                        }
                    })
                    .cloned()
                    .collect()
            })
            .unwrap_or_default();

        Ok(results)
    }

    /// Gets a health summary for a service.
    ///
    /// This method aggregates health check results to provide a summary of the service's health,
    /// including success rate, average response time, and overall status.
    ///
    /// # Parameters
    ///
    /// - `service_name`: Name of the service to get a summary for
    ///
    /// # Returns
    ///
    /// A `RiResult<RiHealthSummary>` containing the health summary
    pub async fn get_service_health_summary(&self, service_name: &str) -> RiResult<RiHealthSummary> {
        let results = self.get_health_status(service_name).await?;
        
        if results.is_empty() {
            return Ok(RiHealthSummary {
                service_name: service_name.to_string(),
                total_checks: 0,
                healthy_checks: 0,
                unhealthy_checks: 0,
                success_rate: 0.0,
                average_response_time: Duration::from_secs(0),
                last_check_time: None,
                overall_status: RiHealthStatus::Unknown,
            });
        }

        let total_checks = results.len();
        let healthy_checks = results.iter().filter(|r| r.is_healthy).count();
        let unhealthy_checks = total_checks - healthy_checks;
        let success_rate = (healthy_checks as f64) / (total_checks as f64) * 100.0;

        let total_response_time: Duration = results.iter()
            .map(|r| r.response_time)
            .sum();
        let average_response_time = total_response_time / total_checks as u32;

        let last_check_time = results.last().map(|r| r.timestamp);

        let overall_status = if success_rate >= 80.0 {
            RiHealthStatus::Healthy
        } else if success_rate >= 50.0 {
            RiHealthStatus::Degraded
        } else {
            RiHealthStatus::Unhealthy
        };

        Ok(RiHealthSummary {
            service_name: service_name.to_string(),
            total_checks,
            healthy_checks,
            unhealthy_checks,
            success_rate,
            average_response_time,
            last_check_time,
            overall_status,
        })
    }

    /// Starts background health check tasks.
    ///
    /// This method initializes and starts all background health monitoring tasks,
    /// including periodic health checks for registered services and cleanup tasks.
    ///
    /// # Returns
    ///
    /// A `RiResult<()>` indicating success or failure
    pub async fn start_background_tasks(&self) -> RiResult<()> {
        // Start periodic cleanup task to remove old health check results
        let check_results = Arc::clone(&self.check_results);
        let cleanup_interval = self.check_interval * 10; // Cleanup every 10 check intervals
        
        let cleanup_task = tokio::spawn(async move {
            let mut interval = tokio::time::interval(cleanup_interval);
            
            loop {
                interval.tick().await;
                
                let mut results = check_results.write().await;
                let now = SystemTime::now();
                let max_age = Duration::from_secs(3600); // Keep results for 1 hour
                
                // Remove health check results older than max_age
                for service_results in results.values_mut() {
                    service_results.retain(|result| {
                        now.duration_since(result.timestamp)
                            .map(|age| age < max_age)
                            .unwrap_or(false)
                    });
                }
                
                // Remove services with no recent results
                results.retain(|_, results| !results.is_empty());
            }
        });
        
        // Store cleanup task
        let mut tasks = self.background_tasks.write().await;
        tasks.push(cleanup_task);
        
        log::info!("Background health check tasks started successfully");
        Ok(())
    }

    /// Stops all background health check tasks.
    ///
    /// This method aborts all running background health check tasks and cleans up resources.
    ///
    /// # Returns
    ///
    /// A `RiResult<()>` indicating success or failure
    pub async fn stop_background_tasks(&self) -> RiResult<()> {
        let mut tasks = self.background_tasks.write().await;
        for task in tasks.drain(..) {
            task.abort();
        }
        Ok(())
    }

    /// Performs a health check on the health checker itself.
    ///
    /// # Returns
    ///
    /// A `RiResult<bool>` indicating whether the health checker is healthy
    pub async fn health_check(&self) -> RiResult<bool> {
        Ok(true)
    }
}

#[cfg(feature = "pyo3")]
/// Python bindings for RiHealthChecker
#[pyo3::prelude::pymethods]
impl RiHealthChecker {
    #[new]
    fn py_new(check_interval: u64) -> PyResult<Self> {
        Ok(Self::new(Duration::from_secs(check_interval)))
    }
    
    /// Get service health summary from Python
    #[pyo3(name = "get_service_health_summary")]
    fn get_service_health_summary_impl(&self, service_name: String) -> PyResult<RiHealthSummary> {
        let rt = tokio::runtime::Runtime::new().map_err(|e| {
            pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to create runtime: {}", e))
        })?;
        
        rt.block_on(async {
            self.get_service_health_summary(&service_name)
                .await
                .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to get health summary: {e}")))
        })
    }
    
    /// Start health check from Python
    #[pyo3(name = "start_health_check")]
    fn start_health_check_impl(&self, service_name: String, endpoint: String) -> PyResult<()> {
        let rt = tokio::runtime::Runtime::new().map_err(|e| {
            pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to create runtime: {}", e))
        })?;
        
        rt.block_on(async {
            self.start_health_check(&service_name, &endpoint)
                .await
                .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to start health check: {e}")))
        })
    }
    
    /// Stop health check from Python
    #[pyo3(name = "stop_health_check")]
    fn stop_health_check_impl(&self, service_name: String, endpoint: String) -> PyResult<()> {
        let rt = tokio::runtime::Runtime::new().map_err(|e| {
            pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to create runtime: {}", e))
        })?;
        
        rt.block_on(async {
            self.stop_health_check(&service_name, &endpoint)
                .await
                .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to stop health check: {e}")))
        })
    }
    
    /// Get health status from Python
    #[pyo3(name = "get_health_status")]
    fn get_health_status_impl(&self, service_name: String) -> PyResult<Vec<RiHealthCheckResult>> {
        let rt = tokio::runtime::Runtime::new().map_err(|e| {
            pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to create runtime: {}", e))
        })?;
        
        rt.block_on(async {
            self.get_health_status(&service_name)
                .await
                .map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(format!("Failed to get health status: {e}")))
        })
    }
}

/// Health status enum.
///
/// This enum represents the overall health status of a service.
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
#[derive(Debug, Clone)]
pub enum RiHealthStatus {
    /// Service is healthy
    Healthy,
    /// Service is degraded but still functional
    Degraded,
    /// Service is unhealthy
    Unhealthy,
    /// Health status is unknown
    Unknown,
}

/// Summary of health check results.
///
/// This struct provides an aggregated view of a service's health, including
/// total checks, success rate, average response time, and overall status.
#[cfg_attr(feature = "pyo3", pyo3::prelude::pyclass)]
#[derive(Debug, Clone)]
pub struct RiHealthSummary {
    /// Name of the service
    pub service_name: String,
    /// Total number of health checks performed
    pub total_checks: usize,
    /// Number of successful health checks
    pub healthy_checks: usize,
    /// Number of failed health checks
    pub unhealthy_checks: usize,
    /// Success rate percentage (0.0 to 100.0)
    pub success_rate: f64,
    /// Average response time for health checks
    pub average_response_time: Duration,
    /// Timestamp of the last health check
    pub last_check_time: Option<SystemTime>,
    /// Overall health status
    pub overall_status: RiHealthStatus,
}

#[cfg(feature = "pyo3")]
#[pyo3::prelude::pymethods]
impl RiHealthSummary {
    fn get_service_name(&self) -> String {
        self.service_name.clone()
    }
    
    fn get_total_checks(&self) -> usize {
        self.total_checks
    }
    
    fn get_healthy_checks(&self) -> usize {
        self.healthy_checks
    }
    
    fn get_unhealthy_checks(&self) -> usize {
        self.unhealthy_checks
    }
    
    fn get_success_rate(&self) -> f64 {
        self.success_rate
    }
    
    fn get_average_response_time_ms(&self) -> u64 {
        self.average_response_time.as_millis() as u64
    }
    
    fn get_overall_status(&self) -> String {
        match self.overall_status {
            RiHealthStatus::Healthy => "Healthy".to_string(),
            RiHealthStatus::Degraded => "Degraded".to_string(),
            RiHealthStatus::Unhealthy => "Unhealthy".to_string(),
            RiHealthStatus::Unknown => "Unknown".to_string(),
        }
    }
}