Asterisk - The Open Source Telephony Project GIT-master-545c459
Loading...
Searching...
No Matches
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

static void __reg_module (void)
 
static void __unreg_module (void)
 
static AO2_GLOBAL_OBJ_STATIC (global_config)
 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 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.
 
static void module_config_dtor (void *obj)
 Configuration object destructor.
 
int prometheus_callback_register (struct prometheus_callback *callback)
 
void prometheus_callback_unregister (struct prometheus_callback *callback)
 Remove a registered callback.
 
static void prometheus_config_post_apply (void)
 Post-apply callback for the config framework.
 
static int prometheus_config_pre_apply (void)
 Pre-apply callback for the config framework.
 
struct prometheus_metricprometheus_counter_create (const char *name, const char *help)
 Create a malloc'd counter metric.
 
struct prometheus_metricprometheus_gauge_create (const char *name, const char *help)
 Create a malloc'd gauge metric.
 
void * prometheus_general_config_alloc (void)
 Allocate a new configuration object.
 
static void prometheus_general_config_dtor (void *obj)
 
struct prometheus_general_configprometheus_general_config_get (void)
 Retrieve the current configuration of the module.
 
void prometheus_general_config_set (struct prometheus_general_config *config)
 Set the configuration for the module.
 
int64_t prometheus_last_scrape_duration_get (void)
 Retrieve the amount of time it took to perform the last scrape.
 
struct timeval prometheus_last_scrape_time_get (void)
 Retrieve the timestamp when the last scrape occurred.
 
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.
 
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.
 
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.
 
void prometheus_metrics_provider_register (const struct prometheus_metrics_provider *provider)
 Register a metrics provider.
 
struct ast_strprometheus_scrape_to_string (void)
 Get the raw output of what a scrape would produce.
 
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 = ASTERISK_GPL_KEY , .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.
 
static struct prometheus_metric core_scrape_metric
 The scrape duration metric.
 
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 = AST_MUTEX_INIT_VALUE
 Lock that protects data structures during an HTTP scrape.
 

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

struct ast_module * AST_MODULE_SELF_SYM (   void)    __internal_res_prometheus_self

Definition at line 1083 of file res_prometheus.c.

◆ CORE_LAST_RELOAD_HELP

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

Definition at line 244 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 246 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 240 of file res_prometheus.c.

◆ CORE_UPTIME_HELP

#define CORE_UPTIME_HELP   "Asterisk instance uptime in seconds."

Definition at line 242 of file res_prometheus.c.

◆ METRIC_CORE_PROPS_ARRAY_INDEX

#define METRIC_CORE_PROPS_ARRAY_INDEX   0

Definition at line 280 of file res_prometheus.c.

Function Documentation

◆ __reg_module()

static void __reg_module ( void  )
static

Definition at line 1083 of file res_prometheus.c.

◆ __unreg_module()

static void __unreg_module ( void  )
static

Definition at line 1083 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 248 of file res_prometheus.c.

249{
250 struct timeval now = ast_tvnow();
251 int64_t duration = ast_tvdiff_sec(now, ast_startuptime);
252
253 snprintf(metric->value, sizeof(metric->value), "%" PRIu64, duration);
254}
struct timeval ast_startuptime
Definition asterisk.c:344
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 256 of file res_prometheus.c.

257{
258 struct timeval now = ast_tvnow();
259 int64_t duration = ast_tvdiff_sec(now, ast_lastreloadtime);
260
261 snprintf(metric->value, sizeof(metric->value), "%" PRIu64, duration);
262}
struct timeval ast_lastreloadtime
Definition asterisk.c:345

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 652 of file res_prometheus.c.

