Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
collector_registry.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
46#include <functional>
47#include <map>
48#include <memory>
49#include <mutex>
50#include <string>
51#include <string_view>
52#include <unordered_map>
53#include <vector>
54
55#include "collector_plugin.h"
56#include "plugin_loader.h"
57
58namespace kcenon {
59namespace monitoring {
60
83public:
88 static auto instance() -> collector_registry&;
89
99 auto register_plugin(std::unique_ptr<collector_plugin> plugin) -> bool;
100
108 auto unregister_plugin(std::string_view name) -> bool;
109
118 template <typename T>
119 void register_factory(std::string_view name) {
120 static_assert(std::is_base_of_v<collector_plugin, T>,
121 "T must derive from collector_plugin");
122
123 std::lock_guard<std::mutex> lock(mutex_);
124 factories_[std::string(name)] = []() -> std::unique_ptr<collector_plugin> {
125 return std::make_unique<T>();
126 };
127 }
128
137 auto get_plugin(std::string_view name) -> collector_plugin*;
138
145 auto get_plugins() -> std::vector<collector_plugin*>;
146
155 -> std::vector<collector_plugin*>;
156
165 auto initialize_all(const config_map& config = {}) -> size_t;
166
174
185 auto get_registry_stats() const -> std::map<std::string, size_t>;
186
192 auto has_plugin(std::string_view name) const -> bool;
193
198 auto plugin_count() const -> size_t;
199
212 auto load_plugin(std::string_view path) -> bool;
213
226 auto unload_plugin(std::string_view name) -> bool;
227
232 auto get_plugin_loader_error() const -> std::string;
233
240 void clear();
241
242 // Non-copyable, non-moveable
244 collector_registry& operator=(const collector_registry&) = delete;
247
248private:
251
257 auto instantiate_from_factory(const std::string& name) -> bool;
258
259 // Plugin storage
260 std::unordered_map<std::string, std::unique_ptr<collector_plugin>> plugins_;
261
262 // Factory storage for lazy instantiation
263 std::unordered_map<std::string, plugin_factory_fn> factories_;
264
265 // Initialization tracking
266 std::unordered_map<std::string, bool> initialized_;
267
268 // Thread safety
269 mutable std::mutex mutex_;
270
271 // Shutdown flag
272 bool shutdown_{false};
273
274 // Dynamic plugin loader
275 std::unique_ptr<dynamic_plugin_loader> plugin_loader_;
276};
277
278} // namespace monitoring
279} // namespace kcenon
Pure virtual interface for metric collector plugins.
Thread-safe registry for managing collector plugins.
auto get_registry_stats() const -> std::map< std::string, size_t >
Get registry statistics.
auto register_plugin(std::unique_ptr< collector_plugin > plugin) -> bool
Register a plugin instance.
auto plugin_count() const -> size_t
Get the number of registered plugins.
std::unordered_map< std::string, std::unique_ptr< collector_plugin > > plugins_
std::unordered_map< std::string, bool > initialized_
auto get_plugins() -> std::vector< collector_plugin * >
Get all registered plugins.
auto has_plugin(std::string_view name) const -> bool
Check if a plugin is registered.
auto load_plugin(std::string_view path) -> bool
Load a plugin from a shared library.
auto unload_plugin(std::string_view name) -> bool
Unload a dynamically loaded plugin.
void shutdown_all()
Shutdown all registered plugins.
auto unregister_plugin(std::string_view name) -> bool
Unregister a plugin by name.
auto get_plugin_loader_error() const -> std::string
Get the last error from plugin loader.
std::unique_ptr< dynamic_plugin_loader > plugin_loader_
auto get_plugins_by_category(plugin_category category) -> std::vector< collector_plugin * >
Get plugins in a specific category.
std::unordered_map< std::string, plugin_factory_fn > factories_
void clear()
Clear all plugins (for testing)
auto instantiate_from_factory(const std::string &name) -> bool
Instantiate a plugin from factory if needed.
auto initialize_all(const config_map &config={}) -> size_t
Initialize all registered plugins.
auto get_plugin(std::string_view name) -> collector_plugin *
Get a plugin by name.
static auto instance() -> collector_registry &
Get the singleton instance.
void register_factory(std::string_view name)
Register a factory function for lazy instantiation.
Plugin interface for metric collectors.
std::unique_ptr< collector_plugin >(*)() plugin_factory_fn
Type alias for plugin factory function.
plugin_category
Categorization of collector plugins.
std::unordered_map< std::string, std::string > config_map
Type alias for configuration map.
Dynamic plugin loading from shared libraries.