1xHTTP_PROM Module
2
3Vicente Hernando
4
5 <vhernando@sonoc.io>
6
7Edited by
8
9Vicente Hernando
10
11 <vhernando@sonoc.io>
12
13Javier Gallart
14
15 <jgallart@sonoc.io>
16
17 Copyright © 2019 www.sonoc.io
18 __________________________________________________________________
19
20 Table of Contents
21
22 1. Admin Guide
23
24 1. Overview
25 2. Dependencies
26
27 2.1. Kamailio Modules
28 2.2. External Libraries or Applications
29
30 3. Parameters
31
32 3.1. xhttp_prom_buf_size (integer)
33 3.2. xhttp_prom_timeout (integer)
34 3.3. xhttp_prom_stats (str)
35 3.4. prom_counter (str)
36 3.5. prom_gauge (str)
37
38 4. Functions
39
40 4.1. prom_counter_reset(name, l0, l1, l2)
41 4.2. prom_gauge_reset(name, l0, l1, l2)
42 4.3. prom_counter_inc(name, number, l0, l1, l2)
43 4.4. prom_gauge_set(name, number, l0, l1, l2)
44 4.5. prom_dispatch()
45 4.6. prom_check_uri()
46
47 5. RPC Commands
48
49 5.1. xhttp_prom.counter_reset
50 5.2. xhttp_prom.counter_inc
51 5.3. xhttp_prom.gauge_reset
52 5.4. xhttp_prom.gauge_set
53 5.5. xhttp_prom.metric_list_print
54
55 List of Examples
56
57 1.1. Set xhttp_prom_buf_size parameter
58 1.2. Set xhttp_prom_timeout parameter
59 1.3. Set xhttp_prom_stats parameter
60 1.4. prom_counter label example
61 1.5. Set prom_counter parameter
62 1.6. prom_gauge label example
63 1.7. Set prom_gauge parameter
64 1.8. prom_counter_reset usage
65 1.9. prom_gauge_reset usage
66 1.10. prom_counter_inc usage
67 1.11. prom_gauge_set usage
68 1.12. prom_dispatch usage
69 1.13. prom_dispatch usage (more complete)
70 1.14. prom_check_uri usage
71 1.15. xhttp_prom.counter_reset usage
72 1.16. xhttp_prom.counter_inc usage
73 1.17. xhttp_prom.gauge_reset usage
74 1.18. xhttp_prom.gauge_set usage
75 1.19. xhttp_prom.metric_list_print usage
76
77Chapter 1. Admin Guide
78
79 Table of Contents
80
81 1. Overview
82 2. Dependencies
83
84 2.1. Kamailio Modules
85 2.2. External Libraries or Applications
86
87 3. Parameters
88
89 3.1. xhttp_prom_buf_size (integer)
90 3.2. xhttp_prom_timeout (integer)
91 3.3. xhttp_prom_stats (str)
92 3.4. prom_counter (str)
93 3.5. prom_gauge (str)
94
95 4. Functions
96
97 4.1. prom_counter_reset(name, l0, l1, l2)
98 4.2. prom_gauge_reset(name, l0, l1, l2)
99 4.3. prom_counter_inc(name, number, l0, l1, l2)
100 4.4. prom_gauge_set(name, number, l0, l1, l2)
101 4.5. prom_dispatch()
102 4.6. prom_check_uri()
103
104 5. RPC Commands
105
106 5.1. xhttp_prom.counter_reset
107 5.2. xhttp_prom.counter_inc
108 5.3. xhttp_prom.gauge_reset
109 5.4. xhttp_prom.gauge_set
110 5.5. xhttp_prom.metric_list_print
111
1121. Overview
113
114 This module generates suitable metrics for a Prometheus monitoring
115 platform.
116
117 It answers Prometheus pull requests (HTTP requests to /metrics URL).
118
119 The module generates metrics based on Kamailio statistics, and also the
120 user can create his own metrics (currently counters and gauges).
121
122 The xHTTP_PROM module uses the xHTTP module to handle HTTP requests.
123 Read the documentation of the xHTTP module for more details.
124
125 NOTE: This module is based on xHTTP_RPC one.
126
127 IMPORTANT: This module uses private memory to generate HTTP responses,
128 and shared memory to store all the metrics. Remember to increase size
129 of private and shared memory if you use a huge amount of metrics.
130
131 Prometheus URLs:
132 * https://prometheus.io/
133 * https://prometheus.io/docs/concepts/data_model/#metric-names-and-la
134 bels
135 * https://prometheus.io/docs/instrumenting/exposition_formats/
136
1372. Dependencies
138
139 2.1. Kamailio Modules
140 2.2. External Libraries or Applications
141
1422.1. Kamailio Modules
143
144 The following modules must be loaded before this module:
145 * xhttp -- xHTTP.
146
1472.2. External Libraries or Applications
148
149 The following libraries or applications must be installed before
150 running Kamailio with this module loaded:
151 * None
152
1533. Parameters
154
155 3.1. xhttp_prom_buf_size (integer)
156 3.2. xhttp_prom_timeout (integer)
157 3.3. xhttp_prom_stats (str)
158 3.4. prom_counter (str)
159 3.5. prom_gauge (str)
160
1613.1. xhttp_prom_buf_size (integer)
162
163 Specifies the maximum length of the buffer (in bytes) used to write the
164 metric reply information in order to build the HTML response.
165
166 Default value is 0 (auto set to 1/3 of the size of the configured pkg
167 mem).
168
169 Example 1.1. Set xhttp_prom_buf_size parameter
170...
171modparam("xhttp_prom", "xhttp_prom_buf_size", 1024)
172...
173
1743.2. xhttp_prom_timeout (integer)
175
176 Specifies a timeout in minutes. A metric not used during this timeout
177 is automatically deleted. Listing metrics does not count as using them.
178
179 Default value is 60 minutes.
180
181 Example 1.2. Set xhttp_prom_timeout parameter
182...
183# Set timeout to 10 hours
184modparam("xhttp_prom", "xhttp_prom_timeout", 600)
185...
186
1873.3. xhttp_prom_stats (str)
188
189 Specifies which internal statistics from Kamailio to show. Possible
190 values:
191 * all - Show whole Kamailio statistics
192 * group_name: - Show all statistics for a group
193 * statistic_name - Show a specific statistic. It automatically finds
194 the group.
195
196 Default value is "", meaning do not display any Kamailio statistics.
197
198 Example 1.3. Set xhttp_prom_stats parameter
199...
200# show all kamailio statistics.
201modparam("xhttp_prom", "xhttp_prom_stats", "all")
202
203# show statistics for sl group.
204modparam("xhttp_prom", "xhttp_prom_stats", "sl:")
205
206# Show statistic for 200_replies in sl group.
207modparam("xhttp_prom", "xhttp_prom_stats", "200_replies")
208
209# Do not display internal Kamailio statistics. This is the default option.
210modparam("xhttp_prom", "xhttp_prom_stats", "")
211...
212
2133.4. prom_counter (str)
214
215 Create a counter metric.
216
217 This function declares a counter but the actual counter is only created
218 when using it (by adding to or resetting it)
219
220 It takes a list of attribute=value separated by semicolon, the
221 attributes can be name and label.
222 * name - name of the counter. This attribute is mandatory. It is used
223 to generate the metric name. Each name is unique, no metric shall
224 repeat a name.
225 * label - names of labels in the counter. Optional. Only one label
226 parameter at most allowed in counters. Each label name is separated
227 by : without spaces. At most only three label names allowed in each
228 label parameter.
229 Example 1.4. prom_counter label example
230# Create two labels called method and handler
231label = method:handler
232This would generate {method="whatever", handler="whatever2"} when building
233the metric.
234
235 Example 1.5. Set prom_counter parameter
236...
237
238# Create cnt_first counter with no labels.
239modparam("xhttp_prom", "prom_counter", "name=cnt_first;");
240
241# Create cnt_second counter with no labels.
242modparam("xhttp_prom", "prom_counter", "name=cnt_second;");
243
244
245# Create cnt_third counter with label method
246modparam("xhttp_prom", "prom_counter", "name=cnt_third; label=method");
247
248These lines declare the counter but the actual metric will be created when
249using it by prom_counter_inc or prom_counter_reset functions.
250
251...
252
2533.5. prom_gauge (str)
254
255 Create a gauge metric.
256
257 This function declares the gauge but the actual gauge is only created
258 when using it (by setting or resetting it)
259
260 It takes a list of attribute=value separated by semicolon, the
261 attributes can be name and value.
262 * name - name of the gauge. This attribute is mandatory. It is used
263 to generate the metric name. Each name is unique, no metric shall
264 repeat a name.
265 * label - names of labels in the gauge. Optional. Only one label
266 parameter at most allowed in gauges. Each label name is separated
267 by : without spaces. At most only three label names allowed inside
268 each label parameter.
269 Example 1.6. prom_gauge label example
270# Create two labels called method and handler
271label = method:handler
272This would generate {method="whatever", handler="whatever2"} when building
273the metric.
274
275 Example 1.7. Set prom_gauge parameter
276...
277
278# Create gg_first gauge with no labels
279modparam("xhttp_prom", "prom_gauge", "name=gg_first;");
280
281# Create gg_second gauge with no labels
282modparam("xhttp_prom", "prom_gauge", "name=gg_second;");
283
284
285# Create gg_third gauge with two labels method and handler:
286modparam("xhttp_prom", "prom_gauge", "name=gg_third; label=method:handler;");
287
288...
289
2904. Functions
291
292 4.1. prom_counter_reset(name, l0, l1, l2)
293 4.2. prom_gauge_reset(name, l0, l1, l2)
294 4.3. prom_counter_inc(name, number, l0, l1, l2)
295 4.4. prom_gauge_set(name, number, l0, l1, l2)
296 4.5. prom_dispatch()
297 4.6. prom_check_uri()
298
2994.1. prom_counter_reset(name, l0, l1, l2)
300
301 Get a counter based on its name and labels and reset its value to 0.
302 Name parameter is mandatory. Values of labels are optional (from none
303 up to three). Name in prom_counter_reset has to match same name in
304 prom_counter parameter. Number of labels in prom_counter_reset has to
305 match number of labels in prom_counter parameter. First time a counter
306 is used with this reset function the counter is created if it does not
307 exist already.
308
309 This function accepts pseudovariables on its parameters.
310
311 Available via KEMI framework as counter_reset_l0, counter_reset_l1,
312 counter_reset_l2 and counter_reset_l3.
313
314 Example 1.8. prom_counter_reset usage
315...
316# Definition of counter with prom_counter with labels method and IP
317modparam("xhttp_prom", "prom_counter", "name=cnt01; label=method:IP;");
318...
319# Reset cnt01 counter with two values "push" and "192.168.0.1" in labels to zero
320.
321# First time we execute this function the counter will be created.
322prom_counter_reset("cnt01", "push", "192.168.0.1");
323...
324# A metric like this will appear when listing this counter:
325kamailio_cnt01 {method="push", IP="192.168.0.1"} 0 1234567890
326...
327
3284.2. prom_gauge_reset(name, l0, l1, l2)
329
330 Get a gauge based on its name and labels and reset its value to 0. Name
331 parameter is mandatory. Values of labels are optional (from none up to
332 three). Name in prom_gauge_reset has to match same name in prom_gauge
333 parameter. Number of labels in prom_gauge_reset has to match number of
334 labels in prom_gauge parameter. First time a gauge is used with this
335 reset function the gauge is created if it does not exist already.
336
337 This function accepts pseudovariables on its parameters.
338
339 Available via KEMI framework as gauge_reset_l0, gauge_reset_l1,
340 gauge_reset_l2 and gauge_reset_l3.
341
342 Example 1.9. prom_gauge_reset usage
343...
344# Definition of gauge with prom_gauge with labels method and IP
345modparam("xhttp_prom", "prom_gauge", "name=cnt01; label=method:IP;");
346...
347# Reset cnt01 gauge with two values "push" and "192.168.0.1" in labels to zero.
348# First time we execute this function the gauge will be created.
349prom_gauge_reset("cnt01", "push", "192.168.0.1");
350...
351# A metric like this will appear when listing this gauge:
352kamailio_cnt01 {method="push", IP="192.168.0.1"} 0 1234567890
353...
354
3554.3. prom_counter_inc(name, number, l0, l1, l2)
356
357 Get a counter identified by its name and labels and increase its value
358 by a number. If counter does not exist it creates the counter,
359 initializes it to zero and adds the number.
360
361 Name is mandatory, number is mandatory. Number has to be positive or
362 zero (integer). l0, l1, l2 are values of labels and are optional.
363
364 name value and number of labels have to match a previous counter
365 definition with prom_counter.
366
367 This function accepts pseudovariables on its parameters.
368
369 Available via KEMI framework as counter_inc_l0, counter_inc_l1,
370 counter_inc_l2 and counter_inc_l3.
371
372 Example 1.10. prom_counter_inc usage
373...
374# Definition of cnt01 counter with no labels.
375modparam("xhttp_prom", "prom_counter", "name=cnt01;");
376...
377# Add 10 to value of cnt01 counter (with no labels) If counter does not exist it
378 gets created.
379prom_counter_inc("cnt01", "10");
380...
381
382# Definition of cnt02 counter with two labels method and IP
383modparam("xhttp_prom", "prom_counter", "name=cnt02; label=method:IP;");
384...
385# Add 15 to value of cnt02 counter with labels method and IP. It creates the cou
386nter if it does not exist.
387prom_counter_inc("cnt02", "15", "push", "192.168.0.1");
388# When listed the metric it will show a line like this:
389kamailio_cnt02 {method="push", IP="192.168.0.1"} 15 1234567890
390...
391
3924.4. prom_gauge_set(name, number, l0, l1, l2)
393
394 Get a gauge identified by its name and labels and set its value to a
395 number. If gauge does not exist it creates the gauge and assigns the
396 value to it.
397
398 Name is mandatory, number is mandatory. Number is a string that will be
399 parsed as a float. l0, l1, l2 are values of labels and are optional.
400
401 name value and number of labels have to match a previous gauge
402 definition with prom_gauge.
403
404 This function accepts pseudovariables on its parameters.
405
406 Available via KEMI framework as gauge_set_l0, gauge_set_l1,
407 gauge_set_l2 and gauge_set_l3.
408
409 Example 1.11. prom_gauge_set usage
410...
411# Definition of gg01 gauge with no labels.
412modparam("xhttp_prom", "prom_gauge", "name=gg01;");
413...
414# Assign -12.5 to value of gg01 gauge (with no labels) If gauge does not exist i
415t gets created
416prom_gauge_set("gg01", "-12.5");
417...
418
419# Definition of gg02 gauge with two labels method and IP
420modparam("xhttp_prom", "prom_gauge", "name=cnt02; label=method:IP;");
421...
422# Assign 2.8 to value of gg02 gauge with labels method and IP. It creates the ga
423uge if it does not exist.
424prom_gauge_set("gg02", "2.8", "push", "192.168.0.1");
425# When listed the metric it will show a line like this:
426kamailio_gg02 {method="push", IP="192.168.0.1"} 2.8 1234567890
427...
428
4294.5. prom_dispatch()
430
431 Handle the HTTP request and generate a response.
432
433 Available via KEMI framework as xhttp_prom.dispatch
434
435 Example 1.12. prom_dispatch usage
436...
437# Needed to use SIP frames as HTTP ones.
438tcp_accept_no_cl=yes
439...
440# xhttp module depends on sl one.
441loadmodule "sl.so"
442loadmodule "xhttp.so"
443loadmodule "xhttp_prom.so"
444...
445# show all kamailio statistics.
446modparam("xhttp_prom", "xhttp_prom_stats", "all")
447...
448event_route[xhttp:request] {
449 $var(xhttp_prom_root) = $(hu{s.substr,0,8});
450 if ($var(xhttp_prom_root) == "/metrics")
451 prom_dispatch();
452 else
453 xhttp_reply("200", "OK", "text/html",
454 "<html><body>Wrong URL $hu</body></html>");
455}
456...
457
458 Example 1.13. prom_dispatch usage (more complete)
459
460 Another example with counters and gauge:
461...
462# Needed to use SIP frames as HTTP ones.
463tcp_accept_no_cl=yes
464
465# xhttp module depends on sl one.
466loadmodule "sl.so"
467loadmodule "xhttp.so"
468loadmodule "xhttp_prom.so"
469
470# show Kamailio statistics for sl group
471modparam("xhttp_prom", "xhttp_prom_stats", "sl:")
472
473# Define two counters and a gauge
474modparam("xhttp_prom", "prom_counter", "name=cnt_first;");
475modparam("xhttp_prom", "prom_counter", "name=cnt_second; label=method:IP");
476modparam("xhttp_prom", "prom_gauge", "name=gg_first; label=handler");
477
478event_route[xhttp:request] {
479 $var(xhttp_prom_root) = $(hu{s.substr,0,8});
480 if ($var(xhttp_prom_root) == "/metrics") {
481 prom_counter_reset("cnt_first");
482 prom_counter_inc("cnt_second", "10", "push", "192.168.0.1");
483 prom_gauge_set("gg_first", "5.2", "my_handler");
484 prom_dispatch();
485 } else
486 xhttp_reply("200", "OK", "text/html",
487 "<html><body>Wrong URL $hu</body></html>");
488}
489...
490
491We can manually check the result with a web browser:
492We assume Kamailio runs in localhost and port is set to default (same as SIP: 50
49360)
494http://localhost:5060
495...
496
497# User defined metrics
498kamailio_cnt_first 0 1554839325427
499kamailio_cnt_second {method="push", IP="192.168.0.1"} 10 1554839325427
500kamailio_gg_first{handler="my_handler"} 5.2 1554839325427
501
502# Kamailio internal statistics
503kamailio_sl_1xx_replies 0 1554839325427
504kamailio_sl_200_replies 15 1554839325427
505kamailio_sl_202_replies 0 1554839325427
506kamailio_sl_2xx_replies 0 1554839325427
507kamailio_sl_300_replies 0 1554839325427
508kamailio_sl_301_replies 0 1554839325427
509kamailio_sl_302_replies 0 1554839325427
510kamailio_sl_3xx_replies 0 1554839325427
511kamailio_sl_400_replies 0 1554839325427
512kamailio_sl_401_replies 0 1554839325427
513kamailio_sl_403_replies 0 1554839325427
514kamailio_sl_404_replies 0 1554839325427
515kamailio_sl_407_replies 0 1554839325427
516kamailio_sl_408_replies 0 1554839325427
517kamailio_sl_483_replies 0 1554839325427
518kamailio_sl_4xx_replies 0 1554839325427
519kamailio_sl_500_replies 0 1554839325427
520kamailio_sl_5xx_replies 0 1554839325427
521kamailio_sl_6xx_replies 0 1554839325427
522kamailio_sl_failures 0 1554839325427
523kamailio_sl_received_ACKs 0 1554839325427
524kamailio_sl_sent_err_replies 0 1554839325427
525kamailio_sl_sent_replies 15 1554839325427
526kamailio_sl_xxx_replies 0 1554839325461
527...
528
5294.6. prom_check_uri()
530
531 Check if path of HTTP URL equals /metrics. This avoids us to check hu
532 variable from xHTTP module.
533
534 NOTE: Remember not to block /metrics URL in xHTTP module
535
536 Available via KEMI framework as xhttp_prom.check_uri
537
538 Example 1.14. prom_check_uri usage
539...
540# Needed to use SIP frames as HTTP ones.
541tcp_accept_no_cl=yes
542...
543# xhttp module depends on sl one.
544loadmodule "sl.so"
545loadmodule "xhttp.so"
546loadmodule "xhttp_prom.so"
547...
548# show all kamailio statistics.
549modparam("xhttp_prom", "xhttp_prom_stats", "all")
550...
551event_route[xhttp:request] {
552 if (prom_check_uri())
553 prom_dispatch();
554 else
555 xhttp_reply("200", "OK", "text/html",
556 "<html><body>Wrong URL $hu</body></html>");
557}
558...
559
5605. RPC Commands
561
562 5.1. xhttp_prom.counter_reset
563 5.2. xhttp_prom.counter_inc
564 5.3. xhttp_prom.gauge_reset
565 5.4. xhttp_prom.gauge_set
566 5.5. xhttp_prom.metric_list_print
567
5685.1. xhttp_prom.counter_reset
569
570 Set a counter to zero.
571
572 Name: xhttp_prom.counter_reset
573
574 Parameters:
575 * name: name of the counter (mandatory)
576 * l0: value of the first label (optional)
577 * l1: value of second label (optional)
578 * l2: value of the third label (optional)
579
580 Example 1.15. xhttp_prom.counter_reset usage
581 ...
582 kamcmd xhttp_prom.counter_reset "cnt01" "push" "192.168.0.1"
583 ...
584
5855.2. xhttp_prom.counter_inc
586
587 Add a number to a counter based on its name and labels.
588
589 Name: xhttp_prom.counter_inc
590
591 Parameters:
592 * name: name of the counter (mandatory)
593 * number: integer to add to counter value. Negative values not
594 allowed.
595 * l0: value of the first label (optional)
596 * l1: value of second label (optional)
597 * l2: value of the third label (optional)
598
599 Example 1.16. xhttp_prom.counter_inc usage
600 ...
601 kamcmd xhttp_prom.counter_inc "cnt01" 15 "push" "192.168.0.1"
602 ...
603
6045.3. xhttp_prom.gauge_reset
605
606 Set gauge value to zero. Select gauge based on its name and labels.
607
608 Name: xhttp_prom.gauge_reset
609
610 Parameters:
611 * name: name of the gauge (mandatory)
612 * l0: value of the first label (optional)
613 * l1: value of second label (optional)
614 * l2: value of the third label (optional)
615
616 Example 1.17. xhttp_prom.gauge_reset usage
617 ...
618 kamcmd xhttp_prom.gauge_reset "gg01" "push" "192.168.0.1"
619 ...
620
6215.4. xhttp_prom.gauge_set
622
623 Set a gauge to a number. Select the gauge by its name and labels.
624
625 Name: xhttp_prom.gauge_set
626
627 Parameters:
628 * name: name of the gauge (mandatory)
629 * number: float value to set the gauge to (mandatory)
630 * l0: value of the first label (optional)
631 * l1: value of second label (optional)
632 * l2: value of the third label (optional)
633
634 Example 1.18. xhttp_prom.gauge_set usage
635 ...
636 kamcmd xhttp_prom.gauge_set "gg01" -- -5.2
637 ...
638
6395.5. xhttp_prom.metric_list_print
640
641 List of all user defined metrics.
642
643 Name: xhttp_prom.metric_list_print
644
645 Parameters:none
646
647 Example 1.19. xhttp_prom.metric_list_print usage
648 ...
649 kamcmd xhttp_prom.metric_list_print
650 ...
651