Asterisk - The Open Source Telephony Project  21.4.1
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"

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.
 
 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. 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 1 .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. 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.
 

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.

Function Documentation

int prometheus_callback_register ( struct prometheus_callback callback)

Register a metric callback

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

Definition at line 535 of file res_prometheus.c.

References AST_VECTOR_APPEND, prometheus_callback::callback_fn, lock, prometheus_callback::name, SCOPED_MUTEX, and scrape_lock.

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

536 {
538 
539  if (!callback || !callback->callback_fn || ast_strlen_zero(callback->name)) {
540  return -1;
541  }
542 
543  AST_VECTOR_APPEND(&callbacks, callback);
544 
545  return 0;
546 }
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition: vector.h:256
const char * name
The name of our callback (always useful for debugging)
void(* callback_fn)(struct ast_str **output)
The callback function to invoke.
ast_mutex_t lock
#define SCOPED_MUTEX(varname, lock)
scoped lock specialization for mutexes
Definition: lock.h:589
static ast_mutex_t scrape_lock
Lock that protects data structures during an HTTP scrape.
void prometheus_callback_unregister ( struct prometheus_callback callback)

Remove a registered callback.

Parameters
callbackThe callback to unregister

Definition at line 548 of file res_prometheus.c.

References AST_VECTOR_GET, AST_VECTOR_REMOVE, AST_VECTOR_SIZE, lock, prometheus_callback::name, SCOPED_MUTEX, and scrape_lock.

549 {
551  int i;
552 
553  for (i = 0; i < AST_VECTOR_SIZE(&callbacks); i++) {
554  struct prometheus_callback *entry = AST_VECTOR_GET(&callbacks, i);
555 
556  if (!strcmp(callback->name, entry->name)) {
557  AST_VECTOR_REMOVE(&callbacks, i, 1);
558  return;
559  }
560  }
561 }
const char * name
The name of our callback (always useful for debugging)
ast_mutex_t lock
#define SCOPED_MUTEX(varname, lock)
scoped lock specialization for mutexes
Definition: lock.h:589
static ast_mutex_t scrape_lock
Lock that protects data structures during an HTTP scrape.
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:680
Definition: search.h:40
#define AST_VECTOR_REMOVE(vec, idx, preserve_ordered)
Remove an element from a vector by index.
Definition: vector.h:412
Defines a callback that will be invoked when the HTTP route is called.
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:609
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 827 of file res_prometheus.c.

References ao2_global_obj_ref, ast_eid_default, ast_eid_to_str(), ast_get_build_opts(), ast_get_version(), prometheus_metric_register(), PROMETHEUS_METRIC_SET_LABEL, prometheus_metric_unregister(), and RAII_VAR.

Referenced by prometheus_general_config_set().

828 {
829  RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(global_config), ao2_cleanup);
830  int i;
831 
832  /* We can get away with this as the lifetime of the URI
833  * registered with the HTTP core is contained within
834  * the lifetime of the module configuration
835  */
836  prometheus_uri.uri = mod_cfg->general->uri;
837 
838  /* Re-register the core metrics */
839  for (i = 0; i < ARRAY_LEN(core_metrics); i++) {
841  }
842  if (mod_cfg->general->core_metrics_enabled) {
843  char eid_str[32];
844  ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
845 
846  PROMETHEUS_METRIC_SET_LABEL(&core_scrape_metric, 0, "eid", eid_str);
847 
848  PROMETHEUS_METRIC_SET_LABEL(&core_metrics[METRIC_CORE_PROPS_ARRAY_INDEX],
849  1, "version", ast_get_version());
850  PROMETHEUS_METRIC_SET_LABEL(&core_metrics[METRIC_CORE_PROPS_ARRAY_INDEX],
851  2, "build_options", ast_get_build_opts());
852  PROMETHEUS_METRIC_SET_LABEL(&core_metrics[METRIC_CORE_PROPS_ARRAY_INDEX],
853  3, "build_date", ast_build_date);
854  PROMETHEUS_METRIC_SET_LABEL(&core_metrics[METRIC_CORE_PROPS_ARRAY_INDEX],
855  4, "build_os", ast_build_os);
856  PROMETHEUS_METRIC_SET_LABEL(&core_metrics[METRIC_CORE_PROPS_ARRAY_INDEX],
857  5, "build_kernel", ast_build_kernel);
858  PROMETHEUS_METRIC_SET_LABEL(&core_metrics[METRIC_CORE_PROPS_ARRAY_INDEX],
859  6, "build_host", ast_build_hostname);
860  snprintf(core_metrics[METRIC_CORE_PROPS_ARRAY_INDEX].value,
861  sizeof(core_metrics[METRIC_CORE_PROPS_ARRAY_INDEX].value),
862  "%d", 1);
863 
864  for (i = 0; i < ARRAY_LEN(core_metrics); i++) {
865  PROMETHEUS_METRIC_SET_LABEL(&core_metrics[i], 0, "eid", eid_str);
867  }
868  }
869 }
int prometheus_metric_unregister(struct prometheus_metric *metric)
Remove a registered metric.
char * ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
Convert an EID to a string.
Definition: utils.c:2839
const char * ast_get_version(void)
Retrieve the Asterisk version string.
Definition: version.c:18
const char * ast_get_build_opts(void)
Definition: version.c:28
#define ao2_global_obj_ref(holder)
Get a reference to the object stored in the global holder.
Definition: astobj2.h:918
#define PROMETHEUS_METRIC_SET_LABEL(metric, label, n, v)
Convenience macro for setting a label / value in a metric.
The configuration settings for this module.
Definition: cdr.c:264
int prometheus_metric_register(struct prometheus_metric *metric)
static struct prometheus_metric core_metrics[]
Core metrics to scrape.
struct ast_eid ast_eid_default
Global EID.
Definition: options.c:93
#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
static struct prometheus_metric core_scrape_metric
The scrape duration metric.
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 804 of file res_prometheus.c.

References aco_pending_config(), and module_config::general.

805 {
806  struct module_config *config = aco_pending_config(&cfg_info);
807 
808  if (!config->general->enabled) {
809  /* If we're not enabled, we don't care about anything else */
810  return 0;
811  }
812 
813  if (!ast_strlen_zero(config->general->auth_username)
814  && ast_strlen_zero(config->general->auth_password)) {
815  ast_log(AST_LOG_ERROR, "'auth_username' set without a corresponding 'auth_password'\n");
816  return -1;
817  }
818 
819  return 0;
820 }
void * aco_pending_config(struct aco_info *info)
Get pending config changes.
The configuration settings for this module.
Definition: cdr.c:264
struct ast_cdr_config * general
Definition: cdr.c:265
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 452 of file res_prometheus.c.

References PROMETHEUS_METRIC_COUNTER, and prometheus_metric::type.

453 {
454  struct prometheus_metric *metric;
455 
456  metric = prometheus_metric_create(name, help);
457  if (!metric) {
458  return NULL;
459  }
461 
462  return metric;
463 }
An actual, honest to god, metric.
A metric whose value always goes up.
enum prometheus_metric_type type
What type of metric we are.
const char * help
Pointer to a static string defining this metric's help text.
char name[PROMETHEUS_MAX_NAME_LENGTH]
Our metric name.
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 439 of file res_prometheus.c.

References PROMETHEUS_METRIC_GAUGE, and prometheus_metric::type.

440 {
441  struct prometheus_metric *metric;
442 
443  metric = prometheus_metric_create(name, help);
444  if (!metric) {
445  return NULL;
446  }
447  metric->type = PROMETHEUS_METRIC_GAUGE;
448 
449  return metric;
450 }
An actual, honest to god, metric.
enum prometheus_metric_type type
What type of metric we are.
const char * help
Pointer to a static string defining this metric's help text.
char name[PROMETHEUS_MAX_NAME_LENGTH]
Our metric name.
A metric whose value can bounce around like a jackrabbit.
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 726 of file res_prometheus.c.

References ast_string_field_init.

Referenced by module_config_alloc().

727 {
728  struct prometheus_general_config *config;
729 
730  config = ao2_alloc(sizeof(*config), prometheus_general_config_dtor);
731  if (!config || ast_string_field_init(config, 32)) {
732  return NULL;
733  }
734 
735  return config;
736 }
Prometheus general configuration.
#define ast_string_field_init(x, size)
Initialize a field pool and fields.
Definition: stringfields.h:359
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 738 of file res_prometheus.c.

References ao2_bump, ao2_global_obj_ref, and RAII_VAR.

739 {
740  RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(global_config), ao2_cleanup);
741 
742  if (!mod_cfg) {
743  return NULL;
744  }
745  ao2_bump(mod_cfg->general);
746 
747  return mod_cfg->general;
748 }
#define ao2_global_obj_ref(holder)
Get a reference to the object stored in the global holder.
Definition: astobj2.h:918
#define ao2_bump(obj)
Bump refcount on an AO2 object by one, returning the object.
Definition: astobj2.h:480
The configuration settings for this module.
Definition: cdr.c:264
#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
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 750 of file res_prometheus.c.

References ao2_global_obj_ref, ao2_replace, prometheus_config_post_apply(), and RAII_VAR.

751 {
752  RAII_VAR(struct module_config *, mod_cfg, ao2_global_obj_ref(global_config), ao2_cleanup);
753 
754  if (!mod_cfg) {
755  return;
756  }
757  ao2_replace(mod_cfg->general, config);
759 }
#define ao2_global_obj_ref(holder)
Get a reference to the object stored in the global holder.
Definition: astobj2.h:918
The configuration settings for this module.
Definition: cdr.c:264
#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.
#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
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 701 of file res_prometheus.c.

References prometheus_metric::value.

702 {
703  int64_t duration;
704 
705  if (sscanf(core_scrape_metric.value, "%" PRIu64, &duration) != 1) {
706  return -1;
707  }
708 
709  return duration;
710 }
char value[PROMETHEUS_MAX_VALUE_LENGTH]
The current value.
static struct prometheus_metric core_scrape_metric
The scrape duration metric.
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 712 of file res_prometheus.c.

References lock, SCOPED_MUTEX, and scrape_lock.

713 {
715 
716  return last_scrape;
717 }
ast_mutex_t lock
#define SCOPED_MUTEX(varname, lock)
scoped lock specialization for mutexes
Definition: lock.h:589
static ast_mutex_t scrape_lock
Lock that protects data structures during an HTTP scrape.
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 393 of file res_prometheus.c.

References prometheus_metric::allocation_strategy, AST_LIST_REMOVE_HEAD, prometheus_metric::children, prometheus_metric::lock, PROMETHEUS_METRIC_ALLOCD, and PROMETHEUS_METRIC_MALLOCD.

Referenced by prometheus_metric_unregister().

394 {
395  struct prometheus_metric *child;
396 
397  if (!metric) {
398  return;
399  }
400 
401  while ((child = AST_LIST_REMOVE_HEAD(&metric->children, entry))) {
402  prometheus_metric_free(child);
403  }
404  ast_mutex_destroy(&metric->lock);
405 
407  return;
408  } else if (metric->allocation_strategy == PROMETHEUS_METRIC_MALLOCD) {
409  ast_free(metric);
410  }
411 }
An actual, honest to god, metric.
enum prometheus_metric_allocation_strategy allocation_strategy
How this metric was allocated.
The metric was allocated on the stack.
struct prometheus_metric::@274 children
A list of children metrics.
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:833
ast_mutex_t lock
A lock protecting the metric value.
Definition: search.h:40
The metric was allocated on the heap.
void prometheus_metric_free(struct prometheus_metric *metric)
Destroy a metric and all its children.
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 288 of file res_prometheus.c.

References ast_debug, AST_LIST_INSERT_TAIL, AST_LIST_TRAVERSE, AST_VECTOR_APPEND, AST_VECTOR_GET, AST_VECTOR_SIZE, prometheus_metric::children, lock, prometheus_metric::name, SCOPED_MUTEX, and scrape_lock.

Referenced by prometheus_config_post_apply().

289 {
291  int i;
292 
293  if (!metric) {
294  return -1;
295  }
296 
297  for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) {
298  struct prometheus_metric *existing = AST_VECTOR_GET(&metrics, i);
299  struct prometheus_metric *child;
300 
301  if (prometheus_metric_cmp(existing, metric)) {
302  ast_log(AST_LOG_NOTICE,
303  "Refusing registration of existing Prometheus metric: %s\n",
304  metric->name);
305  return -1;
306  }
307 
308  AST_LIST_TRAVERSE(&existing->children, child, entry) {
309  if (prometheus_metric_cmp(child, metric)) {
310  ast_log(AST_LOG_NOTICE,
311  "Refusing registration of existing Prometheus metric: %s\n",
312  metric->name);
313  return -1;
314  }
315  }
316 
317  if (!strcmp(metric->name, existing->name)) {
318  ast_debug(3, "Nesting metric '%s' as child (%p) under existing (%p)\n",
319  metric->name, metric, existing);
320  AST_LIST_INSERT_TAIL(&existing->children, metric, entry);
321  return 0;
322  }
323  }
324 
325  ast_debug(3, "Tracking new root metric '%s'\n", metric->name);
326  if (AST_VECTOR_APPEND(&metrics, metric)) {
327  ast_log(AST_LOG_WARNING, "Failed to grow vector to make room for Prometheus metric: %s\n",
328  metric->name);
329  return -1;
330  }
331 
332  return 0;
333 }
An actual, honest to god, metric.
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition: vector.h:256
ast_mutex_t lock
#define SCOPED_MUTEX(varname, lock)
scoped lock specialization for mutexes
Definition: lock.h:589
struct prometheus_metric::@274 children
A list of children metrics.
#define ast_debug(level,...)
Log a DEBUG message.
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:731
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:491
char name[PROMETHEUS_MAX_NAME_LENGTH]
Our metric name.
static ast_mutex_t scrape_lock
Lock that protects data structures during an HTTP scrape.
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:680
Definition: search.h:40
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:609
int prometheus_metric_registered_count ( void  )

The current number of registered metrics

Return values
Thecurrent number of registered metrics

Definition at line 281 of file res_prometheus.c.

References AST_VECTOR_SIZE, lock, SCOPED_MUTEX, and scrape_lock.

282 {
284 
285  return AST_VECTOR_SIZE(&metrics);
286 }
ast_mutex_t lock
#define SCOPED_MUTEX(varname, lock)
scoped lock specialization for mutexes
Definition: lock.h:589
static ast_mutex_t scrape_lock
Lock that protects data structures during an HTTP scrape.
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:609
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 521 of file res_prometheus.c.

References AST_LIST_TRAVERSE, ast_str_append(), prometheus_metric::children, prometheus_metric::help, prometheus_metric::name, and prometheus_metric::type.

523 {
524  struct prometheus_metric *child;
525 
526  ast_str_append(output, 0, "# HELP %s %s\n", metric->name, metric->help);
527  ast_str_append(output, 0, "# TYPE %s %s\n", metric->name,
528  prometheus_metric_type_to_string(metric->type));
529  prometheus_metric_full_to_string(metric, output);
530  AST_LIST_TRAVERSE(&metric->children, child, entry) {
531  prometheus_metric_full_to_string(child, output);
532  }
533 }
An actual, honest to god, metric.
enum prometheus_metric_type type
What type of metric we are.
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
struct prometheus_metric::@274 children
A list of children metrics.
#define AST_LIST_TRAVERSE(head, var, field)
Loops over (traverses) the entries in a list.
Definition: linkedlists.h:491
const char * help
Pointer to a static string defining this metric's help text.
char name[PROMETHEUS_MAX_NAME_LENGTH]
Our metric name.
Definition: search.h:40
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 335 of file res_prometheus.c.

References ast_debug, AST_LIST_INSERT_TAIL, AST_LIST_REMOVE_CURRENT, AST_LIST_REMOVE_HEAD, AST_LIST_TRAVERSE_SAFE_BEGIN, AST_LIST_TRAVERSE_SAFE_END, AST_VECTOR_GET, AST_VECTOR_INSERT_AT, AST_VECTOR_REMOVE, AST_VECTOR_SIZE, prometheus_metric::children, lock, prometheus_metric::name, prometheus_metric_free(), SCOPED_MUTEX, and scrape_lock.

Referenced by prometheus_config_post_apply().

336 {
337  if (!metric) {
338  return -1;
339  }
340 
341  {
343  int i;
344 
345  ast_debug(3, "Removing metric '%s'\n", metric->name);
346  for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) {
347  struct prometheus_metric *existing = AST_VECTOR_GET(&metrics, i);
348 
349  /*
350  * If this is a complete match, remove the matching metric
351  * and place its children back into the list
352  */
353  if (prometheus_metric_cmp(existing, metric)) {
354  struct prometheus_metric *root;
355 
356  AST_VECTOR_REMOVE(&metrics, i, 1);
357  root = AST_LIST_REMOVE_HEAD(&existing->children, entry);
358  if (root) {
359  struct prometheus_metric *child;
360  AST_LIST_TRAVERSE_SAFE_BEGIN(&existing->children, child, entry) {
362  AST_LIST_INSERT_TAIL(&root->children, child, entry);
363  }
365  AST_VECTOR_INSERT_AT(&metrics, i, root);
366  }
367  prometheus_metric_free(existing);
368  return 0;
369  }
370 
371  /*
372  * Name match, but labels don't match. Find the matching entry with
373  * labels and remove it along with all of its children
374  */
375  if (!strcmp(existing->name, metric->name)) {
376  struct prometheus_metric *child;
377 
378  AST_LIST_TRAVERSE_SAFE_BEGIN(&existing->children, child, entry) {
379  if (prometheus_metric_cmp(child, metric)) {
381  prometheus_metric_free(child);
382  return 0;
383  }
384  }
386  }
387  }
388  }
389 
390  return -1;
391 }
An actual, honest to god, metric.
#define AST_LIST_TRAVERSE_SAFE_END
Closes a safe loop traversal block.
Definition: linkedlists.h:615
ast_mutex_t lock
#define SCOPED_MUTEX(varname, lock)
scoped lock specialization for mutexes
Definition: lock.h:589
struct prometheus_metric::@274 children
A list of children metrics.
#define AST_LIST_REMOVE_CURRENT(field)
Removes the current entry from a list during a traversal.
Definition: linkedlists.h:557
#define ast_debug(level,...)
Log a DEBUG message.
#define AST_LIST_REMOVE_HEAD(head, field)
Removes and returns the head entry from a list.
Definition: linkedlists.h:833
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:731
char name[PROMETHEUS_MAX_NAME_LENGTH]
Our metric name.
static ast_mutex_t scrape_lock
Lock that protects data structures during an HTTP scrape.
#define AST_VECTOR_GET(vec, idx)
Get an element from a vector.
Definition: vector.h:680
#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
Definition: search.h:40
#define AST_LIST_TRAVERSE_SAFE_BEGIN(head, var, field)
Loops safely over (traverses) the entries in a list.
Definition: linkedlists.h:529
void prometheus_metric_free(struct prometheus_metric *metric)
Destroy a metric and all its children.
#define AST_VECTOR_REMOVE(vec, idx, preserve_ordered)
Remove an element from a vector by index.
Definition: vector.h:412
#define AST_VECTOR_SIZE(vec)
Get the number of elements in a vector.
Definition: vector.h:609
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 871 of file res_prometheus.c.

References AST_VECTOR_APPEND.

Referenced by bridge_metrics_init(), channel_metrics_init(), cli_init(), endpoint_metrics_init(), and pjsip_outbound_registration_metrics_init().

872 {
873  AST_VECTOR_APPEND(&providers, provider);
874 }
#define AST_VECTOR_APPEND(vec, elem)
Append an element to a vector, growing the vector if needed.
Definition: vector.h:256
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 685 of file res_prometheus.c.

References ast_str_create, and scrape_lock.

686 {
687  struct ast_str *response;
688 
689  response = ast_str_create(512);
690  if (!response) {
691  return NULL;
692  }
693 
694  ast_mutex_lock(&scrape_lock);
695  scrape_metrics(&response);
696  ast_mutex_unlock(&scrape_lock);
697 
698  return response;
699 }
Support for dynamic strings.
Definition: strings.h:623
static ast_mutex_t scrape_lock
Lock that protects data structures during an HTTP scrape.
#define ast_str_create(init_len)
Create a malloc'ed dynamic length string.
Definition: strings.h:659

Variable Documentation

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

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
An aco_type structure to link the "general" category to the skel_global_config type.
Definition: app_skel.c:241

Definition at line 163 of file res_prometheus.c.