Asterisk - The Open Source Telephony Project GIT-master-8f1982c
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Modules Pages
Data Structures | Macros | Functions | Variables
res_prometheus.c File Reference

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"
Include dependency graph for res_prometheus.c:

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_metricprometheus_counter_create (const char *name, const char *help)
 Create a malloc'd counter metric. More...
 
struct prometheus_metricprometheus_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_configprometheus_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_metricprometheus_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_strprometheus_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_infoast_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_typeglobal_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...
 

Detailed Description

Core Prometheus metrics API.

Author
Matt Jordan mjord.nosp@m.an@d.nosp@m.igium.nosp@m..com

Definition in file res_prometheus.c.

Macro Definition Documentation

◆ AST_MODULE_SELF_SYM

#define AST_MODULE_SELF_SYM (   void)    __internal_res_prometheus_self

Definition at line 143 of file res_prometheus.c.

◆ CORE_LAST_RELOAD_HELP

#define CORE_LAST_RELOAD_HELP   "Time since last Asterisk reload in seconds."

Definition at line 206 of file res_prometheus.c.

◆ CORE_METRICS_SCRAPE_TIME_HELP

#define CORE_METRICS_SCRAPE_TIME_HELP   "Total time taken to collect metrics, in milliseconds"

Definition at line 208 of file res_prometheus.c.

◆ CORE_PROPERTIES_HELP

#define CORE_PROPERTIES_HELP   "Asterisk instance properties. The value of this will always be 1."

Definition at line 202 of file res_prometheus.c.

◆ CORE_UPTIME_HELP

#define CORE_UPTIME_HELP   "Asterisk instance uptime in seconds."

Definition at line 204 of file res_prometheus.c.

◆ METRIC_CORE_PROPS_ARRAY_INDEX

#define METRIC_CORE_PROPS_ARRAY_INDEX   0

Definition at line 242 of file res_prometheus.c.

Function Documentation

◆ __internal_res_prometheus_self()

struct ast_module * __internal_res_prometheus_self ( void  )

Definition at line 1043 of file res_prometheus.c.

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 1043 of file res_prometheus.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 1043 of file res_prometheus.c.

◆ AO2_GLOBAL_OBJ_STATIC()

static AO2_GLOBAL_OBJ_STATIC ( global_config  )
static

The module configuration container.

◆ CONFIG_INFO_STANDARD()

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.

◆ get_core_uptime_cb()

static void get_core_uptime_cb ( struct prometheus_metric metric)
static

Definition at line 210 of file res_prometheus.c.

211{
212 struct timeval now = ast_tvnow();
213 int64_t duration = ast_tvdiff_sec(now, ast_startuptime);
214
215 snprintf(metric->value, sizeof(metric->value), "%" PRIu64, duration);
216}
struct timeval ast_startuptime
Definition: asterisk.c:343
char value[PROMETHEUS_MAX_VALUE_LENGTH]
The current value.
int64_t ast_tvdiff_sec(struct timeval end, struct timeval start)
Computes the difference (in seconds) between two struct timeval instances.
Definition: time.h:73
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:159

References ast_startuptime, ast_tvdiff_sec(), ast_tvnow(), and prometheus_metric::value.

◆ get_last_reload_cb()

static void get_last_reload_cb ( struct prometheus_metric metric)
static

Definition at line 218 of file res_prometheus.c.

219{
220 struct timeval now = ast_tvnow();
221 int64_t duration = ast_tvdiff_sec(now, ast_lastreloadtime);
222
223 snprintf(metric->value, sizeof(metric->value), "%" PRIu64, duration);
224}
struct timeval ast_lastreloadtime
Definition: asterisk.c:344

References ast_lastreloadtime, ast_tvdiff_sec(), ast_tvnow(), and prometheus_metric::value.

◆ http_callback()

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

Definition at line 614 of file res_prometheus.c.

