Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
kcenon::common::interfaces::composite_health_check Class Reference

Aggregates multiple health checks into a single check. More...

#include <composite_health_check.h>

Inheritance diagram for kcenon::common::interfaces::composite_health_check:
Inheritance graph
Collaboration diagram for kcenon::common::interfaces::composite_health_check:
Collaboration graph

Public Member Functions

 composite_health_check (std::string name, health_check_type type=health_check_type::custom, bool critical=true)
 Construct a composite health check.
 
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.
 
bool is_critical () const override
 Check if this health check is critical.
 
health_check_result check () override
 Execute all child health checks and aggregate results.
 
void add_check (std::shared_ptr< health_check > check)
 Add a child health check.
 
bool remove_check (const std::string &name)
 Remove a child health check by name.
 
std::size_t size () const
 Get the number of child checks.
 
bool empty () const
 Check if this composite has no child checks.
 
void clear ()
 Clear all child checks.
 
std::vector< std::string > get_check_names () const
 Get all child check names.
 
- Public Member Functions inherited from kcenon::common::interfaces::health_check
 health_check ()=default
 
virtual ~health_check ()=default
 
 health_check (const health_check &)=delete
 
health_checkoperator= (const health_check &)=delete
 
 health_check (health_check &&)=delete
 
health_checkoperator= (health_check &&)=delete
 
virtual std::chrono::milliseconds get_timeout () const
 Get timeout duration for this health check.
 

Private Attributes

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

Detailed Description

Aggregates multiple health checks into a single check.

This class implements the Composite pattern for health checks, allowing multiple checks to be grouped and executed together. The overall health status is determined by the worst status among all child checks.

Example usage:

auto composite = std::make_shared<composite_health_check>("system_health");
composite->add_check(database_check);
composite->add_check(cache_check);
composite->add_check(queue_check);
auto result = composite->check(); // Runs all checks

Definition at line 43 of file composite_health_check.h.

Constructor & Destructor Documentation

◆ composite_health_check()

kcenon::common::interfaces::composite_health_check::composite_health_check ( std::string name,
health_check_type type = health_check_type::custom,
bool critical = true )
inlineexplicit

Construct a composite health check.

Parameters
nameName for this composite check
typeHealth check type (default: custom)
criticalWhether this composite is critical (default: true)

Definition at line 51 of file composite_health_check.h.

Member Function Documentation

◆ add_check()

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

Add a child health check.

Parameters
checkHealth check to add

Definition at line 139 of file composite_health_check.h.

139 {
140 if (!check) {
141 return;
142 }
143 std::lock_guard<std::mutex> lock(mutex_);
144 checks_.push_back(std::move(check));
145 }
std::vector< std::shared_ptr< health_check > > checks_
health_check_result check() override
Execute all child health checks and aggregate results.

References check(), checks_, and mutex_.

Here is the call graph for this function:

◆ check()

health_check_result kcenon::common::interfaces::composite_health_check::check ( )
inlineoverridevirtual

Execute all child health checks and aggregate results.

Returns
Aggregated health check result

The overall status is determined as follows:

  • UNHEALTHY if any child is UNHEALTHY
  • DEGRADED if any child is DEGRADED (and none are UNHEALTHY)
  • UNKNOWN if any child is UNKNOWN (and none are worse)
  • HEALTHY only if all children are HEALTHY

Implements kcenon::common::interfaces::health_check.

Definition at line 73 of file composite_health_check.h.

73 {
74 std::lock_guard<std::mutex> lock(mutex_);
75
76 health_check_result result;
77 result.status = health_status::healthy;
78 auto start_time = std::chrono::steady_clock::now();
79
80 std::vector<std::string> messages;
81 std::size_t healthy_count = 0;
82 std::size_t degraded_count = 0;
83 std::size_t unhealthy_count = 0;
84
85 for (const auto& check : checks_) {
86 auto child_result = check->check();
87
88 switch (child_result.status) {
90 ++unhealthy_count;
91 result.status = health_status::unhealthy;
92 messages.push_back(check->get_name() + ": " + child_result.message);
93 break;
95 ++degraded_count;
96 if (result.status != health_status::unhealthy) {
97 result.status = health_status::degraded;
98 }
99 messages.push_back(check->get_name() + ": " + child_result.message);
100 break;
102 if (result.status == health_status::healthy) {
103 result.status = health_status::unknown;
104 }
105 messages.push_back(check->get_name() + ": " + child_result.message);
106 break;
108 ++healthy_count;
109 break;
110 }
111 }
112
113 auto end_time = std::chrono::steady_clock::now();
114 result.check_duration = std::chrono::duration_cast<std::chrono::milliseconds>(
115 end_time - start_time);
116
117 if (messages.empty()) {
118 result.message = "All " + std::to_string(healthy_count) + " checks passed";
119 } else {
120 result.message = std::to_string(unhealthy_count) + " unhealthy, " +
121 std::to_string(degraded_count) + " degraded";
122 for (const auto& msg : messages) {
123 result.message += "; " + msg;
124 }
125 }
126
127 result.metadata["total_checks"] = std::to_string(checks_.size());
128 result.metadata["healthy_count"] = std::to_string(healthy_count);
129 result.metadata["degraded_count"] = std::to_string(degraded_count);
130 result.metadata["unhealthy_count"] = std::to_string(unhealthy_count);
131
132 return result;
133 }

