|
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
|
|
|
/** |
|
17
|
|
|
* Class PrometheusExport |
|
18
|
|
|
* To export the measurements into the Prometheus format. |
|
19
|
|
|
* @package PHPProm |
|
20
|
|
|
*/ |
|
21
|
|
|
class PrometheusExport { |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Gets the header of a metric like its type or help. |
|
25
|
|
|
* |
|
26
|
|
|
* @param $type |
|
27
|
|
|
* the header type, "HELP" or "TYPE" |
|
28
|
|
|
* @param $metric |
|
29
|
|
|
* the metric |
|
30
|
|
|
* @param $label |
|
31
|
|
|
* the label of the header like the actual help text or Prometheus type |
|
32
|
|
|
* |
|
33
|
|
|
* @return string |
|
34
|
|
|
* the complete header |
|
35
|
|
|
*/ |
|
36
|
|
|
protected function getHeader($type, $metric, $label) { |
|
37
|
|
|
$header = ''; |
|
38
|
|
|
if ($label !== null) { |
|
39
|
|
|
$header .= '# '.$type.' '.$metric.' '.$label."\n"; |
|
40
|
|
|
} |
|
41
|
|
|
return $header; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Gets a metric with header and values. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $metric |
|
48
|
|
|
* the metric |
|
49
|
|
|
* @param string $label |
|
50
|
|
|
* the categorizing label |
|
51
|
|
|
* @param array $labelsToValues |
|
52
|
|
|
* each label value mapping to the metric value |
|
53
|
|
|
* @param string $help |
|
54
|
|
|
* a helping text about the metric |
|
55
|
|
|
* @param string $type |
|
56
|
|
|
* the Prometheus type of the metric |
|
57
|
|
|
* |
|
58
|
|
|
* @return string |
|
59
|
|
|
* the Prometheus export string of this metric |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function getMetric($metric, $label, array $labelsToValues, $help, $type) { |
|
62
|
|
|
$result = $this->getHeader('HELP', $metric, $help); |
|
63
|
|
|
$result .= $this->getHeader('TYPE', $metric, $type); |
|
64
|
|
|
$result .= implode("\n", array_map(function($value, $labelValue) use ($metric, $label) { |
|
65
|
|
|
return $metric.'{'.$label.'="'.$labelValue.'"} '.$value; |
|
66
|
|
|
}, $labelsToValues, array_keys($labelsToValues))); |
|
67
|
|
|
return $result."\n"; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Gets a Prometheus export of the given storage. |
|
72
|
|
|
* |
|
73
|
|
|
* @param AbstractStorage $storage |
|
74
|
|
|
* the storage to export |
|
75
|
|
|
* @param $keys |
|
76
|
|
|
* the measurement keys to export |
|
77
|
|
|
* |
|
78
|
|
|
* @return string |
|
79
|
|
|
* the Prometheus export string of all available metrics |
|
80
|
|
|
*/ |
|
81
|
|
|
public function getExport(AbstractStorage $storage, $keys) { |
|
82
|
|
|
$export = ''; |
|
83
|
|
|
foreach ($storage->getAvailableMetrics() as $availableMetric) { |
|
84
|
|
|
$measurements = $storage->getMeasurements($availableMetric['metric'], $keys, $availableMetric['defaultValue']); |
|
85
|
|
|
$export .= $this->getMetric($availableMetric['metric'], $availableMetric['label'], $measurements, $availableMetric['help'], $availableMetric['type']); |
|
86
|
|
|
} |
|
87
|
|
|
return $export; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
} |
|
91
|
|
|
|