11<?php
22# php -S 127.0.0.1:8080 lrpm-prometheus-metrics.php
33
4- $ program = __DIR__ . '/bin/lrpmctl ' ;
5- $ request = 'jsonstatus ' ;
6- $ json = shell_exec ("$ program $ request " );
7- $ status = json_decode ($ json , true );
8- $ output = lrpm_status_to_prometheus_metrics ($ status );
9- echo ($ output );
4+ class PrometheusMetric
5+ {
6+ private $ name ;
7+ private $ value ;
8+ private $ labels ;
9+ private $ timestamp ;
10+
11+ public function __construct (string $ name , $ value , array $ labels = [], $ timestamp = null )
12+ {
13+ $ this ->name = $ name ;
14+ $ this ->value = $ value ;
15+ $ this ->labels = $ labels ;
16+ $ this ->timestamp = $ timestamp ;
17+ }
18+
19+ public function __toString (): string
20+ {
21+ $ output = $ this ->name ;
22+ if (!empty ($ this ->labels )) {
23+ $ output .= '{ ' ;
24+ $ sep = '' ;
25+ foreach ($ this ->labels as $ labelName => $ labelValue ) {
26+ $ output .= $ sep . $ labelName . '= ' . $ labelValue ;
27+ $ sep = ', ' ;
28+ }
29+ $ output .= '} ' ;
30+ }
31+ $ output .= ' ' . $ this ->value ;
32+ if (isset ($ this ->timestamp )) $ output .= ' ' . $ this ->timestamp ;
33+ return $ output ;
34+ }
35+ }
36+
37+ class PrometheusMetricsBuilder
38+ {
39+ private $ metrics = [];
40+ private $ text = '' ;
41+
42+ public function add (PrometheusMetric $ metric ): void
43+ {
44+ $ this ->metrics [] = $ metric ;
45+ }
46+
47+ public function __toString (): string
48+ {
49+ foreach ($ this ->metrics as $ metric ) {
50+ $ this ->text .= $ metric ->__toString () . PHP_EOL ;
51+ }
52+ return $ this ->text ;
53+ }
54+ }
1055
1156function lrpm_status_to_prometheus_metrics (array $ status ): string
1257{
1358 $ taskCount = count ($ status );
1459 $ activeTaskCount = get_number_of_active_tasks ($ status );
1560 $ inactiveTaskCount = $ taskCount - $ activeTaskCount ;
16- $ output = "lrpm_task_count $ taskCount " . PHP_EOL ;
17- $ output .= "lrpm_inactive_task_count $ inactiveTaskCount " . PHP_EOL ;
61+ $ metricsBuilder = new PrometheusMetricsBuilder ();
62+ $ metricsBuilder ->add (new PrometheusMetric ('lrpm_task_count ' , $ taskCount ));
63+ $ metricsBuilder ->add (new PrometheusMetric ('lrpm_inactive_task_count ' , $ inactiveTaskCount ));
1864
1965 foreach ($ status as $ jobId => $ jobStatus ) {
2066 $ config = $ jobStatus ['config ' ];
@@ -29,35 +75,25 @@ function lrpm_status_to_prometheus_metrics(array $status): string
2975 'name ' => $ name
3076 ];
3177 if (isset ($ state ['lastExitCode ' ])) $ labels ['last_exit_code ' ] = $ state ['lastExitCode ' ];
32- $ metric = serialize_prometheus_metric ('lrpm_task_backoff ' , $ backoffDuration , $ labels );
33- $ output .= $ metric . PHP_EOL ;
78+ $ metricsBuilder ->add (new PrometheusMetric ('lrpm_task_backoff ' , $ backoffDuration , $ labels ));
3479 }
3580 }
36-
37- return $ output ;
81+ return $ metricsBuilder ->__toString ();
3882}
3983
40- function get_number_of_active_tasks (array $ status )
84+ function get_number_of_active_tasks (array $ status ): int
4185{
4286 $ active_tasks = array_filter ($ status , function ($ task ) {
4387 return isset ($ task ['state ' ]['pid ' ]) && is_int ($ task ['state ' ]['pid ' ]);
4488 });
4589 return count ($ active_tasks );
4690}
4791
48- function serialize_prometheus_metric (string $ name , $ value , array $ labels = [], $ timestamp = null ): string
49- {
50- $ output = $ name ;
51- if (!empty ($ labels )) {
52- $ output .= '{ ' ;
53- $ sep = '' ;
54- foreach ($ labels as $ labelName => $ labelValue ) {
55- $ output .= $ sep . $ labelName . '= ' . $ labelValue ;
56- $ sep = ', ' ;
57- }
58- $ output .= '} ' ;
59- }
60- $ output .= ' ' . $ value ;
61- if (isset ($ timestamp )) $ output .= ' ' . $ timestamp ;
62- return $ output ;
63- }
92+ // main entry point
93+
94+ $ program = __DIR__ . '/bin/lrpmctl ' ;
95+ $ request = 'jsonstatus ' ;
96+ $ json = shell_exec ("$ program $ request " );
97+ $ status = json_decode ($ json , true );
98+ $ output = lrpm_status_to_prometheus_metrics ($ status );
99+ echo ($ output );
0 commit comments