feat(health): make /health report per-provider probe status
Introduce ProviderHealthChecker which probes each configured provider via AutowireIterator. Wire it into GraphController so /health returns detailed per-provider status and responds 503 when any provider is in a degraded state. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,8 +1,11 @@
|
|||||||
<?php
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\Controller;
|
namespace App\Controller;
|
||||||
|
|
||||||
use App\Service\ContributionAggregator;
|
use App\Service\ContributionAggregator;
|
||||||
|
use App\Service\ProviderHealthChecker;
|
||||||
use App\Service\SvgRenderer;
|
use App\Service\SvgRenderer;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||||
@@ -13,7 +16,7 @@ use Symfony\Component\Routing\Attribute\Route;
|
|||||||
use Symfony\Contracts\Cache\CacheInterface;
|
use Symfony\Contracts\Cache\CacheInterface;
|
||||||
use Symfony\Contracts\Cache\ItemInterface;
|
use Symfony\Contracts\Cache\ItemInterface;
|
||||||
|
|
||||||
class GraphController
|
final class GraphController
|
||||||
{
|
{
|
||||||
/** @var list<string> */
|
/** @var list<string> */
|
||||||
private readonly array $allowedHosts;
|
private readonly array $allowedHosts;
|
||||||
@@ -23,6 +26,7 @@ class GraphController
|
|||||||
private readonly SvgRenderer $renderer,
|
private readonly SvgRenderer $renderer,
|
||||||
private readonly CacheInterface $cache,
|
private readonly CacheInterface $cache,
|
||||||
private readonly LoggerInterface $logger,
|
private readonly LoggerInterface $logger,
|
||||||
|
private readonly ProviderHealthChecker $healthChecker,
|
||||||
#[Autowire(env: 'ALLOWED_HOSTS')]
|
#[Autowire(env: 'ALLOWED_HOSTS')]
|
||||||
string $allowedHosts = '',
|
string $allowedHosts = '',
|
||||||
) {
|
) {
|
||||||
@@ -73,8 +77,13 @@ class GraphController
|
|||||||
#[Route('/health', name: 'health', methods: ['GET'])]
|
#[Route('/health', name: 'health', methods: ['GET'])]
|
||||||
public function health(): Response
|
public function health(): Response
|
||||||
{
|
{
|
||||||
return new Response('{"status":"ok"}', 200, ['Content-Type' => 'application/json']);
|
$result = $this->healthChecker->check();
|
||||||
}
|
$statusCode = $result['status'] === 'degraded' ? 503 : 200;
|
||||||
|
|
||||||
|
|
||||||
|
return new Response(
|
||||||
|
json_encode($result, JSON_PRETTY_PRINT | JSON_THROW_ON_ERROR),
|
||||||
|
$statusCode,
|
||||||
|
['Content-Type' => 'application/json'],
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,39 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Service;
|
||||||
|
|
||||||
|
use Symfony\Component\DependencyInjection\Attribute\AutowireIterator;
|
||||||
|
|
||||||
|
final class ProviderHealthChecker
|
||||||
|
{
|
||||||
|
public function __construct(
|
||||||
|
#[AutowireIterator('app.provider')]
|
||||||
|
private readonly iterable $providers,
|
||||||
|
) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return array{status: string, providers: array<string, array<string, string>>}
|
||||||
|
*/
|
||||||
|
public function check(): array
|
||||||
|
{
|
||||||
|
$statuses = [];
|
||||||
|
$hasError = false;
|
||||||
|
|
||||||
|
/** @var ProviderInterface $provider */
|
||||||
|
foreach ($this->providers as $provider) {
|
||||||
|
$status = $provider->probe();
|
||||||
|
$statuses[$status->name] = $status->toArray();
|
||||||
|
|
||||||
|
if ($status->status === ProviderStatusType::Error) {
|
||||||
|
$hasError = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return [
|
||||||
|
'status' => $hasError ? 'degraded' : 'ok',
|
||||||
|
'providers' => $statuses,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user