|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Krenor\Prometheus; |
|
4
|
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
|
6
|
|
|
use Krenor\Prometheus\Metrics\Gauge; |
|
7
|
|
|
use Krenor\Prometheus\Metrics\Counter; |
|
8
|
|
|
use Krenor\Prometheus\Metrics\Summary; |
|
9
|
|
|
use Krenor\Prometheus\Contracts\Metric; |
|
10
|
|
|
use Krenor\Prometheus\Metrics\Histogram; |
|
11
|
|
|
use Tightenco\Collect\Support\Collection; |
|
12
|
|
|
|
|
13
|
|
|
class CollectorRegistry |
|
14
|
|
|
{ |
|
15
|
|
|
protected Collection $counters; |
|
16
|
|
|
|
|
17
|
|
|
protected Collection $gauges; |
|
18
|
|
|
|
|
19
|
|
|
protected Collection $histograms; |
|
20
|
|
|
|
|
21
|
|
|
protected Collection $summaries; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* CollectorRegistry constructor. |
|
25
|
|
|
*/ |
|
26
|
37 |
|
public function __construct() |
|
27
|
|
|
{ |
|
28
|
37 |
|
$this->counters = new Collection; |
|
29
|
37 |
|
$this->gauges = new Collection; |
|
30
|
37 |
|
$this->histograms = new Collection; |
|
31
|
37 |
|
$this->summaries = new Collection; |
|
32
|
37 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return Collection|MetricFamilySamples[] |
|
36
|
|
|
*/ |
|
37
|
33 |
|
public function collect(): Collection |
|
38
|
|
|
{ |
|
39
|
|
|
return $this |
|
40
|
33 |
|
->collectible() |
|
41
|
33 |
|
->collapse() |
|
42
|
33 |
|
->map(fn(Metric $metric) => new MetricFamilySamples($metric, $metric::storage()->collect($metric))); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param Metric $metric |
|
47
|
|
|
* |
|
48
|
|
|
* @return Metric |
|
49
|
|
|
*/ |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* @template T |
|
53
|
|
|
* @psalm-param T $metric |
|
54
|
|
|
* |
|
55
|
|
|
* @return T |
|
56
|
|
|
*/ |
|
57
|
34 |
|
public function register(Metric $metric): Metric |
|
58
|
|
|
{ |
|
59
|
34 |
|
$collector = $this->collector($metric); |
|
60
|
33 |
|
$namespace = $metric::class; |
|
61
|
|
|
|
|
62
|
33 |
|
if (!$collector->contains($namespace)) { |
|
63
|
33 |
|
$collector->put($namespace, $metric); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
33 |
|
return $collector->get($namespace); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param Metric $metric |
|
71
|
|
|
* |
|
72
|
|
|
* @return self |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public function unregister(Metric $metric): self |
|
75
|
|
|
{ |
|
76
|
1 |
|
$this->collector($metric) |
|
77
|
1 |
|
->forget($metric::class); |
|
78
|
|
|
|
|
79
|
1 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @return Collection |
|
84
|
|
|
*/ |
|
85
|
1 |
|
public function counters(): Collection |
|
86
|
|
|
{ |
|
87
|
1 |
|
return $this->counters; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @return Collection |
|
92
|
|
|
*/ |
|
93
|
2 |
|
public function gauges(): Collection |
|
94
|
|
|
{ |
|
95
|
2 |
|
return $this->gauges; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return Collection |
|
100
|
|
|
*/ |
|
101
|
1 |
|
public function histograms(): Collection |
|
102
|
|
|
{ |
|
103
|
1 |
|
return $this->histograms; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @return Collection |
|
108
|
|
|
*/ |
|
109
|
1 |
|
public function summaries(): Collection |
|
110
|
|
|
{ |
|
111
|
1 |
|
return $this->summaries; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param string $namespace |
|
116
|
|
|
* |
|
117
|
|
|
* @return Counter|null |
|
118
|
|
|
*/ |
|
119
|
1 |
|
public function counter(string $namespace): ?Counter |
|
120
|
|
|
{ |
|
121
|
1 |
|
return $this->counters->get($namespace); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param string $namespace |
|
126
|
|
|
* |
|
127
|
|
|
* @return Gauge|null |
|
128
|
|
|
*/ |
|
129
|
2 |
|
public function gauge(string $namespace): ?Gauge |
|
130
|
|
|
{ |
|
131
|
2 |
|
return $this->gauges->get($namespace); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* @param string $namespace |
|
136
|
|
|
* |
|
137
|
|
|
* @return Histogram|null |
|
138
|
|
|
*/ |
|
139
|
1 |
|
public function histogram(string $namespace): ?Histogram |
|
140
|
|
|
{ |
|
141
|
1 |
|
return $this->histograms->get($namespace); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @param string $namespace |
|
146
|
|
|
* |
|
147
|
|
|
* @return Summary|null |
|
148
|
|
|
*/ |
|
149
|
1 |
|
public function summary(string $namespace): ?Summary |
|
150
|
|
|
{ |
|
151
|
1 |
|
return $this->summaries->get($namespace); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @param Metric $metric |
|
156
|
|
|
* |
|
157
|
|
|
* @return Collection |
|
158
|
|
|
*/ |
|
159
|
34 |
|
protected function collector(Metric $metric): Collection |
|
160
|
|
|
{ |
|
161
|
|
|
switch ($metric) { |
|
162
|
34 |
|
case $metric instanceof Counter: |
|
163
|
7 |
|
return $this->counters; |
|
164
|
29 |
|
case $metric instanceof Gauge: |
|
165
|
18 |
|
return $this->gauges; |
|
166
|
13 |
|
case $metric instanceof Histogram: |
|
167
|
7 |
|
return $this->histograms; |
|
168
|
8 |
|
case $metric instanceof Summary: |
|
169
|
7 |
|
return $this->summaries; |
|
170
|
|
|
default: |
|
171
|
1 |
|
throw new InvalidArgumentException("Could not determine collector of unknown metric type."); |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* @return Collection|Collection[] |
|
177
|
|
|
*/ |
|
178
|
33 |
|
protected function collectible(): Collection |
|
179
|
|
|
{ |
|
180
|
33 |
|
return new Collection([ |
|
181
|
33 |
|
$this->counters, |
|
182
|
33 |
|
$this->gauges, |
|
183
|
33 |
|
$this->histograms, |
|
184
|
33 |
|
$this->summaries, |
|
185
|
|
|
]); |
|
186
|
|
|
} |
|
187
|
|
|
} |
|
188
|
|
|
|