617{
619 struct ast_str *response = NULL;
620 struct ast_str *content_type_header = NULL;
621 struct timeval start;
622 struct timeval end;
623
624 /* If there is no module config or we're not enabled, we can't handle requests */
625 if (!mod_cfg || !mod_cfg->general->enabled) {
626 goto err503;
627 }
628
629 if (!ast_strlen_zero(mod_cfg->general->auth_username)) {
630 struct ast_http_auth *http_auth;
631
632 http_auth = ast_http_get_auth(headers);
633 if (!http_auth) {
634 goto err401;
635 }
636
637 if (strcmp(http_auth->userid, mod_cfg->general->auth_username)) {
638 ast_debug(5, "Invalid username provided for auth request: %s\n", http_auth->userid);
639 ao2_ref(http_auth, -1);
640 goto err401;
641 }
642
643 if (strcmp(http_auth->password, mod_cfg->general->auth_password)) {
644 ast_debug(5, "Invalid password provided for auth request: %s\n", http_auth->password);
645 ao2_ref(http_auth, -1);
646 goto err401;
647 }
648
649 ao2_ref(http_auth, -1);
650 }
651
652 response = ast_str_create(512);
653 content_type_header = ast_str_create(32);
654 if (!response || !content_type_header) {
655 goto err500;
656 }
657
658 ast_str_set(&content_type_header, 0, "Content-Type: text/plain\r\n");
659
660 start = ast_tvnow();
661
663
664 last_scrape = start;
665 scrape_metrics(&response);
666
667 if (mod_cfg->general->core_metrics_enabled) {
668 int64_t duration;
669
670 end = ast_tvnow();
671 duration = ast_tvdiff_ms(end, start);
672 snprintf(core_scrape_metric.value,
674 "%" PRIu64,
675 duration);
677 }
679
680 ast_http_send(ser, method, 200, "OK", content_type_header, response, 0, 0);
681
682 return 0;
683
684err401:
685 {
686 struct ast_str *auth_challenge_headers;
687
688 auth_challenge_headers = ast_str_create(128);
689 if (!auth_challenge_headers) {
690 goto err500;
691 }
692 ast_str_append(&auth_challenge_headers, 0,
693 "WWW-Authenticate: Basic realm=\"%s\"\r\n",
694 mod_cfg->general->auth_realm);
695 /* ast_http_send takes ownership of the ast_str */
696 ast_http_send(ser, method, 401, "Unauthorized", auth_challenge_headers, NULL, 0, 1);
697 }
698 ast_free(response);
699 ast_free(content_type_header);
700 return 0;
701err503:
702 ast_http_send(ser, method, 503, "Service Unavailable", NULL, NULL, 0, 1);
703 ast_free(response);
704 ast_free(content_type_header);
705 return 0;
706err500:
707 ast_http_send(ser, method, 500, "Server Error", NULL, NULL, 0, 1);
708 ast_free(response);
709 ast_free(content_type_header);
710 return 0;
711}
#define ast_free(a)
Definition: astmm.h:180
#define ao2_cleanup(obj)
Definition: astobj2.h:1934
#define ao2_global_obj_ref(holder)
Get a reference to the object stored in the global holder.
Definition: astobj2.h:918
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
char * end
Definition: eagi_proxy.c:73
void ast_http_send(struct ast_tcptls_session_instance *ser, enum ast_http_method method, int status_code, const char *status_title, struct ast_str *http_header, struct ast_str *out, int fd, unsigned int static_content)
Generic function for sending HTTP/1.1 response.
Definition: http.c:472
struct ast_http_auth * ast_http_get_auth(struct ast_variable *headers)
Get HTTP authentication information from headers.
Definition: http.c:1611
#define ast_debug(level,...)
Log a DEBUG message.
#define ast_mutex_unlock(a)
Definition: lock.h:197
#define ast_mutex_lock(a)
Definition: lock.h:196
const char * method
Definition: res_pjsip.c:1279
static struct timeval last_scrape
static ast_mutex_t scrape_lock
Lock that protects data structures during an HTTP scrape.
static struct prometheus_metric core_scrape_metric
The scrape duration metric.
void prometheus_metric_to_string(struct prometheus_metric *metric, struct ast_str **output)
Convert a metric (and its children) into Prometheus compatible text.
static void scrape_metrics(struct ast_str **response)
#define NULL
Definition: resample.c:96
int ast_str_append(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Append to a thread local dynamic string.
Definition: strings.h:1139
static force_inline int attribute_pure ast_strlen_zero(const char *s)
Definition: strings.h:65
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:659
int ast_str_set(struct ast_str **buf, ssize_t max_len, const char *fmt,...)
Set a dynamic string using variable arguments.
Definition: strings.h:1113
HTTP authentication information.
Definition: http.h:125
char * password
Definition: http.h:129
char * userid
Definition: http.h:127
Support for dynamic strings.
Definition: strings.h:623
The configuration settings for this module.
Definition: cdr.c:318
int64_t ast_tvdiff_ms(struct timeval end, struct timeval start)
Computes the difference (in milliseconds) between two struct timeval instances.
Definition: time.h:107
#define RAII_VAR(vartype, varname, initval, dtor)
Declare a variable that will call a destructor function when it goes out of scope.
Definition: utils.h:941

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_str_set(), 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.

◆ load_module()

static int load_module ( void  )
static

Definition at line 973 of file res_prometheus.c.

974{
976
977 if (AST_VECTOR_INIT(&metrics, 64)) {
978 goto cleanup;
979 }
980
981 if (AST_VECTOR_INIT(&callbacks, 8)) {
982 goto cleanup;
983 }
984
985 if (AST_VECTOR_INIT(&providers, 8)) {
986 goto cleanup;
987 }
988
989 if (aco_info_init(&cfg_info)) {
990 goto cleanup;
991 }
993 aco_option_register(&cfg_info, "core_metrics_enabled", ACO_EXACT, global_options, "yes", OPT_BOOL_T, 1, FLDSET(struct prometheus_general_config, core_metrics_enabled));
995 aco_option_register(&cfg_info, "auth_username", ACO_EXACT, global_options, "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct prometheus_general_config, auth_username));
996 aco_option_register(&cfg_info, "auth_password", ACO_EXACT, global_options, "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct prometheus_general_config, auth_password));
997 aco_option_register(&cfg_info, "auth_realm", ACO_EXACT, global_options, "Asterisk Prometheus Metrics", OPT_STRINGFIELD_T, 0, STRFLDSET(struct prometheus_general_config, auth_realm));
998 if (aco_process_config(&cfg_info, 0) == ACO_PROCESS_ERROR) {
999 goto cleanup;
1000 }
1001
1002 if (cli_init()
1005 || bridge_metrics_init()) {
1006 goto cleanup;
1007 }
1008
1009 if(ast_module_check("res_pjsip_outbound_registration.so")) {
1010 /* Call a local function, used in the core prometheus code only */
1012 goto cleanup;
1013 }
1014
1016 goto cleanup;
1017 }
1018
1020
1021cleanup:
1023 aco_info_destroy(&cfg_info);
1027
1029}
ast_mutex_t lock
Definition: app_sla.c:337
int bridge_metrics_init(void)
Initialize bridge metrics.
Definition: bridges.c:206
int channel_metrics_init(void)
Initialize channel metrics.
Definition: channels.c:241
@ ACO_EXACT
void aco_info_destroy(struct aco_info *info)
Destroy an initialized aco_info struct.
@ ACO_PROCESS_ERROR
Their was an error and no changes were applied.
#define STRFLDSET(type,...)
Convert a struct and a list of stringfield fields to an argument list of field offsets.
int aco_info_init(struct aco_info *info)
Initialize an aco_info structure.
#define FLDSET(type,...)
Convert a struct and list of fields to an argument list of field offsets.
#define aco_option_register(info, name, matchtype, types, default_val, opt_type, flags,...)
Register a config option.
@ OPT_BOOL_T
Type for default option handler for bools (ast_true/ast_false)
@ OPT_STRINGFIELD_T
Type for default option handler for stringfields.
enum aco_process_status aco_process_config(struct aco_info *info, int reload)
Process a config info via the options registered with an aco_info.
static int enabled
Definition: dnsmgr.c:91
void ast_http_uri_unlink(struct ast_http_uri *urihandler)
Unregister a URI handler.
Definition: http.c:721
int ast_http_uri_link(struct ast_http_uri *urihandler)
Register a URI handler.
Definition: http.c:689
#define SCOPED_MUTEX(varname, lock)
scoped lock specialization for mutexes
Definition: lock.h:596
int ast_module_check(const char *name)
Check if module with the name given is loaded.
Definition: loader.c:2832
@ AST_MODULE_LOAD_SUCCESS
Definition: module.h:70
@ AST_MODULE_LOAD_DECLINE
Module has failed to load, may be in an inconsistent state.
Definition: module.h:78
static void * cleanup(void *unused)
Definition: pbx_realtime.c:124
int pjsip_outbound_registration_metrics_init(void)
Initialize PJSIP outbound registration metrics.
int cli_init(void)
Initialize CLI command.
int endpoint_metrics_init(void)
Initialize endpoint metrics.
struct @472 providers
struct aco_type * global_options[]
struct @471 callbacks
struct @470 metrics
static struct ast_http_uri prometheus_uri
Prometheus general configuration.
#define AST_VECTOR_FREE(vec)
Deallocates this vector.
Definition: vector.h:174
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
Definition: vector.h:113

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.

◆ module_config_alloc()

static void * module_config_alloc ( void  )
static

Module config constructor.

Definition at line 801 of file res_prometheus.c.

802{
803 struct module_config *config;
804
806 if (!config) {
807 return NULL;
808 }
809
811 if (!config->general) {
812 ao2_ref(config, -1);
813 config = NULL;
814 }
815
816 return config;
817}
#define ao2_alloc(data_size, destructor_fn)
Definition: astobj2.h:409
static const char config[]
Definition: chan_ooh323.c:111
static void module_config_dtor(void *obj)
Configuration object destructor.
void * prometheus_general_config_alloc(void)
Allocate a new configuration object.

References ao2_alloc, ao2_ref, config, module_config_dtor(), NULL, and prometheus_general_config_alloc().

◆ module_config_dtor()

static void module_config_dtor ( void *  obj)
static

Configuration object destructor.

Definition at line 791 of file res_prometheus.c.

792{
793 struct module_config *config = obj;
794
795 if (config->general) {
796 ao2_ref(config->general, -1);
797 }
798}

References ao2_ref, and config.

Referenced by module_config_alloc().

◆ prometheus_callback_register()

int prometheus_callback_register ( struct prometheus_callback callback)

Register a metric callback

Parameters
callbackThe callback to register
Return values
0success
-1error

Definition at line 556 of file res_prometheus.c.

557{
559
560 if (!callback || !callback->callback_fn || ast_strlen_zero(callback->name)) {
561 return -1;
562 }
563
565
566 return 0;
567}
static struct ast_channel * callback(struct ast_channelstorage_instance *driver, ao2_callback_data_fn *cb_fn, void *arg, void *data, int ao2_flags)
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition: vector.h:256

References ast_strlen_zero(), AST_VECTOR_APPEND, callback(), callbacks, lock, SCOPED_MUTEX, and scrape_lock.

Referenced by AST_TEST_DEFINE(), bridge_metrics_init(), channel_metrics_init(), and endpoint_metrics_init().

◆ prometheus_callback_unregister()

void prometheus_callback_unregister ( struct prometheus_callback callback)

Remove a registered callback.

Parameters
callbackThe callback to unregister

Definition at line 569 of file res_prometheus.c.

570{
572 int i;
573
574 for (i = 0; i < AST_VECTOR_SIZE(&callbacks); i++) {
575 struct prometheus_callback *entry = AST_VECTOR_GET(&callbacks, i);
576
577 if (!strcmp(callback->name, entry->name)) {
579 return;
580 }
581 }
582}
Defines a callback that will be invoked when the HTTP route is called.
const char * name
The name of our callback (always useful for debugging)
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:609
#define AST_VECTOR_REMOVE(vec, idx, preserve_ordered)
Remove an element from a vector by index.
Definition: vector.h:412
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:680

References AST_VECTOR_GET, AST_VECTOR_REMOVE, AST_VECTOR_SIZE, callback(), 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().

◆ prometheus_config_post_apply()

static void prometheus_config_post_apply ( void  )
static

Post-apply callback for the config framework.

This sets any run-time information derived from the configuration

Definition at line 855 of file res_prometheus.c.

856{
858 int i;
859
860 /* We can get away with this as the lifetime of the URI
861 * registered with the HTTP core is contained within
862 * the lifetime of the module configuration
863 */
864 prometheus_uri.uri = mod_cfg->general->uri;
865
866 /* Re-register the core metrics */
867 for (i = 0; i < ARRAY_LEN(core_metrics); i++) {
869 }
870 if (mod_cfg->general->core_metrics_enabled) {
871 char eid_str[32];
872 ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
873
875
877 1, "version", ast_get_version());
879 2, "build_options", ast_get_build_opts());
881 3, "build_date", ast_build_date);
883 4, "build_os", ast_build_os);
885 5, "build_kernel", ast_build_kernel);
887 6, "build_host", ast_build_hostname);
890 "%d", 1);
891
892 for (i = 0; i < ARRAY_LEN(core_metrics); i++) {
893 PROMETHEUS_METRIC_SET_LABEL(&core_metrics[i], 0, "eid", eid_str);
895 }
896 }
897}
const char * ast_get_build_opts(void)
const char * ast_get_version(void)
Retrieve the Asterisk version string.
const char * ast_build_os
Definition: buildinfo.c:32
const char * ast_build_hostname
Definition: buildinfo.c:29
const char * ast_build_date
Definition: buildinfo.c:33
const char * ast_build_kernel
Definition: buildinfo.c:30
int prometheus_metric_unregister(struct prometheus_metric *metric)
Remove a registered metric.
int prometheus_metric_register(struct prometheus_metric *metric)
static struct prometheus_metric core_metrics[]
Core metrics to scrape.
#define METRIC_CORE_PROPS_ARRAY_INDEX
#define PROMETHEUS_METRIC_SET_LABEL(metric, label, n, v)
Convenience macro for setting a label / value in a metric.
const char * uri
Definition: http.h:105
int value
Definition: syslog.c:37
char * ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
Convert an EID to a string.
Definition: utils.c:2839
#define ARRAY_LEN(a)
Definition: utils.h:666
struct ast_eid ast_eid_default
Global EID.
Definition: options.c:94

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().

