eqs/health-check-provider

Provides healthcheck endpoints in accordance with IETF's healthcheck draft RFC

0.1.1 2024-01-26 08:53 UTC

README

Provides structure for healthcheck endpoints in accordance with IETF's healthcheck draft RFC

About

Package provides endpoints which conform to draft 06 version of IETF's healthcheck RFC.

Integrations

We are shipping following integrations, but it's very easy to implement your own by reusing CallableHealthChecker:

  • HTTP request
  • Doctrine Connection

Installation

Install this package as a dependency using Composer.

composer require eqs/health-check-provider

Usage

This library provides PSR-15 HTTP Server Request Handler, which guarantees compatibility with wide range of PHP frameworks. In case your framework does not natively support it, you can find a PSR bridge which supports it.

Example controller for Symfony framework

For this example, on top of standard symfony packages, you also need php-http/discovery and symfony/psr-http-message-bridge packages.

use Doctrine\DBAL\Connection;
use GuzzleHttp\Psr7\HttpFactory;
use EQS\HealthCheckProvider\DTO\CheckDetails;
use EQS\HealthCheckProvider\DTO\HealthResponse;
use EQS\HealthCheckProvider\HealthChecker\CallableHealthChecker;
use EQS\HealthCheckProvider\HealthChecker\DoctrineConnectionHealthChecker;
use EQS\HealthCheckProvider\HealthChecker\HttpHealthChecker;
use EQS\HealthCheckProvider\RequestHandler;
use Psr\Http\Client\ClientInterface;
use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Messenger\Transport\Receiver\MessageCountAwareInterface;
use Symfony\Component\Messenger\Transport\TransportInterface;
use Symfony\Component\Routing\Annotation\Route;

class GetHealthCheckController extends AbstractController
{
    public function __construct(
        #[Autowire(service: 'messenger.transport.amqp_dc_user_update')]
        private MessageCountAwareInterface&TransportInterface $transport,
        private Connection $connection,
        private ClientInterface $httpClient,
    ) {}

    #[Route(path: '/api/health_check')]
    public function __invoke(Request $request): Response
    {
        $psr17Factory = new HttpFactory();
        $psrBridge = new HttpFoundationFactory();

        return $psrBridge->createResponse(
            (new RequestHandler(
                new HealthResponse(),
                [
                    new CallableHealthChecker(new CheckDetails('AMQP', true), fn () => $this->transport->getMessageCount()),
                    new DoctrineConnectionHealthChecker(new CheckDetails('Database', true), $this->connection),
                    new HttpHealthChecker(
                        new CheckDetails('External API', false),
                        $this->httpClient,
                        new \GuzzleHttp\Psr7\Request('GET', 'https://www.google.com'),
                    ),
                ],
                $psr17Factory,
                $psr17Factory,
            ))
                ->handle((new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory))
                    ->createRequest($request)),
        );
    }
}

Contributing

Contributions are welcome! To contribute, please familiarize yourself with CONTRIBUTING.md.

Copyright and License

eqs/health-check-provider is copyright © EQS Group and licensed for use under the terms of the MIT License (MIT). Please see LICENSE for more information.