This file demonstrates how to create a collector plugin that can be loaded as a shared library at runtime.
#include <chrono>
#include <random>
public:
auto name()
const -> std::string_view
override {
return "example_plugin";
}
(void)config;
return true;
}
}
auto collect() -> std::vector<metric_data>
override {
std::vector<metric_data> metrics;
return metrics;
}
metric_data cpu_metric;
cpu_metric.name = "example.cpu_usage";
cpu_metric.unit = "%";
cpu_metric.timestamp = std::chrono::system_clock::now();
cpu_metric.labels["plugin"] = "example";
cpu_metric.labels["type"] = "cpu";
metrics.push_back(cpu_metric);
metric_data memory_metric;
memory_metric.name = "example.memory_usage";
memory_metric.unit = "MB";
memory_metric.timestamp = std::chrono::system_clock::now();
memory_metric.labels["plugin"] = "example";
memory_metric.labels["type"] = "memory";
metrics.push_back(memory_metric);
metric_data counter_metric;
counter_metric.name = "example.request_count";
counter_metric.unit = "requests";
counter_metric.timestamp = std::chrono::system_clock::now();
counter_metric.labels["plugin"] = "example";
counter_metric.labels["type"] = "counter";
metrics.push_back(counter_metric);
return metrics;
}
return true;
}
return plugin_metadata_t{
plugin_category::custom,
plugin_type::collector,
"Example Plugin",
"1.0.0",
"Demonstrates dynamic plugin loading"
};
}
private:
std::uniform_real_distribution<double> dist(min, max);
}
};
IMPLEMENT_PLUGIN(
"example_plugin",
"1.0.0",
"Example dynamically loaded collector plugin",
"kcenon",
"custom"
)
Example collector plugin that generates dummy metrics.
auto collect() -> std::vector< metric_data > override
Collect current metrics from this plugin.
auto initialize(const config_map &config) -> bool override
Initialize plugin with configuration.
uint64_t request_counter_
auto generate_random_value(double min, double max) -> double
auto shutdown() -> void override
Shutdown plugin and release resources.
auto get_metadata() const -> plugin_metadata_t override
Get plugin metadata.
auto name() const -> std::string_view override
Get the unique name of this plugin.
auto is_available() const -> bool override
Check if this plugin is available on the current system.
Pure virtual interface for metric collector plugins.
Plugin interface for metric collectors.
std::unordered_map< std::string, std::string > config_map
Type alias for configuration map.
C API for dynamically loaded collector plugins.