Completed
Push — master ( 5b6e0a...ce7f33 )
by Anton
03:40 queued 11s
created

Metrics   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 2
dl 0
loc 85
c 0
b 0
f 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A add() 0 8 2
A sub() 0 8 2
A observe() 0 8 2
A set() 0 8 2
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
declare(strict_types=1);
9
10
namespace Spiral\RoadRunner;
11
12
use Spiral\Goridge\Exceptions\RPCException;
13
use Spiral\Goridge\RPC;
14
use Spiral\RoadRunner\Exception\MetricException;
15
16
/**
17
 * Application metrics.
18
 */
19
final class Metrics
20
{
21
    /** @var RPC */
22
    private $rpc;
23
24
    /**
25
     * @param RPC $rpc
26
     */
27
    public function __construct(RPC $rpc)
28
    {
29
        $this->rpc = $rpc;
30
    }
31
32
    /**
33
     * Add collector value. Fallback to appropriate method of related collector.
34
     *
35
     * @param string $collector
36
     * @param float  $value
37
     * @param array  $labels
38
     *
39
     * @throws MetricException
40
     */
41
    public function add(string $collector, float $value, array $labels = [])
42
    {
43
        try {
44
            $this->rpc->call('metrics.Add', compact('collector', 'value', 'labels'));
45
        } catch (RPCException $e) {
46
            throw new MetricException($e->getMessage(), $e->getCode(), $e);
47
        }
48
    }
49
50
    /**
51
     * Subtract the collector value, only for gauge collector.
52
     *
53
     * @param string $collector
54
     * @param float  $value
55
     * @param array  $labels
56
     *
57
     * @throws MetricException
58
     */
59
    public function sub(string $collector, float $value, array $labels = [])
60
    {
61
        try {
62
            $this->rpc->call('metrics.Sub', compact('collector', 'value', 'labels'));
63
        } catch (RPCException $e) {
64
            throw new MetricException($e->getMessage(), $e->getCode(), $e);
65
        }
66
    }
67
68
    /**
69
     * Observe collector value, only for histogram and summary collectors.
70
     *
71
     * @param string $collector
72
     * @param float  $value
73
     * @param array  $labels
74
     *
75
     * @throws MetricException
76
     */
77
    public function observe(string $collector, float $value, array $labels = [])
78
    {
79
        try {
80
            $this->rpc->call('metrics.Observe', compact('collector', 'value', 'labels'));
81
        } catch (RPCException $e) {
82
            throw new MetricException($e->getMessage(), $e->getCode(), $e);
83
        }
84
    }
85
86
    /**
87
     * Set collector value, only for gauge collector.
88
     *
89
     * @param string $collector
90
     * @param float  $value
91
     * @param array  $labels
92
     *
93
     * @throws MetricException
94
     */
95
    public function set(string $collector, float $value, array $labels = [])
96
    {
97
        try {
98
            $this->rpc->call('metrics.Set', compact('collector', 'value', 'labels'));
99
        } catch (RPCException $e) {
100
            throw new MetricException($e->getMessage(), $e->getCode(), $e);
101
        }
102
    }
103
}