dbpclient OK
This commit is contained in:
97
DBPcollector/DBPcollector.cpp
Normal file
97
DBPcollector/DBPcollector.cpp
Normal file
@@ -0,0 +1,97 @@
|
||||
//
|
||||
// Created by test on 2022/11/3.
|
||||
//
|
||||
|
||||
#include "DBPcollector.h"
|
||||
|
||||
DBPcollector::DBPcollector(const YAML::Node &config, std::shared_ptr<DBPclient> dbp_client) :
|
||||
registry_(std::make_shared<prometheus::Registry>()),
|
||||
dbp_client_(dbp_client) {
|
||||
buildMetrics(config);
|
||||
}
|
||||
|
||||
void DBPcollector::clearMetrics() {
|
||||
dbp_read_node_ids.clear();
|
||||
for (auto &node: nodes) {
|
||||
if (nullptr != node.second.family) {
|
||||
if (nullptr != node.second.data) {
|
||||
node.second.family->Remove(node.second.data);
|
||||
}
|
||||
registry_->Remove(*node.second.family);
|
||||
}
|
||||
}
|
||||
nodes.clear();
|
||||
metrics_json_.clear();
|
||||
}
|
||||
|
||||
void DBPcollector::buildMetrics(const YAML::Node &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}
|
||||
});
|
||||
dbp_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_);
|
||||
nodes[node_id] = {
|
||||
name,
|
||||
&node_family,
|
||||
&(node_family.Add({{"data", "data"}})),
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
bool DBPcollector::updateMetrics() {
|
||||
do {
|
||||
if (dbp_read_node_ids.empty()) {
|
||||
break;
|
||||
}
|
||||
auto values = dbp_client_->readValue(dbp_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;
|
||||
}
|
||||
|
||||
|
||||
prometheus::Labels DBPcollector::getNodeLabels(const YAML::Node &labels) {
|
||||
return prometheus::Labels();
|
||||
}
|
||||
|
||||
std::vector<prometheus::MetricFamily> DBPcollector::Collect() const {
|
||||
if (const_cast<DBPcollector *>(this)->updateMetrics()) {
|
||||
return registry_->Collect();
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
nlohmann::json const &DBPcollector::getMetricsJson() {
|
||||
return metrics_json_;
|
||||
}
|
||||
|
||||
void DBPcollector::st_node::setValue(double val) {
|
||||
if (nullptr != data) {
|
||||
data->Set(val);
|
||||
}
|
||||
}
|
||||
55
DBPcollector/DBPcollector.h
Normal file
55
DBPcollector/DBPcollector.h
Normal file
@@ -0,0 +1,55 @@
|
||||
//
|
||||
// Created by test on 2022/11/3.
|
||||
//
|
||||
|
||||
#ifndef OPCUA_EXPORTER_DBPCOLLECTOR_H
|
||||
#define OPCUA_EXPORTER_DBPCOLLECTOR_H
|
||||
|
||||
#include <prometheus/collectable.h>
|
||||
#include <prometheus/family.h>
|
||||
#include <prometheus/labels.h>
|
||||
#include <prometheus/metric_family.h>
|
||||
#include <prometheus/registry.h>
|
||||
#include <prometheus/gauge.h>
|
||||
#include <yaml-cpp/yaml.h>
|
||||
#include <DBPclient/DBPclient.h>
|
||||
#include <json.hpp>
|
||||
#include <spdlog/spdlog.h>
|
||||
|
||||
class DBPcollector : public prometheus::Collectable {
|
||||
public:
|
||||
explicit DBPcollector(YAML::Node const &config, std::shared_ptr<DBPclient> dbp_client);
|
||||
|
||||
~DBPcollector() override = default;
|
||||
|
||||
std::vector<prometheus::MetricFamily> Collect() const override;
|
||||
|
||||
nlohmann::json const &getMetricsJson();
|
||||
|
||||
private:
|
||||
void buildMetrics(YAML::Node const &config);
|
||||
|
||||
bool updateMetrics();
|
||||
|
||||
void clearMetrics();
|
||||
|
||||
prometheus::Labels getNodeLabels(YAML::Node const &labels);
|
||||
|
||||
private:
|
||||
std::shared_ptr<prometheus::Registry> registry_;
|
||||
std::shared_ptr<DBPclient> dbp_client_;
|
||||
std::vector<std::string> dbp_read_node_ids;
|
||||
|
||||
struct st_node {
|
||||
std::string name;
|
||||
prometheus::Family<prometheus::Gauge> *family{nullptr};
|
||||
prometheus::Gauge *data{nullptr};
|
||||
void setValue(double val);
|
||||
};
|
||||
|
||||
std::map<std::string, st_node> nodes;
|
||||
nlohmann::json metrics_json_;
|
||||
};
|
||||
|
||||
|
||||
#endif //OPCUA_EXPORTER_DBPCOLLECTOR_H
|
||||
Reference in New Issue
Block a user