|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vend\Statsd; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Log\LoggerAwareTrait; |
|
6
|
|
|
use Psr\Log\NullLogger; |
|
7
|
|
|
|
|
8
|
|
|
class Factory implements FactoryInterface |
|
9
|
|
|
{ |
|
10
|
|
|
use LoggerAwareTrait; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Factory constructor |
|
14
|
|
|
*/ |
|
15
|
21 |
|
public function __construct() |
|
16
|
|
|
{ |
|
17
|
21 |
|
$this->logger = new NullLogger(); |
|
18
|
21 |
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @param string $key |
|
22
|
|
|
* @param mixed $value |
|
23
|
|
|
* @param string $type |
|
24
|
|
|
* @return Metric|null |
|
25
|
|
|
*/ |
|
26
|
9 |
|
protected function createMetric($key, $value, $type) |
|
27
|
|
|
{ |
|
28
|
9 |
|
if (!$this->validate($key, $value, $type)) { |
|
29
|
3 |
|
return null; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
6 |
|
return new Metric($key, $value, $type); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
3 |
|
public function counter($key, $delta = 1) |
|
36
|
|
|
{ |
|
37
|
3 |
|
return $this->createMetric($key, $delta, Type::COUNTER); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
public function increment($key) |
|
41
|
|
|
{ |
|
42
|
1 |
|
return $this->counter($key, 1); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function decrement($key) |
|
46
|
|
|
{ |
|
47
|
1 |
|
return $this->counter($key, -1); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
2 |
|
public function gauge($key, $value) |
|
51
|
|
|
{ |
|
52
|
2 |
|
return $this->createMetric($key, $value, Type::GAUGE); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
2 |
|
public function timer($key, $time) |
|
56
|
|
|
{ |
|
57
|
2 |
|
return $this->createMetric($key, $time, Type::TIMER); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
2 |
|
public function set($key, $value) |
|
61
|
|
|
{ |
|
62
|
2 |
|
return $this->createMetric($key, $value, Type::SET); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Validates the value against the type |
|
67
|
|
|
* |
|
68
|
|
|
* Most metrics require a numeric value. If the value is empty, this can cause receivers to think the format of |
|
69
|
|
|
* the emitted datagrams is invalid. (And, heck, it might be: it's not like etsy/statsd is a formal standard.) |
|
70
|
|
|
* |
|
71
|
|
|
* We treat these errors silently: we log a message and return null from createMetric(), excluding the metric from |
|
72
|
|
|
* being sent via the client. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $key |
|
75
|
|
|
* @param mixed $value |
|
76
|
|
|
* @param string $type |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
18 |
|
protected function validate($key, $value, $type) |
|
80
|
|
|
{ |
|
81
|
18 |
|
if (in_array($type, $this->getNumericMetricTypes(), true) && !is_numeric($value) && !preg_match('/^(-|\+)?\d+/', $value)) { |
|
82
|
6 |
|
$this->logger->error('Could not emit metric: requires an numeric value', [ |
|
83
|
6 |
|
'key' => $key, |
|
84
|
6 |
|
'value' => $value, |
|
85
|
6 |
|
'type' => $type, |
|
86
|
6 |
|
]); |
|
87
|
|
|
|
|
88
|
6 |
|
return false; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
12 |
|
return true; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
18 |
|
protected function getNumericMetricTypes() |
|
95
|
|
|
{ |
|
96
|
18 |
|
return [Type::COUNTER, Type::GAUGE, Type::TIMER]; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|