Passed
Push — master ( fa6214...4cbce0 )
by Biao
03:09
created

PrometheusExporter::render()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 16
c 1
b 0
f 0
nc 5
nop 0
dl 0
loc 21
rs 9.7333
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Hhxsv5\LaravelS\Components\Prometheus;
4
5
class PrometheusExporter
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class PrometheusExporter
Loading history...
6
{
7
    const REDNER_MIME_TYPE = 'text/plain; version=0.0.4';
8
9
    private $config;
0 ignored issues
show
Coding Style introduced by
Private member variable "config" must be prefixed with an underscore
Loading history...
10
11
    public function __construct(array $config)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
12
    {
13
        $this->config = $config;
14
    }
15
16
    public function getMetrics()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getMetrics()
Loading history...
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()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function render()
Loading history...
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
}