Debian Codegraph @

res/prometheus/pjsip_outbound_registrations.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 PJSIP Outbound Registration Metrics
22 *
23 * \author Matt Jordan <[email protected]>
24 *
25 */
26
27 #include "asterisk.h"
28
29 #include "asterisk/stasis_message_router.h"
30 #include "asterisk/stasis_system.h"
31 #include "asterisk/res_prometheus.h"
32
33 #ifdef HAVE_PJPROJECT
34 #include "asterisk/res_pjsip.h"
35 #endif /* HAVE_PJPROJECT */
36
37 #include "prometheus_internal.h"
38
39 #ifdef HAVE_PJPROJECT
40
41 /*! \internal \brief Our one and only Stasis message router */
42 static struct stasis_message_router *router;
43
44 /*!
45 * \internal
46 * \brief Wrapper object around our Metrics
47 *
48 * \details We keep a wrapper around the metric so we can easily
49 * update the value when the state of the registration changes, as
50 * well as remove and unregister the metric when someone destroys
51 * or reloads the registration
52 */
53 struct prometheus_metric_wrapper {
54 /*!
55 * \brief The actual metric. Worth noting that we do *NOT*
56 * own the metric, as it is registered with res_prometheus.
57 * Luckily, that module doesn't destroy metrics unless we
58 * tell it to or if the module unloads.
59 */
60 struct prometheus_metric *metric;
61 /*!
62 * \brief Unique key to look up the metric
63 */
64 char key[128];
65 };
66
67 /*!
68 * \internal Vector of metric wrappers
69 *
70 * \details
71 * Why a vector and not an ao2_container? Two reasons:
72 * (1) There's rarely a ton of outbound registrations, so an ao2_container
73 * is overkill when we can just walk a vector
74 * (2) The lifetime of wrappers is well contained
75 */
76 static AST_VECTOR(, struct prometheus_metric_wrapper *) metrics;
77
78 AST_MUTEX_DEFINE_STATIC(metrics_lock);
79
80 /*!
81 * \internal
82 * \brief Create a wrapper for a metric given a key
83 *
84 * \param key The unique key
85 *
86 * \retval NULL on error
87 * \retval malloc'd metric wrapper on success
88 */
89 static struct prometheus_metric_wrapper *create_wrapper(const char *key)
90 {
91 struct prometheus_metric_wrapper *wrapper;
92
93 wrapper = ast_calloc(1, sizeof(*wrapper));
94 if (!wrapper) {
95 return NULL;
96 }
97
98 ast_copy_string(wrapper->key, key, sizeof(wrapper->key));
99 return wrapper;
100 }
101
102 /*!
103 * \internal
104 * \brief Get a wrapper by its key
105 *
106 * \param key The unqiue key for the wrapper
107 *
108 * \retval NULL on no wrapper found :-\
109 * \retval wrapper on success
110 */
111 static struct prometheus_metric_wrapper *get_wrapper(const char *key)
112 {
113 int i;
114 SCOPED_MUTEX(lock, &metrics_lock);
115
116 for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) {
117 struct prometheus_metric_wrapper *wrapper = AST_VECTOR_GET(&metrics, i);
118
119 if (!strcmp(wrapper->key, key)) {
120 return wrapper;
121 }
122 }
123
124 return NULL;
125 }
126
127 /*!
128 * \internal
129 * \brief Convert an outbound registration state to a numeric value
130 *
131 * \param state The state to convert
132 *
133 * \retval int representation of the state
134 */
135 static int registration_state_to_int(const char *state)
136 {
137 if (!strcasecmp(state, "Registered")) {
138 return 1;
139 } else if (!strcasecmp(state, "Rejected")) {
140 return 2;
141 }
142 return 0;
143 }
144
145 /*!
146 * \internal
147 * \brief Sorcery observer callback called when a registration object is deleted
148 *
149 * \param obj The opaque object that was deleted
150 */
151 static void registration_deleted_observer(const void *obj)
152 {
153 struct ast_variable *fields;
154 struct ast_variable *it_fields;
155 int i;
156 SCOPED_MUTEX(lock, &metrics_lock);
157
158 /*
159 * Because our object is opaque, we have to do some pretty ... interesting
160 * things here to try and figure out what just happened.
161 */
162 fields = ast_sorcery_objectset_create(ast_sip_get_sorcery(), obj);
163 if (!fields) {
164 ast_debug(1, "Unable to convert presumed registry object %p to strings; bailing on delete\n", obj);
165 return;
166 }
167
168 for (it_fields = fields; it_fields; it_fields = it_fields->next) {
169 if (strcasecmp(it_fields->name, "client_uri")) {
170 continue;
171 }
172
173 for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) {
174 struct prometheus_metric_wrapper *wrapper = AST_VECTOR_GET(&metrics, i);
175
176 if (strcmp(wrapper->key, it_fields->value)) {
177 continue;
178 }
179
180 ast_debug(1, "Registration metric '%s' deleted; purging with prejudice\n", wrapper->key);
181 AST_VECTOR_REMOVE(&metrics, i, 1);
182 /* This will free the metric as well */
183 prometheus_metric_unregister(wrapper->metric);
184 ast_free(wrapper);
185 }
186 }
187
188 ast_variables_destroy(fields);
189 }
190
191 static const struct ast_sorcery_observer registration_observer = {
192 .deleted = registration_deleted_observer,
193 };
194
195 /*!
196 * \internal
197 * \brief Sorcery observer called when an object is loaded/reloaded
198 *
199 * \param name The name of the object
200 * \param sorcery The sorcery handle
201 * \param object_type The type of object
202 * \param reloaded Whether or not we reloaded the state/definition of the object
203 *
204 * \details
205 * In our case, we only care when we re-load the registration object. We
206 * wait for the registration to occur in order to create our Prometheus
207 * metric, so we just punt on object creation. On reload, however, fundamental
208 * properties of the metric may have been changed, which means we have to remove
209 * the existing definition of the metric and allow the new registration stasis
210 * message to re-build it.
211 */
212 static void registration_loaded_observer(const char *name, const struct ast_sorcery *sorcery, const char *object_type, int reloaded)
213 {
214 SCOPED_MUTEX(lock, &metrics_lock);
215 int i;
216
217 if (!reloaded) {
218 /* Meh */
219 return;
220 }
221
222 if (strcmp(object_type, "registration")) {
223 /* Not interested */
224 return;
225 }
226
227 for (i = 0; i < AST_VECTOR_SIZE(&metrics); i++) {
228 struct prometheus_metric_wrapper *wrapper = AST_VECTOR_GET(&metrics, i);
229 struct ast_variable search_fields = {
230 .name = "client_uri",
231 .value = wrapper->key,
232 .next = NULL,
233 };
234 void *obj;
235
236 ast_debug(1, "Checking for the existance of registration metric %s\n", wrapper->key);
237 obj = ast_sorcery_retrieve_by_fields(ast_sip_get_sorcery(), object_type, AST_RETRIEVE_FLAG_DEFAULT, &search_fields);
238 if (!obj) {
239 ast_debug(1, "Registration metric '%s' not found; purging with prejudice\n", wrapper->key);
240 AST_VECTOR_REMOVE(&metrics, i, 1);
241 /* This will free the metric as well */
242 prometheus_metric_unregister(wrapper->metric);
243 ast_free(wrapper);
244 continue;
245 }
246 ao2_ref(obj, -1);
247 }
248
249 }
250
251 static const struct ast_sorcery_instance_observer observer_callbacks_registrations = {
252 .object_type_loaded = registration_loaded_observer,
253 };
254
255 /*!
256 * \internal
257 * \brief Callback for Stasis Registry messages
258 *
259 * \param data Callback data, always NULL
260 * \param sub Stasis subscription
261 * \param message Our Registry message
262 *
263 * \details
264 * The Stasis Registry message both updates the state of the Prometheus metric
265 * as well as forces its creation.
266 */
267 static void registry_message_cb(void *data, struct stasis_subscription *sub,
268 struct stasis_message *message)
269 {
270 struct ast_json_payload *payload = stasis_message_data(message);
271 struct ast_json *json = payload->json;
272 const char *username = ast_json_string_get(ast_json_object_get(json, "username"));
273 const char *status_str = ast_json_string_get(ast_json_object_get(json, "status"));
274 const char *domain = ast_json_string_get(ast_json_object_get(json, "domain"));
275 const char *channel_type = ast_json_string_get(ast_json_object_get(json, "channeltype"));
276 struct prometheus_metric metric = PROMETHEUS_METRIC_STATIC_INITIALIZATION(
277 PROMETHEUS_METRIC_GAUGE,
278 "asterisk_pjsip_outbound_registration_status",
279 "Current registration status. 0=Unregistered; 1=Registered; 2=Rejected.",
280 NULL
281 );
282 struct prometheus_metric_wrapper *wrapper;
283 char eid_str[32];
284
285 ast_eid_to_str(eid_str, sizeof(eid_str), &ast_eid_default);
286
287 PROMETHEUS_METRIC_SET_LABEL(&metric, 0, "eid", eid_str);
288 PROMETHEUS_METRIC_SET_LABEL(&metric, 1, "username", username);
289 PROMETHEUS_METRIC_SET_LABEL(&metric, 2, "domain", domain);
290 PROMETHEUS_METRIC_SET_LABEL(&metric, 3, "channel_type", channel_type);
291 snprintf(metric.value, sizeof(metric.value), "%d", registration_state_to_int(status_str));
292
293 wrapper = get_wrapper(username);
294 if (wrapper) {
295 ast_mutex_lock(&wrapper->metric->lock);
296 /* Safe */
297 strcpy(wrapper->metric->value, metric.value);
298 ast_mutex_unlock(&wrapper->metric->lock);
299 } else {
300 wrapper = create_wrapper(username);
301 if (!wrapper) {
302 return;
303 }
304
305 wrapper->metric = prometheus_gauge_create(metric.name, metric.help);
306 if (!wrapper->metric) {
307 ast_free(wrapper);
308 return;
309 }
310 *(wrapper->metric) = metric;
311
312 prometheus_metric_register(wrapper->metric);
313 AST_VECTOR_APPEND(&metrics, wrapper);
314 }
315 }
316
317 #endif /* HAVE_PJPROJECT */
318
319 /*!
320 * \internal
321 * \brief Callback invoked when the core module is unloaded
322 */
323 static void pjsip_outbound_registration_metrics_unload_cb(void)
324 {
325 #ifdef HAVE_PJPROJECT
326 stasis_message_router_unsubscribe_and_join(router);
327 router = NULL;
328 ast_sorcery_instance_observer_remove(ast_sip_get_sorcery(), &observer_callbacks_registrations);
329 ast_sorcery_observer_remove(ast_sip_get_sorcery(), "registration", &registration_observer);
330 #endif /* HAVE_PJPROJECT */
331 }
332
333 /*!
334 * \internal
335 * \brief Metrics provider definition
336 */
337 static struct prometheus_metrics_provider provider = {
338 .name = "pjsip_outbound_registration",
339 .unload_cb = pjsip_outbound_registration_metrics_unload_cb,
340 };
341
342 int pjsip_outbound_registration_metrics_init(void)
343 {
344 prometheus_metrics_provider_register(&provider);
345
346 #ifdef HAVE_PJPROJECT
347 router = stasis_message_router_create(ast_system_topic());
348 if (!router) {
349 goto cleanup;
350 }
351
352 if (stasis_message_router_add(router, ast_system_registry_type(), registry_message_cb, NULL)) {
353 goto cleanup;
354 }
355
356 if (ast_sorcery_instance_observer_add(ast_sip_get_sorcery(), &observer_callbacks_registrations)) {
357 goto cleanup;
358 }
359
360 if (ast_sorcery_observer_add(ast_sip_get_sorcery(), "registration", &registration_observer)) {
361 goto cleanup;
362 }
363 #endif /* HAVE_PJPROJECT */
364 return 0;
365
366 #ifdef HAVE_PJPROJECT
367 cleanup:
368 ao2_cleanup(router);
369 router = NULL;
370 ast_sorcery_instance_observer_remove(ast_sip_get_sorcery(), &observer_callbacks_registrations);
371 ast_sorcery_observer_remove(ast_sip_get_sorcery(), "registration", &registration_observer);
372
373 return -1;
374 #endif /* HAVE_PJPROJECT */
375 }