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

Unified factory for metric collector instantiation. More...

#include <metric_factory.h>

Collaboration diagram for kcenon::monitoring::metric_factory:
Collaboration graph

Public Member Functions

 ~metric_factory ()=default
 
 metric_factory (const metric_factory &)=delete
 
metric_factoryoperator= (const metric_factory &)=delete
 
 metric_factory (metric_factory &&)=delete
 
metric_factoryoperator= (metric_factory &&)=delete
 
bool register_collector (const std::string &name, collector_factory_fn factory)
 Register a collector factory function.
 
template<typename T >
bool register_collector (const std::string &name)
 Register a collector type using template.
 
bool unregister_collector (const std::string &name)
 Unregister a collector.
 
bool is_registered (const std::string &name) const
 Check if a collector is registered.
 
std::vector< std::string > get_registered_collectors () const
 Get list of registered collector names.
 
create_result create (const std::string &name, const config_map &config={})
 Create a collector instance.
 
std::unique_ptr< collector_interfacecreate_or_null (const std::string &name, const config_map &config={})
 Create a collector and return raw pointer (for compatibility)
 
std::vector< std::unique_ptr< collector_interface > > create_multiple (const std::unordered_map< std::string, config_map > &configs)
 Create multiple collectors from configuration.
 
void clear ()
 Clear all registered collectors.
 

Static Public Member Functions

static metric_factoryinstance ()
 Get the singleton instance.
 

Private Member Functions

 metric_factory ()=default
 

Private Attributes

std::mutex mutex_
 
std::unordered_map< std::string, collector_factory_fnfactories_
 

Detailed Description

Unified factory for metric collector instantiation.

This singleton factory provides centralized creation and configuration of metric collectors. Features include:

  • Type-safe collector registration
  • Centralized configuration validation
  • Consistent error handling
  • Support for custom collectors

Thread-safe for all operations.

Definition at line 118 of file metric_factory.h.

Constructor & Destructor Documentation

◆ ~metric_factory()

kcenon::monitoring::metric_factory::~metric_factory ( )
default

◆ metric_factory() [1/3]

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

◆ metric_factory() [2/3]

kcenon::monitoring::metric_factory::metric_factory ( metric_factory && )
delete

◆ metric_factory() [3/3]

kcenon::monitoring::metric_factory::metric_factory ( )
privatedefault

Member Function Documentation

◆ clear()

void kcenon::monitoring::metric_factory::clear ( )
inline

Clear all registered collectors.

Definition at line 292 of file metric_factory.h.

292 {
293 std::lock_guard<std::mutex> lock(mutex_);
294 factories_.clear();
295 }
std::unordered_map< std::string, collector_factory_fn > factories_

References factories_, and mutex_.

◆ create()

create_result kcenon::monitoring::metric_factory::create ( const std::string & name,
const config_map & config = {} )
inline

Create a collector instance.

Parameters
nameThe collector name
configConfiguration options
Returns
create_result containing the collector or error information

Definition at line 205 of file metric_factory.h.

205 {}) {
206 create_result result;
207
208 // Find factory
209 collector_factory_fn factory;
210 {
211 std::lock_guard<std::mutex> lock(mutex_);
212 auto it = factories_.find(name);
213 if (it == factories_.end()) {
214 result.error_message = "Unknown collector: " + name;
215 return result;
216 }
217 factory = it->second;
218 }
219
220 // Create collector
221 try {
222 result.collector = factory();
223 if (!result.collector) {
224 result.error_message = "Factory returned null for: " + name;
225 return result;
226 }
227 } catch (const std::exception& e) {
228 result.error_message = "Failed to create collector '" + name + "': " + e.what();
229 return result;
230 } catch (...) {
231 result.error_message = "Unknown error creating collector: " + name;
232 return result;
233 }
234
235 // Initialize collector
236 try {
237 if (!result.collector->initialize(config)) {
238 result.error_message = "Initialization failed for: " + name;
239 result.collector.reset();
240 return result;
241 }
242 } catch (const std::exception& e) {
243 result.error_message =
244 "Exception during initialization of '" + name + "': " + e.what();
245 result.collector.reset();
246 return result;
247 } catch (...) {
248 result.error_message = "Unknown error initializing collector: " + name;
249 result.collector.reset();
250 return result;
251 }
252
253 result.success = true;
254 return result;
255 }
std::function< std::unique_ptr< collector_interface >()> collector_factory_fn
Factory function type for creating collectors.

Referenced by create_multiple().

Here is the caller graph for this function:

◆ create_multiple()

std::vector< std::unique_ptr< collector_interface > > kcenon::monitoring::metric_factory::create_multiple ( const std::unordered_map< std::string, config_map > & configs)
inline

