Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
kcenon::monitoring::error_boundary< T > Class Template Reference

Error boundary implementation for resilient operations. More...

#include <error_boundary.h>

Collaboration diagram for kcenon::monitoring::error_boundary< T >:
Collaboration graph

Public Types

using config = error_boundary_config
 

Public Member Functions

 error_boundary ()
 
 error_boundary (const std::string &name)
 
 error_boundary (const std::string &name, const config &cfg)
 
template<typename Func >
auto execute (Func &&func) -> common::Result< T >
 Execute a function within the error boundary.
 
template<typename Func , typename FallbackFunc >
auto execute (Func &&func, FallbackFunc &&fallback) -> common::Result< T >
 Execute with custom fallback function.
 
void set_error_handler (std::function< void(const error_info &, degradation_level)> handler)
 Set error handler callback.
 
void set_fallback_strategy (std::shared_ptr< fallback_strategy_interface< T > > strategy)
 Set fallback strategy.
 
degradation_level get_degradation_level () const
 Get current degradation level.
 
void force_degradation (degradation_level level)
 Force degradation to a specific level.
 
common::Result< bool > is_healthy () const
 Check if the boundary is healthy.
 
error_boundary_metrics get_metrics () const
 Get metrics.
 
const std::string & get_name () const
 Get boundary name.
 

Private Member Functions

common::Result< T > handle_failure (const common::error_info &err)
 
void handle_success ()
 
void upgrade_degradation ()
 
void downgrade_degradation ()
 

Private Attributes

std::string name_
 
config config_
 
std::function< void(const error_info &, degradation_level)> error_handler_
 
std::shared_ptr< fallback_strategy_interface< T > > fallback_strategy_
 
error_boundary_metrics metrics_
 
degradation_level current_degradation_level_ = degradation_level::normal
 
size_t consecutive_failures_ = 0
 

Detailed Description

template<typename T>
class kcenon::monitoring::error_boundary< T >

Error boundary implementation for resilient operations.

Examples
health_reliability_example.cpp.

Definition at line 198 of file error_boundary.h.

Member Typedef Documentation

◆ config

template<typename T >
using kcenon::monitoring::error_boundary< T >::config = error_boundary_config

Definition at line 200 of file error_boundary.h.

Constructor & Destructor Documentation

◆ error_boundary() [1/3]

template<typename T >
kcenon::monitoring::error_boundary< T >::error_boundary ( )
inline

Definition at line 202 of file error_boundary.h.

◆ error_boundary() [2/3]

template<typename T >
kcenon::monitoring::error_boundary< T >::error_boundary ( const std::string & name)
inlineexplicit

◆ error_boundary() [3/3]

template<typename T >
kcenon::monitoring::error_boundary< T >::error_boundary ( const std::string & name,
const config & cfg )
inlineexplicit

Definition at line 208 of file error_boundary.h.

209 : name_(name), config_(cfg) {
210 config_.name = name;
211 }

References kcenon::monitoring::error_boundary< T >::config_, and kcenon::monitoring::error_boundary_config::name.

Member Function Documentation

◆ downgrade_degradation()

template<typename T >
void kcenon::monitoring::error_boundary< T >::downgrade_degradation ( )
inlineprivate

Definition at line 378 of file error_boundary.h.

378 {
379 auto current = static_cast<int>(current_degradation_level_);
380 if (current > 0) {
381 current_degradation_level_ = static_cast<degradation_level>(current - 1);
382 }
383 }
degradation_level current_degradation_level_
degradation_level
Degradation levels for error boundary.

References kcenon::monitoring::error_boundary< T >::current_degradation_level_.

Referenced by kcenon::monitoring::error_boundary< T >::handle_success().

Here is the caller graph for this function:

◆ execute() [1/2]

template<typename T >
template<typename Func >
auto kcenon::monitoring::error_boundary< T >::execute ( Func && func) -> common::Result<T>
inline

Execute a function within the error boundary.

Definition at line 217 of file error_boundary.h.

