PoolStatsExporter   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 14
dl 0
loc 37
rs 10
c 0
b 0
f 0
wmc 2

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 17 1
A stop() 0 3 1
1
<?php
2
/**
3
 * Pool stats exporter
4
 * User: moyo
5
 * Date: 2018/5/19
6
 * Time: 5:11 PM
7
 */
8
9
namespace Carno\Monitor\Builtin;
10
11
use Carno\Monitor\Metrics;
12
use Carno\Monitor\Metrics\Gauge;
13
use Carno\Monitor\Ticker;
14
use Carno\Pool\Pool;
15
16
class PoolStatsExporter
17
{
18
    /**
19
     * @var string
20
     */
21
    private $tick = null;
22
23
    /**
24
     * PoolStatsExporter constructor.
25
     * @param Pool $pool
26
     */
27
    public function __construct(Pool $pool)
28
    {
29
        $labels = [
30
            'resource' => $pool->resource(),
31
            'pid' => spl_object_hash($pool),
32
        ];
33
34
        $this->tick = Ticker::new(
35
            [
36
                Metrics::gauge()->named('pool.conn.idle')->labels($labels),
37
                Metrics::gauge()->named('pool.conn.busy')->labels($labels),
38
                Metrics::gauge()->named('pool.select.wait')->labels($labels),
39
            ],
40
            static function (Gauge $idle, Gauge $busy, Gauge $wait) use ($pool) {
41
                $idle->set($pool->stats()->cIdling());
42
                $busy->set($pool->stats()->cBusying());
43
                $wait->set($pool->stats()->sPending());
44
            }
45
        );
46
    }
47
48
    /**
49
     */
50
    public function stop() : void
51
    {
52
        Ticker::stop($this->tick);
53
    }
54
}
55