|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace ReactInspector\HttpMiddleware; |
|
6
|
|
|
|
|
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
|
8
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
9
|
|
|
use React\Promise\PromiseInterface; |
|
|
|
|
|
|
10
|
|
|
use WyriHaximus\Metrics\Label; |
|
11
|
|
|
use WyriHaximus\Metrics\Registry\Counters; |
|
12
|
|
|
use WyriHaximus\Metrics\Registry\Gauges; |
|
13
|
|
|
use WyriHaximus\Metrics\Registry\Summaries; |
|
14
|
|
|
|
|
15
|
|
|
use function React\Promise\resolve; |
|
16
|
|
|
use function Safe\hrtime; |
|
17
|
|
|
use function strtoupper; |
|
18
|
|
|
|
|
19
|
|
|
final class MiddlewareCollector |
|
20
|
|
|
{ |
|
21
|
|
|
public const LABELS_ATTRIBUTE = 'react-inspector-http-middleware-labels-iugioawfe'; |
|
22
|
|
|
|
|
23
|
|
|
/** @var array<Label> */ |
|
24
|
|
|
private array $defaultLabels; |
|
25
|
|
|
|
|
26
|
|
|
private Gauges $inflight; |
|
27
|
|
|
|
|
28
|
|
|
private Counters $requests; |
|
29
|
|
|
|
|
30
|
|
|
private Summaries $responseTime; |
|
31
|
|
|
|
|
32
|
|
|
public function __construct(Metrics $metrics) |
|
33
|
|
|
{ |
|
34
|
|
|
$this->defaultLabels = $metrics->defaultLabels(); |
|
35
|
|
|
$this->inflight = $metrics->inflight(); |
|
36
|
|
|
$this->requests = $metrics->requests(); |
|
37
|
|
|
$this->responseTime = $metrics->responseTime(); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** @return PromiseInterface<ResponseInterface> */ |
|
41
|
|
|
public function __invoke(ServerRequestInterface $request, callable $next): PromiseInterface |
|
42
|
|
|
{ |
|
43
|
|
|
$method = strtoupper($request->getMethod()); |
|
44
|
|
|
$gauge = $this->inflight->gauge(new Label('method', $method), ...$this->defaultLabels); |
|
45
|
|
|
$gauge->incr(); |
|
46
|
|
|
$labels = new Labels(); |
|
47
|
|
|
$request = $request->withAttribute(self::LABELS_ATTRIBUTE, $labels); |
|
48
|
|
|
$time = hrtime(true); |
|
49
|
|
|
|
|
50
|
|
|
return resolve($next($request))->then(function (ResponseInterface $response) use ($method, $gauge, $time, $labels): ResponseInterface { |
|
51
|
|
|
/** @psalm-suppress PossiblyInvalidOperand */ |
|
52
|
|
|
$this->responseTime->summary( |
|
53
|
|
|
new Label('method', $method), |
|
54
|
|
|
new Label('code', (string) $response->getStatusCode()), |
|
55
|
|
|
...$this->defaultLabels, |
|
56
|
|
|
...$labels->labels(), |
|
57
|
|
|
)->observe( |
|
58
|
|
|
(hrtime(true) - $time) / 1e+9, /** @phpstan-ignore-line */ |
|
59
|
|
|
); |
|
60
|
|
|
$this->requests->counter( |
|
61
|
|
|
new Label('method', $method), |
|
62
|
|
|
new Label('code', (string) $response->getStatusCode()), |
|
63
|
|
|
...$this->defaultLabels, |
|
64
|
|
|
...$labels->labels(), |
|
65
|
|
|
)->incr(); |
|
66
|
|
|
$gauge->dcr(); |
|
67
|
|
|
|
|
68
|
|
|
return $response; |
|
69
|
|
|
}); |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths