error handling and cleanup
This commit is contained in:
@ -1,3 +1,8 @@
|
||||
/**
|
||||
* @file metrics.c
|
||||
* @brief Implementation of Prometheus metrics endpoint.
|
||||
*/
|
||||
|
||||
#include "metrics.h"
|
||||
#include "outputs.h"
|
||||
#include "inputs.h"
|
||||
@ -15,41 +20,50 @@
|
||||
#include <time.h>
|
||||
#include <sys/time.h>
|
||||
|
||||
static const char *TAG = "smart-oil-heater-control-system-metrics";
|
||||
static const char *TAG = "metrics";
|
||||
|
||||
char caHtmlResponse[HTML_RESPONSE_SIZE];
|
||||
SemaphoreHandle_t xMutexAccessMetricResponse = NULL;
|
||||
static char caHtmlResponse[HTML_RESPONSE_SIZE];
|
||||
static SemaphoreHandle_t xMutexAccessMetricResponse = NULL;
|
||||
static sMetric aMetrics[METRIC_MAX_COUNT];
|
||||
static uint16_t u16MetricCounter = 0U;
|
||||
|
||||
void taskMetrics(void *pvParameters);
|
||||
httpd_handle_t setup_server(void);
|
||||
esp_err_t get_metrics_handler(httpd_req_t *req);
|
||||
/* Private function prototypes */
|
||||
static void taskMetrics(void *pvParameters);
|
||||
static httpd_handle_t setup_server(void);
|
||||
static esp_err_t get_metrics_handler(httpd_req_t *req);
|
||||
|
||||
void initMetrics(void)
|
||||
esp_err_t initMetrics(void)
|
||||
{
|
||||
setup_server();
|
||||
httpd_handle_t server = setup_server();
|
||||
if (server == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to start HTTP server");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
BaseType_t taskCreated = xTaskCreate(
|
||||
taskMetrics, // Function to implement the task
|
||||
"taskMetrics", // Task name
|
||||
32768, // Stack size (in words, not bytes)
|
||||
NULL, // Parameters to the task function (none in this case)
|
||||
5, // Task priority (higher number = higher priority)
|
||||
NULL // Task handle (optional)
|
||||
);
|
||||
taskMetrics,
|
||||
"taskMetrics",
|
||||
32768,
|
||||
NULL,
|
||||
5,
|
||||
NULL);
|
||||
|
||||
if (taskCreated == pdPASS)
|
||||
{
|
||||
ESP_LOGI(TAG, "Task created successfully!");
|
||||
}
|
||||
else
|
||||
if (taskCreated != pdPASS)
|
||||
{
|
||||
ESP_LOGE(TAG, "Failed to create task");
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Initialized successfully");
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
void taskMetrics(void *pvParameters)
|
||||
/**
|
||||
* @brief Metrics collection task.
|
||||
* @param pvParameters Task parameters (unused).
|
||||
*/
|
||||
static void taskMetrics(void *pvParameters)
|
||||
{
|
||||
while (1)
|
||||
{
|
||||
@ -338,8 +352,6 @@ void vSetMetrics(sMetric *paMetrics, uint16_t u16Size)
|
||||
break;
|
||||
}
|
||||
|
||||
// printf("%s\n", paMetrics[u16Index].caMetricName);
|
||||
// printf("%s\n", caValueBuffer);
|
||||
strcat(caHtmlResponse, paMetrics[u16Index].caMetricName);
|
||||
strcat(caHtmlResponse, caValueBuffer);
|
||||
strcat(caHtmlResponse, "\n");
|
||||
@ -352,7 +364,12 @@ void vSetMetrics(sMetric *paMetrics, uint16_t u16Size)
|
||||
}
|
||||
}
|
||||
|
||||
esp_err_t get_metrics_handler(httpd_req_t *req)
|
||||
/**
|
||||
* @brief HTTP GET handler for /metrics endpoint.
|
||||
* @param req HTTP request.
|
||||
* @return ESP_OK on success.
|
||||
*/
|
||||
static esp_err_t get_metrics_handler(httpd_req_t *req)
|
||||
{
|
||||
if (xSemaphoreTakeRecursive(xMutexAccessMetricResponse, pdMS_TO_TICKS(5000)) == pdTRUE)
|
||||
{
|
||||
@ -367,7 +384,11 @@ esp_err_t get_metrics_handler(httpd_req_t *req)
|
||||
}
|
||||
}
|
||||
|
||||
httpd_handle_t setup_server(void)
|
||||
/**
|
||||
* @brief Setup HTTP server for metrics endpoint.
|
||||
* @return HTTP server handle or NULL on failure.
|
||||
*/
|
||||
static httpd_handle_t setup_server(void)
|
||||
{
|
||||
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
|
||||
config.server_port = 9100;
|
||||
@ -382,14 +403,17 @@ httpd_handle_t setup_server(void)
|
||||
xMutexAccessMetricResponse = xSemaphoreCreateRecursiveMutex();
|
||||
if (xMutexAccessMetricResponse == NULL)
|
||||
{
|
||||
ESP_LOGE(TAG, "Unable to create mutex for metric response");
|
||||
ESP_LOGE(TAG, "Failed to create mutex");
|
||||
return NULL;
|
||||
}
|
||||
xSemaphoreGiveRecursive(xMutexAccessMetricResponse);
|
||||
|
||||
if (httpd_start(&server, &config) == ESP_OK)
|
||||
{
|
||||
httpd_register_uri_handler(server, &uri_get);
|
||||
return server;
|
||||
}
|
||||
|
||||
return server;
|
||||
ESP_LOGE(TAG, "Failed to start HTTP server");
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user