[添加]opcua exporter
This commit is contained in:
127
opcua-expoter/collector/collector.cpp
Normal file
127
opcua-expoter/collector/collector.cpp
Normal file
@@ -0,0 +1,127 @@
|
||||
//
|
||||
// Created by 闫鹏宇 on 2022/7/29.
|
||||
//
|
||||
|
||||
#include "collector.h"
|
||||
|
||||
#include <utility>
|
||||
#include <spdlog/spdlog.h>
|
||||
#include "../client/client.h"
|
||||
|
||||
|
||||
collector::collector(YAML::Node const &config, std::shared_ptr<client> ua_client) :
|
||||
registry_(std::make_shared<prometheus::Registry>()),
|
||||
ua_client_(ua_client) {
|
||||
buildMetrics(config);
|
||||
}
|
||||
|
||||
void collector::clearMetrics() {
|
||||
ua_read_node_ids.clear();
|
||||
for (auto &node: nodes) {
|
||||
if (nullptr != node.second.family) {
|
||||
if (nullptr != node.second.real) {
|
||||
node.second.family->Remove(node.second.real);
|
||||
}
|
||||
if (nullptr != node.second.simulation) {
|
||||
node.second.family->Remove(node.second.simulation);
|
||||
}
|
||||
registry_->Remove(*node.second.family);
|
||||
}
|
||||
}
|
||||
nodes.clear();
|
||||
metrics_json_.clear();
|
||||
}
|
||||
|
||||
prometheus::Labels collector::getNodeLabels(YAML::Node const &labels) {
|
||||
return prometheus::Labels();
|
||||
}
|
||||
|
||||
nlohmann::json const &collector::getMetricsJson() {
|
||||
|
||||
return metrics_json_;
|
||||
}
|
||||
|
||||
void collector::buildMetrics(YAML::Node const &config) {
|
||||
clearMetrics();
|
||||
for (auto item: config) {
|
||||
if (item.IsNull() || item["name"].IsNull() || item["nodeid"].IsNull() || item["type"].IsNull()) {
|
||||
continue;
|
||||
}
|
||||
auto node_id = item["nodeid"].as<std::string>();
|
||||
auto name = item["name"].as<std::string>();
|
||||
auto help = item["help"].as<std::string>(std::string());
|
||||
metrics_json_.push_back({
|
||||
{"node_id", node_id},
|
||||
{"name", name},
|
||||
{"help", help}
|
||||
});
|
||||
ua_read_node_ids.emplace_back(node_id);
|
||||
spdlog::info("load metrics {}({}).", name, node_id);
|
||||
auto &node_family = prometheus::BuildGauge()
|
||||
.Name(name)
|
||||
.Help(help)
|
||||
.Labels(getNodeLabels(item["labels"]))
|
||||
.Register(*registry_);
|
||||
simulation_manager_[name] = new simulation_manager();
|
||||
nodes[node_id] = {
|
||||
name,
|
||||
&node_family,
|
||||
&(node_family.Add({{"data", "real"}})),
|
||||
&(node_family.Add({{"data", "simulate"}})),
|
||||
simulation_manager_[name]
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
bool collector::updateMetrics() {
|
||||
do {
|
||||
if (ua_read_node_ids.empty()) {
|
||||
break;
|
||||
}
|
||||
auto values = ua_client_->readValue(ua_read_node_ids);
|
||||
if (values.empty()) { // none value return
|
||||
break;
|
||||
}
|
||||
for (auto const &value: values) {
|
||||
auto node_iter = nodes.find(value.first);
|
||||
if (node_iter != nodes.end()) {
|
||||
node_iter->second.setValue(value.second);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
} while (false);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
std::vector<prometheus::MetricFamily> collector::Collect() const {
|
||||
if (const_cast<collector *>(this)->updateMetrics()) {
|
||||
return registry_->Collect();
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
void collector::st_node::setValue(double val) {
|
||||
if (nullptr != real) {
|
||||
real->Set(val);
|
||||
}
|
||||
if (nullptr != simulation && manager) {
|
||||
simulation->Set(manager->eval(val));
|
||||
// simulation->Set(val + 100);
|
||||
}
|
||||
}
|
||||
|
||||
nlohmann::json collector::simulationConfig(std::string const &name) {
|
||||
if (simulation_manager_.contains(name)) {
|
||||
return simulation_manager_[name]->config();
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
|
||||
void collector::setSimulationConfig(std::string const &name, nlohmann::json const &config) {
|
||||
if (simulation_manager_.contains(name)) {
|
||||
return simulation_manager_[name]->set_config(config);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user