1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ReactInspector\HttpMiddleware; |
6
|
|
|
|
7
|
|
|
use WyriHaximus\Metrics\Factory; |
8
|
|
|
use WyriHaximus\Metrics\Label; |
9
|
|
|
use WyriHaximus\Metrics\Label\Name; |
10
|
|
|
use WyriHaximus\Metrics\Registry; |
11
|
|
|
use WyriHaximus\Metrics\Registry\Counters; |
12
|
|
|
use WyriHaximus\Metrics\Registry\Gauges; |
13
|
|
|
use WyriHaximus\Metrics\Registry\Summaries; |
14
|
|
|
|
15
|
|
|
use function array_map; |
16
|
|
|
|
17
|
|
|
final class Metrics |
18
|
|
|
{ |
19
|
|
|
/** @var array<Label> */ |
20
|
|
|
private array $defaultLabels; |
21
|
|
|
|
22
|
|
|
public function __construct(private Gauges $inflight, private Counters $requests, private Summaries $responseTime, Label ...$defaultLabels) |
23
|
|
|
{ |
24
|
|
|
$this->defaultLabels = $defaultLabels; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public static function create(Registry $registry, Label ...$defaultLabels): self |
28
|
|
|
{ |
29
|
|
|
$defaultLabelNames = array_map(static fn (Label $label): Name => new Name($label->name()), $defaultLabels); |
30
|
|
|
|
31
|
|
|
return new self( |
32
|
|
|
$registry->gauge( |
33
|
|
|
'http_requests_inflight', |
34
|
|
|
'The number of HTTP requests that are currently inflight within the application', |
35
|
|
|
new Name('method'), |
36
|
|
|
...$defaultLabelNames, |
37
|
|
|
), |
38
|
|
|
$registry->counter( |
39
|
|
|
'http_requests', |
40
|
|
|
'The number of HTTP requests handled by HTTP request method and response status code', |
41
|
|
|
new Name('method'), |
42
|
|
|
new Name('code'), |
43
|
|
|
...$defaultLabelNames, |
44
|
|
|
), |
45
|
|
|
$registry->summary( |
46
|
|
|
'http_response_times', |
47
|
|
|
'The time it took to come to a response by HTTP request method and response status code', |
48
|
|
|
Factory::defaultQuantiles(), |
49
|
|
|
new Name('method'), |
50
|
|
|
new Name('code'), |
51
|
|
|
...$defaultLabelNames, |
52
|
|
|
), |
53
|
|
|
...$defaultLabels, |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** @return array<Label> */ |
58
|
|
|
public function defaultLabels(): array |
59
|
|
|
{ |
60
|
|
|
return $this->defaultLabels; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function inflight(): Gauges |
64
|
|
|
{ |
65
|
|
|
return $this->inflight; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function requests(): Counters |
69
|
|
|
{ |
70
|
|
|
return $this->requests; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function responseTime(): Summaries |
74
|
|
|
{ |
75
|
|
|
return $this->responseTime; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|