Skip to content

Repository files navigation

Laravel Health Check

Build Status Coverage Quality Score Latest Stable Version License

Implementation of MicroProfile Health for Laravel

MicroProfile Health Protocol

Configuration

Register the health check classes in config/health.php

    return [
        /*
         * |--------------------------------------------------------------------------
         * | Health Checks
         * |--------------------------------------------------------------------------
         * |
         */

        'checks' => [
             [
                 'class' => \Health\Checks\NullCheck::class,
                 'params' => []
             ],
             [
                 'class' => \Health\Checks\Servers\Database::class,
                 'params' => []
             ],
             [
                 'class' => \Health\Checks\Filesystem\DiskSpace::class,
                 'params' => [
                    'path' => '/'
                 ]
             ],
             [
                'class' => \Health\Checks\Env\Environment::class,
                'params' => [
                     'APP_ENV' => 'testing'
                 ]
             ]
        ]
    ];

Add the api route

    Route::get('/health', 'Health\Controllers\HealthController@check');

Example Response Payload

{
    "status": "UP",
    "checks": [
        {
            "name": "health-checks-null-check",
            "status": "UP",
            "data": []
        },
        {
            "name": "health-checks-filesystem-disk-space",
            "status": "UP",
            "data": {
                "free_bytes": 119100669952,
                "free_human": "110.92 GB",
                "path": "/",
                "threshold": 100000000
            }
        },
        {
            "name": "health-checks-env-environment",
            "status": "UP",
            "data": {
                "variable": "APP_ENV",
                "value": "testing",
                "value_expected": "testing"
            }
        },
        {
            "name": "health-checks-filesystem-directory-is-readable",
            "status": "UP",
            "data": {
                "paths": [
                    "../tests"
                ]
            }
        },
        {
            "name": "health-checks-filesystem-file-is-readable",
            "status": "UP",
            "data": {
                "files": [
                    "TestCase.php"
                ]
            }
        }
    ]
}

Custom Health Check

    use Health\Checks\BaseCheck;
    use Health\Checks\HealthCheckInterface;

    class ServiceACheck extends BaseCheck implements HealthCheckInterface
    {

        /**
         *
         * {@inheritdoc}
         * @see \Health\Checks\HealthCheckInterface::call()
         */
        public function call()
        {
            $health = $this->getBuilder('Service A');

            if(!$this->serviceA->connect()) {
                $health->withData('error', 'Service A Failed')
                        ->down();
            }
            else {
                $health->up();
            }

            return $health->build();
        }
    }

Health Check Cli Command

$ php artisan health

    ✔ UP Health State
    ==============================

    ✔ UP health-checks-null-check
    ✔ UP health-checks-filesystem-disk-space {"free_bytes":119302803456,"free_human":"111.11 TB","path":"\/tmp","threshold":100000000}
    ✔ UP health-checks-env-environment {"variable":"APP_ENV","value":"testing","value_expected":"testing"}
    ✔ UP health-checks-filesystem-directory-is-readable {"paths":[".\/tests"]}
    ✔ UP health-checks-filesystem-file-is-readable {"files":[".\/tests\/TestCase.php"]}

About

Laravel Health (MicroProfile Health Protocol)

Topics

Resources

Stars

3 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages