52#include <unordered_map>
63using config_map = std::unordered_map<std::string, std::string>;
68using stats_map = std::unordered_map<std::string, double>;
82template <
typename Derived>
101 if (
auto it = config.find(
"enabled"); it != config.end()) {
102 enabled_ = (it->second ==
"true" || it->second ==
"1");
106 return derived().do_initialize(config);
119 auto metrics =
derived().do_collect();
133 return Derived::collector_name;
141 return derived().do_get_metric_types();
152 return derived().is_available();
164 stats[
"enabled"] =
enabled_ ? 1.0 : 0.0;
169 derived().do_add_statistics(stats);
202 const std::unordered_map<std::string, std::string>& tags = {},
203 const std::string& =
"")
const {
207 m.
timestamp = std::chrono::system_clock::now();
209 m.
tags[
"collector"] = Derived::collector_name;
225 Derived&
derived() {
return static_cast<Derived&
>(*this); }
226 const Derived&
derived()
const {
return static_cast<const Derived&
>(*this); }
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.
virtual ~collector_base()=default
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