Browse Source

xhttp_prom: export pkg stats

Ovidiu Sas 1 year ago
parent
commit
24d77c23e1

+ 76 - 0
src/modules/xhttp_prom/prom.c

@@ -35,6 +35,7 @@
 
 #include "../../core/counters.h"
 #include "../../core/ut.h"
+#include "../../core/pt.h"
 
 #include "prom.h"
 #include "prom_metric.h"
@@ -203,6 +204,74 @@ static int metric_generate(
 	return 0;
 }
 
+/**
+ * @brief Generate a string suitable for Prometheus pkgmem metric.
+ *
+ * @return 0 on success.
+ */
+static int prom_metric_pkgmem_print(prom_ctx_t *ctx)
+{
+	int i = 0;
+	uint64_t ts;
+
+	if(get_timestamp(&ts)) {
+		LM_ERR("Fail to get timestamp\n");
+		goto error;
+	}
+
+	for(; i < pkg_proc_stats_no; i++) {
+		if(prom_body_printf(ctx,
+				   "kamailio_pkgmem_used{pid=\"%u\", rank=\"%d\", desc=\"%s\"} "
+				   "%d %" PRIu64 "\n",
+				   pkg_proc_stats[i].pid, pkg_proc_stats[i].rank, pt[i].desc,
+				   pkg_proc_stats[i].used, ts)
+				== -1) {
+			LM_ERR("Fail to print\n");
+			goto error;
+		}
+		if(prom_body_printf(ctx,
+				   "kamailio_pkgmem_available{pid=\"%u\", rank=\"%d\", "
+				   "desc=\"%s\"} %d %" PRIu64 "\n",
+				   pkg_proc_stats[i].pid, pkg_proc_stats[i].rank, pt[i].desc,
+				   pkg_proc_stats[i].available, ts)
+				== -1) {
+			LM_ERR("Fail to print\n");
+			goto error;
+		}
+		if(prom_body_printf(ctx,
+				   "kamailio_pkgmem_real_used{pid=\"%u\", rank=\"%d\", "
+				   "desc=\"%s\"} %d %" PRIu64 "\n",
+				   pkg_proc_stats[i].pid, pkg_proc_stats[i].rank, pt[i].desc,
+				   pkg_proc_stats[i].real_used, ts)
+				== -1) {
+			LM_ERR("Fail to print\n");
+			goto error;
+		}
+		if(prom_body_printf(ctx,
+				   "kamailio_pkgmem_total_frags{pid=\"%u\", rank=\"%d\", "
+				   "desc=\"%s\"} %d %" PRIu64 "\n",
+				   pkg_proc_stats[i].pid, pkg_proc_stats[i].rank, pt[i].desc,
+				   pkg_proc_stats[i].total_frags, ts)
+				== -1) {
+			LM_ERR("Fail to print\n");
+			goto error;
+		}
+		if(prom_body_printf(ctx,
+				   "kamailio_pkgmem_total_size{pid=\"%u\", rank=\"%d\" "
+				   "desc=\"%s\"} %d %" PRIu64 "\n",
+				   pkg_proc_stats[i].pid, pkg_proc_stats[i].rank, pt[i].desc,
+				   pkg_proc_stats[i].total_size, ts)
+				== -1) {
+			LM_ERR("Fail to print\n");
+			goto error;
+		}
+	}
+	return 0;
+
+error:
+	return -1;
+}
+
 /**
  * @brief Statistic getter callback.
  */
@@ -241,6 +310,13 @@ int prom_stats_get(prom_ctx_t *ctx, str *stat)
 		return -1;
 	}
 
+	if(pkgmem_stats_enabled) {
+		if(prom_metric_pkgmem_print(ctx)) {
+			LM_ERR("Fail to print pkgmem metrics\n");
+			return -1;
+		}
+	}
+
 	LM_DBG("Statistics for: %.*s\n", stat->len, stat->s);
 
 	int len = stat->len;

+ 41 - 2
src/modules/xhttp_prom/xhttp_prom.c

@@ -62,6 +62,7 @@ str XHTTP_PROM_CONTENT_TYPE_TEXT_HTML = str_init("text/plain; version=0.0.4");
 
 static rpc_export_t rpc_cmds[];
 static int mod_init(void);