◆ prometheus_config_pre_apply()

static int prometheus_config_pre_apply ( void  )
static

Pre-apply callback for the config framework.

This validates that required fields exist and are populated.

Definition at line 832 of file res_prometheus.c.

833{
834 struct module_config *config = aco_pending_config(&cfg_info);
835
836 if (!config->general->enabled) {
837 /* If we're not enabled, we don't care about anything else */
838 return 0;
839 }
840
841 if (!ast_strlen_zero(config->general->auth_username)
842 && ast_strlen_zero(config->general->auth_password)) {
843 ast_log(AST_LOG_ERROR, "'auth_username' set without a corresponding 'auth_password'\n");
844 return -1;
845 }
846
847 return 0;
848}
#define ast_log
Definition: astobj2.c:42
void * aco_pending_config(struct aco_info *info)
Get pending config changes.
#define AST_LOG_ERROR

References aco_pending_config(), ast_log, AST_LOG_ERROR, ast_strlen_zero(), and config.

◆ prometheus_counter_create()

struct prometheus_metric * prometheus_counter_create ( const char *  name,
const char *  help 
)

Create a malloc'd counter metric.

Note
The metric must be registered after creation
Parameters
nameThe name of the metric
helpHelp text for the metric
Return values
prometheus_metricon success
NULLon error

