23#include <unordered_map>
113 std::lock_guard<std::mutex> lock(
mutex_);
116 if (result.is_ok()) {
128 std::lock_guard<std::mutex> lock(
mutex_);
131 if (result.is_ok()) {
145 std::lock_guard<std::mutex> lock(
mutex_);
147 auto start_time = std::chrono::steady_clock::now();
149 auto end_time = std::chrono::steady_clock::now();
151 if (result.is_ok()) {
157 end_time - start_time);
176 std::lock_guard<std::mutex> lock(
mutex_);
186 return {
error_info{1,
"Health monitor is already running",
"health_monitor"}};
188 return ok(std::monostate{});
197 return {
error_info{1,
"Health monitor is not running",
"health_monitor"}};
199 return ok(std::monostate{});
214 std::lock_guard<std::mutex> lock(
mutex_);
216 auto start_time = std::chrono::steady_clock::now();
225 for (
const auto& name : nodes) {
227 if (result.is_ok()) {
237 auto end_time = std::chrono::steady_clock::now();
240 end_time - start_time);
250 std::lock_guard<std::mutex> lock(
mutex_);
259 std::lock_guard<std::mutex> lock(
mutex_);
268 std::lock_guard<std::mutex> lock(
mutex_);
270 std::ostringstream report;
271 report <<
"=== Health Report ===\n";
278 report <<
"\n--- Individual Checks ---\n";
281 report << name <<
": " <<
to_string(result.status);
282 if (!result.message.empty()) {
283 report <<
" - " << result.message;
296 std::lock_guard<std::mutex> lock(
mutex_);
318 [[nodiscard]]
bool has_check(
const std::string& name)
const {
319 std::lock_guard<std::mutex> lock(
mutex_);
328 std::lock_guard<std::mutex> lock(
mutex_);
Result type for error handling with member function support.
T value_or(T default_value) const
Get value or return default (C++23 std::expected compatible)
Manages dependencies between health checks as a DAG.
Result< bool > add_dependency(const std::string &dependent, const std::string &dependency)
Add a dependency between two nodes.
Result< bool > add_node(const std::string &name, std::shared_ptr< health_check > check)
Add a health check node to the graph.
Result< health_check_result > check_with_dependencies(const std::string &name)
Execute health check with its dependencies.
bool has_node(const std::string &name) const
Check if a node exists.
Result< bool > remove_node(const std::string &name)
Remove a health check node from the graph.
std::vector< std::string > get_all_nodes() const
Get all node names.
Central health monitoring system.
void attempt_recovery(const std::string &name)
std::vector< std::string > get_check_names() const
Get all registered check names.
health_monitor(const health_monitor &)=delete
std::atomic< bool > running_
health_dependency_graph graph_
health_monitor(health_monitor &&)=delete
health_monitor & operator=(health_monitor &&)=delete
health_monitor_stats get_stats() const
Get monitoring statistics.
bool has_check(const std::string &name) const
Check if a health check is registered.
health_monitor & operator=(const health_monitor &)=delete
bool is_running() const
Check if health monitoring is running.
Result< health_check_result > check(const std::string &name)
Execute a specific health check.
Result< bool > add_dependency(const std::string &dependent, const std::string &dependency)
Add a dependency between health checks.
VoidResult start()
Start the health monitoring.
std::string get_overall_status_string() const
Result< bool > unregister_check(const std::string &name)
Unregister a health check.
std::string get_health_report() const
Get a formatted health report.
health_monitor_stats stats_
void update_stats_after_check(const health_check_result &result)
void register_recovery_handler(const std::string &name, recovery_handler handler)
Register a recovery handler for a health check.
health_status get_overall_status() const
Get the overall health status.
Result< bool > register_check(const std::string &name, std::shared_ptr< health_check > check)
Register a health check.
std::unordered_map< std::string, health_check_result > last_results_
std::unordered_map< std::string, recovery_handler > recovery_handlers_
health_status get_overall_status_internal() const
VoidResult stop()
Stop the health monitoring.
void refresh()
Refresh all health checks.
Base classes and types for health checking functionality.
DAG-based health check dependency management.
health_status
Standard health status levels.
std::function< bool()> recovery_handler
Recovery handler function type.
health_monitor & global_health_monitor()
Get the global health monitor instance.
std::string to_string(log_level level)
Convert log level to string.
VoidResult ok()
Create a successful void result.
Standard error information used by Result<T>.
Result of a health check operation.
Statistics for health monitoring.
std::chrono::milliseconds last_check_duration
std::size_t successful_recoveries
std::size_t unknown_count
std::chrono::system_clock::time_point last_check_time
std::size_t unhealthy_count
std::size_t degraded_count
std::size_t check_executions
std::size_t recovery_attempts
std::size_t healthy_count