Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
health_check_example.cpp

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.

See also
kcenon::common::health_check
kcenon::common::health_check_type
// BSD 3-Clause License
// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
// See the LICENSE file in the project root for full license information.
#include <chrono>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
using namespace kcenon::common;
// Custom liveness check — is the service running?
{
public:
std::string get_name() const override { return "app_liveness"; }
health_check_type get_type() const override { return health_check_type::liveness; }
bool is_critical() const override { return true; }
{
result.status = health_status::healthy;
result.message = "Application is running";
return result;
}
};
// Custom readiness check — ready to accept traffic?
{
public:
explicit cache_readiness_check(bool warmed) : cache_warmed_(warmed) {}
std::string get_name() const override { return "cache_readiness"; }
health_check_type get_type() const override { return health_check_type::readiness; }
bool is_critical() const override { return false; }
std::chrono::milliseconds get_timeout() const override
{
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:
};
// Custom dependency check — external service available?
{
public:
explicit database_dependency_check(bool connected) : connected_(connected) {}
std::string get_name() const override { return "database"; }
health_check_type get_type() const override { return health_check_type::dependency; }
bool is_critical() const override { return true; }
{
{
result.status = health_status::healthy;
result.message = "Database connected";
}
else
{
result.status = health_status::unhealthy;
result.message = "Database connection failed";
}
return result;
}
private:
bool connected_;
};
std::string status_string(health_status status)
{
switch (status)
{
case health_status::healthy:
return "HEALTHY";
case health_status::degraded:
return "DEGRADED";
case health_status::unhealthy:
return "UNHEALTHY";
default:
return "UNKNOWN";
}
}
int main()
{
std::cout << "=== Health Check Example ===\n\n";
// 1. Health check type utilities
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";
auto parsed = health_check_type_from_string("readiness");
if (parsed.is_ok())
{
std::cout << " \"readiness\" -> " << to_string(parsed.value()) << "\n";
}
// 2. Create health checks
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() << ": "
<< status_string(result.status) << " - " << result.message
<< " (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";
// 3. Simulate a failure scenario
std::cout << "\n3. Simulating database failure:\n";
database_dependency_check db_down(false);
auto result = db_down.check();
std::cout << " " << db_down.get_name() << ": " << status_string(result.status) << " - "
<< result.message << "\n";
std::cout << "\nDone.\n";
return 0;
}
int main()
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.
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.
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.
Core interfaces.
Definition adapter.h:21
Umbrella header for Result<T> type and related utilities.