[修改]修改了错误的exporter名称

This commit is contained in:
makotocc0107
2024-09-04 09:51:10 +08:00
committed by Coding
parent 211a89778c
commit d93daaab6b
37 changed files with 0 additions and 0 deletions

View 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);
}
}

View File

@@ -0,0 +1,57 @@
//
// Created by 闫鹏宇 on 2022/7/29.
//
#ifndef OPCUA_EXPORTER_COLLECTOR_H
#define OPCUA_EXPORTER_COLLECTOR_H
#include <client/client.h>
#include <nlohmann/json.hpp>
#include <open62541/client.h>
#include <prometheus/collectable.h>
#include <prometheus/family.h>
#include <prometheus/gauge.h>
#include <prometheus/labels.h>
#include <prometheus/metric_family.h>
#include <prometheus/registry.h>
#include <simulation_manager.hpp>
#include <yaml-cpp/yaml.h>
class collector : public prometheus::Collectable {
public:
explicit collector(YAML::Node const &config,
std::shared_ptr<client> ua_client);
~collector() override = default;
std::vector<prometheus::MetricFamily> Collect() const override;
nlohmann::json const &getMetricsJson();
nlohmann::json simulationConfig(std::string const &name);
void setSimulationConfig(std::string const &name,
nlohmann::json const &config);
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<client> ua_client_;
std::vector<std::string> ua_read_node_ids;
struct st_node {
std::string name;
prometheus::Family<prometheus::Gauge> *family{nullptr};
prometheus::Gauge *real{nullptr};
prometheus::Gauge *simulation{nullptr};
simulation_manager *manager;
void setValue(double val);
};
std::map<std::string, st_node> nodes;
std::map<std::string, simulation_manager *> simulation_manager_;
nlohmann::json metrics_json_;
};
#endif // OPCUA_EXPORTER_COLLECTOR_H