655{
657 struct ast_str *response = NULL;
658 struct ast_str *content_type_header = NULL;
659 struct timeval start;
660 struct timeval end;
661
662 /* If there is no module config or we're not enabled, we can't handle requests */
663 if (!mod_cfg || !mod_cfg->general->enabled) {
664 goto err503;
665 }
666
667 if (!ast_strlen_zero(mod_cfg->general->auth_username)) {
668 struct ast_http_auth *http_auth;
669
670 http_auth = ast_http_get_auth(headers);
671 if (!http_auth) {
672 goto err401;
673 }
674
675 if (strcmp(http_auth->userid, mod_cfg->general->auth_username)) {
676 ast_debug(5, "Invalid username provided for auth request: %s\n", http_auth->userid);
677 ao2_ref(http_auth, -1);
678 goto err401;
679 }
680
681 if (strcmp(http_auth->password, mod_cfg->general->auth_password)) {
682 ast_debug(5, "Invalid password provided for auth request: %s\n", http_auth->password);
683 ao2_ref(http_auth, -1);
684 goto err401;
685 }
686
687 ao2_ref(http_auth, -1);
688 }
689
690 response = ast_str_create(512);
691 content_type_header = ast_str_create(32);
692 if (!response || !content_type_header) {
693 goto err500;
694 }
695
696 ast_str_set(&content_type_header, 0, "Content-Type: text/plain\r\n");
697
698 start = ast_tvnow();
699
701
702 last_scrape = start;
703 scrape_metrics(&response);
704
705 if (mod_cfg->general->core_metrics_enabled) {
706 int64_t duration;
707
708 end = ast_tvnow();
709 duration = ast_tvdiff_ms(end, start);
710 snprintf(core_scrape_metric.value,
712 "%" PRIu64,
713 duration);
715 }
717
718 ast_http_send(ser, method, 200, "OK", content_type_header, response, 0, 0);
719
720 return 0;
721
722err401:
723 {
724 struct ast_str *auth_challenge_headers;
725
726 auth_challenge_headers = ast_str_create(128);
727 if (!auth_challenge_headers) {
728 goto err500;
729 }
730 ast_str_append(&auth_challenge_headers, 0,
731 "WWW-Authenticate: Basic realm=\"%s\"\r\n",
732 mod_cfg->general->auth_realm);
733 /* ast_http_send takes ownership of the ast_str */
734 ast_http_send(ser, method, 401, "Unauthorized", auth_challenge_headers, NULL, 0, 1);
735 }
736 ast_free(response);
737 ast_free(content_type_header);
738 return 0;
739err503:
740 ast_http_send(ser, method, 503, "Service Unavailable", NULL, NULL, 0, 1);
741 ast_free(response);
742 ast_free(content_type_header);
743 return 0;
744err500:
745 ast_http_send(ser, method, 500, "Server Error", NULL, NULL, 0, 1);
746 ast_free(response);
747 ast_free(content_type_header);
748 return 0;
749}
#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:522
struct ast_http_auth * ast_http_get_auth(struct ast_variable *headers)
Get HTTP authentication information from headers.
Definition http.c:1677
#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:1277
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:326
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:981

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 1011 of file res_prometheus.c.

1012{
1014
1015 if (AST_VECTOR_INIT(&metrics, 64)) {
1016 goto cleanup;
1017 }
1018
1019 if (AST_VECTOR_INIT(&callbacks, 8)) {
1020 goto cleanup;
1021 }
1022
1023 if (AST_VECTOR_INIT(&providers, 8)) {
1024 goto cleanup;
1025 }
1026
1027 if (aco_info_init(&cfg_info)) {
1028 goto cleanup;
1029 }
1031 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));
1032 aco_option_register(&cfg_info, "channels_detail_metrics_enabled", ACO_EXACT, global_options, "yes", OPT_BOOL_T, 1, FLDSET(struct prometheus_general_config, channels_detail_metrics_enabled));
1033 aco_option_register(&cfg_info, "bridges_detail_metrics_enabled", ACO_EXACT, global_options, "yes", OPT_BOOL_T, 1, FLDSET(struct prometheus_general_config, bridges_detail_metrics_enabled));
1035 aco_option_register(&cfg_info, "auth_username", ACO_EXACT, global_options, "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct prometheus_general_config, auth_username));
1036 aco_option_register(&cfg_info, "auth_password", ACO_EXACT, global_options, "", OPT_STRINGFIELD_T, 0, STRFLDSET(struct prometheus_general_config, auth_password));
1037 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));
1038 if (aco_process_config(&cfg_info, 0) == ACO_PROCESS_ERROR) {
1039 goto cleanup;
1040 }
1041
1042 if (cli_init()
1045 || bridge_metrics_init()) {
1046 goto cleanup;
1047 }
1048
1049 if(ast_module_check("res_pjsip_outbound_registration.so")) {
1050 /* Call a local function, used in the core prometheus code only */
1052 goto cleanup;
1053 }
1054
1056 goto cleanup;
1057 }
1058
1060
1061cleanup:
1063 aco_info_destroy(&cfg_info);
1067
1069}
ast_mutex_t lock
Definition app_sla.c:337
int bridge_metrics_init(void)
Initialize bridge metrics.
Definition bridges.c:212
int channel_metrics_init(void)
Initialize channel metrics.
Definition channels.c:247
@ 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:779
int ast_http_uri_link(struct ast_http_uri *urihandler)
Register a URI handler.
Definition http.c:747
#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
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 aco_type * global_options[]
struct @509 metrics
struct @511 providers
struct @510 callbacks
static struct ast_http_uri prometheus_uri
static void cleanup(void)
Clean up any old apps that we don't need any more.
Definition res_stasis.c:327
Prometheus general configuration.
#define AST_VECTOR_FREE(vec)
Deallocates this vector.
Definition vector.h:185
#define AST_VECTOR_INIT(vec, size)
Initialize a vector.
Definition vector.h:124

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 839 of file res_prometheus.c.

