Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
Slider0007 committed Jun 15, 2024
1 parent 432bb72 commit 4c077f8
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 50 deletions.
2 changes: 1 addition & 1 deletion code/components/openmetrics/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)


110 changes: 64 additions & 46 deletions code/components/openmetrics/openmetrics.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#include "openmetrics.h"
#include "../../include/defines.h"

#include <string>
#include <vector>

#include <esp_log.h>
#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);
Expand All @@ -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()) +
Expand All @@ -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";
Expand All @@ -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";
}

Expand All @@ -62,39 +66,52 @@ 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";
}


/**
* 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";
}

Expand All @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
4 changes: 1 addition & 3 deletions code/components/openmetrics/openmetrics.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
#ifndef OPENMETRICS_H
#define OPENMETRICS_H

#include <string>
#include <vector>
#include <esp_http_server.h>

#include "ClassFlowDefineTypes.h"

void register_openmetrics_uri(httpd_handle_t server);

Expand Down

0 comments on commit 4c077f8

Please sign in to comment.