Asterisk - The Open Source Telephony Project  18.5.0
Data Structures | Macros | Functions | Variables
channels.c File Reference

Prometheus Channel Metrics. More...

#include "asterisk.h"
#include "asterisk/res_prometheus.h"
#include "asterisk/stasis_channels.h"
#include "asterisk/pbx.h"
#include "prometheus_internal.h"
Include dependency graph for channels.c:

Go to the source code of this file.

Data Structures

struct  channel_metric_defs
 

Macros

#define CHANNELS_DURATION_HELP   "Individual channel durations (in seconds)."
 
#define CHANNELS_STATE_HELP   "Individual channel states. 0=down; 1=reserved; 2=offhook; 3=dialing; 4=ring; 5=ringing; 6=up; 7=busy; 8=dialing_offhook; 9=prering."
 

Functions

int channel_metrics_init (void)
 Initialize channel metrics. More...
 
static void channel_metrics_unload_cb (void)
 
static void channels_scrape_cb (struct ast_str **response)
 
static void get_channel_duration (struct prometheus_metric *metric, struct ast_channel_snapshot *snapshot)
 
static void get_channel_state (struct prometheus_metric *metric, struct ast_channel_snapshot *snapshot)
 
static void get_current_call_count (struct prometheus_metric *metric)
 
static void get_total_call_count (struct prometheus_metric *metric)
 

Variables

struct channel_metric_defs channel_metric_defs []
 
struct prometheus_callback channels_callback
 
static struct prometheus_metric global_channel_metrics []
 
static struct prometheus_metrics_provider provider
 

Detailed Description

Prometheus Channel Metrics.

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

Definition in file channels.c.

Macro Definition Documentation

◆ CHANNELS_DURATION_HELP

#define CHANNELS_DURATION_HELP   "Individual channel durations (in seconds)."

Definition at line 36 of file channels.c.

◆ CHANNELS_STATE_HELP

#define CHANNELS_STATE_HELP   "Individual channel states. 0=down; 1=reserved; 2=offhook; 3=dialing; 4=ring; 5=ringing; 6=up; 7=busy; 8=dialing_offhook; 9=prering."

Definition at line 34 of file channels.c.

Function Documentation

◆ channel_metrics_init()

int channel_metrics_init ( void  )

Initialize channel metrics.

Return values
0success
-1error

Definition at line 241 of file channels.c.

References prometheus_callback_register(), and prometheus_metrics_provider_register().

Referenced by load_module().

242 {
245 
246  return 0;
247 }
int prometheus_callback_register(struct prometheus_callback *callback)
void prometheus_metrics_provider_register(const struct prometheus_metrics_provider *provider)
Register a metrics provider.
struct prometheus_callback channels_callback
Definition: channels.c:218
static struct prometheus_metrics_provider provider
Definition: channels.c:236

◆ channel_metrics_unload_cb()

static void channel_metrics_unload_cb ( void  )
static

Definition at line 227 of file channels.c.

References prometheus_callback_unregister().

228 {
230 }
struct prometheus_callback channels_callback
Definition: channels.c:218
void prometheus_callback_unregister(struct prometheus_callback *callback)
Remove a registered callback.

◆ channels_scrape_cb()

static void channels_scrape_cb ( struct ast_str **  response)
static

Definition at line 130 of file channels.c.

References ao2_container_clone, ao2_container_count(), ao2_iterator_destroy(), ao2_iterator_init(), ao2_iterator_next, ao2_ref, ARRAY_LEN, ast_calloc, ast_channel_cache_all(), ast_copy_string(), ast_eid_default, ast_eid_to_str(), ast_free, AST_LIST_INSERT_TAIL, ast_channel_snapshot::base, channel_cache, channels, prometheus_metric::children, prometheus_metric::get_metric_value, channel_metric_defs::get_value, channel_metric_defs::help, prometheus_metric::help, ast_channel_snapshot_peer::linkedid, channel_metric_defs::name, ast_channel_snapshot_base::name, NULL, ast_channel_snapshot::peer, PROMETHEUS_METRIC_GAUGE, PROMETHEUS_METRIC_SET_LABEL, PROMETHEUS_METRIC_STATIC_INITIALIZATION, prometheus_metric_to_string(), ast_channel_snapshot_base::type, prometheus_metric::type, ast_channel_snapshot_base::uniqueid, and prometheus_metric::value.

