diff --git a/code/components/openmetrics/openmetrics.cpp b/code/components/openmetrics/openmetrics.cpp index 4ebadbe3d..feb2eeb14 100644 --- a/code/components/openmetrics/openmetrics.cpp +++ b/code/components/openmetrics/openmetrics.cpp @@ -2,17 +2,81 @@ #include "../../include/defines.h" #include +#include "esp_private/esp_clk.h" #include "MainFlowControl.h" #include "system.h" #include "connect_wlan.h" +extern std::string getFwVersion(void); + static const char *TAG = "OPENMETRICS"; /** - * create a singe metric from the given input + * Create a hardware info metric + **/ +std::string createHardwareInfoMetric(const std::string &metricNamePrefix) +{ + return "# HELP " + metricNamePrefix + "hardware_info Hardware info\n" + + "# TYPE " + metricNamePrefix + "hardware_info info\n" + + metricNamePrefix + "_hardware_info{board_type=\"" + getBoardType() + + "\",chip_model=\"" + getChipModel() + + "\",chip_cores=\"" + std::to_string(getChipCoreCount()) + + "\",chip_revision=\"" + getChipRevision() + + "\",chip_frequency=\"" + std::to_string(esp_clk_cpu_freq()/1000000) + + "\",camera_type=\"" + Camera.getCamType() + + "\",camera_frequency=\"" + std::to_string(Camera.getCamFrequencyMhz()) + + "\",sdcard_capacity=\"" + std::to_string(getSDCardCapacity()) + + "\",sdcard_partition_size=\"" + std::to_string(getSDCardPartitionSize()) + "\"} 1\n"; +} + + +/** + * Create a network info metric + **/ +std::string createNetworkInfoMetric(const std::string &metricNamePrefix) +{ + return "# HELP " + metricNamePrefix + "network_info Network info\n" + + "# TYPE " + metricNamePrefix + "network_info info\n" + + metricNamePrefix + "_network_info{hostname=\"" + getHostname() + + "\",ipv4_address=\"" + getIPAddress() + + "\",mac_address=\"" + getMac() + "\"} 1\n"; +} + + +/** + * Create a firmware info metric + **/ +std::string createFirmwareInfoMetric(const std::string &metricNamePrefix) +{ + return "# HELP " + metricNamePrefix + "firmware_info Firmware info\n" + + "# TYPE " + metricNamePrefix + "firmware_info info\n" + + metricNamePrefix + "_firmware_info{firmware_version=\"" + getFwVersion() + "\"} 1\n"; +} + + +/** + * Create heap data metrics + **/ +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{heap_total_free=\"" + std::to_string(getESPHeapSizeTotalFree()) + "\"\n" + + metricNamePrefix + "heap_info_bytes{heap_internal_free=\"" + std::to_string(getESPHeapSizeInternalFree()) + "\"\n" + + metricNamePrefix + "heap_info_bytes{heap_internal_largest_free=\"" + std::to_string(getESPHeapSizeInternalLargestFree()) + "\"\n" + + metricNamePrefix + "heap_info_bytes{heap_internal_min_free=\"" + std::to_string(getESPHeapSizeInternalMinFree()) + "\"\n" + + metricNamePrefix + "heap_info_bytes{heap_spiram_free=\"" + std::to_string(getESPHeapSizeSPIRAMFree()) + "\"\n" + + metricNamePrefix + "heap_info_bytes{heap_spiram_largest_free=\"" + std::to_string(getESPHeapSizeSPIRAMLargestFree()) + "\"\n" + + metricNamePrefix + "heap_info_bytes{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) { @@ -22,13 +86,27 @@ 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) +{ + return "# HELP " + metricName + "_" + unit + " " + help + "\n" + + "# UNIT " + metricName + "_" + unit + " " + unit + "\n" + + "# TYPE " + metricName + "_" + unit + " " + type + "\n" + + metricName + " " + value + "\n"; +} + + /** * Generate the MetricFamily from all available sequences * @returns the string containing the text wire format of the MetricFamily **/ -std::string createSequenceMetrics(std::string prefix, const std::vector &sequences) +std::string createSequenceMetrics(const std::string &metricNamePrefix, const std::vector &sequences) { - std::string res; + std::string response = "# HELP " + metricNamePrefix + "_actual_value actual value of meter\n" + + "# TYPE " + metricNamePrefix + "_actual_value gauge\n"; for (const auto &sequence : sequences) { std::string sequenceName = sequence->name; @@ -38,15 +116,24 @@ std::string createSequenceMetrics(std::string prefix, const std::vectorsActualValue + "\n"; + response += metricNamePrefix + "_actual_value{sequence=\"" + sequenceName + "\"} " + sequence->sActualValue + "\n"; } - // prepend metadata if a valid metric was created - if (res.length() > 0) { - res = "# HELP " + prefix + "_flow_value current value of meter readout\n# TYPE " + prefix + "_flow_value gauge\n" + res; + response += "# HELP " + metricNamePrefix + "_rate_per_minute rate per minute of meter\n" + + "# TYPE " + metricNamePrefix + "_rate_per_minute gauge\n"; + + for (const auto &sequence : sequences) { + std::string sequenceName = sequence->name; + + // except newline, double quote, and backslash (https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#abnf) + // to keep it simple, these characters are just removed from the label + replaceAll(sequenceName, "\\", ""); + replaceAll(sequenceName, "\"", ""); + replaceAll(sequenceName, "\n", ""); + response += metricNamePrefix + "_rate_per_minute{sequence=\"" + sequenceName + "\"} " + sequence->sRatePerMin + "\n"; } - return res; + return response; } @@ -59,11 +146,11 @@ std::string createSequenceMetrics(std::string prefix, const std::vector &numbers); - void register_openmetrics_uri(httpd_handle_t server); #endif // OPENMETRICS_H