Blog Detail

22

Dec
Monitor the health of your Laravel app with Laravel Health cover image

arrow_back Monitor the health of your Laravel app with Laravel Health

Spatie had introduced an amazing package called Laravel Health. With the help of this package, you can easily track the health of your application by registering available checks.

Requirements

  • The laravel-health package requires PHP 8.0+, Laravel 8+.

  • This package uses json columns. MySQL 5.7 or higher is required.

Installation & setup

You can install the package via composer:

composer require spatie/laravel-health

Publishing the config file

Optionally, you can publish the health config file with this command.

php artisan vendor:publish --tag="health-config"

Next, you’ve to run the migration.

php artisan vendor:publish --tag="health-migrations"
php artisan migrate

These steps are not necessary when using the JsonFileResultStore.

Running the checks by scheduling a command

If you want to let your application send notifications when something is wrong, you should schedule the RunHealthChecksCommand to run every minute.

// in app/Console/Kernel.php

protected function schedule(Schedule $schedule)
{
    $schedule->command(\Spatie\Health\Commands\RunHealthChecksCommand::class)->everyMinute();
}

Running the checks by sending HTTP requests

If you don’t want to let your application send a notification, but let a service like Oh Dear monitor the health of your app, you can trigger a run of all health checks by visiting the HTTP endpoint or JSON endpoint and using the ?fresh parameter in the URL.

Registering your first check

This package offers various checks to monitor different aspects of your application.

You can register the checks you want to run, bypassing an array with checks to Spatie\Health\Facades\Health::check().

Here’s an example where we’re going to register the UsedDiskSpace and DatabaseCheck. Typically, you would put this in a service provider.

// typically, in a service provider

use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\UsedDiskSpaceCheck;
use Spatie\Health\Checks\Checks\DatabaseCheck;

Health::checks([
    UsedDiskSpaceCheck::new(),
    DatabaseCheck::new(),
]);

All registered checks will run when the RunHealthChecksCommand executes. If you followed the installation instructions, you have already scheduled that command to execute every minute.

If you haven’t, scheduled that command you could run the checks and view the results via HTTP or JSON.

When a check results in a warning or a failure, a notification will be sent. You can learn more about notifications in the section on configuring notifications.

Though it’s not required, you can store the results of the checks. This way, you can keep a history of the results in your own database.

For more details, you can visit its documentation and source code on Github.

Published at : 22-12-2021

Author : Rizwan Aslam
AUTHOR
Rizwan Aslam

I am a highly results-driven professional with 12+ years of collective experience in the grounds of web application development especially in laravel, native android application development in java, and desktop application development in the dot net framework. Now managing a team of expert developers at Codebrisk.

Launch your project

Launch project