|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
7
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
8
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
9
|
|
|
* |
|
10
|
|
|
* Copyright (c) 2024 Mykhailo Shtanko [email protected] |
|
11
|
|
|
* |
|
12
|
|
|
* For the full copyright and license information, please view the LICENSE.MD |
|
13
|
|
|
* file that was distributed with this source code. |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
namespace FRZB\Component\MetricsPower\Action; |
|
17
|
|
|
|
|
18
|
|
|
use FRZB\Component\MetricsPower\Enum\Header; |
|
19
|
|
|
use FRZB\Component\MetricsPower\Enum\HttpMethod; |
|
20
|
|
|
use FRZB\Component\MetricsPower\Enum\StatusCode; |
|
21
|
|
|
use FRZB\Component\MetricsPower\Exception\MetricsRenderException; |
|
22
|
|
|
use Prometheus\RegistryInterface; |
|
23
|
|
|
use Prometheus\RenderTextFormat; |
|
24
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
25
|
|
|
use Symfony\Component\HttpKernel\Attribute\AsController; |
|
26
|
|
|
use Symfony\Component\Routing\Attribute\Route; |
|
27
|
|
|
|
|
28
|
|
|
#[AsController] |
|
29
|
|
|
final class MetricsAction |
|
30
|
|
|
{ |
|
31
|
|
|
public function __construct( |
|
32
|
|
|
private readonly RegistryInterface $registry, |
|
33
|
|
|
) {} |
|
34
|
|
|
|
|
35
|
|
|
/** @throws MetricsRenderException */ |
|
36
|
|
|
#[Route(methods: [HttpMethod::GET])] |
|
37
|
|
|
public function __invoke(): Response |
|
38
|
|
|
{ |
|
39
|
|
|
try { |
|
40
|
|
|
return new Response( |
|
41
|
|
|
(new RenderTextFormat())->render($this->registry->getMetricFamilySamples()), |
|
42
|
|
|
StatusCode::OK, |
|
|
|
|
|
|
43
|
|
|
[Header::CONTENT_TYPE => RenderTextFormat::MIME_TYPE], |
|
44
|
|
|
); |
|
45
|
|
|
} catch (\Throwable $e) { |
|
46
|
|
|
throw MetricsRenderException::fromThrowable($e); |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
} |
|
50
|
|
|
|