Tutorial: your first health check
In this tutorial, we install Laravel Actuator in a new Laravel application. We read the health of the application, put it in maintenance mode to see the readiness probe react, and write a health indicator of our own that watches the queue.
It takes about fifteen minutes. You need PHP 8.2 or later, Composer and curl.
Create an application
Section titled “Create an application”-
Create a new Laravel application and go into its directory:
Terminal window composer create-project laravel/laravel actuator-democd actuator-demoThe new application uses SQLite for its database, cache and queue. Composer creates the database file and runs the migrations for us.
-
Install Laravel Actuator:
Terminal window composer require nwidart/laravel-actuator -
Laravel Actuator also records metrics, and keeps them in Redis. Our new application has no Redis, so we keep them in the cache. Add this line to the
.envfile:.env METRICS_STORE=cache -
Start the development server:
Terminal window php artisan serveLeave it running, and open a second terminal in the same directory for the next commands.
Discover the endpoints
Section titled “Discover the endpoints”Ask the actuator what it offers:
curl http://127.0.0.1:8000/actuator{ "_links": { "self": { "href": "http://127.0.0.1:8000/actuator", "templated": false }, "health": { "href": "http://127.0.0.1:8000/actuator/health", "templated": false }, "health-path": { "href": "http://127.0.0.1:8000/actuator/health/{*path}", "templated": true } }}Only health is listed. The other endpoints exist, but they are not exposed until we ask for them.
Read the health
Section titled “Read the health”curl http://127.0.0.1:8000/actuator/health{ "status": "UP", "groups": ["liveness", "readiness"] }The application is UP. We see no detail about why it is up. That is deliberate: the endpoint is
public, so it hides its details by default.
Let us show the details on our machine. Add this line to the .env file:
ACTUATOR_HEALTH_SHOW_DETAILS=alwaysStop the server with Ctrl+C, start it again with php artisan serve, and
repeat the request:
curl http://127.0.0.1:8000/actuator/health{ "status": "UP", "components": { "ping": { "status": "UP" }, "readinessState": { "status": "UP" }, "diskSpace": { "status": "UP", "details": { "total": 494384795648, "free": 213212008448, "threshold": 10485760, "path": "/home/you/actuator-demo", "exists": true } }, "db": { "status": "UP", "details": { "connection": "sqlite", "database": "SQLite", "validationQuery": "select 1" } }, "cache": { "status": "UP", "details": { "store": "database" } } }, "groups": ["liveness", "readiness"]}Your disk figures and path are different. Each entry under components is a health indicator.
The overall status is the most severe status of all components.
Watch the readiness probe
Section titled “Watch the readiness probe”The groups list names two health groups. Each group checks a subset of the indicators. Read the
readiness group:
curl -i http://127.0.0.1:8000/actuator/health/readinessHTTP/1.1 200 OKContent-Type: application/json
{"status":"UP","components":{"readinessState":{"status":"UP"}}}Now put the application in maintenance mode, and ask again:
php artisan downcurl -i http://127.0.0.1:8000/actuator/health/readinessHTTP/1.1 503 Service UnavailableContent-Type: application/json
{"status":"OUT_OF_SERVICE","components":{"readinessState":{"status":"OUT_OF_SERVICE"}}}The status is OUT_OF_SERVICE, and the HTTP status is 503. A load balancer or Kubernetes stops
sending traffic to this instance. Now ask the liveness group:
curl -i http://127.0.0.1:8000/actuator/health/livenessHTTP/1.1 200 OKContent-Type: application/json
{"status":"UP","components":{"ping":{"status":"UP"}}}Liveness stays UP, so nothing restarts the instance. Notice also that the actuator still answers
in JSON, while the rest of the application shows the maintenance page.
Bring the application back:
php artisan upWrite a health indicator
Section titled “Write a health indicator”The built-in indicators do not know about our queue. Let us write an indicator that reports DOWN
when too many jobs wait in the jobs table.
-
Create the file
app/Health/QueueBacklogHealthIndicator.php:app/Health/QueueBacklogHealthIndicator.php <?phpnamespace App\Health;use Illuminate\Support\Facades\DB;use Nwidart\Actuator\Health\Health;use Nwidart\Actuator\Health\HealthIndicator;final class QueueBacklogHealthIndicator implements HealthIndicator{public function __construct(private int $threshold = 100) {}public function health(): Health{$pending = DB::table('jobs')->count();$details = ['pending' => $pending, 'threshold' => $this->threshold];return $pending < $this->threshold? Health::up($details): Health::down($details);}} -
Publish the configuration file:
Terminal window php artisan vendor:publish --tag=actuator-config -
Open
config/actuator.phpand add the indicator tohealth.indicators:config/actuator.php 'indicators' => ['ping' => PingHealthIndicator::class,'readinessState' => ReadinessStateHealthIndicator::class,'diskSpace' => DiskSpaceHealthIndicator::class,'db' => DatabaseHealthIndicator::class,'queue' => App\Health\QueueBacklogHealthIndicator::class,'cache' => CacheHealthIndicator::class,], -
Read the new component. Components have their own path:
Terminal window curl http://127.0.0.1:8000/actuator/health/queue{ "status": "UP", "details": { "pending": 0, "threshold": 100 } }
Make it fail
Section titled “Make it fail”An indicator entry can pass arguments to the constructor. Set the threshold to 0, so that even
an empty queue is too much:
'queue' => [App\Health\QueueBacklogHealthIndicator::class, ['threshold' => 0]],curl -i http://127.0.0.1:8000/actuator/healthHTTP/1.1 503 Service UnavailableContent-Type: application/json{ "status": "DOWN", "components": { "queue": { "status": "DOWN", "details": { "pending": 0, "threshold": 0 } } }}The output above is shortened: the other components are still listed and still UP. One DOWN
component makes the whole application DOWN, with HTTP 503.
Put the threshold back to 100 before you go on.
What we did
Section titled “What we did”We installed Laravel Actuator, read the health of the application with and without details, saw the readiness group take the application out of service during maintenance, and wrote a health indicator with a configurable threshold.
Where to go next
Section titled “Where to go next”- Before you deploy, secure the endpoints and remove
ACTUATOR_HEALTH_SHOW_DETAILS=alwaysfrom production. - Wire the groups into your platform: Kubernetes probes or load balancers and containers.
- Learn how to test an indicator.
- Understand how statuses are aggregated.