Passed
Push — master ( 725c3e...645a59 )
by Biao
03:45
created

Exporter::render()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 20
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 5
nop 0
dl 0
loc 20
rs 9.7666
c 0
b 0
f 0
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 Exporter
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class Exporter
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
            $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()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function render()
Loading history...
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
}