renoki-co/laravel-healthchecks

Laravel Healthchecks is a simple controller class that helps you build your own healthchecks endpoint without issues.

2.1.0 2022-02-09 22:05 UTC

README

CI codecov StyleCI Latest Stable Version Total Downloads Monthly Downloads License

Laravel Healthchecks is a simple controller class that helps you build your own healthchecks endpoint without issues.

🤝 Supporting

If you are using one or more Renoki Co. open-source packages in your production apps, in presentation demos, hobby projects, school projects or so, sponsor our work with Github Sponsors. 📦

🚀 Installation

You can install the package via composer:

composer require renoki-co/laravel-healthchecks

🙌 Usage

First of all, you should create your own Controller for healthchecks, that extends the RenokiCo\LaravelHealthchecks\Http\Controllers\HealthcheckController.

use Illuminate\Http\Request;
use RenokiCo\LaravelHealthchecks\Http\Controllers\HealthcheckController;

class MyHealthcheckController extends HealthcheckController
{
    /**
     * Register the healthchecks.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return void
     */
    public function registerHealthchecks(Request $request)
    {
        $this->addHealthcheck('mysql', function (Request $request) {
            // Try testing the MySQL connection here
            // and return true/false for pass/fail.

            return true;
        });
    }
}
// In your routes
Route::get('/healthcheck', [MyHealthcheckController::class, 'handle']);

Registering healthchecks

Within the controller, you should register the healthchecks closures in the registerHealthchecks method, like the example stated above.

You can add as many healthchecks as you want.

public function registerHealthchecks(Request $request)
{
    $this->addHealthcheck('mysql', function (Request $request) {
        //
    });

    $this->addHealthcheck('redis', function (Request $request) {
        //
    });

    $this->addHealthcheck('some_check', function (Request $request) {
        //
    });

    $this->addHealthcheck('another_check_here', function (Request $request) {
        //
    });
}

Status Codes

In case of failure, the response is 500. For all successful responses, the status code is 200.

To change the http codes being sent out, specify this in your registerHealthchecks method:

public function registerHealthchecks(Request $request)
{
    $this->setPassingHttpCode(203);

    $this->setFailingHttpCode(403);

    $this->addHealthcheck('mysql', function (Request $request) {
        return true;
    });
}

Outputs

By default, the output will be OK or FAIL as string, but in case you want to debug the healthchecks, you can get a JSON with each registered healthchecks and their pass/fail closures.

You have just to call withOutput():

public function registerHealthchecks(Request $request)
{
    $this->withOutput();

    $this->addHealthcheck('mysql', function (Request $request) {
        return true;
    });

    $this->addHealthcheck('redis', function (Request $request) {
        return false;
    });
}

The output in the browser would be like this:

{
    "mysql": true,
    "redis": false
}

🐛 Testing

vendor/bin/phpunit

🤝 Contributing

Please see CONTRIBUTING for details.

🔒 Security

If you discover any security related issues, please email alex@renoki.org instead of using the issue tracker.

🎉 Credits