Create multiple collectors from configuration.

Parameters
configsMap of collector name to configuration
Returns
Vector of created collectors (only successfully created ones)

Definition at line 274 of file metric_factory.h.

275 {
276 std::vector<std::unique_ptr<collector_interface>> collectors;
277 collectors.reserve(configs.size());
278
279 for (const auto& [name, config] : configs) {
280 auto result = create(name, config);
281 if (result) {
282 collectors.push_back(std::move(result.collector));
283 }
284 }
285
286 return collectors;
287 }
create_result create(const std::string &name, const config_map &config={})
Create a collector instance.

References kcenon::monitoring::create_result::collector, and create().

Here is the call graph for this function:

◆ create_or_null()

std::unique_ptr< collector_interface > kcenon::monitoring::metric_factory::create_or_null ( const std::string & name,
const config_map & config = {} )
inline

Create a collector and return raw pointer (for compatibility)

Parameters
nameThe collector name
configConfiguration options
Returns
Unique pointer to collector, or nullptr on failure

Definition at line 263 of file metric_factory.h.

264 {}) {
265 auto result = create(name, config);
266 return std::move(result.collector);
267 }

◆ get_registered_collectors()

std::vector< std::string > kcenon::monitoring::metric_factory::get_registered_collectors ( ) const
inline

Get list of registered collector names.

Returns
Vector of registered collector names

Definition at line 189 of file metric_factory.h.

189 {
190 std::lock_guard<std::mutex> lock(mutex_);
191 std::vector<std::string> names;
192 names.reserve(factories_.size());
193 for (const auto& [name, _] : factories_) {
194 names.push_back(name);
195 }
196 return names;
197 }

References factories_, and mutex_.

◆ instance()

static metric_factory & kcenon::monitoring::metric_factory::instance ( )
inlinestatic

Get the singleton instance.

Returns
Reference to the global factory instance

Definition at line 124 of file metric_factory.h.

124 {
126 return instance;
127 }
static metric_factory & instance()
Get the singleton instance.

References instance().

Referenced by instance(), main(), kcenon::monitoring::register_crtp_collector(), kcenon::monitoring::register_plugin_collector(), and kcenon::monitoring::register_standalone_collector().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ is_registered()

bool kcenon::monitoring::metric_factory::is_registered ( const std::string & name) const
inline

Check if a collector is registered.

Parameters
nameThe collector name to check
Returns
true if collector is registered

Definition at line 180 of file metric_factory.h.

180 {
181 std::lock_guard<std::mutex> lock(mutex_);
182 return factories_.find(name) != factories_.end();
183 }

References factories_, and mutex_.

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ register_collector() [1/2]

template<typename T >
bool kcenon::monitoring::metric_factory::register_collector ( const std::string & name)
inline

Register a collector type using template.

Template Parameters
TThe collector type (must derive from collector_interface)
Parameters
nameThe collector name (identifier)
Returns
true if registration successful

Definition at line 159 of file metric_factory.h.

159 {
160 return register_collector(name, []() {
161 return std::make_unique<T>();
162 });
163 }
bool register_collector(const std::string &name, collector_factory_fn factory)
Register a collector factory function.

References register_collector().

Here is the call graph for this function:

◆ register_collector() [2/2]

bool kcenon::monitoring::metric_factory::register_collector ( const std::string & name,
collector_factory_fn factory )
inline

Register a collector factory function.

Parameters
nameThe collector name (identifier)
factoryFactory function that creates the collector
Returns
true if registration successful, false if name already exists

Definition at line 143 of file metric_factory.h.

143 {
144 std::lock_guard<std::mutex> lock(mutex_);
145 if (factories_.find(name) != factories_.end()) {
146 return false;
147 }
148 factories_[name] = std::move(factory);
149 return true;
150 }

References factories_, and mutex_.

Referenced by register_collector(), kcenon::monitoring::register_crtp_collector(), kcenon::monitoring::register_plugin_collector(), and kcenon::monitoring::register_standalone_collector().

Here is the caller graph for this function:

◆ unregister_collector()

bool kcenon::monitoring::metric_factory::unregister_collector ( const std::string & name)
inline

Unregister a collector.

Parameters
nameThe collector name to unregister
Returns
true if collector was registered and removed

Definition at line 170 of file metric_factory.h.

170 {
171 std::lock_guard<std::mutex> lock(mutex_);
172 return factories_.erase(name) > 0;
173 }

References factories_, and mutex_.

Member Data Documentation

◆ factories_

std::unordered_map<std::string, collector_factory_fn> kcenon::monitoring::metric_factory::factories_
private

◆ mutex_

std::mutex kcenon::monitoring::metric_factory::mutex_
mutableprivate

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