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
|
|
|
} |