131 {
133  struct ao2_container *channels;
134  struct ao2_iterator it_chans;
135  struct ast_channel_snapshot *snapshot;
136  struct prometheus_metric *channel_metrics;
137  char eid_str[32];
138  int num_channels;
139  int i, j;
142  "asterisk_channels_count",
143  "Current channel count.",
144  NULL
145  );
146 
147  ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
148 
149  channel_cache = ast_channel_cache_all();
150  if (!channel_cache) {
151  return;
152  }
153 
154  channels = ao2_container_clone(channel_cache, 0);
155  ao2_ref(channel_cache, -1);
156  if (!channels) {
157  return;
158  }
159 
160  num_channels = ao2_container_count(channels);
161 
162  /* Channel count */
163  PROMETHEUS_METRIC_SET_LABEL(&channel_count, 0, "eid", eid_str);
164  snprintf(channel_count.value, sizeof(channel_count.value), "%d", num_channels);
165  prometheus_metric_to_string(&channel_count, response);
166 
167  /* Global call values */
168  for (i = 0; i < ARRAY_LEN(global_channel_metrics); i++) {
169  PROMETHEUS_METRIC_SET_LABEL(&global_channel_metrics[i], 0, "eid", eid_str);
172  }
173 
174  if (num_channels == 0) {
175  ao2_ref(channels, -1);
176  return;
177  }
178 
179  /* Channel dependent values */
180  channel_metrics = ast_calloc(ARRAY_LEN(channel_metric_defs) * num_channels, sizeof(*channel_metrics));
181  if (!channel_metrics) {
182  ao2_ref(channels, -1);
183  return;
184  }
185 
186  it_chans = ao2_iterator_init(channels, 0);
187  for (i = 0; (snapshot = ao2_iterator_next(&it_chans)); ao2_ref(snapshot, -1), i++) {
188  for (j = 0; j < ARRAY_LEN(channel_metric_defs); j++) {
189  int index = i * ARRAY_LEN(channel_metric_defs) + j;
190 
191  channel_metrics[index].type = PROMETHEUS_METRIC_GAUGE;
192  ast_copy_string(channel_metrics[index].name, channel_metric_defs[j].name, sizeof(channel_metrics[index].name));
193  channel_metrics[index].help = channel_metric_defs[j].help;
194  PROMETHEUS_METRIC_SET_LABEL(&channel_metrics[index], 0, "eid", eid_str);
195  PROMETHEUS_METRIC_SET_LABEL(&channel_metrics[index], 1, "name", (snapshot->base->name));
196  PROMETHEUS_METRIC_SET_LABEL(&channel_metrics[index], 2, "id", (snapshot->base->uniqueid));
197  PROMETHEUS_METRIC_SET_LABEL(&channel_metrics[index], 3, "type", (snapshot->base->type));
198  if (snapshot->peer) {
199  PROMETHEUS_METRIC_SET_LABEL(&channel_metrics[index], 4, "linkedid", (snapshot->peer->linkedid));
200  }
201  channel_metric_defs[j].get_value(&channel_metrics[index], snapshot);
202 
203  if (i > 0) {
204  AST_LIST_INSERT_TAIL(&channel_metrics[j].children, &channel_metrics[index], entry);
205  }
206  }
207  }
208  ao2_iterator_destroy(&it_chans);
209 
210  for (j = 0; j < ARRAY_LEN(channel_metric_defs); j++) {
211  prometheus_metric_to_string(&channel_metrics[j], response);
212  }
213 
214  ast_free(channel_metrics);
215  ao2_ref(channels, -1);
216 }
struct ast_channel_snapshot_base * base
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
Structure representing a snapshot of channel state.
enum prometheus_metric_type type
What type of metric we are.
const ast_string_field uniqueid
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
void(*const get_value)(struct prometheus_metric *metric, struct ast_channel_snapshot *snapshot)
Callback function to generate a metric value for a given channel.
Definition: channels.c:81
void(* get_metric_value)(struct prometheus_metric *metric)
#define NULL
Definition: resample.c:96
const ast_string_field type
#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.
Definition: channels.c:73
#define PROMETHEUS_METRIC_SET_LABEL(metric, label, n, v)
Convenience macro for setting a label / value in a metric.
#define ao2_ref(o, delta)
Definition: astobj2.h:464
static struct channel_usage channels
struct ao2_container * ast_channel_cache_all(void)
struct prometheus_metric::@312 children
A list of children metrics.
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:730
#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
struct ast_eid ast_eid_default
Global EID.
Definition: options.c:93
static struct ao2_container * channel_cache
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
static struct prometheus_metric global_channel_metrics[]
Definition: channels.c:109
Definition: search.h:40
Generic container type.
struct ast_channel_snapshot_peer * peer
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
const ast_string_field name

