Passed
Push — develop ( 500084...288399 )
by Stan
09:28
created

FPM.php$8 ➔ maxActive()   A

Complexity

Conditions 1

Size

Total Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

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