+static int child_init(int rank);
 static void mod_destroy(void);
 static int w_prom_check_uri(sip_msg_t *msg);
 static int w_prom_dispatch(sip_msg_t *msg);
@@ -116,8 +117,26 @@ int prom_histogram_param(modparam_t type, void *val);
  */
 static prom_ctx_t _prom_ctx;
 
+/**
+ * @brief api interface for the xhttp module
+ */
 static xhttp_api_t xhttp_api;
 
+/**
+ * @brief api interface for the kex module
+ */
+static kex_api_t kex_api;
+
+/**
+ * @brief pointer to pkgmem statistics.
+ */
+pkg_proc_stats_t *pkg_proc_stats = NULL;
+
+/**
+ * @brief number of pkgmem statistics.
+ */
+int pkg_proc_stats_no = 0;
+
 /**
  * @brief String to indicate which statistics to display.
  *
@@ -134,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 pkgmem_stats_enabled = 0; /**< enable or disable pkgmem statistics. */
+
 char error_buf[ERROR_REASON_BUF_LEN];
 
 /* module commands */
@@ -191,7 +212,8 @@ static param_export_t params[] = {{"xhttp_prom_buf_size", INT_PARAM, &buf_size},
 		{"prom_gauge", PARAM_STRING | USE_FUNC_PARAM, (void *)prom_gauge_param},
 		{"prom_histogram", PARAM_STRING | USE_FUNC_PARAM,
 				(void *)prom_histogram_param},
-		{"xhttp_prom_timeout", INT_PARAM, &timeout_minutes}, {0, 0, 0}};
+		{"xhttp_prom_timeout", INT_PARAM, &timeout_minutes},
+		{"xhttp_prom_pkg_stats", INT_PARAM, &pkgmem_stats_enabled}, {0, 0, 0}};
 
 struct module_exports exports = {
 		"xhttp_prom", DEFAULT_DLFLAGS, /* dlopen flags */
@@ -199,7 +221,7 @@ struct module_exports exports = {
 		0,							   /* exported pseudo-variables */
 		0,							   /* response function */
 		mod_init,					   /* module initialization function */
-		0,							   /* per child init function */
+		child_init,					   /* per child init function */
 		mod_destroy					   /* destroy function */
 };
 
@@ -247,6 +269,14 @@ static int mod_init(void)
 		return -1;
 	}
 
+	/* bind the kex API */
+	if(pkgmem_stats_enabled) {
+		if(load_kex_api(&kex_api) < 0) {
+			LM_ERR("cannot bind to kex API - required for pkgmem stats\n");
+			return -1;
+		}
+	}
+
 	/* Check xhttp_prom_buf_size param */
 	if(buf_size == 0)
 		buf_size = pkg_mem_size / 3;
@@ -262,6 +292,15 @@ static int mod_init(void)
 	return 0;
 }
 
+static int child_init(int rank)
+{
+	if(pkgmem_stats_enabled) {
+		pkg_proc_stats_no = kex_api.get_pkmem_stats(&pkg_proc_stats);
+	}
+
+	return 0;
+}
+
 static void mod_destroy(void)
 {
 	LM_DBG("cleaning up\n");

+ 16 - 0
src/modules/xhttp_prom/xhttp_prom.h

@@ -33,6 +33,7 @@
 
 #include "../../core/str.h"
 #include "../../core/parser/msg_parser.h"
+#include "../../modules/kex/api.h"
 
 
 #define ERROR_REASON_BUF_LEN 1024
@@ -83,4 +84,19 @@ extern str xhttp_prom_beginning;
  */
 extern int timeout_minutes;
 
+/**
+ * @brief enable or disable pkgmem statistics.
+ */
+extern int pkgmem_stats_enabled;
+
+/**
+ * @brief pointer to pkgmem statistics.
+ */
+extern pkg_proc_stats_t *pkg_proc_stats;
+
+/**
+ * @brief number of pkgmem statistics.
+ */
+extern int pkg_proc_stats_no;
+
 #endif /* _XHTTP_PROM_H */