[Asterisk-code-review] res_prometheus: Add Asterisk endpoint metrics (...asterisk[master])

Matt Jordan asteriskteam at digium.com
Fri May 10 12:54:58 CDT 2019


Matt Jordan has uploaded this change for review. ( https://gerrit.asterisk.org/c/asterisk/+/11373


Change subject: res_prometheus: Add Asterisk endpoint metrics
......................................................................

res_prometheus: Add Asterisk endpoint metrics

This patch adds basic Asterisk endpoint statistics to the res_prometheus
module. This includes:

* asterisk_endpoints_state: The current state (unknown, online, offline)
  for each defined endpoint.

* asterisk_endpoints_channels_count: The current number of channels
  associated with a given endpoint.

* asterisk_endpoints_count: The current number of defined endpoints.

In all cases, enough information is provided with each endpoint metric
to determine a unique instance of Asterisk that provided the data, as well
as the underlying technology and resource definition.

ASTERISK-28403

Change-Id: I46443963330c206a7d12722d08dcaabef672310e
---
A res/prometheus/endpoints.c
M res/prometheus/prometheus_internal.h
M res/res_prometheus.c
3 files changed, 203 insertions(+), 1 deletion(-)



  git pull ssh://gerrit.asterisk.org:29418/asterisk refs/changes/73/11373/1

diff --git a/res/prometheus/endpoints.c b/res/prometheus/endpoints.c
new file mode 100644
index 0000000..b3d06bf
--- /dev/null
+++ b/res/prometheus/endpoints.c
@@ -0,0 +1,194 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2019 Sangoma, Inc.
+ *
+ * Matt Jordan <mjordan at digium.com>
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE file
+ * at the top of the source tree.
+ */
+
+/*!
+ * \file
+ * \brief Prometheus Endpoint Metrics
+ *
+ * \author Matt Jordan <mjordan at digium.com>
+ *
+ */
+
+#include "asterisk.h"
+
+#include "asterisk/stasis_endpoints.h"
+#include "asterisk/res_prometheus.h"
+#include "prometheus_internal.h"
+
+#define ENDPOINTS_STATE_HELP "Individual endpoint states. 0=unknown; 1=offline; 2=online."
+
+#define ENDPOINTS_CHANNELS_COUNT_HELP "Count of the number of channels currently existing that are associated with the endpoint."
+
+/*!
+ * \internal
+ * \brief Callback function to get an endpoint's current state
+ *
+ * \param metric The metric to populate
+ * \snapshot Endpoint snapshot
+ */
+static void get_endpoint_state(struct prometheus_metric *metric, struct ast_endpoint_snapshot *snapshot)
+{
+	snprintf(metric->value, sizeof(metric->value), "%d", snapshot->state);
+}
+
+/*!
+ * \internal
+ * \brief Callback function to get the current number of channel's associated with an endpoint
+ *
+ * \param metric The metric to populate
+ * \param snapshot Endpoint snapshot
+ */
+static void get_endpoint_channel_count(struct prometheus_metric *metric, struct ast_endpoint_snapshot *snapshot)
+{
+	snprintf(metric->value, sizeof(metric->value), "%d", snapshot->num_channels);
+}
+
+/*!
+ * \internal
+ * \brief Helper struct for generating individual endpoint stats
+ */
+struct endpoint_metric_defs {
+	/*!
+	 * \brief Help text to display
+	 */
+	const char *help;
+	/*!
+	 * \brief Name of the metric
+	 */
+	const char *name;
+	/*!
+	 * \brief Callback function to generate a metric value for a given endpoint
+	 */
+	void (* const get_value)(struct prometheus_metric *metric, struct ast_endpoint_snapshot *snapshot);
+} endpoint_metric_defs[] = {
+	{
+		.help = ENDPOINTS_STATE_HELP,
+		.name = "asterisk_endpoints_state",
+		.get_value = get_endpoint_state,
+	},
+	{
+		.help = ENDPOINTS_CHANNELS_COUNT_HELP,
+		.name = "asterisk_endpoints_channels_count",
+		.get_value = get_endpoint_channel_count,
+	},
+};
+
+/*!
+ * \internal
+ * \brief Callback invoked when Prometheus scrapes the server
+ *
+ * \param response The response to populate with formatted metrics
+ */
+static void endpoints_scrape_cb(struct ast_str **response)
+{
+	struct ao2_container *endpoints;
+	struct ao2_iterator it_endpoints;
+	struct stasis_message *message;
+	struct prometheus_metric *endpoint_metrics;
+	char eid_str[32];
+	int i, j, num_endpoints;
+	struct prometheus_metric endpoint_count = PROMETHEUS_METRIC_STATIC_INITIALIZATION(
+		PROMETHEUS_METRIC_GAUGE,
+		"asterisk_endpoints_count",
+		"Current endpoint count.",
+		NULL
+	);
+
+	ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
+
+	endpoints = stasis_cache_dump(ast_endpoint_cache(), ast_endpoint_snapshot_type());
+	if (!endpoints) {
+		return;
+	}
+	num_endpoints = ao2_container_count(endpoints);
+
+	/* Current endpoint count */
+	PROMETHEUS_METRIC_SET_LABEL(&endpoint_count, 0, "eid", eid_str);
+	snprintf(endpoint_count.value, sizeof(endpoint_count.value), "%d", num_endpoints);
+	prometheus_metric_to_string(&endpoint_count, response);
+
+	if (num_endpoints == 0) {
+		ao2_ref(endpoints, -1);
+		return;
+	}
+
+	endpoint_metrics = ast_calloc(ARRAY_LEN(endpoint_metric_defs) * num_endpoints, sizeof(*endpoint_metrics));
+	if (!endpoint_metrics) {
+		ao2_ref(endpoints, -1);
+		return;
+	}
+
+	/* Endpoint dependent values */
+	for (i = 0; i < ARRAY_LEN(endpoint_metric_defs); i++) {
+		it_endpoints = ao2_iterator_init(endpoints, 0);
+		for (j = 0; (message = ao2_iterator_next(&it_endpoints)); ao2_ref(message, -1), j++) {
+			struct ast_endpoint_snapshot *snapshot = stasis_message_data(message);
+			int index = i * num_endpoints + j;
+
+			endpoint_metrics[index].type = PROMETHEUS_METRIC_GAUGE;
+			ast_copy_string(endpoint_metrics[index].name, endpoint_metric_defs[i].name, sizeof(endpoint_metrics[index].name));
+			endpoint_metrics[index].help = endpoint_metric_defs[i].help;
+			PROMETHEUS_METRIC_SET_LABEL(&endpoint_metrics[index], 0, "eid", eid_str);
+			PROMETHEUS_METRIC_SET_LABEL(&endpoint_metrics[index], 1, "id", (snapshot->id));
+			PROMETHEUS_METRIC_SET_LABEL(&endpoint_metrics[index], 2, "tech", (snapshot->tech));
+			PROMETHEUS_METRIC_SET_LABEL(&endpoint_metrics[index], 3, "resource", (snapshot->resource));
+			endpoint_metric_defs[i].get_value(&endpoint_metrics[index], snapshot);
+
+			if (j != 0) {
+				AST_LIST_INSERT_TAIL(&endpoint_metrics[i * num_endpoints].children, &endpoint_metrics[index], entry);
+			}
+		}
+		ao2_iterator_destroy(&it_endpoints);
+
+		prometheus_metric_to_string(&endpoint_metrics[i * num_endpoints], response);
+	}
+
+	ast_free(endpoint_metrics);
+	ao2_ref(endpoints, -1);
+}
+
+struct prometheus_callback endpoints_callback = {
+	.name = "Endpoints callback",
+	.callback_fn = endpoints_scrape_cb,
+};
+
+/*!
+ * \internal
+ * \brief Callback invoked when the core module is unloaded
+ */
+static void endpoint_metrics_unload_cb(void)
+{
+	prometheus_callback_unregister(&endpoints_callback);
+}
+
+/*!
+ * \internal
+ * \brief Metrics provider definition
+ */
+static struct prometheus_metrics_provider provider = {
+	.name = "endpoints",
+	.unload_cb = endpoint_metrics_unload_cb,
+};
+
+int endpoint_metrics_init(void)
+{
+	prometheus_metrics_provider_register(&provider);
+	prometheus_callback_register(&endpoints_callback);
+
+	return 0;
+}
diff --git a/res/prometheus/prometheus_internal.h b/res/prometheus/prometheus_internal.h
index 06cff98..7eb4457 100644
--- a/res/prometheus/prometheus_internal.h
+++ b/res/prometheus/prometheus_internal.h
@@ -38,4 +38,12 @@
  */
 int channel_metrics_init(void);
 
+/*!
+ * \brief Initialize endpoint metrics
+ *
+ * \retval 0 success
+ * \retval -1 error
+ */
+int endpoint_metrics_init(void);
+
 #endif /* #define PROMETHEUS_INTERNAL_H__ */
diff --git a/res/res_prometheus.c b/res/res_prometheus.c
index ef5a7dd..05fc1ed 100644
--- a/res/res_prometheus.c
+++ b/res/res_prometheus.c
@@ -920,7 +920,7 @@
 		goto cleanup;
 	}
 
-	if (channel_metrics_init()) {
+	if (channel_metrics_init() || endpoint_metrics_init()) {
 		goto cleanup;
 	}
 

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/11373
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I46443963330c206a7d12722d08dcaabef672310e
Gerrit-Change-Number: 11373
Gerrit-PatchSet: 1
Gerrit-Owner: Matt Jordan <mjordan at digium.com>
Gerrit-MessageType: newchange
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20190510/98ba984d/attachment-0001.html>


More information about the asterisk-code-review mailing list