References check(), kcenon::common::interfaces::health_check_result::check_duration, checks_, kcenon::common::interfaces::degraded, kcenon::common::interfaces::healthy, kcenon::common::interfaces::health_check_result::message, kcenon::common::interfaces::health_check_result::metadata, mutex_, kcenon::common::interfaces::health_check_result::status, kcenon::common::interfaces::unhealthy, and kcenon::common::interfaces::unknown.

Referenced by add_check(), check(), get_check_names(), and remove_check().

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

◆ clear()

void kcenon::common::interfaces::composite_health_check::clear ( )
inline

Clear all child checks.

Definition at line 186 of file composite_health_check.h.

186 {
187 std::lock_guard<std::mutex> lock(mutex_);
188 checks_.clear();
189 }

References checks_, and mutex_.

◆ empty()

bool kcenon::common::interfaces::composite_health_check::empty ( ) const
inlinenodiscard

Check if this composite has no child checks.

Returns
true if empty

Definition at line 178 of file composite_health_check.h.

178 {
179 std::lock_guard<std::mutex> lock(mutex_);
180 return checks_.empty();
181 }

References checks_, and mutex_.

◆ get_check_names()

std::vector< std::string > kcenon::common::interfaces::composite_health_check::get_check_names ( ) const
inlinenodiscard

Get all child check names.

Returns
Vector of check names

Definition at line 195 of file composite_health_check.h.

195 {
196 std::lock_guard<std::mutex> lock(mutex_);
197 std::vector<std::string> names;
198 names.reserve(checks_.size());
199 for (const auto& check : checks_) {
200 names.push_back(check->get_name());
201 }
202 return names;
203 }

References check(), checks_, and mutex_.

Here is the call graph for this function:

◆ get_name()

std::string kcenon::common::interfaces::composite_health_check::get_name ( ) const
inlinenodiscardoverridevirtual

Get the unique name of this health check.

Returns
Health check name

Implements kcenon::common::interfaces::health_check.

Definition at line 57 of file composite_health_check.h.

57{ return name_; }

References name_.

◆ get_type()

health_check_type kcenon::common::interfaces::composite_health_check::get_type ( ) const
inlinenodiscardoverridevirtual

Get the type of this health check.

Returns
Health check type

Implements kcenon::common::interfaces::health_check.

Definition at line 59 of file composite_health_check.h.

59{ return type_; }

References type_.

◆ is_critical()

bool kcenon::common::interfaces::composite_health_check::is_critical ( ) const
inlinenodiscardoverridevirtual

Check if this health check is critical.

Critical health checks affect the overall system health status. Non-critical checks are reported but don't impact system health.

Returns
true if this is a critical health check

Implements kcenon::common::interfaces::health_check.

Definition at line 61 of file composite_health_check.h.

61{ return critical_; }

References critical_.

◆ remove_check()

bool kcenon::common::interfaces::composite_health_check::remove_check ( const std::string & name)
inline

Remove a child health check by name.

Parameters
nameName of the check to remove
Returns
true if check was found and removed

Definition at line 152 of file composite_health_check.h.

152 {
153 std::lock_guard<std::mutex> lock(mutex_);
154 auto it = std::find_if(checks_.begin(), checks_.end(),
155 [&name](const std::shared_ptr<health_check>& check) {
156 return check->get_name() == name;
157 });
158 if (it != checks_.end()) {
159 checks_.erase(it);
160 return true;
161 }
162 return false;
163 }

References check(), checks_, and mutex_.

Here is the call graph for this function:

◆ size()

std::size_t kcenon::common::interfaces::composite_health_check::size ( ) const
inlinenodiscard

Get the number of child checks.

Returns
Number of child checks

Definition at line 169 of file composite_health_check.h.

169 {
170 std::lock_guard<std::mutex> lock(mutex_);
171 return checks_.size();
172 }

References checks_, and mutex_.

Member Data Documentation

◆ checks_

std::vector<std::shared_ptr<health_check> > kcenon::common::interfaces::composite_health_check::checks_
private

◆ critical_

bool kcenon::common::interfaces::composite_health_check::critical_
private

Definition at line 208 of file composite_health_check.h.

Referenced by is_critical().

◆ mutex_

std::mutex kcenon::common::interfaces::composite_health_check::mutex_
mutableprivate

◆ name_

std::string kcenon::common::interfaces::composite_health_check::name_
private

Definition at line 206 of file composite_health_check.h.

Referenced by get_name().

◆ type_

health_check_type kcenon::common::interfaces::composite_health_check::type_
private

Definition at line 207 of file composite_health_check.h.

Referenced by get_type().


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