Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
health_check.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
13#pragma once
14
15#include <array>
16#include <chrono>
17#include <functional>
18#include <string>
19#include <string_view>
20#include <utility>
21
24
26
32 liveness, // Basic alive check - is the service running?
33 readiness, // Ready to accept traffic?
34 startup, // Has the service completed initialization?
35 dependency, // External dependency check (database, cache, etc.)
36 custom // User-defined health check type
37};
38
39} // namespace kcenon::common::interfaces
40
41namespace kcenon::common {
42
46template<>
47struct enum_traits<interfaces::health_check_type> {
48 static constexpr auto values = std::array{
49 std::pair{interfaces::health_check_type::liveness, std::string_view{"LIVENESS"}},
50 std::pair{interfaces::health_check_type::readiness, std::string_view{"READINESS"}},
51 std::pair{interfaces::health_check_type::startup, std::string_view{"STARTUP"}},
52 std::pair{interfaces::health_check_type::dependency, std::string_view{"DEPENDENCY"}},
53 std::pair{interfaces::health_check_type::custom, std::string_view{"CUSTOM"}},
54 };
55 static constexpr std::string_view module_name = "health_check";
56};
57
58} // namespace kcenon::common
59
61
65inline std::string to_string(health_check_type type) {
66 return enum_to_string(type);
67}
68
75
106public:
107 health_check() = default;
108 virtual ~health_check() = default;
109
110 health_check(const health_check&) = delete;
114
119 [[nodiscard]] virtual std::string get_name() const = 0;
120
125 [[nodiscard]] virtual health_check_type get_type() const = 0;
126
132
137 [[nodiscard]] virtual std::chrono::milliseconds get_timeout() const {
138 return std::chrono::milliseconds{5000};
139 }
140
149 [[nodiscard]] virtual bool is_critical() const { return true; }
150};
151
174public:
175 using check_function = std::function<health_check_result()>;
176
185 lambda_health_check(std::string name,
187 check_function check_fn,
188 bool critical = true,
189 std::chrono::milliseconds timeout = std::chrono::milliseconds{5000})
190 : name_(std::move(name))
191 , type_(type)
192 , check_fn_(std::move(check_fn))
194 , timeout_(timeout) {}
195
196 [[nodiscard]] std::string get_name() const override { return name_; }
197
198 [[nodiscard]] health_check_type get_type() const override { return type_; }
199
201 if (!check_fn_) {
202 health_check_result result;
204 result.message = "No check function configured";
205 return result;
206 }
207 return check_fn_();
208 }
209
210 [[nodiscard]] std::chrono::milliseconds get_timeout() const override { return timeout_; }
211
212 [[nodiscard]] bool is_critical() const override { return critical_; }
213
214private:
215 std::string name_;
219 std::chrono::milliseconds timeout_;
220};
221
222} // namespace kcenon::common::interfaces
Result type for error handling with member function support.
Definition core.cppm:165
Abstract base class for health checks.
health_check & operator=(const health_check &)=delete
virtual bool is_critical() const
Check if this health check is critical.
virtual std::chrono::milliseconds get_timeout() const
Get timeout duration for this health check.
virtual health_check_type get_type() const =0
Get the type of this health check.
health_check & operator=(health_check &&)=delete
virtual health_check_result check()=0
Perform the health check.
health_check(health_check &&)=delete
health_check(const health_check &)=delete
virtual std::string get_name() const =0
Get the unique name of this health check.
Health check implementation using a lambda function.
std::string get_name() const override
Get the unique name of this health check.
health_check_result check() override
Perform the health check.
std::function< health_check_result()> check_function
lambda_health_check(std::string name, health_check_type type, check_function check_fn, bool critical=true, std::chrono::milliseconds timeout=std::chrono::milliseconds{5000})
Construct a lambda health check.
bool is_critical() const override
Check if this health check is critical.
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.
Generic enum serialization utilities using C++20 concepts.
Standard monitoring interface for all systems.
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
Result< Enum > enum_from_string(std::string_view str)
Convert a string to its enum value (case-insensitive)
std::string enum_to_string(Enum value)
Convert an enum value to its string representation.
Primary template for enum traits (must be specialized for each enum)