Asterisk - The Open Source Telephony Project GIT-master-f36a736
|
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.
Data Structures | |
struct | module_config |
The configuration settings for this module. More... | |
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... | |
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 HAVE_PJPROJECT .requires = "res_pjsip", .optional_modules = "res_pjsip_outbound_registration", #endif } |
static const struct ast_module_info * | ast_module_info = &__mod_info |
struct { | |
size_t current | |
struct prometheus_callback ** elems | |
size_t max | |
} | callbacks |
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) |
static struct timeval | last_scrape |
struct { | |
size_t current | |
struct prometheus_metric ** elems | |
size_t max | |
} | metrics |
struct aco_file | prometheus_conf |
static struct ast_http_uri | prometheus_uri |
struct { | |
size_t current | |
const struct prometheus_metrics_provider ** elems | |
size_t max | |
} | providers |
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 | ( | void | ) | __internal_res_prometheus_self |
Definition at line 122 of file res_prometheus.c.
Definition at line 185 of file res_prometheus.c.
Definition at line 187 of file res_prometheus.c.
Definition at line 181 of file res_prometheus.c.
#define CORE_UPTIME_HELP "Asterisk instance uptime in seconds." |
Definition at line 183 of file res_prometheus.c.
#define METRIC_CORE_PROPS_ARRAY_INDEX 0 |
Definition at line 221 of file res_prometheus.c.
struct ast_module * __internal_res_prometheus_self | ( | void | ) |
Definition at line 1015 of file res_prometheus.c.
|
static |
Definition at line 1015 of file res_prometheus.c.
|
static |
Definition at line 1015 of file res_prometheus.c.
|
static |
The module configuration container.
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 189 of file res_prometheus.c.
References ast_startuptime, ast_tvdiff_sec(), ast_tvnow(), and prometheus_metric::value.
|
static |
Definition at line 197 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(), core_scrape_metric, end, last_scrape, method, 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_module_check(), AST_MODULE_LOAD_DECLINE, AST_MODULE_LOAD_SUCCESS, AST_VECTOR_FREE, AST_VECTOR_INIT, bridge_metrics_init(), callbacks, channel_metrics_init(), cleanup(), cli_init(), enabled, endpoint_metrics_init(), FLDSET, global_options, lock, metrics, OPT_BOOL_T, OPT_STRINGFIELD_T, pjsip_outbound_registration_metrics_init(), prometheus_uri, providers, SCOPED_MUTEX, scrape_lock, and STRFLDSET.
|
static |
Module config constructor.
Definition at line 773 of file res_prometheus.c.
References ao2_alloc, ao2_ref, config, 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, and config.
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, callbacks, 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, callbacks, 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(), core_metrics, core_scrape_metric, METRIC_CORE_PROPS_ARRAY_INDEX, prometheus_metric_register(), PROMETHEUS_METRIC_SET_LABEL, prometheus_metric_unregister(), prometheus_uri, 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(), and config.
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 prometheus_metric::help, name, 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 prometheus_metric::help, name, NULL, prometheus_metric_create(), PROMETHEUS_METRIC_GAUGE, and prometheus_metric::type.
Referenced by AST_TEST_DEFINE().
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, config, 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 core_scrape_metric, and 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 last_scrape, lock, SCOPED_MUTEX, and scrape_lock.
Referenced by prometheus_show_status().
|
static |
Definition at line 253 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, name, 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 393 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, prometheus_metric_free(), and PROMETHEUS_METRIC_MALLOCD.
Referenced by AST_TEST_DEFINE(), prometheus_metric_free(), 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 288 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, metrics, prometheus_metric::name, prometheus_metric_cmp(), SCOPED_MUTEX, and scrape_lock.
Referenced by AST_TEST_DEFINE(), and prometheus_config_post_apply().
int prometheus_metric_registered_count | ( | void | ) |
The current number of registered metrics
The | current number of registered metrics |
Definition at line 281 of file res_prometheus.c.
References AST_VECTOR_SIZE, lock, metrics, 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, PROMETHEUS_METRIC_GAUGE, and type.
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 335 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, metrics, prometheus_metric::name, prometheus_metric_cmp(), prometheus_metric_free(), SCOPED_MUTEX, and scrape_lock.
Referenced by AST_TEST_DEFINE(), prometheus_config_post_apply(), and prometheus_metric_free_wrapper().
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, provider, 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 AST_TEST_DEFINE(), and 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(), prometheus_uri, provider, providers, prometheus_metrics_provider::reload_cb, SCOPED_MUTEX, and scrape_lock.
|
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, callbacks, prometheus_metric::get_metric_value, prometheus_metric::lock, metrics, 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, callbacks, lock, metrics, prometheus_metric_free(), prometheus_uri, provider, providers, SCOPED_MUTEX, scrape_lock, and prometheus_metrics_provider::unload_cb.
|
static |
Definition at line 1015 of file res_prometheus.c.
|
static |
Definition at line 1015 of file res_prometheus.c.
struct { ... } callbacks |
Referenced by ast_bucket_file_observer_add(), ast_bucket_file_observer_remove(), ast_bucket_observer_add(), ast_bucket_observer_remove(), ast_cc_agent_register(), ast_cc_agent_unregister(), ast_cc_monitor_register(), ast_cc_monitor_unregister(), ast_sorcery_global_observer_add(), ast_sorcery_global_observer_remove(), ast_sorcery_instance_observer_add(), ast_sorcery_instance_observer_remove(), ast_sorcery_observer_add(), ast_sorcery_observer_remove(), ast_sorcery_wizard_observer_add(), ast_sorcery_wizard_observer_remove(), ast_taskprocessor_listener_alloc(), ast_threadpool_listener_alloc(), check_callback_sanity(), find_agent_callbacks(), find_monitor_callbacks(), load_module(), prometheus_callback_register(), prometheus_callback_unregister(), scrape_metrics(), and unload_module().
|
static |
Core metrics to scrape.
Definition at line 225 of file res_prometheus.c.
Referenced by prometheus_config_post_apply().
|
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 214 of file res_prometheus.c.
Referenced by http_callback(), prometheus_config_post_apply(), and prometheus_last_scrape_duration_get().
size_t current |
Definition at line 139 of file res_prometheus.c.
const struct prometheus_metrics_provider* * elems |
Definition at line 139 of file res_prometheus.c.
|
static |
Definition at line 153 of file res_prometheus.c.
struct aco_type* global_options[] = ACO_TYPES(&global_option) |
Definition at line 161 of file res_prometheus.c.
Referenced by load_module().
|
static |
Definition at line 145 of file res_prometheus.c.
Referenced by http_callback(), and prometheus_last_scrape_time_get().
size_t max |
Definition at line 139 of file res_prometheus.c.
struct { ... } metrics |
struct aco_file prometheus_conf |
Definition at line 163 of file res_prometheus.c.
|
static |
Definition at line 791 of file res_prometheus.c.
Referenced by load_module(), prometheus_config_post_apply(), reload_module(), and unload_module().
struct { ... } providers |
Referenced by load_module(), prometheus_metrics_provider_register(), reload_module(), and unload_module().
|
static |
Lock that protects data structures during an HTTP scrape.
Definition at line 137 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().