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

SwooleWorkerCollector   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 19
c 1
b 0
f 1
dl 0
loc 28
rs 10
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A collect() 0 26 3
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 SwooleWorkerCollector extends PrometheusCollector
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SwooleWorkerCollector
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
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...
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
        // Worker Memory Stats
14
        $labels = http_build_query([
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
15
            'worker_id'   => $swoole->worker_id,
16
            'worker_type' => $swoole->taskworker ? 'task' : 'worker',
17
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
18
19
        // Key Format: prefix+metric_name+metric_type+metric_labels
20
        $memoryKey = implode($this->config['apcu_key_separator'], [$this->config['apcu_key_prefix'], 'swoole_worker_memory_usage', 'gauge', $labels]);
21
        $realMemoryKey = implode($this->config['apcu_key_separator'], [$this->config['apcu_key_prefix'], 'swoole_worker_memory_real_usage', 'gauge', $labels]);
22
        apcu_store($memoryKey, memory_get_usage(), $this->config['apcu_key_max_age']);
23
        apcu_store($realMemoryKey, memory_get_usage(true), $this->config['apcu_key_max_age']);
24
25
        if (PHP_VERSION_ID >= 70300) {
26
            $gcRunsKey = implode($this->config['apcu_key_separator'], [$this->config['apcu_key_prefix'], 'swoole_worker_gc_runs', 'gauge', $labels]);
27
            $gcCollectedKey = implode($this->config['apcu_key_separator'], [$this->config['apcu_key_prefix'], 'swoole_worker_gc_collected', 'gauge', $labels]);
28
            $gcThreshold = implode($this->config['apcu_key_separator'], [$this->config['apcu_key_prefix'], 'swoole_worker_gc_threshold', 'gauge', $labels]);
29
            $gcRootsKey = implode($this->config['apcu_key_separator'], [$this->config['apcu_key_prefix'], 'swoole_worker_gc_roots', 'gauge', $labels]);
30
            $gcStatus = gc_status();
31
            apcu_store($gcRunsKey, $gcStatus['runs'], $this->config['apcu_key_max_age']);
32
            apcu_store($gcCollectedKey, $gcStatus['collected'], $this->config['apcu_key_max_age']);
33
            apcu_store($gcThreshold, $gcStatus['threshold'], $this->config['apcu_key_max_age']);
34
            apcu_store($gcRootsKey, $gcStatus['roots'], $this->config['apcu_key_max_age']);
35
        }
36
    }
37
}