Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
health_check_example.cpp File Reference
#include <kcenon/common/interfaces/monitoring/health_check.h>
#include <kcenon/common/patterns/result.h>
#include <chrono>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
Include dependency graph for health_check_example.cpp:

Go to the source code of this file.

Classes

class  app_liveness_check
 
class  cache_readiness_check
 
class  database_dependency_check
 

Functions

std::string status_string (health_status status)
 
int main ()
 

Function Documentation

◆ main()

int main ( )

Definition at line 124 of file health_check_example.cpp.

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}
std::string status_string(health_status status)
Result< health_check_type > health_check_type_from_string(const std::string &str)
Convert string to health check type.
std::string to_string(log_level level)
Convert log level to string.

References database_dependency_check::check(), database_dependency_check::get_name(), kcenon::common::interfaces::health_check_type_from_string(), status_string(), and kcenon::common::interfaces::to_string().

Here is the call graph for this function:

◆ status_string()

std::string status_string ( health_status status)
Examples
health_check_example.cpp.

Definition at line 109 of file health_check_example.cpp.

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}

Referenced by main().

Here is the caller graph for this function: