1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHPProm package. |
5
|
|
|
* |
6
|
|
|
* (c) Philip Lehmann-Böhm <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace PHPProm; |
13
|
|
|
|
14
|
|
|
use PHPProm\Storage\AbstractStorage; |
15
|
|
|
|
16
|
|
|
class PrometheusExport { |
17
|
|
|
|
18
|
|
|
protected function getHeader($type, $metric, $label) { |
19
|
|
|
$header = ''; |
20
|
|
|
if ($label !== null) { |
21
|
|
|
$header .= '# '.$type.' '.$metric.' '.$label."\n"; |
22
|
|
|
} |
23
|
|
|
return $header; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function getMetric($metric, $label, array $labelsToValues, $help = null, $type = null) { |
27
|
|
|
$result = $this->getHeader('HELP', $metric, $help); |
28
|
|
|
$result .= $this->getHeader('TYPE', $metric, $type); |
29
|
|
|
$result .= implode("\n", array_map(function($value, $labelValue) use ($metric, $label) { |
30
|
|
|
return $metric.'{'.$label.'="'.$labelValue.'"} '.$value; |
31
|
|
|
}, $labelsToValues, array_keys($labelsToValues))); |
32
|
|
|
return $result."\n"; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function getExport(AbstractStorage $storage, $keys) { |
36
|
|
|
$export = ''; |
37
|
|
|
foreach ($storage->getAvailableMetrics() as $availableMetric) { |
38
|
|
|
$measurements = $storage->getMeasurements($availableMetric['storagePrefix'], $keys, $availableMetric['defaultValue']); |
39
|
|
|
$export .= $this->getMetric($availableMetric['metric'], $availableMetric['label'], $measurements, $availableMetric['help'], $availableMetric['type']); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
return $export; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
} |
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.