Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
kcenon::monitoring::collector_registry Class Reference

Thread-safe registry for managing collector plugins. More...

#include <collector_registry.h>

Collaboration diagram for kcenon::monitoring::collector_registry:
Collaboration graph

Public Member Functions

auto register_plugin (std::unique_ptr< collector_plugin > plugin) -> bool
 Register a plugin instance.
 
auto unregister_plugin (std::string_view name) -> bool
 Unregister a plugin by name.
 
template<typename T >
void register_factory (std::string_view name)
 Register a factory function for lazy instantiation.
 
auto get_plugin (std::string_view name) -> collector_plugin *
 Get a plugin by name.
 
auto get_plugins () -> std::vector< collector_plugin * >
 Get all registered plugins.
 
auto get_plugins_by_category (plugin_category category) -> std::vector< collector_plugin * >
 Get plugins in a specific category.
 
auto initialize_all (const config_map &config={}) -> size_t
 Initialize all registered plugins.
 
void shutdown_all ()
 Shutdown all registered plugins.
 
auto get_registry_stats () const -> std::map< std::string, size_t >
 Get registry statistics.
 
auto has_plugin (std::string_view name) const -> bool
 Check if a plugin is registered.
 
auto plugin_count () const -> size_t
 Get the number of registered plugins.
 
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.
 
auto get_plugin_loader_error () const -> std::string
 Get the last error from plugin loader.
 
void clear ()
 Clear all plugins (for testing)
 
 collector_registry (const collector_registry &)=delete
 
collector_registryoperator= (const collector_registry &)=delete
 
 collector_registry (collector_registry &&)=delete
 
collector_registryoperator= (collector_registry &&)=delete
 

Static Public Member Functions

static auto instance () -> collector_registry &
 Get the singleton instance.
 

Private Member Functions

 collector_registry ()=default
 
 ~collector_registry ()
 
auto instantiate_from_factory (const std::string &name) -> bool
 Instantiate a plugin from factory if needed.
 

Private Attributes

std::unordered_map< std::string, std::unique_ptr< collector_plugin > > plugins_
 
std::unordered_map< std::string, plugin_factory_fnfactories_
 
std::unordered_map< std::string, bool > initialized_
 
std::mutex mutex_
 
bool shutdown_ {false}
 
std::unique_ptr< dynamic_plugin_loaderplugin_loader_
 

Detailed Description

Thread-safe registry for managing collector plugins.

This class manages the lifecycle of all collector plugins in the system. It supports both eager registration (with plugin instances) and lazy registration (with factory functions).

Thread Safety:

  • All public methods are thread-safe
  • Uses internal mutex for synchronization
  • Safe to call from multiple threads concurrently