Definition at line 473 of file res_prometheus.c.

474{
475 struct prometheus_metric *metric;
476
478 if (!metric) {
479 return NULL;
480 }
482
483 return metric;
484}
static const char name[]
Definition: format_mp3.c:68
static struct prometheus_metric * prometheus_metric_create(const char *name, const char *help)
@ PROMETHEUS_METRIC_COUNTER
A metric whose value always goes up.
An actual, honest to god, metric.
const char * help
Pointer to a static string defining this metric's help text.
enum prometheus_metric_type type
What type of metric we are.

References prometheus_metric::help, name, NULL, PROMETHEUS_METRIC_COUNTER, prometheus_metric_create(), and prometheus_metric::type.

Referenced by AST_TEST_DEFINE().

◆ prometheus_gauge_create()

struct prometheus_metric * prometheus_gauge_create ( const char *  name,
const char *  help 
)

Create a malloc'd gauge metric.

Note
The metric must be registered after creation
Parameters
nameThe name of the metric
helpHelp text for the metric
Return values
prometheus_metricon success
NULLon error

Definition at line 460 of file res_prometheus.c.

461{
462 struct prometheus_metric *metric;
463
465 if (!metric) {
466 return NULL;
467 }
469
470 return metric;
471}
@ PROMETHEUS_METRIC_GAUGE
A metric whose value can bounce around like a jackrabbit.

