Asterisk - The Open Source Telephony Project GIT-master-7e7a603
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.

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

References channels_callback, prometheus_callback_register(), prometheus_metrics_provider_register(), and provider.

Referenced by load_module().

◆ channel_metrics_unload_cb()

static void channel_metrics_unload_cb ( void  )
static

Definition at line 227 of file channels.c.

228{
230}
void prometheus_callback_unregister(struct prometheus_callback *callback)
Remove a registered callback.

References channels_callback, and prometheus_callback_unregister().

◆ channels_scrape_cb()

static void channels_scrape_cb ( struct ast_str **  response)
static

Definition at line 130 of file channels.c.

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
150 if (!channel_cache) {
151 return;
152 }
153
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++) {
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}
#define ast_free(a)
Definition: astmm.h:180
#define ast_calloc(num, len)
A wrapper for calloc()
Definition: astmm.h:202
#define ao2_iterator_next(iter)
Definition: astobj2.h:1911
int ao2_container_count(struct ao2_container *c)
Returns the number of elements in a container.
struct ao2_iterator ao2_iterator_init(struct ao2_container *c, int flags) attribute_warn_unused_result
Create an iterator for a container.
#define ao2_ref(o, delta)
Reference/unreference an object and return the old refcount.
Definition: astobj2.h:459
void ao2_iterator_destroy(struct ao2_iterator *iter)
Destroy a container iterator.
#define ao2_container_clone(orig, flags)
Create a clone/copy of the given container.
Definition: astobj2.h:1419
static struct prometheus_metric global_channel_metrics[]
Definition: channels.c:109
static struct channel_usage channels
static const char name[]
Definition: format_mp3.c:68
struct ao2_container * ast_channel_cache_all(void)
#define AST_LIST_INSERT_TAIL(head, elm, field)
Appends a list entry to the tail of a list.
Definition: linkedlists.h:731
#define PROMETHEUS_METRIC_SET_LABEL(metric, label, n, v)
Convenience macro for setting a label / value in a metric.
#define PROMETHEUS_METRIC_STATIC_INITIALIZATION(mtype, n, h, cb)
Convenience macro for initializing a metric on the stack.
@ PROMETHEUS_METRIC_GAUGE
A metric whose value can bounce around like a jackrabbit.
void prometheus_metric_to_string(struct prometheus_metric *metric, struct ast_str **output)
Convert a metric (and its children) into Prometheus compatible text.
#define NULL
Definition: resample.c:96
static struct ao2_container * channel_cache
void ast_copy_string(char *dst, const char *src, size_t size)
Size-limited null-terminating string copy.
Definition: strings.h:425
Generic container type.
When we need to walk through a container, we use an ao2_iterator to keep track of the current positio...
Definition: astobj2.h:1821
const ast_string_field uniqueid
const ast_string_field type
const ast_string_field name
Structure representing a snapshot of channel state.
struct ast_channel_snapshot_peer * peer
struct ast_channel_snapshot_base * base
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
const char * help
Help text to display.
Definition: channels.c:73
Definition: search.h:40
An actual, honest to god, metric.
struct prometheus_metric::@270 children
A list of children metrics.
void(* get_metric_value)(struct prometheus_metric *metric)
Callback function to obtain the metric value.
const char * help
Pointer to a static string defining this metric's help text.
char value[PROMETHEUS_MAX_VALUE_LENGTH]
The current value.
enum prometheus_metric_type type
What type of metric we are.
char * ast_eid_to_str(char *s, int maxlen, struct ast_eid *eid)
Convert an EID to a string.
Definition: utils.c:2839
#define ARRAY_LEN(a)
Definition: utils.h:666
struct ast_eid ast_eid_default
Global EID.
Definition: options.c:93

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, global_channel_metrics, prometheus_metric::help, channel_metric_defs::help, ast_channel_snapshot_peer::linkedid, 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(), prometheus_metric::type, ast_channel_snapshot_base::type, ast_channel_snapshot_base::uniqueid, and prometheus_metric::value.

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

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}
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_tvdiff_sec(), ast_tvnow(), ast_channel_snapshot::base, ast_channel_snapshot_base::creationtime, and prometheus_metric::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.

46{
47 snprintf(metric->value, sizeof(metric->value), "%d", snapshot->state);
48}
enum ast_channel_state state

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

◆ get_current_call_count()

static void get_current_call_count ( struct prometheus_metric metric)
static

Definition at line 100 of file channels.c.

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:4760

References ast_active_calls(), and prometheus_metric::value.

◆ get_total_call_count()

static void get_total_call_count ( struct prometheus_metric metric)
static

Definition at line 95 of file channels.c.

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:4765

References ast_processed_calls(), and prometheus_metric::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.

Referenced by channel_metrics_init(), and channel_metrics_unload_cb().

◆ global_channel_metrics

struct prometheus_metric global_channel_metrics[]
static

Definition at line 109 of file channels.c.

Referenced by channels_scrape_cb().

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

Referenced by channel_metrics_init().