From 4c077f87c7a13303f376dbeb7e90135f2d019206 Mon Sep 17 00:00:00 2001 From: Slider0007 Date: Sat, 15 Jun 2024 15:18:09 +0200 Subject: [PATCH] Update --- code/components/openmetrics/CMakeLists.txt | 2 +- code/components/openmetrics/openmetrics.cpp | 110 ++++++++++++-------- code/components/openmetrics/openmetrics.h | 4 +- 3 files changed, 66 insertions(+), 50 deletions(-) diff --git a/code/components/openmetrics/CMakeLists.txt b/code/components/openmetrics/CMakeLists.txt index 8470774ad..17ab47dd0 100644 --- a/code/components/openmetrics/CMakeLists.txt +++ b/code/components/openmetrics/CMakeLists.txt @@ -2,6 +2,6 @@ FILE(GLOB_RECURSE app_sources ${CMAKE_CURRENT_SOURCE_DIR}/*.*) idf_component_register(SRCS ${app_sources} INCLUDE_DIRS "." - REQUIRES jomjol_helper) + REQUIRES jomjol_helper jomjol_flowcontroll jomjol_wlan) diff --git a/code/components/openmetrics/openmetrics.cpp b/code/components/openmetrics/openmetrics.cpp index b92f2d243..afb86c349 100644 --- a/code/components/openmetrics/openmetrics.cpp +++ b/code/components/openmetrics/openmetrics.cpp @@ -1,11 +1,15 @@ #include "openmetrics.h" #include "../../include/defines.h" +#include +#include + #include #include "esp_private/esp_clk.h" -#include "MainFlowControl.h" #include "system.h" +#include "ClassFlowDefineTypes.h" +#include "MainFlowControl.h" #include "connect_wlan.h" extern std::string getFwVersion(void); @@ -19,8 +23,8 @@ static const char *TAG = "OPENMETRICS"; **/ std::string createHardwareInfoMetric(const std::string &metricNamePrefix) { - return "# HELP " + metricNamePrefix + "hardware_info Hardware info\n" + - "# TYPE " + metricNamePrefix + "hardware_info gauge\n" + + return "# TYPE " + metricNamePrefix + "hardware_info gauge\n" + + "# HELP " + metricNamePrefix + "hardware_info Hardware info\n" + metricNamePrefix + "hardware_info{board_type=\"" + getBoardType() + "\",chip_model=\"" + getChipModel() + "\",chip_cores=\"" + std::to_string(getChipCoreCount()) + @@ -38,8 +42,8 @@ std::string createHardwareInfoMetric(const std::string &metricNamePrefix) **/ std::string createNetworkInfoMetric(const std::string &metricNamePrefix) { - return "# HELP " + metricNamePrefix + "network_info Network info\n" + - "# TYPE " + metricNamePrefix + "network_info gauge\n" + + return "# TYPE " + metricNamePrefix + "network_info gauge\n" + + "# HELP " + metricNamePrefix + "network_info Network info\n" + metricNamePrefix + "network_info{hostname=\"" + getHostname() + "\",ipv4_address=\"" + getIPAddress() + "\",mac_address=\"" + getMac() + "\"} 1\n"; @@ -51,8 +55,8 @@ std::string createNetworkInfoMetric(const std::string &metricNamePrefix) **/ std::string createFirmwareInfoMetric(const std::string &metricNamePrefix) { - return "# HELP " + metricNamePrefix + "firmware_info Firmware info\n" + - "# TYPE " + metricNamePrefix + "firmware_info gauge\n" + + return "# TYPE " + metricNamePrefix + "firmware_info gauge\n" + + "# HELP " + metricNamePrefix + "firmware_info Firmware info\n" + metricNamePrefix + "firmware_info{firmware_version=\"" + getFwVersion() + "\"} 1\n"; } @@ -62,26 +66,32 @@ std::string createFirmwareInfoMetric(const std::string &metricNamePrefix) **/ std::string createHeapDataMetric(const std::string &metricNamePrefix) { - return "# HELP " + metricNamePrefix + "heap_info_bytes Heap info\n" + - "# UNIT " + metricNamePrefix + "heap_info_bytes bytes\n" + - "# TYPE " + metricNamePrefix + "heap_info_bytes gauge\n" + - metricNamePrefix + "heap_info_bytes{type=\"heap_total_free\"} " + std::to_string(getESPHeapSizeTotalFree()) + "\n" + - metricNamePrefix + "heap_info_bytes{type=\"heap_internal_free\"} " + std::to_string(getESPHeapSizeInternalFree()) + "\n" + - metricNamePrefix + "heap_info_bytes{type=\"heap_internal_largest_free\"} " + std::to_string(getESPHeapSizeInternalLargestFree()) + "\n" + - metricNamePrefix + "heap_info_bytes{type=\"heap_internal_min_free\"} " + std::to_string(getESPHeapSizeInternalMinFree()) + "\n" + - metricNamePrefix + "heap_info_bytes{type=\"heap_spiram_free\"} " + std::to_string(getESPHeapSizeSPIRAMFree()) + "\n" + - metricNamePrefix + "heap_info_bytes{type=\"heap_spiram_largest_free\"} " + std::to_string(getESPHeapSizeSPIRAMLargestFree()) + "\n" + - metricNamePrefix + "heap_info_bytes{type=\"heap_spiram_min_free\"} " + std::to_string(getESPHeapSizeSPIRAMMinFree()) + "\n"; + return "# TYPE " + metricNamePrefix + "heap_data_bytes gauge\n" + + "# UNIT " + metricNamePrefix + "heap_data_bytes bytes\n" + + "# HELP " + metricNamePrefix + "heap_data_bytes Heap data\n" + + metricNamePrefix + "heap_data_bytes{heap_data=\"heap_total_free\"} " + std::to_string(getESPHeapSizeTotalFree()) + "\n" + + metricNamePrefix + "heap_data_bytes{heap_data=\"heap_internal_free\"} " + std::to_string(getESPHeapSizeInternalFree()) + "\n" + + metricNamePrefix + "heap_data_bytes{heap_data=\"heap_internal_largest_free\"} " + std::to_string(getESPHeapSizeInternalLargestFree()) + "\n" + + metricNamePrefix + "heap_data_bytes{heap_data=\"heap_internal_min_free\"} " + std::to_string(getESPHeapSizeInternalMinFree()) + "\n" + + metricNamePrefix + "heap_data_bytes{heap_data=\"heap_spiram_free\"} " + std::to_string(getESPHeapSizeSPIRAMFree()) + "\n" + + metricNamePrefix + "heap_data_bytes{heap_data=\"heap_spiram_largest_free\"} " + std::to_string(getESPHeapSizeSPIRAMLargestFree()) + "\n" + + metricNamePrefix + "heap_data_bytes{heap_data=\"heap_spiram_min_free\"} " + std::to_string(getESPHeapSizeSPIRAMMinFree()) + "\n"; } /** * Create a generic single metric **/ -std::string createMetric(const std::string &metricName, const std::string &help, const std::string &type, const std::string &value) +std::string createMetric(const std::string &metricName, const std::string &type, const std::string &help, const std::string &value) { - return "# HELP " + metricName + " " + help + "\n" + - "# TYPE " + metricName + " " + type + "\n" + + if (type == "counter") { + return "# TYPE " + metricName + " " + type + "\n" + + "# HELP " + metricName + " " + help + "\n" + + metricName + "_total " + value + "\n"; + } + + return "# TYPE " + metricName + " " + type + "\n" + + "# HELP " + metricName + " " + help + "\n" + metricName + " " + value + "\n"; } @@ -89,12 +99,19 @@ std::string createMetric(const std::string &metricName, const std::string &help, /** * Create a generic single metric with unit **/ -std::string createMetricWithUnit(const std::string &metricName, const std::string &help, const std::string &type, - const std::string &unit, const std::string &value) +std::string createMetricWithUnit(const std::string &metricName, const std::string &type, const std::string &unit, + const std::string &help, const std::string &value) { - return "# HELP " + metricName + "_" + unit + " " + help + "\n" + + if (type == "counter") { + return "# TYPE " + metricName + "_" + unit + " " + type + "\n" + + "# UNIT " + metricName + "_" + unit + " " + unit + "\n" + + "# HELP " + metricName + "_" + unit + " " + help + "\n" + + metricName + "_" + unit + "_total " + value + "\n"; + } + + return "# TYPE " + metricName + "_" + unit + " " + type + "\n" + "# UNIT " + metricName + "_" + unit + " " + unit + "\n" + - "# TYPE " + metricName + "_" + unit + " " + type + "\n" + + "# HELP " + metricName + "_" + unit + " " + help + "\n" + metricName + "_" + unit + " " + value + "\n"; } @@ -121,15 +138,16 @@ std::string createSequenceMetrics(const std::string &metricNamePrefix, const std } // Return if no valid value is available - if (response.empty()) { + if (response.empty()) return response; - } - response += "# HELP " + metricNamePrefix + "actual_value Actual value of meter\n" + - "# TYPE " + metricNamePrefix + "actual_value gauge\n" + response; + // Add metadata to value data if values are avialbale + response = "# TYPE " + metricNamePrefix + "actual_value gauge\n" + + "# HELP " + metricNamePrefix + "actual_value Actual value of meter\n" + response; - response += "# HELP " + metricNamePrefix + "rate_per_minute Rate per minute of meter\n" + - "# TYPE " + metricNamePrefix + "rate_per_minute gauge\n"; + // Add rate per minute + response += "# TYPE " + metricNamePrefix + "rate_per_minute gauge\n" + + "# HELP " + metricNamePrefix + "rate_per_minute Rate per minute of meter\n"; for (const auto &sequence : sequences) { std::string sequenceName = sequence->name; @@ -186,39 +204,39 @@ esp_err_t handler_openmetrics(httpd_req_t *req) response += createFirmwareInfoMetric(metricNamePrefix); // Device uptime - response += createMetricWithUnit(metricNamePrefix + "device_uptime", "Device uptime in seconds", - "gauge", "seconds", std::to_string((long)getUptime())); + response += createMetricWithUnit(metricNamePrefix + "device_uptime", "gauge", "seconds", + "Device uptime in seconds", std::to_string((long)getUptime())); // WLAN signal strength - response += createMetricWithUnit(metricNamePrefix + "wlan_rssi", "WLAN signal strength in dBm", - "gauge", "dBm", std::to_string(get_WIFI_RSSI())); + response += createMetricWithUnit(metricNamePrefix + "wlan_rssi", "gauge", "dBm", + "WLAN signal strength in dBm", std::to_string(get_WIFI_RSSI())); // CPU temperature - response += createMetricWithUnit(metricNamePrefix + "chip_temp", "CPU temperature in celsius", - "gauge", "celsius", std::to_string((int)getSOCTemperature())); + response += createMetricWithUnit(metricNamePrefix + "chip_temp", "gauge", "celsius", + "CPU temperature in celsius", std::to_string((int)getSOCTemperature())); // Heap data response += createHeapDataMetric(metricNamePrefix); // SD card partition free space - response += createMetricWithUnit(metricNamePrefix + "sd_partition_free", "Free SD partition space in MB", - "gauge", "MB", std::to_string(getSDCardFreePartitionSpace())); + response += createMetricWithUnit(metricNamePrefix + "sd_partition_free", "gauge", "megabytes", + "Free SD partition space in megabytes", std::to_string(getSDCardFreePartitionSpace())); // Process error state - response += createMetric(metricNamePrefix + "process_error", "Process error state", - "gauge", std::to_string(flowctrl.getFlowStateErrorOrDeviation())); + response += createMetric(metricNamePrefix + "process_error", "gauge", + "Process error state", std::to_string(flowctrl.getFlowStateErrorOrDeviation())); // Processing interval - response += createMetricWithUnit(metricNamePrefix + "process_interval", "Processing interval", - "gauge", "minutes", to_stringWithPrecision(flowctrl.getProcessInterval(), 1)); + response += createMetricWithUnit(metricNamePrefix + "process_interval", "gauge", "minutes", + "Processing interval", to_stringWithPrecision(flowctrl.getProcessInterval(), 1)); // Processing time - response += createMetricWithUnit(metricNamePrefix + "process_time", "Processing time of one cycle", - "gauge", "seconds", std::to_string(getFlowProcessingTime())); + response += createMetricWithUnit(metricNamePrefix + "process_time", "gauge", "seconds", + "Processing time of one cycle", std::to_string(getFlowProcessingTime())); // Process cycles - response += createMetric(metricNamePrefix + "cycle_counter", "Process cycles since device startup", - "counter", std::to_string(getFlowCycleCounter())); + response += createMetric(metricNamePrefix + "cycle_counter", "counter", + "Process cycles since device startup", std::to_string(getFlowCycleCounter())); // Actual measurement values response += createSequenceMetrics(metricNamePrefix, flowctrl.getNumbers()); diff --git a/code/components/openmetrics/openmetrics.h b/code/components/openmetrics/openmetrics.h index 8915cdd7b..273ceab0d 100644 --- a/code/components/openmetrics/openmetrics.h +++ b/code/components/openmetrics/openmetrics.h @@ -1,10 +1,8 @@ #ifndef OPENMETRICS_H #define OPENMETRICS_H -#include -#include +#include -#include "ClassFlowDefineTypes.h" void register_openmetrics_uri(httpd_handle_t server);