Monitoring System 0.1.0
System resource monitoring with pluggable collectors and alerting
Loading...
Searching...
No Matches
collector_adapters.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
16#include <memory>
17#include <string>
18#include <vector>
19
20#include "metric_factory.h"
21
22namespace kcenon::monitoring {
23
30template <typename T>
32 public:
33 plugin_collector_adapter() : collector_(std::make_unique<T>()) {}
34
35 bool initialize(const config_map& config) override {
36 return collector_->initialize(config);
37 }
38
39 [[nodiscard]] std::string get_name() const override { return std::string(collector_->name()); }
40
41 [[nodiscard]] bool is_healthy() const override { return collector_->is_available(); }
42
43 [[nodiscard]] std::vector<std::string> get_metric_types() const override {
44 return collector_->get_metric_types();
45 }
46
51 [[nodiscard]] T* get_collector() { return collector_.get(); }
52 [[nodiscard]] const T* get_collector() const { return collector_.get(); }
53
54 private:
55 std::unique_ptr<T> collector_;
56};
57
64template <typename T>
66 public:
67 crtp_collector_adapter() : collector_(std::make_unique<T>()) {}
68
69 bool initialize(const config_map& config) override {
70 return collector_->initialize(config);
71 }
72
73 [[nodiscard]] std::string get_name() const override { return collector_->get_name(); }
74
75 [[nodiscard]] bool is_healthy() const override { return collector_->is_available(); }
76
77 [[nodiscard]] std::vector<std::string> get_metric_types() const override {
78 return collector_->get_metric_types();
79 }
80
85 [[nodiscard]] T* get_collector() { return collector_.get(); }
86 [[nodiscard]] const T* get_collector() const { return collector_.get(); }
87
88 private:
89 std::unique_ptr<T> collector_;
90};
91
99template <typename T>
101 public:
102 standalone_collector_adapter() : collector_(std::make_unique<T>()) {}
103
104 bool initialize(const config_map& config) override {
105 return collector_->initialize(config);
106 }
107
108 [[nodiscard]] std::string get_name() const override { return collector_->get_name(); }
109
110 [[nodiscard]] bool is_healthy() const override { return collector_->is_healthy(); }
111
112 [[nodiscard]] std::vector<std::string> get_metric_types() const override {
113 return collector_->get_metric_types();
114 }
115
120 [[nodiscard]] T* get_collector() { return collector_.get(); }
121 [[nodiscard]] const T* get_collector() const { return collector_.get(); }
122
123 private:
124 std::unique_ptr<T> collector_;
125};
126
133template <typename T>
134bool register_plugin_collector(const std::string& name) {
135 return metric_factory::instance().register_collector(name, []() {
136 return std::make_unique<plugin_collector_adapter<T>>();
137 });
138}
139
146template <typename T>
147bool register_crtp_collector(const std::string& name) {
148 return metric_factory::instance().register_collector(name, []() {
149 return std::make_unique<crtp_collector_adapter<T>>();
150 });
151}
152
159template <typename T>
160bool register_standalone_collector(const std::string& name) {
161 return metric_factory::instance().register_collector(name, []() {
162 return std::make_unique<standalone_collector_adapter<T>>();
163 });
164}
165
166} // namespace kcenon::monitoring
Base interface for type-erased collectors.
Adapter for CRTP-based collectors (collector_base<T>)
bool initialize(const config_map &config) override
std::vector< std::string > get_metric_types() const override
T * get_collector()
Get the underlying collector.
static metric_factory & instance()
Get the singleton instance.
bool register_collector(const std::string &name, collector_factory_fn factory)
Register a collector factory function.
Adapter for plugin-based collectors (metric_collector_plugin)
T * get_collector()
Get the underlying collector.
bool initialize(const config_map &config) override
std::vector< std::string > get_metric_types() const override
Adapter for standalone collectors (like vm_collector)
std::vector< std::string > get_metric_types() const override
bool initialize(const config_map &config) override
T * get_collector()
Get the underlying collector.
Unified factory for metric collector instantiation.
bool register_standalone_collector(const std::string &name)
Helper function to register a standalone collector.
bool register_crtp_collector(const std::string &name)
Helper function to register a CRTP-based collector.
bool register_plugin_collector(const std::string &name)
Helper function to register a plugin-based collector.
std::unordered_map< std::string, std::string > config_map
Type alias for configuration map.