Laravel Healthchecks is a simple controller class that helps you build your own healthchecks endpoint without issues.
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. 📦
You can install the package via composer:
composer require renoki-co/laravel-healthchecks
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']);
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) {
//
});
}
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;
});
}
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
}
vendor/bin/phpunit
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.