Debian Codegraph @

res/prometheus/endpoints.c

raw symbols SCIP
1 /*
2 * Asterisk -- An open source telephony toolkit.
3 *
4 * Copyright (C) 2019 Sangoma, Inc.
5 *
6 * Matt Jordan <[email protected]>
7 *
8 * See http://www.asterisk.org for more information about
9 * the Asterisk project. Please do not directly contact
10 * any of the maintainers of this project for assistance;
11 * the project provides a web site, mailing lists and IRC
12 * channels for your use.
13 *
14 * This program is free software, distributed under the terms of
15 * the GNU General Public License Version 2. See the LICENSE file
16 * at the top of the source tree.
17 */
18
19 /*!
20 * \file
21 * \brief Prometheus Endpoint Metrics
22 *
23 * \author Matt Jordan <[email protected]>
24 *
25 */
26
27 #include "asterisk.h"
28
29 #include "asterisk/stasis_endpoints.h"
30 #include "asterisk/res_prometheus.h"
31 #include "prometheus_internal.h"
32
33 #define ENDPOINTS_STATE_HELP "Individual endpoint states. 0=unknown; 1=offline; 2=online."
34
35 #define ENDPOINTS_CHANNELS_COUNT_HELP "Count of the number of channels currently existing that are associated with the endpoint."
36
37 /*!
38 * \internal
39 * \brief Callback function to get an endpoint's current state
40 *
41 * \param metric The metric to populate
42 * \param snapshot Endpoint snapshot
43 */
44 static void get_endpoint_state(struct prometheus_metric *metric, struct ast_endpoint_snapshot *snapshot)
45 {
46 snprintf(metric->value, sizeof(metric->value), "%d", snapshot->state);
47 }
48
49 /*!
50 * \internal
51 * \brief Callback function to get the current number of channel's associated with an endpoint
52 *
53 * \param metric The metric to populate
54 * \param snapshot Endpoint snapshot
55 */
56 static void get_endpoint_channel_count(struct prometheus_metric *metric, struct ast_endpoint_snapshot *snapshot)
57 {
58 snprintf(metric->value, sizeof(metric->value), "%d", snapshot->num_channels);
59 }
60
61 /*!
62 * \internal
63 * \brief Helper struct for generating individual endpoint stats
64 */
65 struct endpoint_metric_defs {
66 /*!
67 * \brief Help text to display
68 */
69 const char *help;
70 /*!
71 * \brief Name of the metric
72 */
73 const char *name;
74 /*!
75 * \brief Callback function to generate a metric value for a given endpoint
76 */
77 void (* const get_value)(struct prometheus_metric *metric, struct ast_endpoint_snapshot *snapshot);
78 } endpoint_metric_defs[] = {
79 {
80 .help = ENDPOINTS_STATE_HELP,
81 .name = "asterisk_endpoints_state",
82 .get_value = get_endpoint_state,
83 },
84 {
85 .help = ENDPOINTS_CHANNELS_COUNT_HELP,
86 .name = "asterisk_endpoints_channels_count",
87 .get_value = get_endpoint_channel_count,
88 },
89 };
90
91 /*!
92 * \internal
93 * \brief Callback invoked when Prometheus scrapes the server
94 *
95 * \param response The response to populate with formatted metrics
96 */
97 static void endpoints_scrape_cb(struct ast_str **response)
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;
106 struct prometheus_metric endpoint_count = PROMETHEUS_METRIC_STATIC_INITIALIZATION(
107 PROMETHEUS_METRIC_GAUGE,
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
115 endpoint_cache = stasis_cache_dump(ast_endpoint_cache(), ast_endpoint_snapshot_type());
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 }
174
175 struct prometheus_callback endpoints_callback = {
176 .name = "Endpoints callback",
177 .callback_fn = endpoints_scrape_cb,
178 };
179
180 /*!
181 * \internal
182 * \brief Callback invoked when the core module is unloaded
183 */
184 static void endpoint_metrics_unload_cb(void)
185 {
186 prometheus_callback_unregister(&endpoints_callback);
187 }
188
189 /*!
190 * \internal
191 * \brief Metrics provider definition
192 */
193 static struct prometheus_metrics_provider provider = {
194 .name = "endpoints",
195 .unload_cb = endpoint_metrics_unload_cb,
196 };
197
198 int endpoint_metrics_init(void)
199 {
200 prometheus_metrics_provider_register(&provider);
201 prometheus_callback_register(&endpoints_callback);
202
203 return 0;
204 }