Prometheus API: Applied code formatting

This commit is contained in:
Thomas Basler 2022-11-10 19:28:48 +01:00
parent bfd112d268
commit 0fdf7f19e4
2 changed files with 19 additions and 19 deletions

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include <ESPAsyncWebServer.h>
#include "Hoymiles.h" #include "Hoymiles.h"
#include <ESPAsyncWebServer.h>
class WebApiPrometheusClass { class WebApiPrometheusClass {
public: public:
@ -11,7 +11,7 @@ public:
private: private:
void onPrometheusMetrics(AsyncWebServerRequest* request); void onPrometheusMetrics(AsyncWebServerRequest* request);
void addField(AsyncResponseStream *stream, const char* serial, uint8_t idx, std::shared_ptr<InverterAbstract> inv, uint8_t channel, uint8_t fieldId, const char* channelName = NULL); void addField(AsyncResponseStream* stream, const char* serial, uint8_t idx, std::shared_ptr<InverterAbstract> inv, uint8_t channel, uint8_t fieldId, const char* channelName = NULL);
AsyncWebServer* _server; AsyncWebServer* _server;
}; };

View File

@ -1,8 +1,8 @@
#include "WebApi_prometheus.h" #include "WebApi_prometheus.h"
#include "Configuration.h" #include "Configuration.h"
#include "NetworkSettings.h"
#include "Hoymiles.h" #include "Hoymiles.h"
#include "NetworkSettings.h"
void WebApiPrometheusClass::init(AsyncWebServer* server) void WebApiPrometheusClass::init(AsyncWebServer* server)
{ {
@ -23,28 +23,28 @@ void WebApiPrometheusClass::onPrometheusMetrics(AsyncWebServerRequest* request)
stream->print(F("# HELP opendtu_build Build info\n")); stream->print(F("# HELP opendtu_build Build info\n"));
stream->print(F("# TYPE opendtu_build gauge\n")); stream->print(F("# TYPE opendtu_build gauge\n"));
stream->printf("opendtu_build{name=\"%s\",id=\"%s\",version=\"%d.%d.%d\"} 1\n", stream->printf("opendtu_build{name=\"%s\",id=\"%s\",version=\"%d.%d.%d\"} 1\n",
NetworkSettings.getHostname().c_str(), AUTO_GIT_HASH, CONFIG_VERSION >> 24 & 0xff, CONFIG_VERSION >> 16 & 0xff, CONFIG_VERSION >> 8 & 0xff); NetworkSettings.getHostname().c_str(), AUTO_GIT_HASH, CONFIG_VERSION >> 24 & 0xff, CONFIG_VERSION >> 16 & 0xff, CONFIG_VERSION >> 8 & 0xff);
stream->print(F("# HELP opendtu_platform Platform info\n")); stream->print(F("# HELP opendtu_platform Platform info\n"));
stream->print(F("# TYPE opendtu_platform gauge\n")); stream->print(F("# TYPE opendtu_platform gauge\n"));
stream->printf("opendtu_platform{arch=\"%s\",mac=\"%s\"} 1\n", ESP.getChipModel(), WiFi.macAddress().c_str()); stream->printf("opendtu_platform{arch=\"%s\",mac=\"%s\"} 1\n", ESP.getChipModel(), WiFi.macAddress().c_str());
stream->print(F("# HELP opendtu_uptime Uptime in seconds\n")); stream->print(F("# HELP opendtu_uptime Uptime in seconds\n"));
stream->print(F("# TYPE opendtu_uptime counter\n")); stream->print(F("# TYPE opendtu_uptime counter\n"));
stream->printf("opendtu_uptime %lld\n", esp_timer_get_time() / 1000000); stream->printf("opendtu_uptime %lld\n", esp_timer_get_time() / 1000000);
stream->print(F("# HELP opendtu_heap_size System memory size\n")); stream->print(F("# HELP opendtu_heap_size System memory size\n"));
stream->print(F("# TYPE opendtu_heap_size gauge\n")); stream->print(F("# TYPE opendtu_heap_size gauge\n"));
stream->printf("opendtu_heap_size %zu\n", ESP.getHeapSize()); stream->printf("opendtu_heap_size %zu\n", ESP.getHeapSize());
stream->print(F("# HELP opendtu_free_heap_size System free memory\n")); stream->print(F("# HELP opendtu_free_heap_size System free memory\n"));
stream->print(F("# TYPE opendtu_free_heap_size gauge\n")); stream->print(F("# TYPE opendtu_free_heap_size gauge\n"));
stream->printf("opendtu_free_heap_size %zu\n", ESP.getFreeHeap()); stream->printf("opendtu_free_heap_size %zu\n", ESP.getFreeHeap());
stream->print(F("# HELP wifi_rssi WiFi RSSI\n")); stream->print(F("# HELP wifi_rssi WiFi RSSI\n"));
stream->print(F("# TYPE wifi_rssi gauge\n")); stream->print(F("# TYPE wifi_rssi gauge\n"));
stream->printf("wifi_rssi %d\n", WiFi.RSSI()); stream->printf("wifi_rssi %d\n", WiFi.RSSI());
for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) { for (uint8_t i = 0; i < Hoymiles.getNumInverters(); i++) {
auto inv = Hoymiles.getInverterByPos(i); auto inv = Hoymiles.getInverterByPos(i);
@ -54,12 +54,12 @@ void WebApiPrometheusClass::onPrometheusMetrics(AsyncWebServerRequest* request)
((uint32_t)((inv->serial() >> 32) & 0xFFFFFFFF)), ((uint32_t)((inv->serial() >> 32) & 0xFFFFFFFF)),
((uint32_t)(inv->serial() & 0xFFFFFFFF))); ((uint32_t)(inv->serial() & 0xFFFFFFFF)));
const char* name = inv->name(); const char* name = inv->name();
if(i==0) { if (i == 0) {
stream->print(F("# HELP opendtu_last_update last update from inverter in s\n")); stream->print(F("# HELP opendtu_last_update last update from inverter in s\n"));
stream->print(F("# TYPE opendtu_last_update gauge\n")); stream->print(F("# TYPE opendtu_last_update gauge\n"));
} }
stream->printf("opendtu_last_update{serial=\"%s\",unit=\"%d\",name=\"%s\"} %d\n", stream->printf("opendtu_last_update{serial=\"%s\",unit=\"%d\",name=\"%s\"} %d\n",
serial, i, name, inv->Statistics()->getLastUpdate() / 1000); serial, i, name, inv->Statistics()->getLastUpdate() / 1000);
// Loop all channels // Loop all channels
for (uint8_t c = 0; c <= inv->Statistics()->getChannelCount(); c++) { for (uint8_t c = 0; c <= inv->Statistics()->getChannelCount(); c++) {
@ -88,13 +88,13 @@ void WebApiPrometheusClass::onPrometheusMetrics(AsyncWebServerRequest* request)
request->send(stream); request->send(stream);
} }
void WebApiPrometheusClass::addField(AsyncResponseStream *stream, const char* serial, uint8_t idx, std::shared_ptr<InverterAbstract> inv, uint8_t channel, uint8_t fieldId, const char* channelName) void WebApiPrometheusClass::addField(AsyncResponseStream* stream, const char* serial, uint8_t idx, std::shared_ptr<InverterAbstract> inv, uint8_t channel, uint8_t fieldId, const char* channelName)
{ {
if (inv->Statistics()->hasChannelFieldValue(channel, fieldId)) { if (inv->Statistics()->hasChannelFieldValue(channel, fieldId)) {
const char* chanName = (channelName == NULL) ? inv->Statistics()->getChannelFieldName(channel, fieldId) : channelName; const char* chanName = (channelName == NULL) ? inv->Statistics()->getChannelFieldName(channel, fieldId) : channelName;
if(idx==0 && channel==0) { if (idx == 0 && channel == 0) {
stream->printf("# HELP opendtu_%s in %s\n", chanName, inv->Statistics()->getChannelFieldUnit(channel, fieldId)); stream->printf("# HELP opendtu_%s in %s\n", chanName, inv->Statistics()->getChannelFieldUnit(channel, fieldId));
stream->printf("# TYPE opendtu_%s gauge\n", chanName); stream->printf("# TYPE opendtu_%s gauge\n", chanName);
} }
stream->printf("opendtu_%s{serial=\"%s\",unit=\"%d\",name=\"%s\",channel=\"%d\"} %f\n", chanName, serial, idx, inv->name(), channel, inv->Statistics()->getChannelFieldValue(channel, fieldId)); stream->printf("opendtu_%s{serial=\"%s\",unit=\"%d\",name=\"%s\",channel=\"%d\"} %f\n", chanName, serial, idx, inv->name(), channel, inv->Statistics()->getChannelFieldValue(channel, fieldId));
} }