1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Krenor\Prometheus\Exporter; |
4
|
|
|
|
5
|
|
|
use Krenor\Prometheus\Metrics\Gauge; |
6
|
|
|
use Krenor\Prometheus\Metrics\Metric; |
7
|
|
|
use Krenor\Prometheus\Metrics\Counter; |
8
|
|
|
use Krenor\Prometheus\Contracts\Exporter; |
9
|
|
|
use Tightenco\Collect\Support\Collection; |
10
|
|
|
use Krenor\Prometheus\MetricFamilySamples; |
11
|
|
|
|
12
|
|
|
class FPM extends Exporter |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @return MetricFamilySamples |
16
|
|
|
*/ |
17
|
1 |
|
public function uptime(): MetricFamilySamples |
18
|
|
|
{ |
19
|
1 |
|
return $this->sampled( |
20
|
1 |
|
new class extends Counter { |
21
|
|
|
protected ?string $namespace = 'php_fpm'; |
22
|
|
|
protected string $name = 'uptime_seconds'; |
23
|
|
|
protected string $description = 'The number of seconds since FPM has started.'; |
24
|
|
|
protected array $labels = [ |
25
|
|
|
'pool', |
26
|
|
|
]; |
27
|
|
|
}, |
28
|
1 |
|
$this->data['start-since'], |
29
|
1 |
|
[$this->data['pool']] |
30
|
|
|
); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return MetricFamilySamples |
35
|
|
|
*/ |
36
|
1 |
|
public function connections(): MetricFamilySamples |
37
|
|
|
{ |
38
|
1 |
|
return $this->sampled( |
39
|
1 |
|
new class extends Counter { |
40
|
|
|
protected ?string $namespace = 'php_fpm'; |
41
|
|
|
protected string $name = 'connections_total'; |
42
|
|
|
protected string $description = 'The number of requests accepted.'; |
43
|
|
|
protected array $labels = [ |
44
|
|
|
'pool', |
45
|
|
|
]; |
46
|
|
|
}, |
47
|
1 |
|
$this->data['accepted-conn'], |
48
|
1 |
|
[$this->data['pool']] |
49
|
|
|
); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @return MetricFamilySamples |
54
|
|
|
*/ |
55
|
1 |
|
public function queued(): MetricFamilySamples |
56
|
|
|
{ |
57
|
1 |
|
return $this->sampled( |
58
|
1 |
|
new class extends Gauge { |
59
|
|
|
protected ?string $namespace = 'php_fpm'; |
60
|
|
|
protected string $name = 'connections_queued_count'; |
61
|
|
|
protected string $description = 'The number of request in the queue of pending connections.'; |
62
|
|
|
protected array $labels = [ |
63
|
|
|
'pool', |
64
|
|
|
]; |
65
|
|
|
}, |
66
|
1 |
|
$this->data['listen-queue'], |
67
|
1 |
|
[$this->data['pool']] |
68
|
|
|
); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return MetricFamilySamples |
73
|
|
|
*/ |
74
|
1 |
|
public function maxQueued(): MetricFamilySamples |
75
|
|
|
{ |
76
|
1 |
|
return $this->sampled( |
77
|
1 |
|
new class extends Counter { |
78
|
|
|
protected ?string $namespace = 'php_fpm'; |
79
|
|
|
protected string $name = 'connections_max_queued_count'; |
80
|
|
|
protected string $description = 'The maximum number of requests in the queue of pending connections.'; |
81
|
|
|
protected array $labels = [ |
82
|
|
|
'pool', |
83
|
|
|
]; |
84
|
|
|
}, |
85
|
1 |
|
$this->data['max-listen-queue'], |
86
|
1 |
|
[$this->data['pool']] |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return MetricFamilySamples |
92
|
|
|
*/ |
93
|
1 |
|
public function queue(): MetricFamilySamples |
94
|
|
|
{ |
95
|
1 |
|
return $this->sampled( |
96
|
1 |
|
new class extends Gauge { |
97
|
|
|
protected ?string $namespace = 'php_fpm'; |
98
|
|
|
protected string $name = 'connections_queue_size'; |
99
|
|
|
protected string $description = 'The size of the socket queue for pending connections.'; |
100
|
|
|
protected array $labels = [ |
101
|
|
|
'pool', |
102
|
|
|
]; |
103
|
|
|
}, |
104
|
1 |
|
$this->data['listen-queue-len'], |
105
|
1 |
|
[$this->data['pool']] |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return MetricFamilySamples |
111
|
|
|
*/ |
112
|
1 |
|
public function idle(): MetricFamilySamples |
113
|
|
|
{ |
114
|
1 |
|
return $this->sampled( |
115
|
1 |
|
new class extends Gauge { |
116
|
|
|
protected ?string $namespace = 'php_fpm'; |
117
|
|
|
protected string $name = 'processes_idle_count'; |
118
|
|
|
protected string $description = 'The number of idle processes.'; |
119
|
|
|
protected array $labels = [ |
120
|
|
|
'pool', |
121
|
|
|
]; |
122
|
|
|
}, |
123
|
1 |
|
$this->data['idle-processes'], |
124
|
1 |
|
[$this->data['pool']] |
125
|
|
|
); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return MetricFamilySamples |
130
|
|
|
*/ |
131
|
1 |
|
public function active(): MetricFamilySamples |
132
|
|
|
{ |
133
|
1 |
|
return $this->sampled( |
134
|
1 |
|
new class extends Gauge { |
135
|
|
|
protected ?string $namespace = 'php_fpm'; |
136
|
|
|
protected string $name = 'processes_active_count'; |
137
|
|
|
protected string $description = 'The number of active processes.'; |
138
|
|
|
protected array $labels = [ |
139
|
|
|
'pool', |
140
|
|
|
]; |
141
|
|
|
}, |
142
|
1 |
|
$this->data['active-processes'], |
143
|
1 |
|
[$this->data['pool']] |
144
|
|
|
); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return MetricFamilySamples |
149
|
|
|
*/ |
150
|
1 |
|
public function total(): MetricFamilySamples |
151
|
|
|
{ |
152
|
1 |
|
return $this->sampled( |
153
|
1 |
|
new class extends Gauge { |
154
|
|
|
protected ?string $namespace = 'php_fpm'; |
155
|
|
|
protected string $name = 'processes_total'; |
156
|
|
|
protected string $description = 'The number of idle and active processes.'; |
157
|
|
|
protected array $labels = [ |
158
|
|
|
'pool', |
159
|
|
|
]; |
160
|
|
|
}, |
161
|
1 |
|
$this->data['total-processes'], |
162
|
1 |
|
[$this->data['pool']] |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* @return MetricFamilySamples |
168
|
|
|
*/ |
169
|
1 |
|
public function maxActive(): MetricFamilySamples |
170
|
|
|
{ |
171
|
1 |
|
return $this->sampled( |
172
|
1 |
|
new class extends Counter { |
173
|
|
|
protected ?string $namespace = 'php_fpm'; |
174
|
|
|
protected string $name = 'processes_max_active_count'; |
175
|
|
|
protected string $description = 'The maximum number of active processes since FPM has started.'; |
176
|
|
|
protected array $labels = [ |
177
|
|
|
'pool', |
178
|
|
|
]; |
179
|
|
|
}, |
180
|
1 |
|
$this->data['max-active-processes'], |
181
|
1 |
|
[$this->data['pool']] |
182
|
|
|
); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* @return MetricFamilySamples |
187
|
|
|
*/ |
188
|
1 |
|
public function maxSpawned(): MetricFamilySamples |
189
|
|
|
{ |
190
|
1 |
|
return $this->sampled( |
191
|
1 |
|
new class extends Counter { |
192
|
|
|
protected ?string $namespace = 'php_fpm'; |
193
|
|
|
protected string $name = 'processes_limit_reached_count'; |
194
|
|
|
protected string $description = 'The number of times the process limit has been reached when trying to start more children.'; |
195
|
|
|
protected array $labels = [ |
196
|
|
|
'pool', |
197
|
|
|
]; |
198
|
|
|
}, |
199
|
1 |
|
$this->data['max-children-reached'], |
200
|
1 |
|
[$this->data['pool']] |
201
|
|
|
); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* @return MetricFamilySamples |
206
|
|
|
*/ |
207
|
1 |
|
public function slow(): MetricFamilySamples |
208
|
|
|
{ |
209
|
1 |
|
return $this->sampled( |
210
|
1 |
|
new class extends Counter { |
211
|
|
|
protected ?string $namespace = 'php_fpm'; |
212
|
|
|
protected string $name = 'connections_slow_count'; |
213
|
|
|
protected string $description = 'The number of requests exceeding the configured \'request_slowlog_timeout\' value.'; |
214
|
|
|
protected array $labels = [ |
215
|
|
|
'pool', |
216
|
|
|
]; |
217
|
|
|
}, |
218
|
1 |
|
$this->data['slow-requests'], |
219
|
1 |
|
[$this->data['pool']] |
220
|
|
|
); |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @return Collection|Metric[] |
225
|
|
|
*/ |
226
|
1 |
|
public function processes(): Collection |
227
|
|
|
{ |
228
|
1 |
|
return (new Collection($this->data['processes'])) |
229
|
1 |
|
->map(function (array $process) { |
230
|
1 |
|
$labels = [ |
231
|
1 |
|
$this->data['pool'], |
232
|
1 |
|
$process['pid'], |
233
|
|
|
]; |
234
|
|
|
|
235
|
1 |
|
return new Collection([ |
236
|
1 |
|
$this->sampled( |
237
|
1 |
|
new class extends Counter { |
238
|
|
|
protected ?string $namespace = 'php_fpm'; |
239
|
|
|
protected string $name = 'process_requests_total'; |
240
|
|
|
protected string $description = 'The number of requests the process has served.'; |
241
|
|
|
protected array $labels = [ |
242
|
|
|
'pool', |
243
|
|
|
'pid', |
244
|
|
|
]; |
245
|
|
|
}, |
246
|
1 |
|
$process['requests'], |
247
|
|
|
$labels |
248
|
|
|
), |
249
|
|
|
|
250
|
1 |
|
$this->sampled( |
251
|
1 |
|
new class extends Gauge { |
252
|
|
|
protected ?string $namespace = 'php_fpm'; |
253
|
|
|
protected string $name = 'process_requests_duration_microseconds'; |
254
|
|
|
protected string $description = 'The duration in microseconds of the requests.'; |
255
|
|
|
protected array $labels = [ |
256
|
|
|
'pool', |
257
|
|
|
'pid', |
258
|
|
|
]; |
259
|
|
|
}, |
260
|
1 |
|
$process['request-duration'], |
261
|
|
|
$labels |
262
|
|
|
), |
263
|
|
|
|
264
|
1 |
|
$this->sampled( |
265
|
1 |
|
new class extends Gauge { |
266
|
|
|
protected ?string $namespace = 'php_fpm'; |
267
|
|
|
protected string $name = 'process_last_cpu_percent'; |
268
|
|
|
protected string $description = 'The percentage of cpu the last request consumed.'; |
269
|
|
|
protected array $labels = [ |
270
|
|
|
'pool', |
271
|
|
|
'pid', |
272
|
|
|
]; |
273
|
|
|
}, |
274
|
1 |
|
$process['last-request-cpu'], |
275
|
|
|
$labels |
276
|
|
|
), |
277
|
|
|
|
278
|
1 |
|
$this->sampled( |
279
|
1 |
|
new class extends Gauge { |
280
|
|
|
protected ?string $namespace = 'php_fpm'; |
281
|
|
|
protected string $name = 'process_last_memory_bytes'; |
282
|
|
|
protected string $description = 'The amount of memory the last request consumed.'; |
283
|
|
|
protected array $labels = [ |
284
|
|
|
'pool', |
285
|
|
|
'pid', |
286
|
|
|
]; |
287
|
|
|
}, |
288
|
1 |
|
$process['last-request-memory'], |
289
|
|
|
$labels |
290
|
|
|
), |
291
|
|
|
]); |
292
|
1 |
|
})->collapse(); |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|