This repository was archived by the owner on Oct 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 210
Expand file tree
/
Copy pathCollectorRegistry.php
More file actions
235 lines (216 loc) · 7.01 KB
/
CollectorRegistry.php
File metadata and controls
235 lines (216 loc) · 7.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php
namespace Prometheus;
use Prometheus\Exception\MetricNotFoundException;
use Prometheus\Exception\MetricsRegistrationException;
use Prometheus\Storage\Adapter;
use Prometheus\Storage\Redis;
class CollectorRegistry
{
/**
* @var CollectorRegistry
*/
private static $defaultRegistry;
/**
* @var Adapter
*/
private $adapter;
/**
* @var Gauge[]
*/
private $gauges = array();
/**
* @var Counter[]
*/
private $counters = array();
/**
* @var Histogram[]
*/
private $histograms = array();
public function __construct(Adapter $adapter)
{
$this->adapter = $adapter;
}
/**
* @return CollectorRegistry
*/
public static function getDefault()
{
if (!self::$defaultRegistry) {
return self::$defaultRegistry = new static(new Redis());
}
return self::$defaultRegistry;
}
/**
* @return MetricFamilySamples[]
*/
public function getMetricFamilySamples()
{
return $this->adapter->collect();
}
/**
* @param string $namespace e.g. cms
* @param string $name e.g. duration_seconds
* @param string $help e.g. The duration something took in seconds.
* @param array $labels e.g. ['controller', 'action']
* @return Gauge
* @throws MetricsRegistrationException
*/
public function registerGauge($namespace, $name, $help, $labels = array())
{
$metricIdentifier = self::metricIdentifier($namespace, $name);
if (isset($this->gauges[$metricIdentifier])) {
throw new MetricsRegistrationException("Metric already registered");
}
$this->gauges[$metricIdentifier] = new Gauge(
$this->adapter,
$namespace,
$name,
$help,
$labels
);
return $this->gauges[$metricIdentifier];
}
/**
* @param string $namespace
* @param string $name
* @return Gauge
* @throws MetricNotFoundException
*/
public function getGauge($namespace, $name)
{
$metricIdentifier = self::metricIdentifier($namespace, $name);
if (!isset($this->gauges[$metricIdentifier])) {
throw new MetricNotFoundException("Metric not found:" . $metricIdentifier);
}
return $this->gauges[$metricIdentifier];
}
/**
* @param string $namespace e.g. cms
* @param string $name e.g. duration_seconds
* @param string $help e.g. The duration something took in seconds.
* @param array $labels e.g. ['controller', 'action']
* @return Gauge
*/
public function getOrRegisterGauge($namespace, $name, $help, $labels = array())
{
try {
$gauge = $this->getGauge($namespace, $name);
} catch (MetricNotFoundException $e) {
$gauge = $this->registerGauge($namespace, $name, $help, $labels);
}
return $gauge;
}
/**
* @param string $namespace e.g. cms
* @param string $name e.g. requests
* @param string $help e.g. The number of requests made.
* @param array $labels e.g. ['controller', 'action']
* @return Counter
* @throws MetricsRegistrationException
*/
public function registerCounter($namespace, $name, $help, $labels = array())
{
$metricIdentifier = self::metricIdentifier($namespace, $name);
if (isset($this->counters[$metricIdentifier])) {
throw new MetricsRegistrationException("Metric already registered");
}
$this->counters[$metricIdentifier] = new Counter(
$this->adapter,
$namespace,
$name,
$help,
$labels
);
return $this->counters[self::metricIdentifier($namespace, $name)];
}
/**
* @param string $namespace
* @param string $name
* @return Counter
* @throws MetricNotFoundException
*/
public function getCounter($namespace, $name)
{
$metricIdentifier = self::metricIdentifier($namespace, $name);
if (!isset($this->counters[$metricIdentifier])) {
throw new MetricNotFoundException("Metric not found:" . $metricIdentifier);
}
return $this->counters[self::metricIdentifier($namespace, $name)];
}
/**
* @param string $namespace e.g. cms
* @param string $name e.g. requests
* @param string $help e.g. The number of requests made.
* @param array $labels e.g. ['controller', 'action']
* @return Counter
*/
public function getOrRegisterCounter($namespace, $name, $help, $labels = array())
{
try {
$counter = $this->getCounter($namespace, $name);
} catch (MetricNotFoundException $e) {
$counter = $this->registerCounter($namespace, $name, $help, $labels);
}
return $counter;
}
/**
* @param string $namespace e.g. cms
* @param string $name e.g. duration_seconds
* @param string $help e.g. A histogram of the duration in seconds.
* @param array $labels e.g. ['controller', 'action']
* @param array $buckets e.g. [100, 200, 300]
* @return Histogram
* @throws MetricsRegistrationException
*/
public function registerHistogram($namespace, $name, $help, $labels = array(), $buckets = null)
{
$metricIdentifier = self::metricIdentifier($namespace, $name);
if (isset($this->histograms[$metricIdentifier])) {
throw new MetricsRegistrationException("Metric already registered");
}
$this->histograms[$metricIdentifier] = new Histogram(
$this->adapter,
$namespace,
$name,
$help,
$labels,
$buckets
);
return $this->histograms[$metricIdentifier];
}
/**
* @param string $namespace
* @param string $name
* @return Histogram
* @throws MetricNotFoundException
*/
public function getHistogram($namespace, $name)
{
$metricIdentifier = self::metricIdentifier($namespace, $name);
if (!isset($this->histograms[$metricIdentifier])) {
throw new MetricNotFoundException("Metric not found:" . $metricIdentifier);
}
return $this->histograms[self::metricIdentifier($namespace, $name)];
}
/**
* @param string $namespace e.g. cms
* @param string $name e.g. duration_seconds
* @param string $help e.g. A histogram of the duration in seconds.
* @param array $labels e.g. ['controller', 'action']
* @param array $buckets e.g. [100, 200, 300]
* @return Histogram
*/
public function getOrRegisterHistogram($namespace, $name, $help, $labels = array(), $buckets = null)
{
try {
$histogram = $this->getHistogram($namespace, $name);
} catch (MetricNotFoundException $e) {
$histogram = $this->registerHistogram($namespace, $name, $help, $labels, $buckets);
}
return $histogram;
}
private static function metricIdentifier($namespace, $name)
{
return $namespace . ":" . $name;
}
}