Laravel Health
repository·main·Indexed 21 days ago
https://github.com/spatie/laravel-healthA package to monitor Laravel application health by registering checks for disk space, database connectivity, CPU load, cache operations, and backup integrity. It supports custom warning and failure thresholds and can automatically trigger notifications via Mail or Slack.
What's inside spatie/laravel-health
- Laravel Health is a package designed to monitor the health of your Laravel application by registering various health checks. It allows you to define thresholds for warnings and failures, and can automatically notify you via channels like Mail and Slack when your application's health status changes.
Use third-party health check packages
mainIn addition to the built-in checks, you can extend your application's monitoring capabilities using third-party health check packages. These are community-maintained packages designed to work with
laravel-health.Examples of third-party checks include:
Domain Expiry Health CheckEnv varsSSL certificate expirationLaravel OctaneMemory Usage CheckQueue Size CheckOpcache Check
Available built-in health checks
mainThe
laravel-healthpackage provides a variety of built-in checks to monitor different aspects of your application's infrastructure and configuration. You can register any of these checks to verify the health of your application.Built-in checks include:
- Application & Environment:
Application Cache,Debug Mode,Environment,Security advisories. - Infrastructure & Resources:
CPU Load,Used Disk Space,Redis,Meilisearch. - Database:
Database Connection,Database Connection Count,Database Table Size. - Laravel Ecosystem:
Backups,Flare Error Count,Horizon,Queue,Schedule. - Connectivity:
Ping.
- Application & Environment:
Configure a heartbeat URL for HorizonCheck
mainYou can configure a heartbeat URL to be pinged automatically whenever the health checks run (via
RunHealthChecksCommand) if the Horizon check passes. This is useful for external monitoring services like Oh Dear, Pingdom, or Envoyer.Note: The URL is only pinged if the check passes. The ping is independent of the check's status; the check might pass while the ping fails (e.g., due to a malformed URL).
HORIZON_HEARTBEAT_URL=https://your-monitoring-service.com/ping/abc123View health check results on a web page
mainTheHealthCheckResultsControllercan be used to display a visual status page containing all health check results for human consumption.Configure an Oh Dear health check endpoint
mainTo prevent notification failures when your application is in a critical state, you can use Oh Dear to monitor your health checks. This requires registering a protected endpoint in your Laravel application that returns health check results as JSON.
1. Publish the configuration file
If you haven't already, publish the
healthconfiguration file:php artisan vendor:publish --tag="health-config"2. Configure the
oh_dear_endpointkeyIn
config/health.php, update theoh_dear_endpointarray with the following settings:enabled: Set totrueto activate the endpoint.secret: A unique string used to protect the endpoint. It is recommended to store this in your.envfile using theOH_DEAR_HEALTH_CHECK_SECRETkey.url: The path where the endpoint will be accessible (defaults to/oh-dear-health-check-results).always_send_fresh_results: Iftrue, health checks run immediately before responding. Iffalse, the endpoint returns results from the last time checks were executed.
3. Configure Oh Dear
In your Oh Dear dashboard, navigate to the Application health settings for your site and enter the
urlandsecretyou configured in your Laravel app.// in app/config/health.php 'oh_dear_endpoint' => [ 'enabled' => true, /* * When this option is enabled, the checks will run before sending a response. * Otherwise, we'll send the results from the last time the checks have run. */ 'always_send_fresh_results' => true, /* * The secret that is displayed at the Application Health settings at Oh Dear. */ 'secret' => env('OH_DEAR_HEALTH_CHECK_SECRET'), /* * The URL that should be configured in the Application health settings at Oh Dear. */ 'url' => '/oh-dear-health-check-results', ],Use the DatabaseCheck to verify DB connectivity
mainThe
DatabaseCheckensures your application can successfully connect to a database. By default, it attempts to connect using your application'sdefaultdatabase connection. If the default connection fails, the check will fail.To use this check, register it within the
Health::checks()method.use Spatie\Health\Facades\Health; use Spatie\Health\Checks\Checks\DatabaseCheck; Health::checks([ DatabaseCheck::new(), ]);Store health check results in the cache
mainYou can use
Spatie\Health\ResultStores\CacheHealthResultStoreto write the latest health check results to your application's cache. This is useful for persisting check results across different requests or processes.To configure this, add the
CacheHealthResultStoreclass to theresult_storesarray within thehealthconfiguration file. You must specify which cache driver to use via thestorekey. The value forstoremust match one of the cache stores defined in yourconfig/cache.phpfile (e.g.,file,redis,database).return [ 'result_stores' => [ CacheHealthResultStore::class => [ 'store' => 'file', // Replace 'file' with your configured cache store name ], ], ];Run health checks immediately using the `fresh` parameter
mainBy default, the
HealthCheckResultsControllerrenders the latest cached results. If you want to force the checks to run during the current request so you see real-time results, append thefreshquery parameter to the URL.https://example.com/health?freshInstall the Slack notification channel
mainTo receive Slack notifications when health checks return warnings or failures, you must first install the official Laravel Slack notification channel via Composer.
composer require laravel/slack-notification-channelUse the HorizonCheck to monitor Laravel Horizon
mainThe
HorizonCheckensures that Laravel Horizon is running. It reports a warning if Horizon is paused and a failure if Horizon is not running at all.To use it, register the check within your
Health::checks()call.use Spatie\Health\Facades\Health; use Spatie\Health\Checks\Checks\HorizonCheck; Health::checks([ HorizonCheck::new(), ]);Use HealthCheckJsonResultsController for detailed JSON health reports
mainThe
HealthCheckJsonResultsControllerreturns a detailed JSON view of all executed health checks, including their individual statuses and metadata.Note: This endpoint always returns a
200status code unless a critical error occurs. Because it exposes detailed internal information, it is highly recommended to protect this route with authentication middleware.Route::middleware('auth')->get('health', \Spatie\Health\Http\Controllers\HealthCheckJsonResultsController::class);