Skip to content

Commit d36ac96

Browse files
WyriHaximusbukka
authored andcommitted
Add support for openmetrics formatting to FPM status
1 parent cd40fc3 commit d36ac96

File tree

3 files changed

+118
-5
lines changed

3 files changed

+118
-5
lines changed

sapi/fpm/fpm/fpm_status.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,61 @@ int fpm_status_handle_request(void) /* {{{ */
388388
full_post = "]}";
389389
}
390390

391+
/* OpenMetrics */
392+
} else if (fpm_php_get_string_from_table(_GET_str, "openmetrics")) {
393+
sapi_add_header_ex(ZEND_STRL("Content-Type: application/openmetrics-text; version=1.0.0; charset=utf-8"), 1, 1);
394+
time_format = "%s";
395+
396+
short_syntax =
397+
"# HELP phpfpm_up Could pool %s using a %s PM on PHP-FPM be reached?\n"
398+
"# TYPE phpfpm_up gauge\n"
399+
"phpfpm_up 1\n"
400+
"# HELP phpfpm_start_time When FPM has started.\n"
401+
"# TYPE phpfpm_start_time gauge\n"
402+
"phpfpm_start_time %lu\n"
403+
"# HELP phpfpm_start_since_total The number of seconds since FPM has started.\n"
404+
"# TYPE phpfpm_start_since_total counter\n"
405+
"phpfpm_start_since_total %lu\n"
406+
"# HELP phpfpm_accepted_connections_total The number of requests accepted by the pool.\n"
407+
"# TYPE phpfpm_accepted_connections_total counter\n"
408+
"phpfpm_accepted_connections_total %lu\n"
409+
"# HELP phpfpm_listen_queue The number of requests in the queue of pending connections.\n"
410+
"# TYPE phpfpm_listen_queue gauge\n"
411+
"phpfpm_listen_queue %lu\n"
412+
"# HELP phpfpm_max_listen_queue_total The maximum number of requests in the queue of pending connections since FPM has started.\n"
413+
"# TYPE phpfpm_max_listen_queue_total counter\n"
414+
"phpfpm_max_listen_queue_total %d\n"
415+
"# TYPE phpfpm_listen_queue_length gauge\n"
416+
"# HELP phpfpm_listen_queue_length The size of the socket queue of pending connections.\n"
417+
"phpfpm_listen_queue_length %u\n"
418+
"# HELP phpfpm_idle_processes The number of idle processes.\n"
419+
"# TYPE phpfpm_idle_processes gauge\n"
420+
"phpfpm_idle_processes %d\n"
421+
"# HELP phpfpm_active_processes The number of active processes.\n"
422+
"# TYPE phpfpm_active_processes gauge\n"
423+
"phpfpm_active_processes %d\n"
424+
"# HELP phpfpm_total_processes The number of idle + active processes.\n"
425+
"# TYPE phpfpm_total_processes gauge\n"
426+
"phpfpm_total_processes %d\n"
427+
"# HELP phpfpm_max_active_processes_total The maximum number of active processes since FPM has started.\n"
428+
"# TYPE phpfpm_max_active_processes_total counter\n"
429+
"phpfpm_max_active_processes_total %d\n"
430+
"# HELP phpfpm_max_children_reached_total The number of times, the process limit has been reached, when pm tries to start more children (works only for pm 'dynamic' and 'ondemand').\n"
431+
"# TYPE phpfpm_max_children_reached_total counter\n"
432+
"phpfpm_max_children_reached_total %u\n"
433+
"# HELP phpfpm_slow_requests_total The number of requests that exceeded your 'request_slowlog_timeout' value.\n"
434+
"# TYPE phpfpm_slow_requests_total counter\n"
435+
"phpfpm_slow_requests_total %lu\n";
436+
437+
if (!full) {
438+
short_post = "";
439+
} else {
440+
full_separator = "";
441+
full_pre = "";
442+
full_syntax = "";
443+
full_post = "";
444+
}
445+
391446
/* TEXT */
392447
} else {
393448
sapi_add_header_ex(ZEND_STRL("Content-Type: text/plain"), 1, 1);

sapi/fpm/tests/status.inc

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,11 @@ class Status
1010
* @var array
1111
*/
1212
private $contentTypes = [
13-
'plain' => 'text/plain',
14-
'html' => 'text/html',
15-
'xml' => 'text/xml',
16-
'json' => 'application/json',
13+
'plain' => 'text/plain',
14+
'html' => 'text/html',
15+
'xml' => 'text/xml',
16+
'json' => 'application/json',
17+
'openmetrics' => 'application/openmetrics-text; version=1.0.0; charset=utf-8',
1718
];
1819

1920
/**
@@ -196,4 +197,61 @@ class Status
196197
true
197198
);
198199
}
200+
201+
/**
202+
* Check openmetrics status page.
203+
*
204+
* @param string $body
205+
* @param array $fields
206+
*/
207+
protected function checkStatusOpenmetrics(string $body, array $fields)
208+
{
209+
$pattern = "|# HELP phpfpm_up Could pool " . $fields['pool'] . " using a " . $fields['process manager'] . " PM on PHP-FPM be reached\?\n" .
210+
"# TYPE phpfpm_up gauge\n" .
211+
"phpfpm_up 1\n" .
212+
"# HELP phpfpm_start_time When FPM has started\.\n" .
213+
"# TYPE phpfpm_start_time gauge\n" .
214+
"phpfpm_start_time \d+\n" .
215+
"# HELP phpfpm_start_since_total The number of seconds since FPM has started\.\n" .
216+
"# TYPE phpfpm_start_since_total counter\n" .
217+
"phpfpm_start_since_total " . $fields['start since'] . "\n" .
218+
"# HELP phpfpm_accepted_connections_total The number of requests accepted by the pool\.\n" .
219+
"# TYPE phpfpm_accepted_connections_total counter\n" .
220+
"phpfpm_accepted_connections_total " . $fields['accepted conn'] . "\n" .
221+
"# HELP phpfpm_listen_queue The number of requests in the queue of pending connections\.\n" .
222+
"# TYPE phpfpm_listen_queue gauge\n" .
223+
"phpfpm_listen_queue " . $fields['listen queue'] . "\n" .
224+
"# HELP phpfpm_max_listen_queue_total The maximum number of requests in the queue of pending connections since FPM has started\.\n" .
225+
"# TYPE phpfpm_max_listen_queue_total counter\n" .
226+
"phpfpm_max_listen_queue_total " . $fields['max listen queue'] . "\n" .
227+
"# TYPE phpfpm_listen_queue_length gauge\n" .
228+
"# HELP phpfpm_listen_queue_length The size of the socket queue of pending connections\.\n" .
229+
"phpfpm_listen_queue_length " . $fields['listen queue len'] . "\n" .
230+
"# HELP phpfpm_idle_processes The number of idle processes\.\n" .
231+
"# TYPE phpfpm_idle_processes gauge\n" .
232+
"phpfpm_idle_processes " . $fields['idle processes'] . "\n" .
233+
"# HELP phpfpm_active_processes The number of active processes\.\n" .
234+
"# TYPE phpfpm_active_processes gauge\n" .
235+
"phpfpm_active_processes " . $fields['active processes'] . "\n" .
236+
"# HELP phpfpm_total_processes The number of idle \+ active processes\.\n" .
237+
"# TYPE phpfpm_total_processes gauge\n" .
238+
"phpfpm_total_processes " . $fields['total processes'] . "\n" .
239+
"# HELP phpfpm_max_active_processes_total The maximum number of active processes since FPM has started\.\n" .
240+
"# TYPE phpfpm_max_active_processes_total counter\n" .
241+
"phpfpm_max_active_processes_total " . $fields['max active processes'] . "\n" .
242+
"# HELP phpfpm_max_children_reached_total The number of times, the process limit has been reached, when pm tries to start more children \(works only for pm 'dynamic' and 'ondemand'\)\.\n" .
243+
"# TYPE phpfpm_max_children_reached_total counter\n" .
244+
"phpfpm_max_children_reached_total " . $fields['max children reached'] . "\n" .
245+
"# HELP phpfpm_slow_requests_total The number of requests that exceeded your 'request_slowlog_timeout' value\.\n" .
246+
"# TYPE phpfpm_slow_requests_total counter\n" .
247+
"phpfpm_slow_requests_total " . $fields['slow requests'] . "|";
248+
249+
if (!preg_match($pattern, $body)) {
250+
echo "ERROR: Expected body does not match pattern\n";
251+
echo "BODY:\n";
252+
var_dump($body);
253+
echo "PATTERN:\n";
254+
var_dump($pattern);
255+
}
256+
}
199257
}

sapi/fpm/tests/tester.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ class Tester
499499
array $expectedFields,
500500
string $address = null,
501501
string $statusPath = '/status',
502-
$formats = ['plain', 'html', 'xml', 'json']
502+
$formats = ['plain', 'html', 'xml', 'json', 'openmetrics']
503503
) {
504504
if (!is_array($formats)) {
505505
$formats = [$formats];

0 commit comments

Comments
 (0)