res_prometheus: Add Asterisk bridge metrics
authorMatt Jordan <mjordan@digium.com>
Thu, 9 May 2019 14:41:49 +0000 (09:41 -0500)
committerMatt Jordan <mjordan@digium.com>
Wed, 22 May 2019 02:43:02 +0000 (21:43 -0500)
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

res/prometheus/bridges.c [new file with mode: 0644]
res/prometheus/endpoints.c
res/prometheus/prometheus_internal.h
res/res_prometheus.c

diff --git a/res/prometheus/bridges.c b/res/prometheus/bridges.c
new file mode 100644 (file)
index 0000000..81e51d4
--- /dev/null
@@ -0,0 +1,182 @@
+/*
+ * Asterisk -- An open source telephony toolkit.
+ *
+ * Copyright (C) 2019 Sangoma, Inc.
+ *
+ * Matt Jordan <mjordan@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@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
index 37ddd81..e4c7e1d 100644 (file)
@@ -136,8 +136,9 @@ static void endpoints_scrape_cb(struct ast_str **response)
        /* 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 @@ static void endpoints_scrape_cb(struct ast_str **response)
                                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);
index 7eb4457..5ee96aa 100644 (file)
@@ -46,4 +46,12 @@ int channel_metrics_init(void);
  */
 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__ */
index 41f449d..8a61ad9 100644 (file)
@@ -917,7 +917,9 @@ static int load_module(void)
                goto cleanup;
        }
 
-       if (channel_metrics_init() || endpoint_metrics_init()) {
+       if (channel_metrics_init()
+               || endpoint_metrics_init()
+               || bridge_metrics_init()) {
                goto cleanup;
        }