renoki-co / laravel-healthchecks by rennokki

Laravel Healthchecks is a simple controller class that helps you build your own healthchecks endpoint without issues.
48,037
55
4
Package Data
Maintainer Username: rennokki
Package Create Date: 2020-05-15
Package Last Update: 2024-03-25
Home Page:
Language: PHP
License: Apache-2.0
Last Refreshed: 2024-03-28 03:16:03
Package Statistics
Total Downloads: 48,037
Monthly Downloads: 448
Daily Downloads: 5
Total Stars: 55
Total Watchers: 4
Total Forks: 4
Total Open Issues: 4

Laravel Healthchecks

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

Renoki Co. on GitHub aims on bringing a lot of open source, MIT-licensed projects and helpful projects to the world. Developing and maintaining projects everyday is a harsh work and tho, we love it.

If you are using your application in your day-to-day job, on presentation demos, hobby projects or even school projects, spread some kind words about our work or sponsor our work. Kind words will touch our chakras and vibe, while the sponsorships will keep the open source projects alive.

🚀 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@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

📄 License

The MIT License (MIT). Please see License File for more information.