Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
kcenon::monitoring::composite_health_check Class Reference

Composite health check that aggregates multiple sub-checks. More...

#include <health_monitor.h>

Inheritance diagram for kcenon::monitoring::composite_health_check:
Inheritance graph
Collaboration diagram for kcenon::monitoring::composite_health_check:
Collaboration graph

Public Member Functions

 composite_health_check (const std::string &name, health_check_type type, bool all_required=true)
 Construct a composite health check.
 
std::string get_name () const override
 Get the human-readable name of this health check.
 
health_check_type get_type () const override
 Get the type of this health check (liveness, readiness, or startup).
 
void add_check (std::shared_ptr< health_check > check)
 Add a child health check to this composite.
 
health_check_result check () override
 Execute all child checks and return the aggregate result.
 
- Public Member Functions inherited from kcenon::monitoring::health_check
virtual ~health_check ()=default
 
virtual std::chrono::milliseconds get_timeout () const
 Get the maximum time allowed for this check to complete.
 
virtual bool is_critical () const
 Whether this check is critical for overall system health.
 

Private Member Functions

health_check_result check_all_required (const std::vector< health_check_result > &results)
 
health_check_result check_any_required (const std::vector< health_check_result > &results)
 

Private Attributes

std::string name_
 
health_check_type type_
 
bool all_required_
 
std::vector< std::shared_ptr< health_check > > checks_
 
std::mutex mutex_
 

Detailed Description

Composite health check that aggregates multiple sub-checks.

Evaluates a collection of child health checks and returns an aggregate result. Supports two aggregation modes:

  • all_required (AND): All checks must pass for healthy status
  • any_required (OR): At least one check must pass for healthy status

Thread Safety

Thread-safe. Uses a mutex to protect the checks_ collection.

See also
health_check For the base interface

Definition at line 219 of file health_monitor.h.

Constructor & Destructor Documentation

◆ composite_health_check()

kcenon::monitoring::composite_health_check::composite_health_check ( const std::string & name,
health_check_type type,
bool all_required = true )
inline

Construct a composite health check.

Parameters
nameHuman-readable name for this composite check
typeThe check type (liveness, readiness, or startup)
all_requiredIf true (default), all sub-checks must pass (AND mode); if false, at least one must pass (OR mode)

Definition at line 228 of file health_monitor.h.

Member Function Documentation

◆ add_check()

void kcenon::monitoring::composite_health_check::add_check ( std::shared_ptr< health_check > check)
inline

Add a child health check to this composite.

Parameters
checkThe health check to add

Definition at line 244 of file health_monitor.h.

244 {
245 std::lock_guard<std::mutex> lock(mutex_);
246 checks_.push_back(std::move(check));
247 }
health_check_result check() override
Execute all child checks and return the aggregate result.
std::vector< std::shared_ptr< health_check > > checks_

References check(), checks_, and mutex_.

Referenced by TEST_F(), and TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check()

health_check_result kcenon::monitoring::composite_health_check::check ( )
inlineoverridevirtual

Execute all child checks and return the aggregate result.

Returns
Aggregate health_check_result based on the aggregation mode

Implements kcenon::monitoring::health_check.

Definition at line 253 of file health_monitor.h.

253 {
254 std::lock_guard<std::mutex> lock(mutex_);
255
256 if (checks_.empty()) {
257 return health_check_result::healthy("No checks configured");
258 }
259
260 std::vector<health_check_result> results;
261 results.reserve(checks_.size());
262
263 for (const auto& chk : checks_) {
264 results.push_back(chk->check());
265 }
266
267 if (all_required_) {
268 return check_all_required(results);
269 } else {
270 return check_any_required(results);
271 }
272 }
health_check_result check_all_required(const std::vector< health_check_result > &results)
health_check_result check_any_required(const std::vector< health_check_result > &results)
static health_check_result healthy(const std::string &msg="OK")

References all_required_, check_all_required(), check_any_required(), checks_, kcenon::monitoring::health_check_result::healthy(), and mutex_.

Referenced by add_check(), TEST_F(), and TEST_F().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_all_required()

health_check_result kcenon::monitoring::composite_health_check::check_all_required ( const std::vector< health_check_result > & results)
inlineprivate

Definition at line 275 of file health_monitor.h.

275 {
276 bool has_unhealthy = false;
277 bool has_degraded = false;
278 std::string message;
279
280 for (const auto& result : results) {
281 if (result.status == health_status::unhealthy) {
282 has_unhealthy = true;
283 message += result.message + "; ";
284 } else if (result.status == health_status::degraded) {
285 has_degraded = true;
286 message += result.message + "; ";
287 }
288 }
289
290 if (has_unhealthy) {
291 return health_check_result::unhealthy(message.empty() ? "One or more checks failed" : message);
292 }
293 if (has_degraded) {
294 return health_check_result::degraded(message.empty() ? "One or more checks degraded" : message);
295 }
296 return health_check_result::healthy("All checks passed");
297 }
static health_check_result unhealthy(const std::string &msg)
static health_check_result degraded(const std::string &msg)

References kcenon::monitoring::health_check_result::degraded(), kcenon::monitoring::degraded, kcenon::monitoring::health_check_result::healthy(), kcenon::monitoring::health_check_result::unhealthy(), and kcenon::monitoring::unhealthy.

Referenced by check().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ check_any_required()

health_check_result kcenon::monitoring::composite_health_check::check_any_required ( const std::vector< health_check_result > & results)
inlineprivate

Definition at line 299 of file health_monitor.h.

299 {
300 bool any_healthy = false;
301 std::string message;
302
303 for (const auto& result : results) {
304 if (result.status == health_status::healthy) {
305 any_healthy = true;
306 break;
307 }
308 message += result.message + "; ";
309 }
310
311 if (any_healthy) {
312 return health_check_result::healthy("At least one check passed");
313 }
314 return health_check_result::unhealthy(message.empty() ? "All checks failed" : message);
315 }

References kcenon::monitoring::health_check_result::healthy(), kcenon::monitoring::healthy, and kcenon::monitoring::health_check_result::unhealthy().

Referenced by check().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_name()

std::string kcenon::monitoring::composite_health_check::get_name ( ) const
inlineoverridevirtual

Get the human-readable name of this health check.

Returns
The health check name

Implements kcenon::monitoring::health_check.

Definition at line 236 of file health_monitor.h.

236{ return name_; }

References name_.

◆ get_type()

health_check_type kcenon::monitoring::composite_health_check::get_type ( ) const
inlineoverridevirtual

Get the type of this health check (liveness, readiness, or startup).

Returns
The health check type

Implements kcenon::monitoring::health_check.

Definition at line 238 of file health_monitor.h.

238{ return type_; }

References type_.

Member Data Documentation

◆ all_required_

bool kcenon::monitoring::composite_health_check::all_required_
private

Definition at line 319 of file health_monitor.h.

Referenced by check().

◆ checks_

std::vector<std::shared_ptr<health_check> > kcenon::monitoring::composite_health_check::checks_
private

Definition at line 320 of file health_monitor.h.

Referenced by add_check(), and check().

◆ mutex_

std::mutex kcenon::monitoring::composite_health_check::mutex_
mutableprivate

Definition at line 321 of file health_monitor.h.

Referenced by add_check(), and check().

◆ name_

std::string kcenon::monitoring::composite_health_check::name_
private

Definition at line 317 of file health_monitor.h.

Referenced by get_name().

◆ type_

health_check_type kcenon::monitoring::composite_health_check::type_
private

Definition at line 318 of file health_monitor.h.

Referenced by get_type().


The documentation for this class was generated from the following file: