Passed
Push — master ( 918514...b89e50 )
by Biao
03:27
created

PrometheusExporter::observeRequest()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 0
Metric Value
cc 3
eloc 14
c 6
b 1
f 0
nc 3
nop 2
dl 0
loc 23
rs 9.7998
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 getSystemLoadAvgMetrics()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getSystemLoadAvgMetrics()
Loading history...
17
    {
18
        $load = sys_getloadavg();
19
        return [
20
            [
21
                'name'  => 'system_load_average_1m',
22
                'help'  => '',
23
                'type'  => 'gauge',
24
                'value' => $load[0],
25
            ],
26
            [
27
                'name'  => 'system_load_average_5m',
28
                'help'  => '',
29
                'type'  => 'gauge',
30
                'value' => $load[1],
31
            ],
32
            [
33
                'name'  => 'system_load_average_15m',
34
                'help'  => '',
35
                'type'  => 'gauge',
36
                'value' => $load[2],
37
            ],
38
        ];
39
    }
40
41
    public function getSwooleMetrics()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getSwooleMetrics()
Loading history...
42
    {
43
        /**@var \Swoole\Http\Server $swoole */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
44
        $swoole = app('swoole');
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

44
        $swoole = /** @scrutinizer ignore-call */ app('swoole');
Loading history...
45
        $stats = $swoole->stats();
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $stats is correct as $swoole->stats() targeting Swoole\Server::stats() seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
46
        // Get worker_num/task_worker_num from setting for the old Swoole.
47
        $setting = $swoole->setting;
48
        if (!isset($stats['worker_num'])) {
49
            $stats['worker_num'] = $setting['worker_num'];
50
        }
51
        if (!isset($stats['task_worker_num'])) {
52
            $stats['task_worker_num'] = isset($setting['task_worker_num']) ? $setting['task_worker_num'] : 0;
53
        }
54
        return [
55
            [
56
                'name'  => 'swoole_cpu_num',
57
                'help'  => '',
58
                'type'  => 'gauge',
59
                'value' => swoole_cpu_num(),
60
            ],
61
            [
62
                'name'  => 'swoole_start_time',
63
                'help'  => '',
64
                'type'  => 'gauge',
65
                'value' => $stats['start_time'],
66
            ],
67
            [
68
                'name'  => 'swoole_connection_num',
69
                'help'  => '',
70
                'type'  => 'gauge',
71
                'value' => $stats['connection_num'],
72
            ],
73
            [
74
                'name'  => 'swoole_request_count',
75
                'help'  => '',
76
                'type'  => 'gauge',
77
                'value' => $stats['request_count'],
78
            ],
79
            [
80
                'name'  => 'swoole_worker_num',
81
                'help'  => '',
82
                'type'  => 'gauge',
83
                'value' => $stats['worker_num'],
84
            ],
85
            [
86
                'name'  => 'swoole_idle_worker_num',
87
                'help'  => '',
88
                'type'  => 'gauge',
89
                'value' => isset($stats['idle_worker_num']) ? $stats['idle_worker_num'] : 0,
90
            ],
91
            [
92
                'name'  => 'swoole_task_worker_num',
93
                'help'  => '',
94
                'type'  => 'gauge',
95
                'value' => $stats['task_worker_num'],
96
            ],
97
            [
98
                'name'  => 'swoole_task_idle_worker_num',
99
                'help'  => '',
100
                'type'  => 'gauge',
101
                'value' => isset($stats['task_idle_worker_num']) ? $stats['task_idle_worker_num'] : 0,
102
            ],
103
            [
104
                'name'  => 'swoole_tasking_num',
105
                'help'  => '',
106
                'type'  => 'gauge',
107
                'value' => $stats['tasking_num'],
108
            ],
109
            [
110
                'name'  => 'swoole_coroutine_num',
111
                'help'  => '',
112
                'type'  => 'gauge',
113
                'value' => isset($stats['coroutine_num']) ? $stats['coroutine_num'] : 0,
114
            ],
115
        ];
116
    }
117
118
    public function getApcuMetrics()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getApcuMetrics()
Loading history...
119
    {
120
        $apcSmaInfo = apcu_sma_info(true);
121
        $metrics = [
122
            [
123
                'name'  => 'apcu_seg_size',
124
                'help'  => '',
125
                'type'  => 'gauge',
126
                'value' => $apcSmaInfo['seg_size'],
127
            ],
128
            [
129
                'name'  => 'apcu_avail_mem',
130
                'help'  => '',
131
                'type'  => 'gauge',
132
                'value' => $apcSmaInfo['avail_mem'],
133
            ],
134
        ];
135
        foreach (new \APCuIterator('/^' . $this->config['apcu_key_prefix'] . $this->config['apcu_key_separator'] . '/') as $item) {
136
            $value = apcu_fetch($item['key'], $success);
137
            if (!$success) {
138
                continue;
139
            }
140
141
            $parts = explode($this->config['apcu_key_separator'], $item['key']);
142
            parse_str($parts[3], $labels);
143
            $metrics[] = [
144
                'name'   => $parts[1],
145
                'help'   => '',
146
                'type'   => $parts[2],
147
                'value'  => $value,
148
                'labels' => $labels,
149
            ];
150
        }
151
        return $metrics;
152
153
    }
154
155
    public function render()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function render()
Loading history...
156
    {
157
        $defaultLabels = ['application' => $this->config['application']];
158
        $metrics = array_merge($this->getSystemLoadAvgMetrics(), $this->getSwooleMetrics(), $this->getApcuMetrics());
159
        $lines = [];
160
        foreach ($metrics as $metric) {
161
            $lines[] = "# HELP " . $metric['name'] . " {$metric['help']}";
162
            $lines[] = "# TYPE " . $metric['name'] . " {$metric['type']}";
163
164
            $metricLabels = isset($metric['labels']) ? $metric['labels'] : [];
165
            $labels = ['{'];
166
            $allLabels = array_merge($defaultLabels, $metricLabels);
167
            foreach ($allLabels as $key => $value) {
168
                $value = addslashes($value);
169
                $labels[] = "{$key}=\"{$value}\",";
170
            }
171
            $labels[] = '}';
172
            $labelStr = implode('', $labels);
173
            $lines[] = $metric['name'] . "$labelStr {$metric['value']}";
174
        }
175
        return implode("\n", $lines);
176
    }
177
}