Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
collector_base.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
5#pragma once
6
48#include <atomic>
49#include <chrono>
50#include <mutex>
51#include <string>
52#include <unordered_map>
53#include <vector>
54
56
57namespace kcenon {
58namespace monitoring {
59
63using config_map = std::unordered_map<std::string, std::string>;
64
68using stats_map = std::unordered_map<std::string, double>;
69
82template <typename Derived>
84 public:
85 collector_base() = default;
86 virtual ~collector_base() = default;
87
88 // Non-copyable, non-moveable
93
99 bool initialize(const config_map& config) {
100 // Parse common configuration
101 if (auto it = config.find("enabled"); it != config.end()) {
102 enabled_ = (it->second == "true" || it->second == "1");
103 }
104
105 // Delegate to derived class for specific initialization
106 return derived().do_initialize(config);
107 }
108
113 std::vector<metric> collect() {
114 if (!enabled_) {
115 return {};
116 }
117
118 try {
119 auto metrics = derived().do_collect();
121 return metrics;
122 } catch (...) {
124 return {};
125 }
126 }
127
132 std::string get_name() const {
133 return Derived::collector_name;
134 }
135
140 std::vector<std::string> get_metric_types() const {
141 return derived().do_get_metric_types();
142 }
143
148 bool is_healthy() const {
149 if (!enabled_) {
150 return true; // Disabled collectors are considered healthy
151 }
152 return derived().is_available();
153 }
154
160 std::lock_guard<std::mutex> lock(stats_mutex_);
161 stats_map stats;
162
163 // Common statistics
164 stats["enabled"] = enabled_ ? 1.0 : 0.0;
165 stats["collection_count"] = static_cast<double>(collection_count_.load());
166 stats["collection_errors"] = static_cast<double>(collection_errors_.load());
167
168 // Let derived class add specific statistics
169 derived().do_add_statistics(stats);
170
171 return stats;
172 }
173
178 bool is_enabled() const { return enabled_; }
179
184 size_t get_collection_count() const { return collection_count_.load(); }
185
190 size_t get_collection_errors() const { return collection_errors_.load(); }
191
192 protected:
201 metric create_base_metric(const std::string& name, double value,
202 const std::unordered_map<std::string, std::string>& tags = {},
203 const std::string& /* unit */ = "") const {
204 metric m;
205 m.name = name;
206 m.value = value;
207 m.timestamp = std::chrono::system_clock::now();
208 m.tags = tags;
209 m.tags["collector"] = Derived::collector_name;
210 return m;
211 }
212
213 // Configuration
214 bool enabled_{true};
215
216 // Statistics
217 mutable std::mutex stats_mutex_;
218 std::atomic<size_t> collection_count_{0};
219 std::atomic<size_t> collection_errors_{0};
220
221 private:
225 Derived& derived() { return static_cast<Derived&>(*this); }
226 const Derived& derived() const { return static_cast<const Derived&>(*this); }
227};
228
229} // namespace monitoring
230} // namespace kcenon
CRTP base class for metric collectors.
collector_base(const collector_base &)=delete
stats_map get_statistics() const
Get collector statistics.
Derived & derived()
Get reference to derived class (CRTP helper)
std::string get_name() const
Get the name of this collector.
const Derived & derived() const
std::atomic< size_t > collection_errors_
metric create_base_metric(const std::string &name, double value, const std::unordered_map< std::string, std::string > &tags={}, const std::string &="") const
Create a metric with common tags.
bool is_enabled() const
Check if collector is enabled.
bool initialize(const config_map &config)
Initialize the collector with configuration.
std::atomic< size_t > collection_count_
bool is_healthy() const
Check if the collector is healthy.
std::vector< metric > collect()
Collect metrics from the data source.
size_t get_collection_count() const
Get collection count.
collector_base & operator=(const collector_base &)=delete
collector_base(collector_base &&)=delete
size_t get_collection_errors() const
Get error count.
std::vector< std::string > get_metric_types() const
Get supported metric types.
collector_base & operator=(collector_base &&)=delete
Adapter for metric types to support interface definitions.
std::unordered_map< std::string, double > stats_map
Type alias for statistics map.
std::unordered_map< std::string, std::string > config_map
Type alias for configuration map.
Basic metric structure for interface compatibility.
std::chrono::system_clock::time_point timestamp
std::variant< double, int64_t, std::string > value
std::unordered_map< std::string, std::string > tags