References prometheus_metric::help, name, NULL, prometheus_metric_create(), PROMETHEUS_METRIC_GAUGE, and prometheus_metric::type.

Referenced by AST_TEST_DEFINE().

◆ prometheus_general_config_alloc()

void * prometheus_general_config_alloc ( void  )

Allocate a new configuration object.

The returned object is an AO2 ref counted object

Return values
NULLon error
configon success

Definition at line 754 of file res_prometheus.c.

755{
757
759 if (!config || ast_string_field_init(config, 32)) {
760 return NULL;
761 }
762
763 return config;
764}
static void prometheus_general_config_dtor(void *obj)
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:359

References ao2_alloc, ast_string_field_init, config, NULL, and prometheus_general_config_dtor().

Referenced by config_alloc(), and module_config_alloc().

◆ prometheus_general_config_dtor()

static void prometheus_general_config_dtor ( void *  obj)
static

Definition at line 747 of file res_prometheus.c.

748{
749 struct prometheus_general_config *config = obj;
750
752}
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object
Definition: stringfields.h:374

References ast_string_field_free_memory, and config.

Referenced by prometheus_general_config_alloc().

◆ prometheus_general_config_get()

struct prometheus_general_config * prometheus_general_config_get ( void  )

Retrieve the current configuration of the module.

config is an AO2 ref counted object

Note
This should primarily be done for testing purposes.
Return values
NULLon error
configon success

Definition at line 766 of file res_prometheus.c.

767{
769
770 if (!mod_cfg) {
771 return NULL;
772 }
773 ao2_bump(mod_cfg->general);
774
775 return mod_cfg->general;
776}
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480

References ao2_bump, ao2_cleanup, ao2_global_obj_ref, NULL, and RAII_VAR.

Referenced by prometheus_show_status(), reload_module(), and test_init_cb().

◆ prometheus_general_config_set()

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.

Note
This should primarily be done for testing purposes

Definition at line 778 of file res_prometheus.c.

779{
781
782 if (!mod_cfg) {
783 return;
784 }
785 ao2_replace(mod_cfg->general, config);
787}
#define ao2_replace(dst, src)
Replace one object reference with another cleaning up the original.
Definition: astobj2.h:501
static void prometheus_config_post_apply(void)
Post-apply callback for the config framework.

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().

◆ prometheus_last_scrape_duration_get()

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

Return values
Thescrape duration, in milliseconds

Definition at line 729 of file res_prometheus.c.

730{
731 int64_t duration;
732
733 if (sscanf(core_scrape_metric.value, "%" PRIu64, &duration) != 1) {
734 return -1;
735 }
736
737 return duration;
738}

References core_scrape_metric, and prometheus_metric::value.

Referenced by prometheus_show_status().

◆ prometheus_last_scrape_time_get()

struct timeval prometheus_last_scrape_time_get ( void  )

Retrieve the timestamp when the last scrape occurred.

Return values
Thetime when the last scrape occurred

Definition at line 740 of file res_prometheus.c.

741{
743
744 return last_scrape;
745}

References last_scrape, lock, SCOPED_MUTEX, and scrape_lock.

Referenced by prometheus_show_status().

◆ prometheus_metric_cmp()

static int prometheus_metric_cmp ( struct prometheus_metric left,
struct prometheus_metric right 
)
static

Definition at line 274 of file res_prometheus.c.

276{
277 int i;
278 ast_debug(5, "Comparison: Names %s == %s\n", left->name, right->name);
279 if (strcmp(left->name, right->name)) {
280 return 0;
281 }
282
283 for (i = 0; i < PROMETHEUS_MAX_LABELS; i++) {
284 ast_debug(5, "Comparison: Label %d Names %s == %s\n", i,
285 left->labels[i].name, right->labels[i].name);
286 if (strcmp(left->labels[i].name, right->labels[i].name)) {
287 return 0;
288 }
289
290 ast_debug(5, "Comparison: Label %d Values %s == %s\n", i,
291 left->labels[i].value, right->labels[i].value);
292 if (strcmp(left->labels[i].value, right->labels[i].value)) {
293 return 0;
294 }
295 }
296
297 ast_debug(5, "Copmarison: %s (%p) is equal to %s (%p)\n",
298 left->name, left, right->name, right);
299 return 1;
300}
#define PROMETHEUS_MAX_LABELS
How many labels a single metric can have.
char name[PROMETHEUS_MAX_NAME_LENGTH]
The name of the label.
char value[PROMETHEUS_MAX_LABEL_LENGTH]
The value of the label.
char name[PROMETHEUS_MAX_NAME_LENGTH]
Our metric name.
struct prometheus_label labels[PROMETHEUS_MAX_LABELS]
The metric's labels.

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().

