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

SwooleStatsCollector   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 39
c 1
b 0
f 1
dl 0
loc 65
rs 10
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B collect() 0 63 7
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Hhxsv5\LaravelS\Components\Prometheus\Collectors;
4
5
use Hhxsv5\LaravelS\Components\Prometheus\PrometheusCollector;
6
7
class SwooleStatsCollector extends PrometheusCollector
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SwooleStatsCollector
Loading history...
8
{
9
    public function collect(array $params = [])
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function collect()
Loading history...
10
    {
11
        /**@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
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
12
        $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

12
        $swoole = /** @scrutinizer ignore-call */ app('swoole');
Loading history...
13
        $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...
14
        // Get worker_num/task_worker_num from setting for the old Swoole.
15
        $setting = $swoole->setting;
16
        if (!isset($stats['worker_num'])) {
17
            $stats['worker_num'] = $setting['worker_num'];
18
        }
19
        if (!isset($stats['task_worker_num'])) {
20
            $stats['task_worker_num'] = isset($setting['task_worker_num']) ? $setting['task_worker_num'] : 0;
21
        }
22
        $metrics = [
23
            [
24
                'name'  => 'swoole_cpu_num',
25
                'type'  => 'gauge',
26
                'value' => swoole_cpu_num(),
27
            ],
28
            [
29
                'name'  => 'swoole_start_time',
30
                'type'  => 'gauge',
31
                'value' => $stats['start_time'],
32
            ],
33
            [
34
                'name'  => 'swoole_connection_num',
35
                'type'  => 'gauge',
36
                'value' => $stats['connection_num'],
37
            ],
38
            [
39
                'name'  => 'swoole_request_count',
40
                'type'  => 'gauge',
41
                'value' => $stats['request_count'],
42
            ],
43
            [
44
                'name'  => 'swoole_worker_num',
45
                'type'  => 'gauge',
46
                'value' => $stats['worker_num'],
47
            ],
48
            [
49
                'name'  => 'swoole_idle_worker_num',
50
                'type'  => 'gauge',
51
                'value' => isset($stats['idle_worker_num']) ? $stats['idle_worker_num'] : 0,
52
            ],
53
            [
54
                'name'  => 'swoole_task_worker_num',
55
                'type'  => 'gauge',
56
                'value' => $stats['task_worker_num'],
57
            ],
58
            [
59
                'name'  => 'swoole_task_idle_worker_num',
60
                'type'  => 'gauge',
61
                'value' => isset($stats['task_idle_worker_num']) ? $stats['task_idle_worker_num'] : 0,
62
            ],
63
            [
64
                'name'  => 'swoole_tasking_num',
65
                'type'  => 'gauge',
66
                'value' => $stats['tasking_num'],
67
            ],
68
        ];
69
        foreach ($metrics as $metric) {
70
            $key = implode($this->config['apcu_key_separator'], [$this->config['apcu_key_prefix'], $metric['name'], $metric['type'], '']);
71
            apcu_store($key, $metric['value'], $this->config['apcu_key_max_age']);
72
        }
73
    }
74
}