217 {
219
220 try {
221 auto op_result = func();
222
223 if (op_result.is_ok()) {
226 return op_result;
227 }
228
230 return handle_failure(op_result.error());
231
232 } catch (const std::exception& e) {
234 error_info err(monitoring_error_code::operation_failed, e.what());
235 return handle_failure(err.to_common_error());
236 } catch (...) {
238 error_info err(monitoring_error_code::operation_failed, "Unknown exception");
239 return handle_failure(err.to_common_error());
240 }
241 }
common::Result< T > handle_failure(const common::error_info &err)

References kcenon::monitoring::error_boundary_metrics::failed_operations, kcenon::monitoring::error_boundary< T >::handle_failure(), kcenon::monitoring::error_boundary< T >::handle_success(), kcenon::monitoring::error_boundary< T >::metrics_, kcenon::monitoring::operation_failed, kcenon::monitoring::error_boundary_metrics::successful_operations, kcenon::monitoring::error_info::to_common_error(), and kcenon::monitoring::error_boundary_metrics::total_operations.

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

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

◆ execute() [2/2]

template<typename T >
template<typename Func , typename FallbackFunc >
auto kcenon::monitoring::error_boundary< T >::execute ( Func && func,
FallbackFunc && fallback ) -> common::Result<T>
inline

Execute with custom fallback function.

Definition at line 247 of file error_boundary.h.

247 {
249
250 try {
251 auto op_result = func();
252
253 if (op_result.is_ok()) {
256 return op_result;
257 }
258
260 error_info err = error_info::from_common_error(op_result.error());
262
263 } catch (const std::exception& e) {
265 error_info err(monitoring_error_code::operation_failed, e.what());
267 }
268 }
static error_info from_common_error(const common::error_info &common_err)
Create from common_system error_info.

References kcenon::monitoring::error_boundary< T >::current_degradation_level_, kcenon::monitoring::error_boundary_metrics::failed_operations, kcenon::monitoring::fallback, kcenon::monitoring::error_info::from_common_error(), kcenon::monitoring::error_boundary< T >::handle_success(), kcenon::monitoring::error_boundary< T >::metrics_, kcenon::monitoring::operation_failed, kcenon::monitoring::error_boundary_metrics::successful_operations, and kcenon::monitoring::error_boundary_metrics::total_operations.

Here is the call graph for this function:

◆ force_degradation()

template<typename T >
void kcenon::monitoring::error_boundary< T >::force_degradation ( degradation_level level)
inline

Force degradation to a specific level.

Definition at line 294 of file error_boundary.h.

294 {
295 if (level <= config_.max_degradation) {
297 }
298 }

References kcenon::monitoring::error_boundary< T >::config_, kcenon::monitoring::error_boundary< T >::current_degradation_level_, and kcenon::monitoring::error_boundary_config::max_degradation.

Referenced by TEST_F().

Here is the caller graph for this function:

◆ get_degradation_level()

template<typename T >
degradation_level kcenon::monitoring::error_boundary< T >::get_degradation_level ( ) const
inline

Get current degradation level.

Definition at line 287 of file error_boundary.h.

287 {
289 }

References kcenon::monitoring::error_boundary< T >::current_degradation_level_.

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

Here is the caller graph for this function:

◆ get_metrics()

template<typename T >
error_boundary_metrics kcenon::monitoring::error_boundary< T >::get_metrics ( ) const
inline

Get metrics.

Definition at line 311 of file error_boundary.h.

311 {
312 return metrics_;
313 }

References kcenon::monitoring::error_boundary< T >::metrics_.

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

Here is the caller graph for this function:

◆ get_name()

template<typename T >
const std::string & kcenon::monitoring::error_boundary< T >::get_name ( ) const
inline

Get boundary name.

Definition at line 318 of file error_boundary.h.

318 {
319 return name_;
320 }

References kcenon::monitoring::error_boundary< T >::name_.

◆ handle_failure()

template<typename T >
common::Result< T > kcenon::monitoring::error_boundary< T >::handle_failure ( const common::error_info & err)
inlineprivate

Definition at line 323 of file error_boundary.h.

