34#include <unordered_map>
44class metric_collector_plugin;
74 [[nodiscard]]
virtual std::string
get_name()
const = 0;
97 explicit operator bool()
const {
return success; }
144 std::lock_guard<std::mutex> lock(
mutex_);
158 template <
typename T>
161 return std::make_unique<T>();
171 std::lock_guard<std::mutex> lock(
mutex_);
181 std::lock_guard<std::mutex> lock(
mutex_);
190 std::lock_guard<std::mutex> lock(
mutex_);
191 std::vector<std::string> names;
194 names.push_back(name);
211 std::lock_guard<std::mutex> lock(
mutex_);
217 factory = it->second;
227 }
catch (
const std::exception& e) {
228 result.
error_message =
"Failed to create collector '" + name +
"': " + e.what();
231 result.
error_message =
"Unknown error creating collector: " + name;
237 if (!result.
collector->initialize(config)) {
242 }
catch (
const std::exception& e) {
244 "Exception during initialization of '" + name +
"': " + e.what();
248 result.
error_message =
"Unknown error initializing collector: " + name;
265 auto result =
create(name, config);
275 const std::unordered_map<std::string, config_map>& configs) {
276 std::vector<std::unique_ptr<collector_interface>> collectors;
277 collectors.reserve(configs.size());
279 for (
const auto& [name, config] : configs) {
280 auto result =
create(name, config);
282 collectors.push_back(std::move(result.
collector));
293 std::lock_guard<std::mutex> lock(
mutex_);
301 std::unordered_map<std::string, collector_factory_fn>
factories_;
312#define REGISTER_COLLECTOR(CollectorType) \
314 static const bool CollectorType##_registered = []() { \
315 return kcenon::monitoring::metric_factory::instance() \
316 .register_collector<CollectorType>(#CollectorType); \
Base interface for type-erased collectors.
virtual bool is_healthy() const =0
virtual ~collector_interface()=default
virtual std::string get_name() const =0
collector_interface(const collector_interface &)=delete
collector_interface(collector_interface &&)=delete
collector_interface & operator=(collector_interface &&)=delete
collector_interface & operator=(const collector_interface &)=delete
collector_interface()=default
virtual bool initialize(const config_map &config)=0
virtual std::vector< std::string > get_metric_types() const =0
Unified factory for metric collector instantiation.
std::vector< std::string > get_registered_collectors() const
Get list of registered collector names.
bool unregister_collector(const std::string &name)
Unregister a collector.
~metric_factory()=default
std::vector< std::unique_ptr< collector_interface > > create_multiple(const std::unordered_map< std::string, config_map > &configs)
Create multiple collectors from configuration.
metric_factory(const metric_factory &)=delete
std::unordered_map< std::string, collector_factory_fn > factories_
std::unique_ptr< collector_interface > create_or_null(const std::string &name, const config_map &config={})
Create a collector and return raw pointer (for compatibility)
static metric_factory & instance()
Get the singleton instance.
bool is_registered(const std::string &name) const
Check if a collector is registered.
void clear()
Clear all registered collectors.
create_result create(const std::string &name, const config_map &config={})
Create a collector instance.
metric_factory & operator=(metric_factory &&)=delete
bool register_collector(const std::string &name, collector_factory_fn factory)
Register a collector factory function.
metric_factory & operator=(const metric_factory &)=delete
metric_factory(metric_factory &&)=delete
bool register_collector(const std::string &name)
Register a collector type using template.
Unified configuration parsing utility.
std::function< std::unique_ptr< collector_interface >()> collector_factory_fn
Factory function type for creating collectors.
std::unordered_map< std::string, std::string > config_map
Type alias for configuration map.
Result of collector creation.
std::string error_message
std::unique_ptr< collector_interface > collector