1
|
|
|
<?php |
2
|
|
|
|
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Components\Prometheus\Collectors; |
4
|
|
|
|
5
|
|
|
use Hhxsv5\LaravelS\Components\Prometheus\PrometheusCollector; |
6
|
|
|
|
7
|
|
|
class SwooleWorkerCollector extends PrometheusCollector |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
public function collect(array $params = []) |
|
|
|
|
10
|
|
|
{ |
11
|
|
|
/**@var \Swoole\Http\Server $swoole */ |
|
|
|
|
12
|
|
|
$swoole = app('swoole'); |
|
|
|
|
13
|
|
|
// Worker Memory Stats |
14
|
|
|
$labels = http_build_query([ |
|
|
|
|
15
|
|
|
'worker_id' => $swoole->worker_id, |
16
|
|
|
'worker_type' => $swoole->taskworker ? 'task' : 'worker', |
17
|
|
|
]); |
|
|
|
|
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
|
|
|
} |