323 {
325
326 // Check if we should degrade
330 }
331
332 // Call error handler if set
333 if (error_handler_) {
334 error_info monitoring_err = error_info::from_common_error(err);
336 }
337
338 // Apply policy
339 switch (config_.policy) {
341 return common::Result<T>::err(err);
342
344 return common::Result<T>::err(error_info(monitoring_error_code::service_degraded, "Service isolated due to error").to_common_error());
345
347 if (fallback_strategy_) {
348 error_info monitoring_err = error_info::from_common_error(err);
349 return fallback_strategy_->get_fallback(monitoring_err, current_degradation_level_);
350 }
351 return common::Result<T>::err(err);
352
354 default:
355 return common::Result<T>::err(err);
356 }
357 }
std::function< void(const error_info &, degradation_level)> error_handler_
std::shared_ptr< fallback_strategy_interface< T > > fallback_strategy_

References kcenon::monitoring::error_boundary< T >::config_, kcenon::monitoring::error_boundary< T >::consecutive_failures_, kcenon::monitoring::error_boundary< T >::current_degradation_level_, kcenon::monitoring::degrade, kcenon::monitoring::error_boundary< T >::error_handler_, kcenon::monitoring::error_boundary_config::error_threshold, kcenon::monitoring::fail_fast, kcenon::monitoring::fallback, kcenon::monitoring::error_boundary< T >::fallback_strategy_, kcenon::monitoring::error_info::from_common_error(), kcenon::monitoring::isolate, kcenon::monitoring::error_boundary_config::policy, kcenon::monitoring::service_degraded, and kcenon::monitoring::error_boundary< T >::upgrade_degradation().

Referenced by kcenon::monitoring::error_boundary< T >::execute().

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

◆ handle_success()

template<typename T >
void kcenon::monitoring::error_boundary< T >::handle_success ( )
inlineprivate

◆ is_healthy()

template<typename T >
common::Result< bool > kcenon::monitoring::error_boundary< T >::is_healthy ( ) const
inline

Check if the boundary is healthy.

Definition at line 303 of file error_boundary.h.

References kcenon::monitoring::error_boundary< T >::current_degradation_level_, kcenon::monitoring::healthy, and kcenon::monitoring::normal.

Referenced by TEST_F().

Here is the caller graph for this function:

◆ set_error_handler()

template<typename T >
void kcenon::monitoring::error_boundary< T >::set_error_handler ( std::function< void(const error_info &, degradation_level)> handler)
inline

Set error handler callback.

Definition at line 273 of file error_boundary.h.

273 {
274 error_handler_ = std::move(handler);
275 }

References kcenon::monitoring::error_boundary< T >::error_handler_.

Referenced by demonstrate_error_boundaries().

Here is the caller graph for this function:

◆ set_fallback_strategy()

template<typename T >
void kcenon::monitoring::error_boundary< T >::set_fallback_strategy ( std::shared_ptr< fallback_strategy_interface< T > > strategy)
inline

Set fallback strategy.

Definition at line 280 of file error_boundary.h.

280 {
281 fallback_strategy_ = std::move(strategy);
282 }

References kcenon::monitoring::error_boundary< T >::fallback_strategy_.

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

Here is the caller graph for this function:

◆ upgrade_degradation()

template<typename T >
void kcenon::monitoring::error_boundary< T >::upgrade_degradation ( )
inlineprivate

Definition at line 370 of file error_boundary.h.

370 {
371 auto current = static_cast<int>(current_degradation_level_);
372 auto max_level = static_cast<int>(config_.max_degradation);
373 if (current < max_level) {
374 current_degradation_level_ = static_cast<degradation_level>(current + 1);
375 }
376 }

References kcenon::monitoring::error_boundary< T >::config_, kcenon::monitoring::error_boundary< T >::current_degradation_level_, and kcenon::monitoring::error_boundary_config::max_degradation.

Referenced by kcenon::monitoring::error_boundary< T >::handle_failure().

Here is the caller graph for this function:

Member Data Documentation

◆ config_

◆ consecutive_failures_

template<typename T >
size_t kcenon::monitoring::error_boundary< T >::consecutive_failures_ = 0
private

◆ current_degradation_level_

◆ error_handler_

template<typename T >
std::function<void(const error_info&, degradation_level)> kcenon::monitoring::error_boundary< T >::error_handler_
private

◆ fallback_strategy_

◆ metrics_

◆ name_

template<typename T >
std::string kcenon::monitoring::error_boundary< T >::name_
private

Definition at line 385 of file error_boundary.h.

Referenced by kcenon::monitoring::error_boundary< T >::get_name().


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