Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
health_check_example.cpp
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
14
17
18#include <chrono>
19#include <iostream>
20#include <memory>
21#include <string>
22#include <vector>
23
24using namespace kcenon::common;
25using namespace kcenon::common::interfaces;
26
27// Custom liveness check — is the service running?
29{
30public:
31 std::string get_name() const override { return "app_liveness"; }
32 health_check_type get_type() const override { return health_check_type::liveness; }
33 bool is_critical() const override { return true; }
34
36 {
38 result.status = health_status::healthy;
39 result.message = "Application is running";
40 return result;
41 }
42};
43
44// Custom readiness check — ready to accept traffic?
46{
47public:
48 explicit cache_readiness_check(bool warmed) : cache_warmed_(warmed) {}
49
50 std::string get_name() const override { return "cache_readiness"; }
51 health_check_type get_type() const override { return health_check_type::readiness; }
52 bool is_critical() const override { return false; }
53
54 std::chrono::milliseconds get_timeout() const override
55 {
56 return std::chrono::milliseconds(2000);
57 }
58
60 {
62 if (cache_warmed_)
63 {
64 result.status = health_status::healthy;
65 result.message = "Cache is warmed";
66 }
67 else
68 {
69 result.status = health_status::degraded;
70 result.message = "Cache warming in progress";
71 }
72 return result;
73 }
74
75private:
77};
78
79// Custom dependency check — external service available?
81{
82public:
83 explicit database_dependency_check(bool connected) : connected_(connected) {}
84
85 std::string get_name() const override { return "database"; }
86 health_check_type get_type() const override { return health_check_type::dependency; }
87 bool is_critical() const override { return true; }
88
90 {
92 if (connected_)
93 {
94 result.status = health_status::healthy;
95 result.message = "Database connected";
96 }
97 else
98 {
99 result.status = health_status::unhealthy;
100 result.message = "Database connection failed";
101 }
102 return result;
103 }
104
105private:
107};
108
109std::string status_string(health_status status)
110{
111 switch (status)
112 {
113 case health_status::healthy:
114 return "HEALTHY";
115 case health_status::degraded:
116 return "DEGRADED";
117 case health_status::unhealthy:
118 return "UNHEALTHY";
119 default:
120 return "UNKNOWN";
121 }
122}
123
124int main()
125{
126 std::cout << "=== Health Check Example ===\n\n";
127
128 // 1. Health check type utilities
129 std::cout << "1. Health check types:\n";
130 std::cout << " liveness -> \"" << to_string(health_check_type::liveness) << "\"\n";
131 std::cout << " readiness -> \"" << to_string(health_check_type::readiness) << "\"\n";
132 std::cout << " dependency -> \"" << to_string(health_check_type::dependency) << "\"\n";
133
134 auto parsed = health_check_type_from_string("readiness");
135 if (parsed.is_ok())
136 {
137 std::cout << " \"readiness\" -> " << to_string(parsed.value()) << "\n";
138 }
139
140 // 2. Create health checks
141 std::cout << "\n2. Running health checks:\n";
142 std::vector<std::unique_ptr<health_check>> checks;
143 checks.push_back(std::make_unique<app_liveness_check>());
144 checks.push_back(std::make_unique<cache_readiness_check>(true));
145 checks.push_back(std::make_unique<database_dependency_check>(true));
146
147 bool all_healthy = true;
148 for (const auto& hc : checks)
149 {
150 auto result = hc->check();
151 std::cout << " [" << to_string(hc->get_type()) << "] " << hc->get_name() << ": "
152 << status_string(result.status) << " - " << result.message
153 << " (critical=" << (hc->is_critical() ? "yes" : "no")
154 << ", timeout=" << hc->get_timeout().count() << "ms)\n";
155
156 if (result.status == health_status::unhealthy && hc->is_critical())
157 {
158 all_healthy = false;
159 }
160 }
161
162 std::cout << "\n Overall: " << (all_healthy ? "HEALTHY" : "UNHEALTHY") << "\n";
163
164 // 3. Simulate a failure scenario
165 std::cout << "\n3. Simulating database failure:\n";
166 database_dependency_check db_down(false);
167 auto result = db_down.check();
168 std::cout << " " << db_down.get_name() << ": " << status_string(result.status) << " - "
169 << result.message << "\n";
170
171 std::cout << "\nDone.\n";
172 return 0;
173}
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)
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.
std::string to_string(log_level level)
Convert log level to string.
Core interfaces.
Definition adapter.h:21
Umbrella header for Result<T> type and related utilities.