[Asterisk-code-review] res_prometheus: Add Asterisk bridge metrics (...asterisk[master])
Friendly Automation
asteriskteam at digium.com
Tue May 21 22:19:01 CDT 2019
Friendly Automation has submitted this change and it was merged. ( https://gerrit.asterisk.org/c/asterisk/+/11374 )
Change subject: res_prometheus: Add Asterisk bridge metrics
......................................................................
res_prometheus: Add Asterisk bridge metrics
This patch adds basic Asterisk bridge statistics to the res_prometheus
module. This includes:
* asterisk_bridges_count: The current number of bridges active on the
system.
* asterisk_bridges_channels_count: The number of channels active in a
bridge.
In all cases, enough information is provided with each bridge metric
to determine a unique instance of Asterisk that provided the data, along
with the technology, subclass, and creator of the bridge.
ASTERISK-28403
Change-Id: Ie27417dd72c5bc7624eb2a7a6a8829d7551788dc
---
A res/prometheus/bridges.c
M res/prometheus/endpoints.c
M res/prometheus/prometheus_internal.h
M res/res_prometheus.c
4 files changed, 197 insertions(+), 3 deletions(-)
Approvals:
Matt Jordan: Looks good to me, approved
Friendly Automation: Approved for Submit
diff --git a/res/prometheus/bridges.c b/res/prometheus/bridges.c
new file mode 100644
index 0000000..81e51d4
--- /dev/null
+++ b/res/prometheus/bridges.c
@@ -0,0 +1,182 @@
+/*
+ * 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 Bridge Metrics
+ *
+ * \author Matt Jordan <mjordan at digium.com>
+ *
+ */
+
+#include "asterisk.h"
+
+#include "asterisk/stasis_bridges.h"
+#include "asterisk/res_prometheus.h"
+#include "prometheus_internal.h"
+
+#define BRIDGES_CHANNELS_COUNT_HELP "Number of channels in the bridge."
+
+/*!
+ * \internal
+ * \brief Callback function to get the number of channels in a bridge
+ *
+ * \param metric The metric to populate
+ * \snapshot Bridge snapshot
+ */
+static void get_bridge_channel_count(struct prometheus_metric *metric, struct ast_bridge_snapshot *snapshot)
+{
+ snprintf(metric->value, sizeof(metric->value), "%d", snapshot->num_channels);
+}
+
+/*!
+ * \internal
+ * \brief Helper struct for generating individual bridge stats
+ */
+struct bridge_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 bridge
+ */
+ void (* const get_value)(struct prometheus_metric *metric, struct ast_bridge_snapshot *snapshot);
+} bridge_metric_defs[] = {
+ {
+ .help = BRIDGES_CHANNELS_COUNT_HELP,
+ .name = "asterisk_bridges_channels_count",
+ .get_value = get_bridge_channel_count,
+ },
+};
+
+/*!
+ * \internal
+ * \brief Callback invoked when Prometheus scrapes the server
+ *
+ * \param response The response to populate with formatted metrics
+ */
+static void bridges_scrape_cb(struct ast_str **response)
+{
+ struct ao2_container *bridges;
+ struct ao2_iterator it_bridges;
+ struct ast_bridge *bridge;
+ struct prometheus_metric *bridge_metrics;
+ char eid_str[32];
+ int i, j, num_bridges;
+ struct prometheus_metric bridge_count = PROMETHEUS_METRIC_STATIC_INITIALIZATION(
+ PROMETHEUS_METRIC_GAUGE,
+ "asterisk_bridges_count",
+ "Current bridge count.",
+ NULL
+ );
+
+ ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
+
+ bridges = ast_bridges();
+ if (!bridges) {
+ return;
+ }
+ num_bridges = ao2_container_count(bridges);
+
+ /* Current endpoint count */
+ PROMETHEUS_METRIC_SET_LABEL(&bridge_count, 0, "eid", eid_str);
+ snprintf(bridge_count.value, sizeof(bridge_count.value), "%d", num_bridges);
+ prometheus_metric_to_string(&bridge_count, response);
+
+ if (num_bridges == 0) {
+ ao2_ref(bridges, -1);
+ return;
+ }
+
+ bridge_metrics = ast_calloc(ARRAY_LEN(bridge_metric_defs) * num_bridges, sizeof(*bridge_metrics));
+ if (!bridge_metrics) {
+ ao2_ref(bridges, -1);
+ return;
+ }
+
+ /* Bridge dependent values */
+ it_bridges = ao2_iterator_init(bridges, 0);
+ for (i = 0; (bridge = ao2_iterator_next(&it_bridges)); ao2_ref(bridge, -1), i++) {
+ struct ast_bridge_snapshot *snapshot = ast_bridge_get_snapshot(bridge);
+
+ for (j = 0; j < ARRAY_LEN(bridge_metric_defs); j++) {
+ int index = i * ARRAY_LEN(bridge_metric_defs) + j;
+
+ bridge_metrics[index].type = PROMETHEUS_METRIC_GAUGE;
+ ast_copy_string(bridge_metrics[index].name, bridge_metric_defs[j].name, sizeof(bridge_metrics[index].name));
+ bridge_metrics[index].help = bridge_metric_defs[j].help;
+ PROMETHEUS_METRIC_SET_LABEL(&bridge_metrics[index], 0, "eid", eid_str);
+ PROMETHEUS_METRIC_SET_LABEL(&bridge_metrics[index], 1, "id", (snapshot->uniqueid));
+ PROMETHEUS_METRIC_SET_LABEL(&bridge_metrics[index], 2, "tech", (snapshot->technology));
+ PROMETHEUS_METRIC_SET_LABEL(&bridge_metrics[index], 3, "subclass", (snapshot->subclass));
+ PROMETHEUS_METRIC_SET_LABEL(&bridge_metrics[index], 4, "creator", (snapshot->creator));
+ PROMETHEUS_METRIC_SET_LABEL(&bridge_metrics[index], 5, "name", (snapshot->name));
+ bridge_metric_defs[j].get_value(&bridge_metrics[index], snapshot);
+
+ if (i > 0) {
+ AST_LIST_INSERT_TAIL(&bridge_metrics[j].children, &bridge_metrics[index], entry);
+ }
+ }
+ ao2_ref(snapshot, -1);
+ }
+ ao2_iterator_destroy(&it_bridges);
+
+ for (j = 0; j < ARRAY_LEN(bridge_metric_defs); j++) {
+ prometheus_metric_to_string(&bridge_metrics[j], response);
+ }
+
+
+ ast_free(bridge_metrics);
+ ao2_ref(bridges, -1);
+}
+
+struct prometheus_callback bridges_callback = {
+ .name = "bridges callback",
+ .callback_fn = bridges_scrape_cb,
+};
+
+/*!
+ * \internal
+ * \brief Callback invoked when the core module is unloaded
+ */
+static void bridge_metrics_unload_cb(void)
+{
+ prometheus_callback_unregister(&bridges_callback);
+}
+
+/*!
+ * \internal
+ * \brief Metrics provider definition
+ */
+static struct prometheus_metrics_provider provider = {
+ .name = "bridges",
+ .unload_cb = bridge_metrics_unload_cb,
+};
+
+int bridge_metrics_init(void)
+{
+ prometheus_metrics_provider_register(&provider);
+ prometheus_callback_register(&bridges_callback);
+
+ return 0;
+}
\ No newline at end of file
diff --git a/res/prometheus/endpoints.c b/res/prometheus/endpoints.c
index 37ddd81..e4c7e1d 100644
--- a/res/prometheus/endpoints.c
+++ b/res/prometheus/endpoints.c
@@ -136,8 +136,9 @@
/* Endpoint dependent values */
it_endpoints = ao2_iterator_init(endpoints, 0);
for (i = 0; (message = ao2_iterator_next(&it_endpoints)); ao2_ref(message, -1), i++) {
+ struct ast_endpoint_snapshot *snapshot = stasis_message_data(message);
+
for (j = 0; j < ARRAY_LEN(endpoint_metric_defs); j++) {
- struct ast_endpoint_snapshot *snapshot = stasis_message_data(message);
int index = i * ARRAY_LEN(endpoint_metric_defs) + j;
endpoint_metrics[index].type = PROMETHEUS_METRIC_GAUGE;
@@ -153,8 +154,9 @@
AST_LIST_INSERT_TAIL(&endpoint_metrics[j].children, &endpoint_metrics[index], entry);
}
}
- ao2_iterator_destroy(&it_endpoints);
+ ao2_ref(snapshot, -1);
}
+ ao2_iterator_destroy(&it_endpoints);
for (j = 0; j < ARRAY_LEN(endpoint_metric_defs); j++) {
prometheus_metric_to_string(&endpoint_metrics[j], response);
diff --git a/res/prometheus/prometheus_internal.h b/res/prometheus/prometheus_internal.h
index 7eb4457..5ee96aa 100644
--- a/res/prometheus/prometheus_internal.h
+++ b/res/prometheus/prometheus_internal.h
@@ -46,4 +46,12 @@
*/
int endpoint_metrics_init(void);
+/*!
+ * \brief Initialize bridge metrics
+ *
+ * \retval 0 success
+ * \retval -1 error
+ */
+int bridge_metrics_init(void);
+
#endif /* #define PROMETHEUS_INTERNAL_H__ */
diff --git a/res/res_prometheus.c b/res/res_prometheus.c
index 41f449d..8a61ad9 100644
--- a/res/res_prometheus.c
+++ b/res/res_prometheus.c
@@ -917,7 +917,9 @@
goto cleanup;
}
- if (channel_metrics_init() || endpoint_metrics_init()) {
+ if (channel_metrics_init()
+ || endpoint_metrics_init()
+ || bridge_metrics_init()) {
goto cleanup;
}
--
To view, visit https://gerrit.asterisk.org/c/asterisk/+/11374
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings
Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: Ie27417dd72c5bc7624eb2a7a6a8829d7551788dc
Gerrit-Change-Number: 11374
Gerrit-PatchSet: 5
Gerrit-Owner: Matt Jordan <mjordan at digium.com>
Gerrit-Reviewer: Benjamin Keith Ford <bford at digium.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Matt Jordan <mjordan at digium.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20190521/dbba97ad/attachment-0001.html>
More information about the asterisk-code-review
mailing list