◆ prometheus_metric_create()

static struct prometheus_metric * prometheus_metric_create ( const char *  name,
const char *  help 
)
static

Definition at line 443 of file res_prometheus.c.

444{
445 struct prometheus_metric *metric = NULL;
446
447 metric = ast_calloc(1, sizeof(*metric));
448 if (!metric) {
449 return NULL;
450 }
452 ast_mutex_init(&metric->lock);
453
454 ast_copy_string(metric->name, name, sizeof(metric->name));
455 metric->help = help;
456
457 return metric;
458}
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ast_mutex_init(pmutex)
Definition: lock.h:193
@ PROMETHEUS_METRIC_MALLOCD
The metric was allocated on the heap.
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
enum prometheus_metric_allocation_strategy allocation_strategy
How this metric was allocated.
ast_mutex_t lock
A lock protecting the metric value.

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().

◆ prometheus_metric_free()

void prometheus_metric_free ( struct prometheus_metric metric)

Destroy a metric and all its children.

Note
If you still want the children, make sure you remove the head of the children list first.
Parameters
metricThe metric to destroy

Definition at line 414 of file res_prometheus.c.

415{
416 struct prometheus_metric *child;
417
418 if (!metric) {
419 return;
420 }
421
422 while ((child = AST_LIST_REMOVE_HEAD(&metric->children, entry))) {
424 }
425 ast_mutex_destroy(&metric->lock);
426
428 return;
429 } else if (metric->allocation_strategy == PROMETHEUS_METRIC_MALLOCD) {
430 ast_free(metric);
431 }
432}
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:833
#define ast_mutex_destroy(a)
Definition: lock.h:195
void prometheus_metric_free(struct prometheus_metric *metric)
Destroy a metric and all its children.
@ PROMETHEUS_METRIC_ALLOCD
The metric was allocated on the stack.
struct prometheus_metric::@273 entry
struct prometheus_metric::@272 children
A list of children metrics.

References prometheus_metric::allocation_strategy, ast_free, AST_LIST_REMOVE_HEAD, ast_mutex_destroy, prometheus_metric::children, prometheus_metric::entry, 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().

◆ prometheus_metric_full_to_string()

static void prometheus_metric_full_to_string ( struct prometheus_metric metric,
struct ast_str **  output 
)
static

Definition at line 506 of file res_prometheus.c.

508{
509 int i;
510 int labels_exist = 0;
511
512 ast_str_append(output, 0, "%s", metric->name);
513
514 for (i = 0; i < PROMETHEUS_MAX_LABELS; i++) {
515 if (!ast_strlen_zero(metric->labels[i].name)) {
516 labels_exist = 1;
517 if (i == 0) {
518 ast_str_append(output, 0, "%s", "{");
519 } else {
520 ast_str_append(output, 0, "%s", ",");
521 }
522 ast_str_append(output, 0, "%s=\"%s\"",
523 metric->labels[i].name,
524 metric->labels[i].value);
525 }
526 }
527
528 if (labels_exist) {
529 ast_str_append(output, 0, "%s", "}");
530 }
531
532 /*
533 * If no value exists, put in a 0. That ensures we don't anger Prometheus.
534 */
535 if (ast_strlen_zero(metric->value)) {
536 ast_str_append(output, 0, " 0\n");
537 } else {
538 ast_str_append(output, 0, " %s\n", metric->value);
539 }
540}

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().

◆ prometheus_metric_register()

int prometheus_metric_register ( struct prometheus_metric metric)

Register a metric for collection

Parameters
metricThe metric to register
Return values
0success
-1error

Definition at line 309 of file res_prometheus.c.

310{
312 int i;
313
314 if (!metric) {
315 return -1;
316 }
317
318 for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) {
319 struct prometheus_metric *existing = AST_VECTOR_GET(&metrics, i);
320 struct prometheus_metric *child;
321
322 if (prometheus_metric_cmp(existing, metric)) {
324 "Refusing registration of existing Prometheus metric: %s\n",
325 metric->name);
326 return -1;
327 }
328
329 AST_LIST_TRAVERSE(&existing->children, child, entry) {
330 if (prometheus_metric_cmp(child, metric)) {
332 "Refusing registration of existing Prometheus metric: %s\n",
333 metric->name);
334 return -1;
335 }
336 }
337
338 if (!strcmp(metric->name, existing->name)) {
339 ast_debug(3, "Nesting metric '%s' as child (%p) under existing (%p)\n",
340 metric->name, metric, existing);
341 AST_LIST_INSERT_TAIL(&existing->children, metric, entry);
342 return 0;
343 }
344 }
345
346 ast_debug(3, "Tracking new root metric '%s'\n", metric->name);
347 if (AST_VECTOR_APPEND(&metrics, metric)) {
348 ast_log(AST_LOG_WARNING, "Failed to grow vector to make room for Prometheus metric: %s\n",
349 metric->name);
350 return -1;
351 }
352
353 return 0;
354}
#define AST_LOG_WARNING
#define AST_LOG_NOTICE
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:491
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:731
static int prometheus_metric_cmp(struct prometheus_metric *left, struct prometheus_metric *right)

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, prometheus_metric::entry, lock, metrics, prometheus_metric::name, prometheus_metric_cmp(), SCOPED_MUTEX, and scrape_lock.

