Asterisk - The Open Source Telephony Project
18.5.0
|
Core Prometheus metrics API. More...
#include "asterisk.h"
#include "asterisk/module.h"
#include "asterisk/vector.h"
#include "asterisk/http.h"
#include "asterisk/config_options.h"
#include "asterisk/ast_version.h"
#include "asterisk/buildinfo.h"
#include "asterisk/res_prometheus.h"
#include "prometheus/prometheus_internal.h"
Go to the source code of this file.
Macros | |
#define | AST_MODULE_SELF_SYM __internal_res_prometheus_self |
#define | CORE_LAST_RELOAD_HELP "Time since last Asterisk reload in seconds." |
#define | CORE_METRICS_SCRAPE_TIME_HELP "Total time taken to collect metrics, in milliseconds" |
#define | CORE_PROPERTIES_HELP "Asterisk instance properties. The value of this will always be 1." |
#define | CORE_UPTIME_HELP "Asterisk instance uptime in seconds." |
#define | METRIC_CORE_PROPS_ARRAY_INDEX 0 |
Functions | |
struct ast_module * | __internal_res_prometheus_self (void) |
static void | __reg_module (void) |
static void | __unreg_module (void) |
static | AO2_GLOBAL_OBJ_STATIC (global_config) |
The module configuration container. More... | |
AST_VECTOR (struct prometheus_metric *) | |
The actual module config. More... | |
CONFIG_INFO_STANDARD (cfg_info, global_config, module_config_alloc,.files=ACO_FILES(&prometheus_conf),.pre_apply_config=prometheus_config_pre_apply,.post_apply_config=prometheus_config_post_apply,) | |
Register information about the configs being processed by this module. More... | |
static void | get_core_uptime_cb (struct prometheus_metric *metric) |
static void | get_last_reload_cb (struct prometheus_metric *metric) |
static int | http_callback (struct ast_tcptls_session_instance *ser, const struct ast_http_uri *urih, const char *uri, enum ast_http_method method, struct ast_variable *get_params, struct ast_variable *headers) |
static int | load_module (void) |
static void * | module_config_alloc (void) |
Module config constructor. More... | |
static void | module_config_dtor (void *obj) |
Configuration object destructor. More... | |
int | prometheus_callback_register (struct prometheus_callback *callback) |
void | prometheus_callback_unregister (struct prometheus_callback *callback) |
Remove a registered callback. More... | |
static void | prometheus_config_post_apply (void) |
Post-apply callback for the config framework. More... | |
static int | prometheus_config_pre_apply (void) |
Pre-apply callback for the config framework. More... | |
struct prometheus_metric * | prometheus_counter_create (const char *name, const char *help) |
Create a malloc'd counter metric. More... | |
struct prometheus_metric * | prometheus_gauge_create (const char *name, const char *help) |
Create a malloc'd gauge metric. More... | |
void * | prometheus_general_config_alloc (void) |
Allocate a new configuration object. More... | |
static void | prometheus_general_config_dtor (void *obj) |
struct prometheus_general_config * | prometheus_general_config_get (void) |
Retrieve the current configuration of the module. More... | |
void | prometheus_general_config_set (struct prometheus_general_config *config) |
Set the configuration for the module. More... | |
int64_t | prometheus_last_scrape_duration_get (void) |
Retrieve the amount of time it took to perform the last scrape. More... | |
struct timeval | prometheus_last_scrape_time_get (void) |
Retrieve the timestamp when the last scrape occurred. More... | |
static int | prometheus_metric_cmp (struct prometheus_metric *left, struct prometheus_metric *right) |
static struct prometheus_metric * | prometheus_metric_create (const char *name, const char *help) |
void | prometheus_metric_free (struct prometheus_metric *metric) |
Destroy a metric and all its children. More... | |
static void | prometheus_metric_full_to_string (struct prometheus_metric *metric, struct ast_str **output) |
int | prometheus_metric_register (struct prometheus_metric *metric) |
int | prometheus_metric_registered_count (void) |
void | prometheus_metric_to_string (struct prometheus_metric *metric, struct ast_str **output) |
Convert a metric (and its children) into Prometheus compatible text. More... | |
static const char * | prometheus_metric_type_to_string (enum prometheus_metric_type type) |
int | prometheus_metric_unregister (struct prometheus_metric *metric) |
Remove a registered metric. More... | |
void | prometheus_metrics_provider_register (const struct prometheus_metrics_provider *provider) |
Register a metrics provider. More... | |
struct ast_str * | prometheus_scrape_to_string (void) |
Get the raw output of what a scrape would produce. More... | |
static int | reload_module (void) |
static void | scrape_metrics (struct ast_str **response) |
static int | unload_module (void) |
Variables | |
static struct ast_module_info | __mod_info = { .name = AST_MODULE, .flags = AST_MODFLAG_GLOBAL_SYMBOLS | AST_MODFLAG_LOAD_ORDER , .description = "Asterisk Prometheus Module" , .key = "This paragraph is copyright (c) 2006 by Digium, Inc. \In order for your module to load, it must return this \key via a function called \"key\". Any code which \includes this paragraph must be licensed under the GNU \General Public License version 2 or later (at your \option). In addition to Digium's general reservations \of rights, Digium expressly reserves the right to \allow other parties to license this paragraph under \different terms. Any use of Digium, Inc. trademarks or \logos (including \"Asterisk\" or \"Digium\") without \express written permission of Digium, Inc. is prohibited.\n" , .buildopt_sum = AST_BUILDOPT_SUM, .support_level = AST_MODULE_SUPPORT_EXTENDED, .load = load_module, .unload = unload_module, .reload = reload_module, .load_pri = AST_MODPRI_DEFAULT, #ifdef 1 .requires = "res_pjsip", #endif } |
static const struct ast_module_info * | ast_module_info = &__mod_info |
static struct prometheus_metric | core_metrics [] |
Core metrics to scrape. More... | |
static struct prometheus_metric | core_scrape_metric |
The scrape duration metric. More... | |
static struct aco_type | global_option |
struct aco_type * | global_options [] = ACO_TYPES(&global_option) |
struct aco_file | prometheus_conf |
static struct ast_http_uri | prometheus_uri |
static ast_mutex_t | scrape_lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} } |
Lock that protects data structures during an HTTP scrape. More... | |
Core Prometheus metrics API.
Definition in file res_prometheus.c.
#define AST_MODULE_SELF_SYM __internal_res_prometheus_self |
Definition at line 121 of file res_prometheus.c.
Definition at line 184 of file res_prometheus.c.
#define CORE_METRICS_SCRAPE_TIME_HELP "Total time taken to collect metrics, in milliseconds" |
Definition at line 186 of file res_prometheus.c.
Definition at line 180 of file res_prometheus.c.
#define CORE_UPTIME_HELP "Asterisk instance uptime in seconds." |
Definition at line 182 of file res_prometheus.c.
#define METRIC_CORE_PROPS_ARRAY_INDEX 0 |
Definition at line 220 of file res_prometheus.c.
Referenced by prometheus_config_post_apply().
struct ast_module* __internal_res_prometheus_self | ( | void | ) |
Definition at line 1008 of file res_prometheus.c.
|
static |
Definition at line 1008 of file res_prometheus.c.
|
static |
Definition at line 1008 of file res_prometheus.c.
|
static |
The module configuration container.
AST_VECTOR | ( | struct prometheus_metric * | ) |
CONFIG_INFO_STANDARD | ( | cfg_info | , |
global_config | , | ||
module_config_alloc | , | ||
. | files = ACO_FILES(&prometheus_conf) , |
||
. | pre_apply_config = prometheus_config_pre_apply , |
||
. | post_apply_config = prometheus_config_post_apply |
||
) |
Register information about the configs being processed by this module.
|
static |
Definition at line 188 of file res_prometheus.c.
References ast_startuptime, ast_tvdiff_sec(), ast_tvnow(), and prometheus_metric::value.
|
static |
Definition at line 196 of file res_prometheus.c.
References ast_lastreloadtime, ast_tvdiff_sec(), ast_tvnow(), and prometheus_metric::value.
|
static |
Definition at line 593 of file res_prometheus.c.
References ao2_cleanup, ao2_global_obj_ref, ao2_ref, ast_debug, ast_free, ast_http_get_auth(), ast_http_send(), ast_mutex_lock, ast_mutex_unlock, ast_str_append(), ast_str_create, ast_strlen_zero, ast_tvdiff_ms(), ast_tvnow(), NULL, ast_http_auth::password, prometheus_metric_to_string(), RAII_VAR, scrape_lock, scrape_metrics(), ast_http_auth::userid, and prometheus_metric::value.
|
static |
Definition at line 945 of file res_prometheus.c.
References ACO_EXACT, aco_info_destroy(), aco_info_init(), aco_option_register, aco_process_config(), ACO_PROCESS_ERROR, ast_http_uri_link(), ast_http_uri_unlink(), AST_MODFLAG_GLOBAL_SYMBOLS, AST_MODFLAG_LOAD_ORDER, AST_MODPRI_DEFAULT, AST_MODULE_INFO(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_MODULE_SUPPORT_EXTENDED, AST_VECTOR_FREE, AST_VECTOR_INIT, ASTERISK_GPL_KEY, bridge_metrics_init(), channel_metrics_init(), cleanup(), cli_init(), enabled, endpoint_metrics_init(), FLDSET, HAVE_PJPROJECT, lock, OPT_BOOL_T, OPT_STRINGFIELD_T, pjsip_outbound_registration_metrics_init(), providers, reload(), reload_module(), SCOPED_MUTEX, scrape_lock, STRFLDSET, and unload_module().
|
static |
Module config constructor.
Definition at line 773 of file res_prometheus.c.
References ao2_alloc, ao2_ref, config, module_config::general, module_config_dtor(), NULL, and prometheus_general_config_alloc().
|
static |
Configuration object destructor.
Definition at line 763 of file res_prometheus.c.
References ao2_ref, config, and module_config::general.
Referenced by module_config_alloc().
int prometheus_callback_register | ( | struct prometheus_callback * | callback | ) |
Register a metric callback
callback | The callback to register |
0 | success |
-1 | error |
Definition at line 535 of file res_prometheus.c.
References ast_strlen_zero, AST_VECTOR_APPEND, prometheus_callback::callback_fn, lock, prometheus_callback::name, SCOPED_MUTEX, and scrape_lock.
Referenced by AST_TEST_DEFINE(), bridge_metrics_init(), channel_metrics_init(), and endpoint_metrics_init().
void prometheus_callback_unregister | ( | struct prometheus_callback * | callback | ) |
Remove a registered callback.
callback | The callback to unregister |
Definition at line 548 of file res_prometheus.c.
References AST_VECTOR_GET, AST_VECTOR_REMOVE, AST_VECTOR_SIZE, lock, prometheus_callback::name, SCOPED_MUTEX, and scrape_lock.
Referenced by AST_TEST_DEFINE(), bridge_metrics_unload_cb(), channel_metrics_unload_cb(), and endpoint_metrics_unload_cb().
|
static |
Post-apply callback for the config framework.
This sets any run-time information derived from the configuration
Definition at line 827 of file res_prometheus.c.
References ao2_cleanup, ao2_global_obj_ref, ARRAY_LEN, ast_build_date, ast_build_hostname, ast_build_kernel, ast_build_os, ast_eid_default, ast_eid_to_str(), ast_get_build_opts(), ast_get_version(), METRIC_CORE_PROPS_ARRAY_INDEX, prometheus_metric_register(), PROMETHEUS_METRIC_SET_LABEL, prometheus_metric_unregister(), RAII_VAR, ast_http_uri::uri, and value.
Referenced by prometheus_general_config_set().
|
static |
Pre-apply callback for the config framework.
This validates that required fields exist and are populated.
Definition at line 804 of file res_prometheus.c.
References aco_pending_config(), ast_log, AST_LOG_ERROR, ast_strlen_zero, config, and module_config::general.
struct prometheus_metric* prometheus_counter_create | ( | const char * | name, |
const char * | help | ||
) |
Create a malloc'd counter metric.
name | The name of the metric |
help | Help text for the metric |
prometheus_metric | on success |
NULL | on error |
Definition at line 452 of file res_prometheus.c.
References NULL, PROMETHEUS_METRIC_COUNTER, prometheus_metric_create(), and prometheus_metric::type.
Referenced by AST_TEST_DEFINE().
struct prometheus_metric* prometheus_gauge_create | ( | const char * | name, |
const char * | help | ||
) |
Create a malloc'd gauge metric.
name | The name of the metric |
help | Help text for the metric |
prometheus_metric | on success |
NULL | on error |
Definition at line 439 of file res_prometheus.c.
References NULL, prometheus_metric_create(), PROMETHEUS_METRIC_GAUGE, and prometheus_metric::type.
Referenced by AST_TEST_DEFINE(), and registry_message_cb().
void* prometheus_general_config_alloc | ( | void | ) |
Allocate a new configuration object.
The returned object is an AO2 ref counted object
NULL | on error |
config | on success |
Definition at line 726 of file res_prometheus.c.
References ao2_alloc, ast_string_field_init, config, NULL, and prometheus_general_config_dtor().
Referenced by config_alloc(), and module_config_alloc().
|
static |
Definition at line 719 of file res_prometheus.c.
References ast_string_field_free_memory, and config.
Referenced by prometheus_general_config_alloc().
struct prometheus_general_config* prometheus_general_config_get | ( | void | ) |
Retrieve the current configuration of the module.
config is an AO2 ref counted object
NULL | on error |
config | on success |
Definition at line 738 of file res_prometheus.c.
References ao2_bump, ao2_cleanup, ao2_global_obj_ref, NULL, and RAII_VAR.
Referenced by prometheus_show_status(), reload_module(), and test_init_cb().
void prometheus_general_config_set | ( | struct prometheus_general_config * | config | ) |
Set the configuration for the module.
This is not a ref-stealing function. The reference count to config
will be incremented as a result of calling this method.
Definition at line 750 of file res_prometheus.c.
References ao2_cleanup, ao2_global_obj_ref, ao2_replace, prometheus_config_post_apply(), and RAII_VAR.
Referenced by AST_TEST_DEFINE(), test_cleanup_cb(), and test_init_cb().
int64_t prometheus_last_scrape_duration_get | ( | void | ) |
Retrieve the amount of time it took to perform the last scrape.
Time returned is in milliseconds
The | scrape duration, in milliseconds |
Definition at line 701 of file res_prometheus.c.
References prometheus_metric::value.
Referenced by prometheus_show_status().
struct timeval prometheus_last_scrape_time_get | ( | void | ) |
Retrieve the timestamp when the last scrape occurred.
The | time when the last scrape occurred |
Definition at line 712 of file res_prometheus.c.
References lock, SCOPED_MUTEX, and scrape_lock.
Referenced by prometheus_show_status().
|
static |
Definition at line 252 of file res_prometheus.c.
References ast_debug, prometheus_metric::labels, prometheus_label::name, prometheus_metric::name, PROMETHEUS_MAX_LABELS, and prometheus_label::value.
Referenced by prometheus_metric_register(), and prometheus_metric_unregister().
|
static |
Definition at line 422 of file res_prometheus.c.
References prometheus_metric::allocation_strategy, ast_calloc, ast_copy_string(), ast_mutex_init, prometheus_metric::help, prometheus_metric::lock, prometheus_metric::name, NULL, and PROMETHEUS_METRIC_MALLOCD.
Referenced by prometheus_counter_create(), and prometheus_gauge_create().
void prometheus_metric_free | ( | struct prometheus_metric * | metric | ) |
Destroy a metric and all its children.
children
list first.metric | The metric to destroy |
Definition at line 392 of file res_prometheus.c.
References prometheus_metric::allocation_strategy, ast_free, AST_LIST_REMOVE_HEAD, ast_mutex_destroy, prometheus_metric::children, prometheus_metric::lock, PROMETHEUS_METRIC_ALLOCD, and PROMETHEUS_METRIC_MALLOCD.
Referenced by AST_TEST_DEFINE(), prometheus_metric_free_wrapper(), prometheus_metric_unregister(), and unload_module().
|
static |
Definition at line 485 of file res_prometheus.c.
References ast_str_append(), ast_strlen_zero, prometheus_metric::labels, prometheus_label::name, prometheus_metric::name, PROMETHEUS_MAX_LABELS, prometheus_label::value, and prometheus_metric::value.
Referenced by prometheus_metric_to_string().
int prometheus_metric_register | ( | struct prometheus_metric * | metric | ) |
Register a metric for collection
metric | The metric to register |
0 | success |
-1 | error |
Definition at line 287 of file res_prometheus.c.
References ast_debug, AST_LIST_INSERT_TAIL, AST_LIST_TRAVERSE, ast_log, AST_LOG_NOTICE, AST_LOG_WARNING, AST_VECTOR_APPEND, AST_VECTOR_GET, AST_VECTOR_SIZE, prometheus_metric::children, lock, prometheus_metric::name, prometheus_metric_cmp(), SCOPED_MUTEX, and scrape_lock.
Referenced by AST_TEST_DEFINE(), prometheus_config_post_apply(), and registry_message_cb().
int prometheus_metric_registered_count | ( | void | ) |
The current number of registered metrics
The | current number of registered metrics |
Definition at line 280 of file res_prometheus.c.
References AST_VECTOR_SIZE, lock, SCOPED_MUTEX, and scrape_lock.
Referenced by AST_TEST_DEFINE().
void prometheus_metric_to_string | ( | struct prometheus_metric * | metric, |
struct ast_str ** | output | ||
) |
Convert a metric (and its children) into Prometheus compatible text.
metric | The metric to convert to a string | |
[out] | output | The ast_str string to populate with the metric(s) |
Definition at line 521 of file res_prometheus.c.
References AST_LIST_TRAVERSE, ast_str_append(), prometheus_metric::children, prometheus_metric::help, prometheus_metric::name, prometheus_metric_full_to_string(), prometheus_metric_type_to_string(), and prometheus_metric::type.
Referenced by AST_TEST_DEFINE(), bridges_scrape_cb(), channels_scrape_cb(), endpoints_scrape_cb(), http_callback(), prometheus_metric_callback(), and scrape_metrics().
|
static |
Definition at line 465 of file res_prometheus.c.
References ast_assert, PROMETHEUS_METRIC_COUNTER, and PROMETHEUS_METRIC_GAUGE.
Referenced by prometheus_metric_to_string().
int prometheus_metric_unregister | ( | struct prometheus_metric * | metric | ) |
Remove a registered metric.
metric | The metric to unregister |
0 | The metric was found, unregistered, and disposed of |
-1 | The metric was not found |
Definition at line 334 of file res_prometheus.c.
References ast_debug, AST_LIST_INSERT_TAIL, AST_LIST_REMOVE_CURRENT, AST_LIST_REMOVE_HEAD, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_VECTOR_GET, AST_VECTOR_INSERT_AT, AST_VECTOR_REMOVE, AST_VECTOR_SIZE, prometheus_metric::children, lock, prometheus_metric::name, prometheus_metric_cmp(), prometheus_metric_free(), SCOPED_MUTEX, and scrape_lock.
Referenced by AST_TEST_DEFINE(), prometheus_config_post_apply(), prometheus_metric_free_wrapper(), registration_deleted_observer(), and registration_loaded_observer().
void prometheus_metrics_provider_register | ( | const struct prometheus_metrics_provider * | provider | ) |
Register a metrics provider.
provider | The provider function table to register |
Definition at line 871 of file res_prometheus.c.
References AST_VECTOR_APPEND, and providers.
Referenced by bridge_metrics_init(), channel_metrics_init(), cli_init(), endpoint_metrics_init(), and pjsip_outbound_registration_metrics_init().
struct ast_str* prometheus_scrape_to_string | ( | void | ) |
Get the raw output of what a scrape would produce.
It can be useful to dump what a scrape will look like. This function returns the raw string representation of the metrics.
NULL | on error |
Malloc'd | ast_str on success |
Definition at line 685 of file res_prometheus.c.
References ast_mutex_lock, ast_mutex_unlock, ast_str_create, NULL, scrape_lock, and scrape_metrics().
Referenced by prometheus_show_metrics().
|
static |
Definition at line 910 of file res_prometheus.c.
References aco_process_config(), ACO_PROCESS_ERROR, ao2_ref, ast_http_uri_link(), ast_http_uri_unlink(), ast_log, AST_LOG_WARNING, AST_VECTOR_GET, AST_VECTOR_SIZE, lock, prometheus_metrics_provider::name, prometheus_general_config_get(), provider, providers, prometheus_metrics_provider::reload_cb, SCOPED_MUTEX, and scrape_lock.
Referenced by load_module().
|
static |
Definition at line 563 of file res_prometheus.c.
References ast_mutex_lock, ast_mutex_unlock, AST_VECTOR_GET, AST_VECTOR_SIZE, prometheus_callback::callback_fn, prometheus_metric::get_metric_value, prometheus_metric::lock, and prometheus_metric_to_string().
Referenced by http_callback(), and prometheus_scrape_to_string().
|
static |
Definition at line 876 of file res_prometheus.c.
References aco_info_destroy(), ao2_global_obj_release, ast_http_uri_unlink(), AST_VECTOR_FREE, AST_VECTOR_GET, AST_VECTOR_SIZE, lock, prometheus_metric_free(), provider, providers, SCOPED_MUTEX, scrape_lock, and prometheus_metrics_provider::unload_cb.
Referenced by load_module().
|
static |
Definition at line 1008 of file res_prometheus.c.
|
static |
Definition at line 1008 of file res_prometheus.c.
|
static |
Core metrics to scrape.
Definition at line 224 of file res_prometheus.c.
|
static |
The scrape duration metric.
This metric is special in that it should never be registered. Instead, the HTTP callback function that walks the metrics will always populate this metric explicitly if core metrics are enabled.
Definition at line 213 of file res_prometheus.c.
|
static |
Definition at line 152 of file res_prometheus.c.
struct aco_type* global_options[] = ACO_TYPES(&global_option) |
Definition at line 160 of file res_prometheus.c.
struct aco_file prometheus_conf |
Definition at line 162 of file res_prometheus.c.
|
static |
Definition at line 791 of file res_prometheus.c.
|
static |
Lock that protects data structures during an HTTP scrape.
Definition at line 136 of file res_prometheus.c.
Referenced by http_callback(), load_module(), prometheus_callback_register(), prometheus_callback_unregister(), prometheus_last_scrape_time_get(), prometheus_metric_register(), prometheus_metric_registered_count(), prometheus_metric_unregister(), prometheus_scrape_to_string(), reload_module(), and unload_module().