Module: kamailio
Branch: master
Commit: 6af0ae461486554481cbace41d50b8639b2ade8a
URL:
https://github.com/kamailio/kamailio/commit/6af0ae461486554481cbace41d50b86…
Author: Ovidiu Sas <osas(a)voipembedded.com>
Committer: Ovidiu Sas <osas(a)voipembedded.com>
Date: 2024-04-05T18:21:29Z
xhttp_prom: new uptime statistic
---
Modified: src/modules/xhttp_prom/prom.c
Modified: src/modules/xhttp_prom/xhttp_prom.c
Modified: src/modules/xhttp_prom/xhttp_prom.h
---
Diff:
https://github.com/kamailio/kamailio/commit/6af0ae461486554481cbace41d50b86…
Patch:
https://github.com/kamailio/kamailio/commit/6af0ae461486554481cbace41d50b86…
---
diff --git a/src/modules/xhttp_prom/prom.c b/src/modules/xhttp_prom/prom.c
index 786a2c3e40a..efe111b752a 100644
--- a/src/modules/xhttp_prom/prom.c
+++ b/src/modules/xhttp_prom/prom.c
@@ -33,6 +33,7 @@
#include <inttypes.h>
#include <stdarg.h>
+#include "../../core/globals.h"
#include "../../core/counters.h"
#include "../../core/ut.h"
#include "../../core/pt.h"
@@ -204,6 +205,36 @@ static int metric_generate(
return 0;
}
+/**
+ * @brief Generate a string suitable for Prometheus pkgmem metric.
+ *
+ * @return 0 on success.
+ */
+static int prom_metric_uptime_print(prom_ctx_t *ctx)
+{
+ int uptime;
+ time_t now;
+ uint64_t ts;
+
+ if(get_timestamp(&ts)) {
+ LM_ERR("Fail to get timestamp\n");
+ goto error;
+ }
+
+ time(&now);
+ uptime = (int)(now - up_since);
+ if(prom_body_printf(ctx, "%.*suptime %d %" PRIu64 "\n",
+ xhttp_prom_beginning.len, xhttp_prom_beginning.s, uptime, ts)
+ == -1) {
+ LM_ERR("Fail to print\n");
+ goto error;
+ }
+ return 0;
+
+error:
+ return -1;
+}
+
/**
* @brief Generate a string suitable for Prometheus pkgmem metric.
*
@@ -322,6 +353,13 @@ int prom_stats_get(prom_ctx_t *ctx, str *stat)
}
}
+ if(uptime_stat_enabled) {
+ if(prom_metric_uptime_print(ctx)) {
+ LM_ERR("Fail to print uptime metric\n");
+ return -1;
+ }
+ }
+
LM_DBG("Statistics for: %.*s\n", stat->len, stat->s);
int len = stat->len;
diff --git a/src/modules/xhttp_prom/xhttp_prom.c b/src/modules/xhttp_prom/xhttp_prom.c
index 6eaffb640a3..f54cfb19f17 100644
--- a/src/modules/xhttp_prom/xhttp_prom.c
+++ b/src/modules/xhttp_prom/xhttp_prom.c
@@ -153,6 +153,8 @@ int buf_size = 0; /**< size of buffer that contains the reply. */
int timeout_minutes = 60; /**< timeout in minutes to delete old metrics. */
+int uptime_stat_enabled = 0; /**< enable or disable uptime statistic. */
+
int pkgmem_stats_enabled = 0; /**< enable or disable pkgmem statistics. */
char error_buf[ERROR_REASON_BUF_LEN];
@@ -213,6 +215,7 @@ static param_export_t params[] = {{"xhttp_prom_buf_size",
INT_PARAM, &buf_size},
{"prom_histogram", PARAM_STRING | USE_FUNC_PARAM,
(void *)prom_histogram_param},
{"xhttp_prom_timeout", INT_PARAM, &timeout_minutes},
+ {"xhttp_prom_uptime_stat", INT_PARAM, &uptime_stat_enabled},
{"xhttp_prom_pkg_stats", INT_PARAM, &pkgmem_stats_enabled}, {0, 0, 0}};
struct module_exports exports = {
diff --git a/src/modules/xhttp_prom/xhttp_prom.h b/src/modules/xhttp_prom/xhttp_prom.h
index 12851368df4..fa1aa611ae2 100644
--- a/src/modules/xhttp_prom/xhttp_prom.h
+++ b/src/modules/xhttp_prom/xhttp_prom.h
@@ -84,6 +84,11 @@ extern str xhttp_prom_beginning;
*/
extern int timeout_minutes;
+/**
+ * @brief enable or disable uptime statistic.
+ */
+extern int uptime_stat_enabled;
+
/**
* @brief enable or disable pkgmem statistics.
*/