Referenced by AST_TEST_DEFINE(), and prometheus_config_post_apply().

◆ prometheus_metric_registered_count()

int prometheus_metric_registered_count ( void  )

The current number of registered metrics

Return values
Thecurrent number of registered metrics

Definition at line 302 of file res_prometheus.c.

303{
305
306 return AST_VECTOR_SIZE(&metrics);
307}

References AST_VECTOR_SIZE, lock, metrics, SCOPED_MUTEX, and scrape_lock.

Referenced by AST_TEST_DEFINE().

◆ prometheus_metric_to_string()

void prometheus_metric_to_string ( struct prometheus_metric metric,
struct ast_str **  output 
)

Convert a metric (and its children) into Prometheus compatible text.

Parameters
metricThe metric to convert to a string
[out]outputThe ast_str string to populate with the metric(s)

Definition at line 542 of file res_prometheus.c.

544{
545 struct prometheus_metric *child;
546
547 ast_str_append(output, 0, "# HELP %s %s\n", metric->name, metric->help);
548 ast_str_append(output, 0, "# TYPE %s %s\n", metric->name,
550 prometheus_metric_full_to_string(metric, output);
551 AST_LIST_TRAVERSE(&metric->children, child, entry) {
553 }
554}
static void prometheus_metric_full_to_string(struct prometheus_metric *metric, struct ast_str **output)
static const char * prometheus_metric_type_to_string(enum prometheus_metric_type type)

References AST_LIST_TRAVERSE, ast_str_append(), prometheus_metric::children, prometheus_metric::entry, 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().

◆ prometheus_metric_type_to_string()

static const char * prometheus_metric_type_to_string ( enum prometheus_metric_type  type)
static

Definition at line 486 of file res_prometheus.c.

487{
488 switch (type) {
490 return "counter";
492 return "gauge";
493 default:
494 ast_assert(0);
495 return "unknown";
496 }
497}
static const char type[]
Definition: chan_ooh323.c:109
#define ast_assert(a)
Definition: utils.h:739

References ast_assert, PROMETHEUS_METRIC_COUNTER, PROMETHEUS_METRIC_GAUGE, and type.

Referenced by prometheus_metric_to_string().

◆ prometheus_metric_unregister()

int prometheus_metric_unregister ( struct prometheus_metric metric)

Remove a registered metric.

Parameters
metricThe metric to unregister
Note
Unregistering also destroys the metric, if found
Return values
0The metric was found, unregistered, and disposed of
-1The metric was not found

Definition at line 356 of file res_prometheus.c.

357{
358 if (!metric) {
359 return -1;
360 }
361
362 {
364 int i;
365
366 ast_debug(3, "Removing metric '%s'\n", metric->name);
367 for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) {
368 struct prometheus_metric *existing = AST_VECTOR_GET(&metrics, i);
369
370 /*
371 * If this is a complete match, remove the matching metric
372 * and place its children back into the list
373 */
374 if (prometheus_metric_cmp(existing, metric)) {
375 struct prometheus_metric *root;
376
378 root = AST_LIST_REMOVE_HEAD(&existing->children, entry);
379 if (root) {
380 struct prometheus_metric *child;
381 AST_LIST_TRAVERSE_SAFE_BEGIN(&existing->children, child, entry) {
383 AST_LIST_INSERT_TAIL(&root->children, child, entry);
384 }
386 AST_VECTOR_INSERT_AT(&metrics, i, root);
387 }
388 prometheus_metric_free(existing);
389 return 0;
390 }
391
392 /*
393 * Name match, but labels don't match. Find the matching entry with
394 * labels and remove it along with all of its children
395 */
396 if (!strcmp(existing->name, metric->name)) {
397 struct prometheus_metric *child;
398
399 AST_LIST_TRAVERSE_SAFE_BEGIN(&existing->children, child, entry) {
400 if (prometheus_metric_cmp(child, metric)) {
403 return 0;
404 }
405 }
407 }
408 }
409 }
410
411 return -1;
412}
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:615
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:529
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:557
#define AST_VECTOR_INSERT_AT(vec, idx, elem)
Insert an element at a specific position in a vector, growing the vector if needed.
Definition: vector.h:338

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, prometheus_metric::entry, 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().

◆ prometheus_metrics_provider_register()

void prometheus_metrics_provider_register ( const struct prometheus_metrics_provider provider)

Register a metrics provider.

Parameters
providerThe provider function table to register

Definition at line 899 of file res_prometheus.c.

900{
902}
static struct prometheus_metrics_provider provider
Definition: bridges.c:201

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().

◆ prometheus_scrape_to_string()

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.

Return values
NULLon error
Malloc'dast_str on success

Definition at line 713 of file res_prometheus.c.

