19#include <unordered_map>
27namespace kcenon {
namespace monitoring {
64 using metric_map = std::unordered_map<std::string, double>;
65 using tag_map = std::unordered_map<std::string, std::string>;
78 :
timestamp_(std::chrono::system_clock::now()) {}
102 void add_tag(
const std::string& key,
const std::string& value) {
111 std::optional<double>
get_metric(
const std::string& key)
const {
124 std::optional<std::string>
get_tag(
const std::string& key)
const {
125 auto it =
tags_.find(key);
126 if (it !=
tags_.end()) {
210 for (
const auto& [key, value] :
other.metrics_) {
211 if (prefix.empty()) {
214 metrics_[prefix +
"." + key] = value;
218 for (
const auto& [key, value] :
other.tags_) {
219 if (prefix.empty()) {
222 tags_[prefix +
"." + key] = value;
409 void update_tag(
const std::string& key,
const std::string& value)
const {
483 [&
id](
const auto& comp) {
484 return comp->get_monitoring_id() == id;
502 if (!component->is_monitoring_enabled()) {
506 auto result = component->get_monitoring_data();
507 if (result.is_ok()) {
508 aggregated.
merge(result.value(), component->get_monitoring_id());
512 component->get_monitoring_id() +
".error",
513 result.error().message
519 aggregated.
add_metric(
"aggregator.component_count",
521 aggregated.
add_metric(
"aggregator.total_metrics",
524 return common::ok(std::move(aggregated));
532 std::shared_ptr<monitorable_interface>
get_component(
const std::string&
id)
const {
534 [&
id](
const auto& comp) {
535 return comp->get_monitoring_id() == id;
546 std::vector<std::string> ids;
550 ids.push_back(component->get_monitoring_id());
Base class providing default monitorable implementation.
monitoring_data cached_data_
common::VoidResult reset_monitoring() override
Reset monitoring data.
void update_metric(const std::string &key, double value) const
Helper to update a metric.
std::string monitoring_id_
monitorable_component(const std::string &id)
Constructor.
bool is_monitoring_enabled() const override
Check if monitoring is enabled.
void update_tag(const std::string &key, const std::string &value) const
Helper to update a tag.
std::string get_monitoring_id() const override
Get monitoring identifier.
common::VoidResult set_monitoring_enabled(bool enable) override
Enable or disable monitoring.
Interface for components that can be monitored.
virtual common::VoidResult reset_monitoring()
Reset monitoring counters and state.
virtual common::VoidResult set_monitoring_enabled(bool enable)
Enable or disable monitoring.
virtual ~monitorable_interface()=default
virtual std::string get_monitoring_id() const =0
Get the component's monitoring identifier.
virtual bool is_monitoring_enabled() const
Check if monitoring is enabled for this component.
virtual common::Result< monitoring_data > get_monitoring_data() const =0
Get current monitoring data from the component.
Utility class to aggregate metrics from multiple monitorable components.
std::size_t size() const
Get the number of registered components.
common::Result< monitoring_data > collect_all() const
Collect data from all components.
std::vector< std::shared_ptr< monitorable_interface > > components_
std::vector< std::string > get_component_ids() const
Get all component IDs.
monitoring_aggregator(const std::string &id="aggregator")
Constructor.
bool remove_component(const std::string &id)
Remove a component by ID.
std::shared_ptr< monitorable_interface > get_component(const std::string &id) const
Get a specific component by ID.
void clear()
Clear all components.
void add_component(std::shared_ptr< monitorable_interface > component)
Add a component to monitor.
std::string aggregator_id_
Monitoring system specific error codes.
Result pattern type definitions for monitoring system.
Container for monitoring metrics from a component.
void merge(const monitoring_data &other, const std::string &prefix="")
Merge another monitoring_data into this one.
std::size_t metric_count() const
Get the number of metrics.
bool empty() const
Check if data is empty.
std::string component_name_
monitoring_data(const std::string &name)
Constructor with component name.
std::chrono::system_clock::time_point get_timestamp() const
Get the timestamp.
std::optional< std::string > get_tag(const std::string &key) const
Get a tag value.
void set_component_name(const std::string &name)
Set the component name.
std::size_t tag_count() const
Get the number of tags.
std::unordered_map< std::string, std::string > tag_map
std::chrono::system_clock::time_point timestamp_
void clear()
Clear all metrics and tags.
std::optional< double > get_metric(const std::string &key) const
Get a metric value.
const std::string & get_component_name() const
Get the component name.
const tag_map & get_tags() const
Get all tags.
const metric_map & get_metrics() const
Get all metrics.
void add_metric(const std::string &key, double value)
Add a numeric metric.
monitoring_data()
Default constructor.
std::unordered_map< std::string, double > metric_map
void add_tag(const std::string &key, const std::string &value)
Add a tag (string metadata)