1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Components\Prometheus; |
4
|
|
|
|
5
|
|
|
class PrometheusExporter |
|
|
|
|
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
|
|
|
$value = apcu_fetch($item['key'], $success); |
35
|
|
|
if (!$success) { |
36
|
|
|
continue; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
$parts = explode($this->config['apcu_key_separator'], $item['key']); |
40
|
|
|
parse_str($parts[3], $labels); |
41
|
|
|
$metrics[] = [ |
42
|
|
|
'name' => $parts[1], |
43
|
|
|
'help' => '', |
44
|
|
|
'type' => $parts[2], |
45
|
|
|
'value' => $value, |
46
|
|
|
'labels' => $labels, |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
return $metrics; |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function render() |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$defaultLabels = ['application' => $this->config['application']]; |
56
|
|
|
$metrics = $this->getMetrics(); |
57
|
|
|
$lines = []; |
58
|
|
|
foreach ($metrics as $metric) { |
59
|
|
|
$lines[] = "# HELP " . $metric['name'] . " {$metric['help']}"; |
60
|
|
|
$lines[] = "# TYPE " . $metric['name'] . " {$metric['type']}"; |
61
|
|
|
|
62
|
|
|
$metricLabels = isset($metric['labels']) ? $metric['labels'] : []; |
63
|
|
|
$labels = ['{']; |
64
|
|
|
$allLabels = array_merge($defaultLabels, $metricLabels); |
65
|
|
|
foreach ($allLabels as $key => $value) { |
66
|
|
|
$value = addslashes($value); |
67
|
|
|
$labels[] = "{$key}=\"{$value}\","; |
68
|
|
|
} |
69
|
|
|
$labels[] = '}'; |
70
|
|
|
$labelStr = implode('', $labels); |
71
|
|
|
$lines[] = $metric['name'] . "$labelStr {$metric['value']}"; |
72
|
|
|
} |
73
|
|
|
return implode("\n", $lines); |
74
|
|
|
} |
75
|
|
|
} |