840{
841 struct module_config *config;
842
844 if (!config) {
845 return NULL;
846 }
847
849 if (!config->general) {
850 ao2_ref(config, -1);
851 config = NULL;
852 }
853
854 return config;
855}
#define ao2_alloc(data_size, destructor_fn)
Definition astobj2.h:409
static const char config[]
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 829 of file res_prometheus.c.

830{
831 struct module_config *config = obj;
832
833 if (config->general) {
834 ao2_ref(config->general, -1);
835 }
836}

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 594 of file res_prometheus.c.

595{
597
598 if (!callback || !callback->callback_fn || ast_strlen_zero(callback->name)) {
599 return -1;
600 }
601
603
604 return 0;
605}
static struct ast_channel * callback(struct ast_channelstorage_instance *driver, ao2_callback_data_fn *cb_fn, void *arg, void *data, int ao2_flags, int rdlock)
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition vector.h:267

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 607 of file res_prometheus.c.

608{
610 int i;
611
612 for (i = 0; i < AST_VECTOR_SIZE(&callbacks); i++) {
613 struct prometheus_callback *entry = AST_VECTOR_GET(&callbacks, i);
614
615 if (!strcmp(callback->name, entry->name)) {
617 return;
618 }
619 }
620}
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:637
#define AST_VECTOR_REMOVE(vec, idx, preserve_ordered)
Remove an element from a vector by index.
Definition vector.h:440
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition vector.h:708

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 893 of file res_prometheus.c.

894{
896 int i;
897
898 /* We can get away with this as the lifetime of the URI
899 * registered with the HTTP core is contained within
900 * the lifetime of the module configuration
901 */
902 prometheus_uri.uri = mod_cfg->general->uri;
903
904 /* Re-register the core metrics */
905 for (i = 0; i < ARRAY_LEN(core_metrics); i++) {
907 }
908 if (mod_cfg->general->core_metrics_enabled) {
909 char eid_str[32];
910 ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
911
913
915 1, "version", ast_get_version());
917 2, "build_options", ast_get_build_opts());
919 3, "build_date", ast_build_date);
921 4, "build_os", ast_build_os);
923 5, "build_kernel", ast_build_kernel);
925 6, "build_host", ast_build_hostname);
928 "%d", 1);
929
930 for (i = 0; i < ARRAY_LEN(core_metrics); i++) {
931 PROMETHEUS_METRIC_SET_LABEL(&core_metrics[i], 0, "eid", eid_str);
933 }
934 }
935}
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:2873
#define ARRAY_LEN(a)
Definition utils.h:706
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 870 of file res_prometheus.c.

871{
872 struct module_config *config = aco_pending_config(&cfg_info);
873
874 if (!config->general->enabled) {
875 /* If we're not enabled, we don't care about anything else */
876 return 0;
877 }
878
879 if (!ast_strlen_zero(config->general->auth_username)
880 && ast_strlen_zero(config->general->auth_password)) {
881 ast_log(AST_LOG_ERROR, "'auth_username' set without a corresponding 'auth_password'\n");
882 return -1;
883 }
884
885 return 0;
886}
#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 511 of file res_prometheus.c.

512{
513 struct prometheus_metric *metric;
514
516 if (!metric) {
517 return NULL;
518 }
520
521 return metric;
522}
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(), and 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 498 of file res_prometheus.c.

499{
500 struct prometheus_metric *metric;
501
503 if (!metric) {
504 return NULL;
505 }
507
508 return metric;
509}
@ 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(), and 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 792 of file res_prometheus.c.

