|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Components\Prometheus; |
|
4
|
|
|
|
|
5
|
|
|
class Exporter |
|
|
|
|
|
|
6
|
|
|
{ |
|
7
|
|
|
const REDNER_MIME_TYPE = 'text/plain; version=0.0.4'; |
|
8
|
|
|
|
|
9
|
|
|
private $config; |
|
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
public function __construct(array $config) |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
|
|
$this->config = $config; |
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
|
|
public function getMetrics() |
|
|
|
|
|
|
17
|
|
|
{ |
|
18
|
|
|
$apcSmaInfo = apcu_sma_info(true); |
|
19
|
|
|
$metrics = [ |
|
20
|
|
|
[ |
|
21
|
|
|
'name' => 'apcu_seg_size', |
|
22
|
|
|
'help' => '', |
|
23
|
|
|
'type' => 'gauge', |
|
24
|
|
|
'value' => $apcSmaInfo['seg_size'], |
|
25
|
|
|
], |
|
26
|
|
|
[ |
|
27
|
|
|
'name' => 'apcu_avail_mem', |
|
28
|
|
|
'help' => '', |
|
29
|
|
|
'type' => 'gauge', |
|
30
|
|
|
'value' => $apcSmaInfo['avail_mem'], |
|
31
|
|
|
], |
|
32
|
|
|
]; |
|
33
|
|
|
foreach (new \APCuIterator('/^' . $this->config['apcu_key_prefix'] . $this->config['apcu_key_separator'] . '/') as $item) { |
|
34
|
|
|
$parts = explode($this->config['apcu_key_separator'], $item['key']); |
|
35
|
|
|
parse_str($parts[3], $labels); |
|
36
|
|
|
if (is_array($item['value'])) { |
|
37
|
|
|
foreach ($item['value'] as $metric) { |
|
38
|
|
|
$metrics[] = [ |
|
39
|
|
|
'name' => $metric['name'], |
|
40
|
|
|
'help' => '', |
|
41
|
|
|
'type' => $metric['type'], |
|
42
|
|
|
'value' => $metric['value'], |
|
43
|
|
|
'labels' => $labels, |
|
44
|
|
|
]; |
|
45
|
|
|
} |
|
46
|
|
|
} else { |
|
47
|
|
|
$metrics[] = [ |
|
48
|
|
|
'name' => $parts[1], |
|
49
|
|
|
'help' => '', |
|
50
|
|
|
'type' => $parts[2], |
|
51
|
|
|
'value' => $item['value'], |
|
52
|
|
|
'labels' => $labels, |
|
53
|
|
|
]; |
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
return $metrics; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
public function render() |
|
|
|
|
|
|
60
|
|
|
{ |
|
61
|
|
|
$defaultLabels = ['application' => $this->config['application']]; |
|
62
|
|
|
$metrics = $this->getMetrics(); |
|
63
|
|
|
$lines = []; |
|
64
|
|
|
foreach ($metrics as $metric) { |
|
65
|
|
|
$lines[] = "# HELP " . $metric['name'] . " {$metric['help']}"; |
|
66
|
|
|
$lines[] = "# TYPE " . $metric['name'] . " {$metric['type']}"; |
|
67
|
|
|
|
|
68
|
|
|
$metricLabels = isset($metric['labels']) ? $metric['labels'] : []; |
|
69
|
|
|
$labels = ['{']; |
|
70
|
|
|
$allLabels = array_merge($defaultLabels, $metricLabels); |
|
71
|
|
|
foreach ($allLabels as $key => $value) { |
|
72
|
|
|
$labels[] = "{$key}=\"{$value}\","; |
|
73
|
|
|
} |
|
74
|
|
|
$labels[] = '}'; |
|
75
|
|
|
$labelStr = implode('', $labels); |
|
76
|
|
|
$lines[] = $metric['name'] . "$labelStr {$metric['value']}"; |
|
77
|
|
|
} |
|
78
|
|
|
return implode("\n", $lines); |
|
79
|
|
|
} |
|
80
|
|
|
} |