File modules/cms/status/Metrics.class.php
Last commit: Mon Oct 28 22:27:13 2024 +0100 Jan Dankert New: Metrics in the Openmetrics text format. For now only some system infos, maybe some special cms metrics in the future.
1 <?php 2 3 namespace cms\status; 4 5 6 /** 7 * Metrics. 8 */ 9 class Metrics 10 { 11 /** 12 * Creating the metrics 13 */ 14 public static function execute() 15 { 16 $data = ''; 17 18 $data .= self::createGauge('interpreter_version','Major version of PHP Interpreter',PHP_MAJOR_VERSION); 19 $data .= self::createGauge('resource_usage','current resource usage',getrusage() ); 20 $data .= self::createGauge('memory','memory usage', [ 21 'allocated' => memory_get_usage(true), 22 'used' => memory_get_usage(), 23 'peak_allocated' => memory_get_peak_usage(true), 24 'peak_used' => memory_get_peak_usage(), 25 ]); 26 27 28 $data .= "# EOF\n"; 29 30 header('Content-Type: application/openmetrics-text; version=1.0.0; charset=UTF-8'); 31 32 if (!headers_sent()) { 33 // HTTP Spec: 34 // "Applications SHOULD use this field to indicate the transfer-length of the 35 // message-body, unless this is prohibited by the rules in section 4.4." 36 // 37 // And the overhead of 'Transfer-Encoding: chunked' is eliminated... 38 header('HTTP/1.0 200 OK' ); 39 header('Content-Length: ' . strlen($data)); 40 } 41 42 echo $data; 43 } 44 45 46 /** 47 * Creating an openmetrics gauge. 48 * 49 * @param $name string Name of the metric 50 * @param $description string short description for tooltips 51 * @param $value int a numeric value 52 * @return string 53 */ 54 private static function createGauge($name,$description,$value) 55 { 56 return 57 "# TYPE $name gauge\n". 58 "# HELP $name $description\n". 59 (is_array($value)?implode("\n",array_map( function($key,$val) use ($name) { 60 return "$name{id=\"$key\"} $val"; 61 },array_keys($value),$value)):"$name $value"). 62 "\n"; 63 64 } 65 }
Downloadmodules/cms/status/Metrics.class.php
History Mon, 28 Oct 2024 22:27:13 +0100 Jan Dankert New: Metrics in the Openmetrics text format. For now only some system infos, maybe some special cms metrics in the future.