Lifecycle:

  1. Construction (via instance())
  2. Plugin registration (register_plugin, register_factory)
  3. Initialization (initialize_all)
  4. Collection (periodic calls to each plugin's collect())
  5. Shutdown (shutdown_all)
  6. Destruction (automatic on program exit)

Definition at line 82 of file collector_registry.h.

Constructor & Destructor Documentation

◆ collector_registry() [1/3]

kcenon::monitoring::collector_registry::collector_registry ( const collector_registry & )
delete

◆ collector_registry() [2/3]

kcenon::monitoring::collector_registry::collector_registry ( collector_registry && )
delete

◆ collector_registry() [3/3]

kcenon::monitoring::collector_registry::collector_registry ( )
privatedefault

◆ ~collector_registry()

kcenon::monitoring::collector_registry::~collector_registry ( )
private

Member Function Documentation

◆ clear()

void kcenon::monitoring::collector_registry::clear ( )

Clear all plugins (for testing)

Calls shutdown_all() and removes all plugins. Use with caution in production code.

◆ get_plugin()

auto kcenon::monitoring::collector_registry::get_plugin ( std::string_view name) -> collector_plugin *

Get a plugin by name.

Parameters
namePlugin name
Returns
Pointer to plugin, or nullptr if not found

If the plugin was registered via factory and not yet instantiated, this will trigger instantiation.

◆ get_plugin_loader_error()

auto kcenon::monitoring::collector_registry::get_plugin_loader_error ( ) const -> std::string

Get the last error from plugin loader.

Returns
Error message string (empty if no error)

◆ get_plugins()

auto kcenon::monitoring::collector_registry::get_plugins ( ) -> std::vector< collector_plugin * >

Get all registered plugins.

Returns
Vector of plugin pointers

Triggers instantiation of any factory-registered plugins.

◆ get_plugins_by_category()

auto kcenon::monitoring::collector_registry::get_plugins_by_category ( plugin_category category) -> std::vector< collector_plugin * >

Get plugins in a specific category.

Parameters
categoryPlugin category filter
Returns
Vector of matching plugin pointers

Triggers instantiation of any factory-registered plugins.

◆ get_registry_stats()

auto kcenon::monitoring::collector_registry::get_registry_stats ( ) const -> std::map< std::string, size_t >

Get registry statistics.

Returns
Map of statistic name to value

Available statistics:

  • "total_plugins": Total number of registered plugins
  • "initialized_plugins": Number of initialized plugins
  • "available_plugins": Number of available plugins
  • "category_<name>_count": Count per category

◆ has_plugin()

auto kcenon::monitoring::collector_registry::has_plugin ( std::string_view name) const -> bool

Check if a plugin is registered.

Parameters
namePlugin name
Returns
True if plugin exists (either instantiated or factory-registered)

◆ initialize_all()

auto kcenon::monitoring::collector_registry::initialize_all ( const config_map & config = {}) -> size_t

Initialize all registered plugins.

Parameters
configConfiguration map (optional)
Returns
Number of successfully initialized plugins

Calls initialize() on each plugin. Plugins that fail initialization remain registered but may fail collection.

◆ instance()

static auto kcenon::monitoring::collector_registry::instance ( ) -> collector_registry &
static

Get the singleton instance.

Returns
Reference to the global registry instance

Referenced by main(), kcenon::monitoring::register_builtin_collectors(), CollectorRegistryIntegrationTest::SetUp(), CollectorRegistryIntegrationTest::TearDown(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), TEST_F(), and TEST_F().

Here is the caller graph for this function:

◆ instantiate_from_factory()

auto kcenon::monitoring::collector_registry::instantiate_from_factory ( const std::string & name) -> bool
private

Instantiate a plugin from factory if needed.

Parameters
namePlugin name
Returns
True if plugin was instantiated or already exists

◆ load_plugin()

auto kcenon::monitoring::collector_registry::load_plugin ( std::string_view path) -> bool

Load a plugin from a shared library.

Parameters
pathPath to the shared library (.so/.dylib/.dll)
Returns
True if plugin loaded and registered successfully

This method:

  1. Loads the shared library using dynamic_plugin_loader
  2. Creates the plugin instance
  3. Registers it in the registry

If loading fails, check the loader's error message via get_plugin_loader_error().

◆ operator=() [1/2]

collector_registry & kcenon::monitoring::collector_registry::operator= ( collector_registry && )
delete

◆ operator=() [2/2]

collector_registry & kcenon::monitoring::collector_registry::operator= ( const collector_registry & )
delete

◆ plugin_count()

auto kcenon::monitoring::collector_registry::plugin_count ( ) const -> size_t

Get the number of registered plugins.

Returns
Count of plugins (both instantiated and factory-registered)

◆ register_factory()

template<typename T >
void kcenon::monitoring::collector_registry::register_factory ( std::string_view name)
inline

Register a factory function for lazy instantiation.

Template Parameters
TPlugin type (must derive from collector_plugin)
Parameters
namePlugin name (used for lookup)

The factory function will be called when the plugin is first accessed. This allows deferring plugin construction until needed.

Definition at line 119 of file collector_registry.h.

119 {
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 }
std::unordered_map< std::string, plugin_factory_fn > factories_

References factories_, and mutex_.

◆ register_plugin()

auto kcenon::monitoring::collector_registry::register_plugin ( std::unique_ptr< collector_plugin > plugin) -> bool

Register a plugin instance.

Parameters
pluginUnique pointer to the plugin
Returns
True if registration succeeded, false if plugin already exists

The registry takes ownership of the plugin. If a plugin with the same name already exists, registration fails. Unavailable plugins (is_available() returns false) are not registered.

◆ shutdown_all()

void kcenon::monitoring::collector_registry::shutdown_all ( )

Shutdown all registered plugins.

Calls shutdown() on each plugin in reverse registration order. Safe to call multiple times.

◆ unload_plugin()

auto kcenon::monitoring::collector_registry::unload_plugin ( std::string_view name) -> bool

Unload a dynamically loaded plugin.

Parameters
namePlugin name
Returns
True if plugin was unloaded successfully

This method:

  1. Unregisters the plugin from registry
  2. Destroys the plugin instance
  3. Unloads the shared library

Only plugins loaded via load_plugin() can be unloaded this way.

◆ unregister_plugin()

auto kcenon::monitoring::collector_registry::unregister_plugin ( std::string_view name) -> bool

Unregister a plugin by name.

Parameters
namePlugin name
Returns
True if plugin was found and removed

Calls shutdown() on the plugin before removal.

Member Data Documentation

◆ factories_

std::unordered_map<std::string, plugin_factory_fn> kcenon::monitoring::collector_registry::factories_
private

Definition at line 263 of file collector_registry.h.

Referenced by register_factory().

◆ initialized_

std::unordered_map<std::string, bool> kcenon::monitoring::collector_registry::initialized_
private

Definition at line 266 of file collector_registry.h.

◆ mutex_

std::mutex kcenon::monitoring::collector_registry::mutex_
mutableprivate

Definition at line 269 of file collector_registry.h.

Referenced by register_factory().

◆ plugin_loader_

std::unique_ptr<dynamic_plugin_loader> kcenon::monitoring::collector_registry::plugin_loader_
private

Definition at line 275 of file collector_registry.h.

◆ plugins_

std::unordered_map<std::string, std::unique_ptr<collector_plugin> > kcenon::monitoring::collector_registry::plugins_
private

Definition at line 260 of file collector_registry.h.

◆ shutdown_

bool kcenon::monitoring::collector_registry::shutdown_ {false}
private

Definition at line 272 of file collector_registry.h.

272{false};

The documentation for this class was generated from the following file: