Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Functions | Variables
res/prometheus/endpoints.c File Reference

Prometheus Endpoint Metrics. More...

#include "asterisk.h"
#include "asterisk/stasis_endpoints.h"
#include "asterisk/res_prometheus.h"
#include "prometheus_internal.h"
Include dependency graph for res/prometheus/endpoints.c:

Go to the source code of this file.

Data Structures

struct  endpoint_metric_defs
 

Macros

#define ENDPOINTS_CHANNELS_COUNT_HELP   "Count of the number of channels currently existing that are associated with the endpoint."
 
#define ENDPOINTS_STATE_HELP   "Individual endpoint states. 0=unknown; 1=offline; 2=online."
 

Functions

int endpoint_metrics_init (void)
 Initialize endpoint metrics. More...
 
static void endpoint_metrics_unload_cb (void)
 
static void endpoints_scrape_cb (struct ast_str **response)
 
static void get_endpoint_channel_count (struct prometheus_metric *metric, struct ast_endpoint_snapshot *snapshot)
 
static void get_endpoint_state (struct prometheus_metric *metric, struct ast_endpoint_snapshot *snapshot)
 

Variables

struct endpoint_metric_defs endpoint_metric_defs []
 
struct prometheus_callback endpoints_callback
 
static struct prometheus_metrics_provider provider
 

Detailed Description

Prometheus Endpoint Metrics.

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

Definition in file res/prometheus/endpoints.c.

Macro Definition Documentation

◆ ENDPOINTS_CHANNELS_COUNT_HELP

#define ENDPOINTS_CHANNELS_COUNT_HELP   "Count of the number of channels currently existing that are associated with the endpoint."

Definition at line 35 of file res/prometheus/endpoints.c.

◆ ENDPOINTS_STATE_HELP

#define ENDPOINTS_STATE_HELP   "Individual endpoint states. 0=unknown; 1=offline; 2=online."

Definition at line 33 of file res/prometheus/endpoints.c.

Function Documentation

◆ endpoint_metrics_init()

int endpoint_metrics_init ( void  )

Initialize endpoint metrics.

Return values
0success
-1error

Definition at line 198 of file res/prometheus/endpoints.c.

References prometheus_callback_register(), and prometheus_metrics_provider_register().

Referenced by load_module().

199 {
202 
203  return 0;
204 }
int prometheus_callback_register(struct prometheus_callback *callback)
void prometheus_metrics_provider_register(const struct prometheus_metrics_provider *provider)
Register a metrics provider.
static struct prometheus_metrics_provider provider
struct prometheus_callback endpoints_callback

◆ endpoint_metrics_unload_cb()

static void endpoint_metrics_unload_cb ( void  )
static

Definition at line 184 of file res/prometheus/endpoints.c.

References prometheus_callback_unregister().

185 {
187 }
void prometheus_callback_unregister(struct prometheus_callback *callback)
Remove a registered callback.
struct prometheus_callback endpoints_callback

◆ endpoints_scrape_cb()

static void endpoints_scrape_cb ( struct ast_str **  response)
static

Definition at line 97 of file res/prometheus/endpoints.c.

References ao2_container_clone, ao2_container_count(), ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, ARRAY_LEN, ast_calloc, ast_copy_string(), ast_eid_default, ast_eid_to_str(), ast_endpoint_cache(), ast_endpoint_snapshot_type(), ast_free, AST_LIST_INSERT_TAIL, endpoints, endpoint_metric_defs::get_value, endpoint_metric_defs::help, prometheus_metric::help, ast_endpoint_snapshot::id, endpoint_metric_defs::name, NULL, PROMETHEUS_METRIC_GAUGE, PROMETHEUS_METRIC_SET_LABEL, PROMETHEUS_METRIC_STATIC_INITIALIZATION, prometheus_metric_to_string(), ast_endpoint_snapshot::resource, stasis_cache_dump(), stasis_message_data(), ast_endpoint_snapshot::tech, prometheus_metric::type, and prometheus_metric::value.

98 {
99  struct ao2_container *endpoint_cache;
100  struct ao2_container *endpoints;
101  struct ao2_iterator it_endpoints;
102  struct stasis_message *message;
103  struct prometheus_metric *endpoint_metrics;
104  char eid_str[32];
105  int i, j, num_endpoints;
108  "asterisk_endpoints_count",
109  "Current endpoint count.",
110  NULL
111  );
112 
113  ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
114 
116  if (!endpoint_cache) {
117  return;
118  }
119  endpoints = ao2_container_clone(endpoint_cache, 0);
120  ao2_ref(endpoint_cache, -1);
121  if (!endpoints) {
122  return;
123  }
124 
125  num_endpoints = ao2_container_count(endpoints);
126 
127  /* Current endpoint count */
128  PROMETHEUS_METRIC_SET_LABEL(&endpoint_count, 0, "eid", eid_str);
129  snprintf(endpoint_count.value, sizeof(endpoint_count.value), "%d", num_endpoints);
130  prometheus_metric_to_string(&endpoint_count, response);
131 
132  if (num_endpoints == 0) {
133  ao2_ref(endpoints, -1);
134  return;
135  }
136 
137  endpoint_metrics = ast_calloc(ARRAY_LEN(endpoint_metric_defs) * num_endpoints, sizeof(*endpoint_metrics));
138  if (!endpoint_metrics) {
139  ao2_ref(endpoints, -1);
140  return;
141  }
142 
143  /* Endpoint dependent values */
144  it_endpoints = ao2_iterator_init(endpoints, 0);
145  for (i = 0; (message = ao2_iterator_next(&it_endpoints)); ao2_ref(message, -1), i++) {
146  struct ast_endpoint_snapshot *snapshot = stasis_message_data(message);
147 
148  for (j = 0; j < ARRAY_LEN(endpoint_metric_defs); j++) {
149  int index = i * ARRAY_LEN(endpoint_metric_defs) + j;
150 
151  endpoint_metrics[index].type = PROMETHEUS_METRIC_GAUGE;
152  ast_copy_string(endpoint_metrics[index].name, endpoint_metric_defs[j].name, sizeof(endpoint_metrics[index].name));
153  endpoint_metrics[index].help = endpoint_metric_defs[j].help;
154  PROMETHEUS_METRIC_SET_LABEL(&endpoint_metrics[index], 0, "eid", eid_str);
155  PROMETHEUS_METRIC_SET_LABEL(&endpoint_metrics[index], 1, "id", (snapshot->id));
156  PROMETHEUS_METRIC_SET_LABEL(&endpoint_metrics[index], 2, "tech", (snapshot->tech));
157  PROMETHEUS_METRIC_SET_LABEL(&endpoint_metrics[index], 3, "resource", (snapshot->resource));
158  endpoint_metric_defs[j].get_value(&endpoint_metrics[index], snapshot);
159 
160  if (i != 0) {
161  AST_LIST_INSERT_TAIL(&endpoint_metrics[j].children, &endpoint_metrics[index], entry);
162  }
163  }
164  }
165  ao2_iterator_destroy(&it_endpoints);
166 
167  for (j = 0; j < ARRAY_LEN(endpoint_metric_defs); j++) {
168  prometheus_metric_to_string(&endpoint_metrics[j], response);
169  }
170 
171  ast_free(endpoint_metrics);
172  ao2_ref(endpoints, -1);
173 }
An actual, honest to god, metric.
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
#define ARRAY_LEN(a)
Definition: isdn_lib.c:42
char * ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
Convert an EID to a string.
Definition: main/utils.c:2587
#define ao2_container_clone(orig, flags)
Definition: astobj2.h:1430
struct stasis_message_type * ast_endpoint_snapshot_type(void)
Message type for ast_endpoint_snapshot.
enum prometheus_metric_type type
What type of metric we are.
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
const ast_string_field id
struct ao2_container * stasis_cache_dump(struct stasis_cache *cache, struct stasis_message_type *type)
Dump cached items to a subscription for the ast_eid_default entity.
Definition: stasis_cache.c:736
#define NULL
Definition: resample.c:96
#define PROMETHEUS_METRIC_STATIC_INITIALIZATION(mtype, n, h, cb)
Convenience macro for initializing a metric on the stack.
void prometheus_metric_to_string(struct prometheus_metric *metric, struct ast_str **output)
Convert a metric (and its children) into Prometheus compatible text.
const char * help
Help text to display.
#define PROMETHEUS_METRIC_SET_LABEL(metric, label, n, v)
Convenience macro for setting a label / value in a metric.
const ast_string_field resource
static struct ao2_container * endpoints
#define ao2_ref(o, delta)
Definition: astobj2.h:464
A snapshot of an endpoint&#39;s state.
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:730
void * stasis_message_data(const struct stasis_message *msg)
Get the data contained in a message.
const ast_string_field tech
#define ao2_iterator_next(iter)
Definition: astobj2.h:1933
char value[PROMETHEUS_MAX_VALUE_LENGTH]
The current value.
const char * help
Pointer to a static string defining this metric&#39;s help text.
static const char name[]
Definition: cdr_mysql.c:74
#define ast_free(a)
Definition: astmm.h:182
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:204
void(*const get_value)(struct prometheus_metric *metric, struct ast_endpoint_snapshot *snapshot)
Callback function to generate a metric value for a given endpoint.
struct ast_eid ast_eid_default
Global EID.
Definition: options.c:93
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1841
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:401
Definition: search.h:40
Generic container type.
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
struct stasis_cache * ast_endpoint_cache(void)
Backend cache for ast_endpoint_topic_all_cached().

◆ get_endpoint_channel_count()

static void get_endpoint_channel_count ( struct prometheus_metric metric,
struct ast_endpoint_snapshot snapshot 
)
static

Definition at line 56 of file res/prometheus/endpoints.c.

References ast_endpoint_snapshot::num_channels, and prometheus_metric::value.

57 {
58  snprintf(metric->value, sizeof(metric->value), "%d", snapshot->num_channels);
59 }
char value[PROMETHEUS_MAX_VALUE_LENGTH]
The current value.

◆ get_endpoint_state()

static void get_endpoint_state ( struct prometheus_metric metric,
struct ast_endpoint_snapshot snapshot 
)
static

Definition at line 44 of file res/prometheus/endpoints.c.

References ast_endpoint_snapshot::state, and prometheus_metric::value.

45 {
46  snprintf(metric->value, sizeof(metric->value), "%d", snapshot->state);
47 }
char value[PROMETHEUS_MAX_VALUE_LENGTH]
The current value.
enum ast_endpoint_state state

Variable Documentation

◆ endpoint_metric_defs

◆ endpoints_callback

struct prometheus_callback endpoints_callback
Initial value:
= {
.name = "Endpoints callback",
.callback_fn = endpoints_scrape_cb,
}
static void endpoints_scrape_cb(struct ast_str **response)

Definition at line 175 of file res/prometheus/endpoints.c.

◆ provider

struct prometheus_metrics_provider provider
static
Initial value:
= {
.name = "endpoints",
}
static void endpoint_metrics_unload_cb(void)

Definition at line 193 of file res/prometheus/endpoints.c.