793{
795
797 if (!config || ast_string_field_init(config, 32)) {
798 return NULL;
799 }
800
801 return config;
802}
static void prometheus_general_config_dtor(void *obj)
#define ast_string_field_init(x, size)
Initialize a field pool and fields.

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 785 of file res_prometheus.c.

786{
787 struct prometheus_general_config *config = obj;
788
790}
#define ast_string_field_free_memory(x)
free all memory - to be called before destroying the object

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 804 of file res_prometheus.c.

805{
807
808 if (!mod_cfg) {
809 return NULL;
810 }
811 ao2_bump(mod_cfg->general);
812
813 return mod_cfg->general;
814}
#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 bridges_scrape_cb(), channels_scrape_cb(), 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 816 of file res_prometheus.c.

817{
819
820 if (!mod_cfg) {
821 return;
822 }
823 ao2_replace(mod_cfg->general, config);
825}
#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(), AST_TEST_DEFINE(), AST_TEST_DEFINE(), 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 767 of file res_prometheus.c.

768{
769 int64_t duration;
770
771 if (sscanf(core_scrape_metric.value, "%" PRIu64, &duration) != 1) {
772 return -1;
773 }
774
775 return duration;
776}

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 778 of file res_prometheus.c.

779{
781
782 return last_scrape;
783}

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 312 of file res_prometheus.c.

314{
315 int i;
316 ast_debug(5, "Comparison: Names %s == %s\n", left->name, right->name);
317 if (strcmp(left->name, right->name)) {
318 return 0;
319 }
320
321 for (i = 0; i < PROMETHEUS_MAX_LABELS; i++) {
322 ast_debug(5, "Comparison: Label %d Names %s == %s\n", i,
323 left->labels[i].name, right->labels[i].name);
324 if (strcmp(left->labels[i].name, right->labels[i].name)) {
325 return 0;
326 }
327
328 ast_debug(5, "Comparison: Label %d Values %s == %s\n", i,
329 left->labels[i].value, right->labels[i].value);
330 if (strcmp(left->labels[i].value, right->labels[i].value)) {
331 return 0;
332 }
333 }
334
335 ast_debug(5, "Copmarison: %s (%p) is equal to %s (%p)\n",
336 left->name, left, right->name, right);
337 return 1;
338}
#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 481 of file res_prometheus.c.

482{
483 struct prometheus_metric *metric = NULL;
484
485 metric = ast_calloc(1, sizeof(*metric));
486 if (!metric) {
487 return NULL;
488 }
490 ast_mutex_init(&metric->lock);
491
492 ast_copy_string(metric->name, name, sizeof(metric->name));
493 metric->help = help;
494
495 return metric;
496}
#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 452 of file res_prometheus.c.

453{
454 struct prometheus_metric *child;
455
456 if (!metric) {
457 return;
458 }
459
460 while ((child = AST_LIST_REMOVE_HEAD(&metric->children, entry))) {
462 }
463 ast_mutex_destroy(&metric->lock);
464
466 return;
467 } else if (metric->allocation_strategy == PROMETHEUS_METRIC_MALLOCD) {
468 ast_free(metric);
469 }
470}
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
#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::@288 entry
struct prometheus_metric::@287 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 544 of file res_prometheus.c.

546{
547 int i;
548 int labels_exist = 0;
549
550 ast_str_append(output, 0, "%s", metric->name);
551
552 for (i = 0; i < PROMETHEUS_MAX_LABELS; i++) {
553 if (!ast_strlen_zero(metric->labels[i].name)) {
554 labels_exist = 1;
555 if (i == 0) {
556 ast_str_append(output, 0, "%s", "{");
557 } else {
558 ast_str_append(output, 0, "%s", ",");
559 }
560 ast_str_append(output, 0, "%s=\"%s\"",
561 metric->labels[i].name,
562 metric->labels[i].value);
563 }
564 }
565
566 if (labels_exist) {
567 ast_str_append(output, 0, "%s", "}");
568 }
569
570 /*
571 * If no value exists, put in a 0. That ensures we don't anger Prometheus.
572 */
573 if (ast_strlen_zero(metric->value)) {
574 ast_str_append(output, 0, " 0\n");
575 } else {
576 ast_str_append(output, 0, " %s\n", metric->value);
577 }
578}

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 347 of file res_prometheus.c.

