<p>Friendly Automation <strong>merged</strong> this change.</p><p><a href="https://gerrit.asterisk.org/c/asterisk/+/11375">View Change</a></p><div style="white-space:pre-wrap">Approvals:
Matt Jordan: Looks good to me, approved
Friendly Automation: Approved for Submit
</div><pre style="font-family: monospace,monospace; white-space: pre-wrap;">res_prometheus: Add CLI commands<br><br>This patch adds a few CLI commands to the res_prometheus module to aid<br>system administrators setting up and configuring the module. This includes:<br><br>* prometheus show status: Display basic statistics about the Prometheus<br> module, including its essential configuration, when it was last scraped,<br> and how long the scrape took. The last two bits of information are useful<br> when Prometheus isn't generating metrics appropriately, as it will at<br> least tell you if Asterisk has had its HTTP route hit by the remote<br> server.<br><br>* prometheus show metrics: Dump the current metrics to the CLI. Useful for<br> system administrators to see what metrics are currently available without<br> having to cURL or go to Prometheus itself.<br><br>ASTERISK-28403<br><br>Change-Id: Ic09813e5e14b901571c5c96ebeae2a02566c5172<br>---<br>M res/prometheus/bridges.c<br>A res/prometheus/cli.c<br>M res/prometheus/endpoints.c<br>M res/prometheus/prometheus_internal.h<br>M res/res_prometheus.c<br>5 files changed, 253 insertions(+), 22 deletions(-)<br><br></pre><pre style="font-family: monospace,monospace; white-space: pre-wrap;"><span>diff --git a/res/prometheus/bridges.c b/res/prometheus/bridges.c</span><br><span>index 81e51d4..47dee9a 100644</span><br><span>--- a/res/prometheus/bridges.c</span><br><span>+++ b/res/prometheus/bridges.c</span><br><span>@@ -145,7 +145,6 @@</span><br><span> prometheus_metric_to_string(&bridge_metrics[j], response);</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span> ast_free(bridge_metrics);</span><br><span> ao2_ref(bridges, -1);</span><br><span> }</span><br><span>diff --git a/res/prometheus/cli.c b/res/prometheus/cli.c</span><br><span>new file mode 100644</span><br><span>index 0000000..e99d0b5</span><br><span>--- /dev/null</span><br><span>+++ b/res/prometheus/cli.c</span><br><span>@@ -0,0 +1,143 @@</span><br><span style="color: hsl(120, 100%, 40%);">+/*</span><br><span style="color: hsl(120, 100%, 40%);">+ * Asterisk -- An open source telephony toolkit.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Copyright (C) 2019 Sangoma, Inc.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * Matt Jordan <mjordan@digium.com></span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * See http://www.asterisk.org for more information about</span><br><span style="color: hsl(120, 100%, 40%);">+ * the Asterisk project. Please do not directly contact</span><br><span style="color: hsl(120, 100%, 40%);">+ * any of the maintainers of this project for assistance;</span><br><span style="color: hsl(120, 100%, 40%);">+ * the project provides a web site, mailing lists and IRC</span><br><span style="color: hsl(120, 100%, 40%);">+ * channels for your use.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * This program is free software, distributed under the terms of</span><br><span style="color: hsl(120, 100%, 40%);">+ * the GNU General Public License Version 2. See the LICENSE file</span><br><span style="color: hsl(120, 100%, 40%);">+ * at the top of the source tree.</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \file</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Prometheus CLI Commands</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \author Matt Jordan <mjordan@digium.com></span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk.h"</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/cli.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/localtime.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "asterisk/res_prometheus.h"</span><br><span style="color: hsl(120, 100%, 40%);">+#include "prometheus_internal.h"</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static char *prometheus_show_metrics(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_str *response;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (cmd == CLI_INIT) {</span><br><span style="color: hsl(120, 100%, 40%);">+ e->command = "prometheus show metrics";</span><br><span style="color: hsl(120, 100%, 40%);">+ e->usage =</span><br><span style="color: hsl(120, 100%, 40%);">+ "Usage: prometheus show metrics\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ " Displays the current metrics and their values,\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ " without counting as an actual scrape.\n";</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (cmd == CLI_GENERATE) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (a->argc != 3) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return CLI_SHOWUSAGE;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ response = prometheus_scrape_to_string();</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!response) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "Egads! An unknown error occurred getting the metrics\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ return CLI_FAILURE;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "%s\n", ast_str_buffer(response));</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_free(response);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return CLI_SUCCESS;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static char *prometheus_show_status(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct prometheus_general_config *config;</span><br><span style="color: hsl(120, 100%, 40%);">+ char time_buffer[64];</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_tm last_scrape_local;</span><br><span style="color: hsl(120, 100%, 40%);">+ struct timeval last_scrape_time;</span><br><span style="color: hsl(120, 100%, 40%);">+ int64_t scrape_duration;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (cmd == CLI_INIT) {</span><br><span style="color: hsl(120, 100%, 40%);">+ e->command = "prometheus show status";</span><br><span style="color: hsl(120, 100%, 40%);">+ e->usage =</span><br><span style="color: hsl(120, 100%, 40%);">+ "Usage: prometheus show status\n"</span><br><span style="color: hsl(120, 100%, 40%);">+ " Displays the status of metrics collection.\n";</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ } else if (cmd == CLI_GENERATE) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (a->argc != 3) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return CLI_SHOWUSAGE;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ config = prometheus_general_config_get();</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "Prometheus Metrics Status:\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "\tEnabled: %s\n", config->enabled ? "Yes" : "No");</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "\tURI: %s\n", config->uri);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "\tBasic Auth: %s\n", ast_strlen_zero(config->auth_username) ? "No": "Yes");</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "\tLast Scrape Time: ");</span><br><span style="color: hsl(120, 100%, 40%);">+ last_scrape_time = prometheus_last_scrape_time_get();</span><br><span style="color: hsl(120, 100%, 40%);">+ if (last_scrape_time.tv_sec == 0 && last_scrape_time.tv_usec == 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ snprintf(time_buffer, sizeof(time_buffer), "%s", "(N/A)");</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_localtime(&last_scrape_time, &last_scrape_local, NULL);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_strftime(time_buffer, sizeof(time_buffer), "%Y-%m-%d %H:%M:%S", &last_scrape_local);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "%s\n", time_buffer);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "\tLast Scrape Duration: ");</span><br><span style="color: hsl(120, 100%, 40%);">+ scrape_duration = prometheus_last_scrape_duration_get();</span><br><span style="color: hsl(120, 100%, 40%);">+ if (scrape_duration < 0) {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "(N/A)\n");</span><br><span style="color: hsl(120, 100%, 40%);">+ } else {</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli(a->fd, "%" PRIu64 " ms\n", scrape_duration);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ao2_ref(config, -1);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return CLI_SUCCESS;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+static struct ast_cli_entry cli_prometheus[] = {</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_CLI_DEFINE(prometheus_show_metrics, "Display the current metrics and their values"),</span><br><span style="color: hsl(120, 100%, 40%);">+ AST_CLI_DEFINE(prometheus_show_status, "Display the status of Prometheus metrics collection"),</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \internal</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Callback invoked when the core module is unloaded</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static void cli_unload_cb(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli_unregister_multiple(cli_prometheus, ARRAY_LEN(cli_prometheus));</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \internal</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Provider definition</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+static struct prometheus_metrics_provider provider = {</span><br><span style="color: hsl(120, 100%, 40%);">+ .name = "cli",</span><br><span style="color: hsl(120, 100%, 40%);">+ .unload_cb = cli_unload_cb,</span><br><span style="color: hsl(120, 100%, 40%);">+};</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+int cli_init(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ prometheus_metrics_provider_register(&provider);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_cli_register_multiple(cli_prometheus, ARRAY_LEN(cli_prometheus));</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return 0;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span>\ No newline at end of file</span><br><span>diff --git a/res/prometheus/endpoints.c b/res/prometheus/endpoints.c</span><br><span>index e4c7e1d..5f758c5 100644</span><br><span>--- a/res/prometheus/endpoints.c</span><br><span>+++ b/res/prometheus/endpoints.c</span><br><span>@@ -154,7 +154,6 @@</span><br><span> AST_LIST_INSERT_TAIL(&endpoint_metrics[j].children, &endpoint_metrics[index], entry);</span><br><span> }</span><br><span> }</span><br><span style="color: hsl(0, 100%, 40%);">- ao2_ref(snapshot, -1);</span><br><span> }</span><br><span> ao2_iterator_destroy(&it_endpoints);</span><br><span> </span><br><span>diff --git a/res/prometheus/prometheus_internal.h b/res/prometheus/prometheus_internal.h</span><br><span>index 5ee96aa..3b3618a 100644</span><br><span>--- a/res/prometheus/prometheus_internal.h</span><br><span>+++ b/res/prometheus/prometheus_internal.h</span><br><span>@@ -31,6 +31,43 @@</span><br><span> */</span><br><span> </span><br><span> /*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Retrieve the amount of time it took to perform the last scrape</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \details Time returned is in milliseconds</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval The scrape duration, in milliseconds</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+int64_t prometheus_last_scrape_duration_get(void);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Retrieve the timestamp when the last scrape occurred</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval The time when the last scrape occurred</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+struct timeval prometheus_last_scrape_time_get(void);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Get the raw output of what a scrape would produce</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \details</span><br><span style="color: hsl(120, 100%, 40%);">+ * It can be useful to dump what a scrape will look like.</span><br><span style="color: hsl(120, 100%, 40%);">+ * This function returns the raw string representation</span><br><span style="color: hsl(120, 100%, 40%);">+ * of the metrics.</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval NULL on error</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval Malloc'd ast_str on success</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+struct ast_str *prometheus_scrape_to_string(void);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span style="color: hsl(120, 100%, 40%);">+ * \brief Initialize CLI command</span><br><span style="color: hsl(120, 100%, 40%);">+ *</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval 0 success</span><br><span style="color: hsl(120, 100%, 40%);">+ * \retval -1 error</span><br><span style="color: hsl(120, 100%, 40%);">+ */</span><br><span style="color: hsl(120, 100%, 40%);">+int cli_init(void);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+/*!</span><br><span> * \brief Initialize channel metrics</span><br><span> *</span><br><span> * \retval 0 success</span><br><span>diff --git a/res/res_prometheus.c b/res/res_prometheus.c</span><br><span>index 8a61ad9..5181af7 100644</span><br><span>--- a/res/res_prometheus.c</span><br><span>+++ b/res/res_prometheus.c</span><br><span>@@ -139,6 +139,8 @@</span><br><span> </span><br><span> AST_VECTOR(, const struct prometheus_metrics_provider *) providers;</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static struct timeval last_scrape;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> /*! \brief The actual module config */</span><br><span> struct module_config {</span><br><span> /*! \brief General settings */</span><br><span>@@ -556,6 +558,36 @@</span><br><span> }</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+static void scrape_metrics(struct ast_str **response)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ int i;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ for (i = 0; i < AST_VECTOR_SIZE(&callbacks); i++) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct prometheus_callback *callback = AST_VECTOR_GET(&callbacks, i);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!callback) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ callback->callback_fn(response);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) {</span><br><span style="color: hsl(120, 100%, 40%);">+ struct prometheus_metric *metric = AST_VECTOR_GET(&metrics, i);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!metric) {</span><br><span style="color: hsl(120, 100%, 40%);">+ continue;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(&metric->lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (metric->get_metric_value) {</span><br><span style="color: hsl(120, 100%, 40%);">+ metric->get_metric_value(metric);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+ prometheus_metric_to_string(metric, response);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(&metric->lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static int http_callback(struct ast_tcptls_session_instance *ser,</span><br><span> const struct ast_http_uri *urih, const char *uri, enum ast_http_method method,</span><br><span> struct ast_variable *get_params, struct ast_variable *headers)</span><br><span>@@ -564,7 +596,6 @@</span><br><span> struct ast_str *response = NULL;</span><br><span> struct timeval start;</span><br><span> struct timeval end;</span><br><span style="color: hsl(0, 100%, 40%);">- int i;</span><br><span> </span><br><span> /* If there is no module config or we're not enabled, we can't handle requests */</span><br><span> if (!mod_cfg || !mod_cfg->general->enabled) {</span><br><span>@@ -599,27 +630,12 @@</span><br><span> goto err500;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (mod_cfg->general->core_metrics_enabled) {</span><br><span style="color: hsl(0, 100%, 40%);">- start = ast_tvnow();</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+ start = ast_tvnow();</span><br><span> </span><br><span> ast_mutex_lock(&scrape_lock);</span><br><span style="color: hsl(0, 100%, 40%);">- for (i = 0; i < AST_VECTOR_SIZE(&callbacks); i++) {</span><br><span style="color: hsl(0, 100%, 40%);">- struct prometheus_callback *callback = AST_VECTOR_GET(&callbacks, i);</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- callback->callback_fn(&response);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) {</span><br><span style="color: hsl(0, 100%, 40%);">- struct prometheus_metric *metric = AST_VECTOR_GET(&metrics, i);</span><br><span style="color: hsl(0, 100%, 40%);">-</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_lock(&metric->lock);</span><br><span style="color: hsl(0, 100%, 40%);">- if (metric->get_metric_value) {</span><br><span style="color: hsl(0, 100%, 40%);">- metric->get_metric_value(metric);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(0, 100%, 40%);">- prometheus_metric_to_string(metric, &response);</span><br><span style="color: hsl(0, 100%, 40%);">- ast_mutex_unlock(&metric->lock);</span><br><span style="color: hsl(0, 100%, 40%);">- }</span><br><span style="color: hsl(120, 100%, 40%);">+ last_scrape = start;</span><br><span style="color: hsl(120, 100%, 40%);">+ scrape_metrics(&response);</span><br><span> </span><br><span> if (mod_cfg->general->core_metrics_enabled) {</span><br><span> int64_t duration;</span><br><span>@@ -664,6 +680,40 @@</span><br><span> return 0;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(120, 100%, 40%);">+struct ast_str *prometheus_scrape_to_string(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ struct ast_str *response;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ response = ast_str_create(512);</span><br><span style="color: hsl(120, 100%, 40%);">+ if (!response) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return NULL;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_lock(&scrape_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+ scrape_metrics(&response);</span><br><span style="color: hsl(120, 100%, 40%);">+ ast_mutex_unlock(&scrape_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return response;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+int64_t prometheus_last_scrape_duration_get(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ int64_t duration;</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ if (sscanf(core_scrape_metric.value, "%" PRIu64, &duration) != 1) {</span><br><span style="color: hsl(120, 100%, 40%);">+ return -1;</span><br><span style="color: hsl(120, 100%, 40%);">+ }</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return duration;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+struct timeval prometheus_last_scrape_time_get(void)</span><br><span style="color: hsl(120, 100%, 40%);">+{</span><br><span style="color: hsl(120, 100%, 40%);">+ SCOPED_MUTEX(lock, &scrape_lock);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span style="color: hsl(120, 100%, 40%);">+ return last_scrape;</span><br><span style="color: hsl(120, 100%, 40%);">+}</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> static void prometheus_general_config_dtor(void *obj)</span><br><span> {</span><br><span> struct prometheus_general_config *config = obj;</span><br><span>@@ -846,7 +896,9 @@</span><br><span> AST_VECTOR_FREE(&metrics);</span><br><span> </span><br><span> AST_VECTOR_FREE(&callbacks);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> AST_VECTOR_FREE(&providers);</span><br><span style="color: hsl(120, 100%, 40%);">+</span><br><span> aco_info_destroy(&cfg_info);</span><br><span> ao2_global_obj_release(global_config);</span><br><span> </span><br><span>@@ -917,7 +969,8 @@</span><br><span> goto cleanup;</span><br><span> }</span><br><span> </span><br><span style="color: hsl(0, 100%, 40%);">- if (channel_metrics_init()</span><br><span style="color: hsl(120, 100%, 40%);">+ if (cli_init()</span><br><span style="color: hsl(120, 100%, 40%);">+ || channel_metrics_init()</span><br><span> || endpoint_metrics_init()</span><br><span> || bridge_metrics_init()) {</span><br><span> goto cleanup;</span><br><span></span><br></pre><p>To view, visit <a href="https://gerrit.asterisk.org/c/asterisk/+/11375">change 11375</a>. To unsubscribe, or for help writing mail filters, visit <a href="https://gerrit.asterisk.org/settings">settings</a>.</p><div itemscope itemtype="http://schema.org/EmailMessage"><div itemscope itemprop="action" itemtype="http://schema.org/ViewAction"><link itemprop="url" href="https://gerrit.asterisk.org/c/asterisk/+/11375"/><meta itemprop="name" content="View Change"/></div></div>
<div style="display:none"> Gerrit-Project: asterisk </div>
<div style="display:none"> Gerrit-Branch: master </div>
<div style="display:none"> Gerrit-Change-Id: Ic09813e5e14b901571c5c96ebeae2a02566c5172 </div>
<div style="display:none"> Gerrit-Change-Number: 11375 </div>
<div style="display:none"> Gerrit-PatchSet: 4 </div>
<div style="display:none"> Gerrit-Owner: Matt Jordan <mjordan@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Benjamin Keith Ford <bford@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Friendly Automation </div>
<div style="display:none"> Gerrit-Reviewer: George Joseph <gjoseph@digium.com> </div>
<div style="display:none"> Gerrit-Reviewer: Matt Jordan <mjordan@digium.com> </div>
<div style="display:none"> Gerrit-MessageType: merged </div>