714{
715 struct ast_str *response;
716
717 response = ast_str_create(512);
718 if (!response) {
719 return NULL;
720 }
721
723 scrape_metrics(&response);
725
726 return response;
727}

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().

◆ reload_module()

static int reload_module ( void  )
static

Definition at line 938 of file res_prometheus.c.

938 {
940 int i;
941 struct prometheus_general_config *general_config;
942
944 if (aco_process_config(&cfg_info, 1) == ACO_PROCESS_ERROR) {
945 return -1;
946 }
947
948 /* Our config should be all reloaded now */
949 general_config = prometheus_general_config_get();
950 for (i = 0; i < AST_VECTOR_SIZE(&providers); i++) {
952
953 if (!provider->reload_cb) {
954 continue;
955 }
956
957 if (provider->reload_cb(general_config)) {
958 ast_log(AST_LOG_WARNING, "Failed to reload metrics provider %s\n", provider->name);
959 ao2_ref(general_config, -1);
960 return -1;
961 }
962 }
963 ao2_ref(general_config, -1);
964
966 ast_log(AST_LOG_WARNING, "Failed to re-register Prometheus Metrics URI during reload\n");
967 return -1;
968 }
969
970 return 0;
971}
struct prometheus_general_config * prometheus_general_config_get(void)
Retrieve the current configuration of the module.
A function table for a metrics provider.
int(*const reload_cb)(struct prometheus_general_config *config)
Reload callback.
const char * name
Handy name of the provider for debugging purposes.

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.

◆ scrape_metrics()

static void scrape_metrics ( struct ast_str **  response)
static

Definition at line 584 of file res_prometheus.c.

585{
586 int i;
587
588 for (i = 0; i < AST_VECTOR_SIZE(&callbacks); i++) {
590
591 if (!callback) {
592 continue;
593 }
594
595 callback->callback_fn(response);
596 }
597
598 for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) {
599 struct prometheus_metric *metric = AST_VECTOR_GET(&metrics, i);
600
601 if (!metric) {
602 continue;
603 }
604
605 ast_mutex_lock(&metric->lock);
606 if (metric->get_metric_value) {
607 metric->get_metric_value(metric);
608 }
609 prometheus_metric_to_string(metric, response);
610 ast_mutex_unlock(&metric->lock);
611 }
612}
void(* get_metric_value)(struct prometheus_metric *metric)
Callback function to obtain the metric value.

References ast_mutex_lock, ast_mutex_unlock, AST_VECTOR_GET, AST_VECTOR_SIZE, callback(), callbacks, prometheus_metric::get_metric_value, prometheus_metric::lock, metrics, and prometheus_metric_to_string().

Referenced by http_callback(), and prometheus_scrape_to_string().

◆ unload_module()

static int unload_module ( void  )
static

Definition at line 904 of file res_prometheus.c.

905{
907 int i;
908
910
911 for (i = 0; i < AST_VECTOR_SIZE(&providers); i++) {
913
914 if (!provider->unload_cb) {
915 continue;
916 }
917
919 }
920
921 for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) {
922 struct prometheus_metric *metric = AST_VECTOR_GET(&metrics, i);
923
925 }
927
929
931
932 aco_info_destroy(&cfg_info);
934
935 return 0;
936}
#define ao2_global_obj_release(holder)
Release the ao2 object held in the global holder.
Definition: astobj2.h:859
void(*const unload_cb)(void)
Unload callback.

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.

Variable Documentation

◆ __mod_info

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

Definition at line 1043 of file res_prometheus.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 1043 of file res_prometheus.c.

◆ 

struct { ... } callbacks

◆ core_metrics

struct prometheus_metric core_metrics[]
static

Core metrics to scrape.

Definition at line 246 of file res_prometheus.c.

Referenced by prometheus_config_post_apply().

◆ core_scrape_metric

struct prometheus_metric core_scrape_metric
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 235 of file res_prometheus.c.

Referenced by http_callback(), prometheus_config_post_apply(), and prometheus_last_scrape_duration_get().

◆ current

size_t current

Definition at line 160 of file res_prometheus.c.

◆ elems

const struct prometheus_metrics_provider* * elems

Definition at line 160 of file res_prometheus.c.

◆ global_option

struct aco_type global_option
static

Definition at line 174 of file res_prometheus.c.

◆ global_options

struct aco_type* global_options[] = ACO_TYPES(&global_option)

Definition at line 182 of file res_prometheus.c.

Referenced by load_module().

◆ last_scrape

struct timeval last_scrape
static

Definition at line 166 of file res_prometheus.c.

Referenced by http_callback(), and prometheus_last_scrape_time_get().

◆ max

size_t max

Definition at line 160 of file res_prometheus.c.

◆ 

struct { ... } metrics

◆ prometheus_conf

struct aco_file prometheus_conf
Initial value:
= {
.filename = "prometheus.conf",
}
#define ACO_TYPES(...)
A helper macro to ensure that aco_info types always have a sentinel.
static struct aco_type global_option

Definition at line 184 of file res_prometheus.c.

◆ prometheus_uri

struct ast_http_uri prometheus_uri
static

◆ 

struct { ... } providers

◆ scrape_lock

ast_mutex_t scrape_lock = { PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP , NULL, {1, 0} }
static