Demonstrates the health check interface for service monitoring.
Demonstrates the health check interface for service monitoring.Shows implementing custom health checks for liveness, readiness, and dependency probes, with type conversion utilities.
#include <chrono>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
{
public:
std::string
get_name()
const override {
return "app_liveness"; }
{
result.
status = health_status::healthy;
result.
message =
"Application is running";
return result;
}
};
{
public:
std::string
get_name()
const override {
return "cache_readiness"; }
{
return std::chrono::milliseconds(2000);
}
{
{
result.
status = health_status::healthy;
result.
message =
"Cache is warmed";
}
else
{
result.
status = health_status::degraded;
result.
message =
"Cache warming in progress";
}
return result;
}
private:
};
{
public:
std::string
get_name()
const override {
return "database"; }
{
{
result.
status = health_status::healthy;
result.
message =
"Database connected";
}
else
{
result.
status = health_status::unhealthy;
result.
message =
"Database connection failed";
}
return result;
}
private:
};
{
switch (status)
{
case health_status::healthy:
return "HEALTHY";
case health_status::degraded:
return "DEGRADED";
case health_status::unhealthy:
return "UNHEALTHY";
default:
return "UNKNOWN";
}
}
{
std::cout << "=== Health Check Example ===\n\n";
std::cout << "1. Health check types:\n";
std::cout <<
" liveness -> \"" <<
to_string(health_check_type::liveness) <<
"\"\n";
std::cout <<
" readiness -> \"" <<
to_string(health_check_type::readiness) <<
"\"\n";
std::cout <<
" dependency -> \"" <<
to_string(health_check_type::dependency) <<
"\"\n";
if (parsed.is_ok())
{
std::cout <<
" \"readiness\" -> " <<
to_string(parsed.value()) <<
"\n";
}
std::cout << "\n2. Running health checks:\n";
std::vector<std::unique_ptr<health_check>> checks;
checks.push_back(std::make_unique<app_liveness_check>());
checks.push_back(std::make_unique<cache_readiness_check>(true));
checks.push_back(std::make_unique<database_dependency_check>(true));
bool all_healthy = true;
for (const auto& hc : checks)
{
auto result = hc->check();
std::cout <<
" [" <<
to_string(hc->get_type()) <<
"] " << hc->get_name() <<
": "
<< " (critical=" << (hc->is_critical() ? "yes" : "no")
<< ", timeout=" << hc->get_timeout().count() << "ms)\n";
if (result.status == health_status::unhealthy && hc->is_critical())
{
all_healthy = false;
}
}
std::cout << "\n Overall: " << (all_healthy ? "HEALTHY" : "UNHEALTHY") << "\n";
std::cout << "\n3. Simulating database failure:\n";
auto result = db_down.check();
std::cout <<
" " << db_down.get_name() <<
": " <<
status_string(result.status) <<
" - "
<< result.message << "\n";
std::cout << "\nDone.\n";
return 0;
}
bool is_critical() const override
Check if this health check is critical.
std::string get_name() const override
Get the unique name of this health check.
health_check_type get_type() const override
Get the type of this health check.
health_check_result check() override
Perform the health check.
health_check_result check() override
Perform the health check.
cache_readiness_check(bool warmed)
bool is_critical() const override
Check if this health check is critical.
std::string get_name() const override
Get the unique name of this health check.
std::chrono::milliseconds get_timeout() const override
Get timeout duration for this health check.
health_check_type get_type() const override
Get the type of this health check.
bool is_critical() const override
Check if this health check is critical.
health_check_result check() override
Perform the health check.
health_check_type get_type() const override
Get the type of this health check.
database_dependency_check(bool connected)
std::string get_name() const override
Get the unique name of this health check.
Abstract base class for health checks.
Base classes and types for health checking functionality.
std::string status_string(health_status status)
const char * to_string(service_lifetime lifetime)
Convert service_lifetime to string representation.
health_status
Standard health status levels.
Result< health_check_type > health_check_type_from_string(const std::string &str)
Convert string to health check type.
health_check_type
Types of health checks supported by the system.
Umbrella header for Result<T> type and related utilities.
Result of a health check operation.