Common System 0.2.0
Common interfaces and patterns for system integration
Loading...
Searching...
No Matches
kcenon::common::interfaces::IStats Interface Referenceabstract

Interface for components that expose statistics. More...

#include <stats_interface.h>

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

Public Member Functions

virtual ~IStats ()=default
 
virtual auto get_stats () const -> std::unordered_map< std::string, stats_value >=0
 Get current statistics as key-value pairs.
 
virtual auto to_json () const -> std::string=0
 Get statistics as JSON string.
 
virtual auto name () const -> std::string_view=0
 Get component name for identification.
 
virtual auto get_snapshot () const -> stats_snapshot
 Get a complete statistics snapshot with metadata.
 

Detailed Description

Interface for components that expose statistics.

All systems that want to provide metrics for monitoring should implement this interface. This enables the monitoring_system to generically collect stats from any component.

Usage Example:

class my_component : public IStats {
public:
auto get_stats() const -> std::unordered_map<std::string, stats_value> override {
return {
{"request_count", request_count_},
{"error_rate", calculate_error_rate()},
{"is_healthy", is_healthy_}
};
}
auto to_json() const -> std::string override {
auto snapshot = get_snapshot();
return snapshot.to_json();
}
auto name() const -> std::string_view override {
return "my_component";
}
};
Interface for components that expose statistics.
virtual auto to_json() const -> std::string=0
Get statistics as JSON string.
virtual auto name() const -> std::string_view=0
Get component name for identification.
virtual auto get_snapshot() const -> stats_snapshot
Get a complete statistics snapshot with metadata.
virtual auto get_stats() const -> std::unordered_map< std::string, stats_value >=0
Get current statistics as key-value pairs.
std::variant< std::int64_t, double, std::string, bool > stats_value
Type-safe value type for statistics.

Thread Safety:

  • Implementations should ensure get_stats() is thread-safe.
  • The interface itself does not mandate internal synchronization, allowing implementations to choose appropriate locking strategies.

Definition at line 73 of file stats_interface.h.

Constructor & Destructor Documentation

◆ ~IStats()

virtual kcenon::common::interfaces::IStats::~IStats ( )
virtualdefault

Member Function Documentation

◆ get_snapshot()

virtual auto kcenon::common::interfaces::IStats::get_snapshot ( ) const -> stats_snapshot
inlinenodiscardvirtual

Get a complete statistics snapshot with metadata.

Convenience method that bundles stats with component name and timestamp. Useful for monitoring systems that need full context.

Returns
Complete snapshot with component name, timestamp, and values

Definition at line 121 of file stats_interface.h.

122 {
123 return stats_snapshot{
124 .component_name = std::string(name()),
125 .timestamp = std::chrono::system_clock::now(),
126 .values = get_stats()
127 };
128 }

References get_stats(), and name().

Referenced by kcenon::common::resilience::circuit_breaker::to_json().

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

◆ get_stats()

virtual auto kcenon::common::interfaces::IStats::get_stats ( ) const -> std::unordered_map< std::string, stats_value >
nodiscardpure virtual

Get current statistics as key-value pairs.

Returns a snapshot of current metrics. Keys should be descriptive and stable across calls (use snake_case naming convention).

Common metric names:

  • Counters: "request_count", "error_count", "total_operations"
  • Rates: "error_rate", "success_rate", "failure_rate"
  • Gauges: "active_connections", "queue_size", "memory_usage_mb"
  • State: "current_state", "is_healthy", "is_enabled"
Returns
Map of metric names to values

Implemented in kcenon::common::resilience::circuit_breaker.

Referenced by get_snapshot().

Here is the caller graph for this function:

◆ name()

virtual auto kcenon::common::interfaces::IStats::name ( ) const -> std::string_view
nodiscardpure virtual

Get component name for identification.

Returns a unique identifier for this component. Used by monitoring systems to distinguish stats from different sources.

Returns
Component name (e.g., "circuit_breaker", "http_client", "database_pool")

Implemented in kcenon::common::resilience::circuit_breaker.

Referenced by get_snapshot().

Here is the caller graph for this function:

◆ to_json()

virtual auto kcenon::common::interfaces::IStats::to_json ( ) const -> std::string
nodiscardpure virtual

Get statistics as JSON string.

Serializes current statistics to JSON format for logging, monitoring, or API responses.

Returns
JSON string representation (e.g., {"request_count": 100, "error_rate": 0.05})

Implemented in kcenon::common::resilience::circuit_breaker.


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