alahaxe/healthcheck-bundle

Extensible healthcheck bundle for SF

v1.2.1 2022-01-06 20:02 UTC

This package is auto-updated.

Last update: 2022-10-06 21:37:00 UTC


README

DeepSource Packagist PHP Version Support Packagist Version GitHub Workflow Status (branch)

This bundle allows to easily expose a healthcheck on a Symfony application. You can add as many checks you want and create your own checks.

Installation

    composer require alahaxe/healthcheck-bundle

Quickstart

1- Router

Register package routes in your application

lahaxearnaud_healthcheck:
    resource: "@HealthCheckBundle/Resources/config/router.yaml"

Default route for healthcheck is /_healthcheck

2- Firewall

Allow any requests to call the healthcheck endpoint.

security:
    firewalls:
        healthcheck:
            pattern: ^/_healthcheck
            security: false

Use custom route

Do not load resource @HealthCheckBundle/Resources/config/router.yaml file in your router but add:

lahaxearnaud_healthcheck:
    path: /my-healthcheck
    controller: Alahaxe\HealthCheckBundle\Controller\HealthCheckController

Adapte firewall pattern:

security:
    firewalls:
        healthcheck:
            pattern: ^/my-healthcheck
            security: false

Use available checks

Name Package Current version
Doctrine check alahaxe/healthcheck-doctrine Packagist Version
System check alahaxe/healthcheck-system Packagist Version
Redis check alahaxe/healthcheck-redis Packagist Version
Curl check alahaxe/healthcheck-curl Packagist Version

Add a custom check

Create a custom class that implements CheckInterface:

<?php

namespace App\Service\HealthCheck;

use Alahaxe\HealthCheckBundle\CheckStatus;
use Alahaxe\HealthCheckBundle\Contract\CheckInterface;

class AppCheck implements CheckInterface
{
    public function check(): CheckStatus
    {
        return new CheckStatus(
            'app', // the name in the final json
            __CLASS__, // only for debug
            CheckStatus::STATUS_OK, // or CheckStatus::STATUS_WARNING or CheckStatus::STATUS_INCIDENT
            'An optional message, publicly exposed',
            200 // an HTTP status
        );
    }
}

The output on /_healthcheck will be:

{
    "checks": {
        "app": {
            "payload": "An optional message, publicly exposed",
            "status": "ok"
        }
    }
}

Register the service with the tag lahaxearnaud.healthcheck.check :

    App\Service\HealthCheck\AppCheck:
        tags: ['lahaxearnaud.healthcheck.check']

Or if you have many checks you can add the tag on a folder:

    App\Service\HealthCheck\:
        resource: '../src/Service/HealthCheck'
        tags: ['lahaxearnaud.healthcheck.check']

Http verbosity

Verbosity configuration allows to redure informations exposed publicly. If your healthcheck is protected (firewall, network rules...) you should use a full configuration. Default verbosity is minimal

Full configuration

In your symfony configs:

health_check:
    http:
        format: full

Example of http response:

{
    "context": {
        "environment": "dev",
        "datetime": "2022-01-05T17:00:53+00:00"
    },
    "health": false,
    "checks": {
        "databaseConnectivity": {
            "payload": null,
            "status": "ok"
        },
        "freeSpace": {
            "payload": null,
            "status": "warning"
        },
        "cpuLoad": {
            "payload": null,
            "status": "incident"
        },
        "redis": {
            "payload": null,
            "status": "ok"
        },
        "app": {
            "payload": null,
            "status": "ok"
        }
    }
}

Minimal configuration:

In your symfony configs:

health_check:
    http:
        format: minimal

Example of http response:

{
    "health": false
}

Events / Listeners

Name When
Alahaxe\HealthCheckBundle\Event\HealthCheckAllEvent::class When all checks are tested
Alahaxe\HealthCheckBundle\Event\HealthCheckEvent::class When a check is tested

By default a log is written on each Alahaxe\HealthCheckBundle\Event\HealthCheckAllEvent::class, take a look at Alahaxe\HealthCheckBundle\Event\Subscriber\LoggerSubscriber for an example.

License

This bundle is under the MIT license. See the complete license in the bundle.