348{
350 int i;
351
352 if (!metric) {
353 return -1;
354 }
355
356 for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) {
357 struct prometheus_metric *existing = AST_VECTOR_GET(&metrics, i);
358 struct prometheus_metric *child;
359
360 if (prometheus_metric_cmp(existing, metric)) {
362 "Refusing registration of existing Prometheus metric: %s\n",
363 metric->name);
364 return -1;
365 }
366
367 AST_LIST_TRAVERSE(&existing->children, child, entry) {
368 if (prometheus_metric_cmp(child, metric)) {
370 "Refusing registration of existing Prometheus metric: %s\n",
371 metric->name);
372 return -1;
373 }
374 }
375
376 if (!strcmp(metric->name, existing->name)) {
377 ast_debug(3, "Nesting metric '%s' as child (%p) under existing (%p)\n",
378 metric->name, metric, existing);
379 AST_LIST_INSERT_TAIL(&existing->children, metric, entry);
380 return 0;
381 }
382 }
383
384 ast_debug(3, "Tracking new root metric '%s'\n", metric->name);
385 if (AST_VECTOR_APPEND(&metrics, metric)) {
386 ast_log(AST_LOG_WARNING, "Failed to grow vector to make room for Prometheus metric: %s\n",
387 metric->name);
388 return -1;
389 }
390
391 return 0;
392}
#define AST_LOG_WARNING
#define AST_LOG_NOTICE
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
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(), 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 340 of file res_prometheus.c.

341{
343
344 return AST_VECTOR_SIZE(&metrics);
345}

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 580 of file res_prometheus.c.

582{
583 struct prometheus_metric *child;
584
585 ast_str_append(output, 0, "# HELP %s %s\n", metric->name, metric->help);
586 ast_str_append(output, 0, "# TYPE %s %s\n", metric->name,
588 prometheus_metric_full_to_string(metric, output);
589 AST_LIST_TRAVERSE(&metric->children, child, entry) {
591 }
592}
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(), 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 524 of file res_prometheus.c.

525{
526 switch (type) {
528 return "counter";
530 return "gauge";
531 default:
532 ast_assert(0);
533 return "unknown";
534 }
535}
static const char type[]
#define ast_assert(a)
Definition utils.h:779

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 394 of file res_prometheus.c.

395{
396 if (!metric) {
397 return -1;
398 }
399
400 {
402 int i;
403
404 ast_debug(3, "Removing metric '%s'\n", metric->name);
405 for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) {
406 struct prometheus_metric *existing = AST_VECTOR_GET(&metrics, i);
407
408 /*
409 * If this is a complete match, remove the matching metric
410 * and place its children back into the list
411 */
412 if (prometheus_metric_cmp(existing, metric)) {
413 struct prometheus_metric *root;
414
416 root = AST_LIST_REMOVE_HEAD(&existing->children, entry);
417 if (root) {
418 struct prometheus_metric *child;
419 AST_LIST_TRAVERSE_SAFE_BEGIN(&existing->children, child, entry) {
421 AST_LIST_INSERT_TAIL(&root->children, child, entry);
422 }
424 AST_VECTOR_INSERT_AT(&metrics, i, root);
425 }
426 prometheus_metric_free(existing);
427 return 0;
428 }
429
430 /*
431 * Name match, but labels don't match. Find the matching entry with
432 * labels and remove it along with all of its children
433 */
434 if (!strcmp(existing->name, metric->name)) {
435 struct prometheus_metric *child;
436
437 AST_LIST_TRAVERSE_SAFE_BEGIN(&existing->children, child, entry) {
438 if (prometheus_metric_cmp(child, metric)) {
441 return 0;
442 }
443 }
445 }
446 }
447 }
448
449 return -1;
450}
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
#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:349

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(), 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 937 of file res_prometheus.c.

938{
940}
static struct prometheus_metrics_provider provider
Definition bridges.c:207

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 751 of file res_prometheus.c.

752{
753 struct ast_str *response;
754
755 response = ast_str_create(512);
756 if (!response) {
757 return NULL;
758 }
759
761 scrape_metrics(&response);
763
764 return response;
765}

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 976 of file res_prometheus.c.

976 {
978 int i;
979 struct prometheus_general_config *general_config;
980
982 if (aco_process_config(&cfg_info, 1) == ACO_PROCESS_ERROR) {
983 return -1;
984 }
985
986 /* Our config should be all reloaded now */
987 general_config = prometheus_general_config_get();
988 for (i = 0; i < AST_VECTOR_SIZE(&providers); i++) {
990
991 if (!provider->reload_cb) {
992 continue;
993 }
994
995 if (provider->reload_cb(general_config)) {
996 ast_log(AST_LOG_WARNING, "Failed to reload metrics provider %s\n", provider->name);
997 ao2_ref(general_config, -1);
998 return -1;
999 }
1000 }
1001 ao2_ref(general_config, -1);
1002
1004 ast_log(AST_LOG_WARNING, "Failed to re-register Prometheus Metrics URI during reload\n");
1005 return -1;
1006 }
1007
1008 return 0;
1009}
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 622 of file res_prometheus.c.

623{
624 int i;
625
626 for (i = 0; i < AST_VECTOR_SIZE(&callbacks); i++) {
628
629 if (!callback) {
630 continue;
631 }
632
633 callback->callback_fn(response);
634 }
635
636 for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) {
637 struct prometheus_metric *metric = AST_VECTOR_GET(&metrics, i);
638
639 if (!metric) {
640 continue;
641 }
642
643 ast_mutex_lock(&metric->lock);
644 if (metric->get_metric_value) {
645 metric->get_metric_value(metric);
646 }
647 prometheus_metric_to_string(metric, response);
648 ast_mutex_unlock(&metric->lock);
649 }
650}
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 942 of file res_prometheus.c.

943{
945 int i;
946
948
949 for (i = 0; i < AST_VECTOR_SIZE(&providers); i++) {
951
952 if (!provider->unload_cb) {
953 continue;
954 }
955
957 }
958
959 for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) {
960 struct prometheus_metric *metric = AST_VECTOR_GET(&metrics, i);
961
963 }
965
967
969
970 aco_info_destroy(&cfg_info);
972
973 return 0;
974}
#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 = ASTERISK_GPL_KEY , .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 1083 of file res_prometheus.c.

◆ ast_module_info

const struct ast_module_info* ast_module_info = &__mod_info
static

Definition at line 1083 of file res_prometheus.c.

◆ [struct]

struct { ... } callbacks

◆ core_metrics

struct prometheus_metric core_metrics[]
static

Core metrics to scrape.

Definition at line 284 of file res_prometheus.c.

284 {
287 "asterisk_core_properties",
289 NULL),
292 "asterisk_core_uptime_seconds",
297 "asterisk_core_last_reload_seconds",
300};
#define CORE_LAST_RELOAD_HELP
static void get_core_uptime_cb(struct prometheus_metric *metric)
static void get_last_reload_cb(struct prometheus_metric *metric)
#define CORE_UPTIME_HELP
#define CORE_PROPERTIES_HELP
#define PROMETHEUS_METRIC_STATIC_INITIALIZATION(mtype, n, h, cb)
Convenience macro for initializing a metric on the stack.

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 273 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 198 of file res_prometheus.c.

◆ elems

const struct prometheus_metrics_provider* * elems

Definition at line 198 of file res_prometheus.c.

◆ global_option

struct aco_type global_option
static

Definition at line 212 of file res_prometheus.c.

212 {
213 .type = ACO_GLOBAL,
214 .name = "general",
215 .item_offset = offsetof(struct module_config, general),
216 .category_match = ACO_WHITELIST_EXACT,
217 .category = "general",
218};
@ ACO_GLOBAL
@ ACO_WHITELIST_EXACT

◆ global_options

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

Definition at line 220 of file res_prometheus.c.

Referenced by load_module().

◆ last_scrape

struct timeval last_scrape
static

Definition at line 204 of file res_prometheus.c.

Referenced by http_callback(), and prometheus_last_scrape_time_get().

◆ max

size_t max

Definition at line 198 of file res_prometheus.c.

◆ [struct]

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 222 of file res_prometheus.c.

222 {
223 .filename = "prometheus.conf",
224 .types = ACO_TYPES(&global_option),
225};

◆ prometheus_uri

struct ast_http_uri prometheus_uri
static

Definition at line 857 of file res_prometheus.c.

857 {
858 .description = "Prometheus Metrics URI",
859 .callback = http_callback,
860 .has_subtree = 1,
861 .data = NULL,
862 .key = __FILE__,
863};
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)

Referenced by load_module(), prometheus_config_post_apply(), reload_module(), and unload_module().

◆ [struct]

struct { ... } providers

◆ scrape_lock

ast_mutex_t scrape_lock = AST_MUTEX_INIT_VALUE
static