◆ get_channel_duration()

static void get_channel_duration ( struct prometheus_metric metric,
struct ast_channel_snapshot snapshot 
)
static

Definition at line 57 of file channels.c.

References ast_tvdiff_sec(), ast_tvnow(), ast_channel_snapshot::base, ast_channel_snapshot_base::creationtime, and prometheus_metric::value.

58 {
59  struct timeval now = ast_tvnow();
60  int64_t duration = ast_tvdiff_sec(now, snapshot->base->creationtime);
61 
62  snprintf(metric->value, sizeof(metric->value), "%" PRIu64, duration);
63 }
struct ast_channel_snapshot_base * base
int64_t ast_tvdiff_sec(struct timeval end, struct timeval start)
Computes the difference (in seconds) between two struct timeval instances.
Definition: time.h:64
struct timeval ast_tvnow(void)
Returns current timeval. Meant to replace calls to gettimeofday().
Definition: time.h:150
char value[PROMETHEUS_MAX_VALUE_LENGTH]
The current value.

◆ get_channel_state()

static void get_channel_state ( struct prometheus_metric metric,
struct ast_channel_snapshot snapshot 
)
static

Definition at line 45 of file channels.c.

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

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

◆ get_current_call_count()

static void get_current_call_count ( struct prometheus_metric metric)
static

Definition at line 100 of file channels.c.

References ast_active_calls(), and prometheus_metric::value.

101 {
102  snprintf(metric->value, sizeof(metric->value), "%d", ast_active_calls());
103 }
int ast_active_calls(void)
Retrieve the number of active calls.
Definition: pbx.c:4764
char value[PROMETHEUS_MAX_VALUE_LENGTH]
The current value.

◆ get_total_call_count()

static void get_total_call_count ( struct prometheus_metric metric)
static

Definition at line 95 of file channels.c.

References ast_processed_calls(), and prometheus_metric::value.

96 {
97  snprintf(metric->value, sizeof(metric->value), "%d", ast_processed_calls());
98 }
int ast_processed_calls(void)
Retrieve the total number of calls processed through the PBX since last restart.
Definition: pbx.c:4769
char value[PROMETHEUS_MAX_VALUE_LENGTH]
The current value.

Variable Documentation

◆ channel_metric_defs

◆ channels_callback

struct prometheus_callback channels_callback
Initial value:
= {
.name = "Channels callback",
.callback_fn = channels_scrape_cb,
}
static void channels_scrape_cb(struct ast_str **response)
Definition: channels.c:130

Definition at line 218 of file channels.c.

◆ global_channel_metrics

struct prometheus_metric global_channel_metrics[]
static

Definition at line 109 of file channels.c.

◆ provider

struct prometheus_metrics_provider provider
static
Initial value:
= {
.name = "channels",
}
static void channel_metrics_unload_cb(void)
Definition: channels.c:227

Definition at line 236 of file channels.c.