Skip to content

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.

  1. Create a new Laravel application and go into its directory:

    Terminal window
    composer create-project laravel/laravel actuator-demo
    cd actuator-demo

    The new application uses SQLite for its database, cache and queue. Composer creates the database file and runs the migrations for us.

  2. Install Laravel Actuator:

    Terminal window
    composer require nwidart/laravel-actuator
  3. 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 .env file:

    .env
    METRICS_STORE=cache
  4. Start the development server:

    Terminal window
    php artisan serve

    Leave it running, and open a second terminal in the same directory for the next commands.

Ask the actuator what it offers:

Terminal window
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.

Terminal window
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:

.env
ACTUATOR_HEALTH_SHOW_DETAILS=always

Stop the server with Ctrl+C, start it again with php artisan serve, and repeat the request:

Terminal window
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.

The groups list names two health groups. Each group checks a subset of the indicators. Read the readiness group:

Terminal window
curl -i http://127.0.0.1:8000/actuator/health/readiness
HTTP/1.1 200 OK
Content-Type: application/json
{"status":"UP","components":{"readinessState":{"status":"UP"}}}

Now put the application in maintenance mode, and ask again:

Terminal window
php artisan down
curl -i http://127.0.0.1:8000/actuator/health/readiness
HTTP/1.1 503 Service Unavailable
Content-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:

Terminal window
curl -i http://127.0.0.1:8000/actuator/health/liveness
HTTP/1.1 200 OK
Content-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:

Terminal window
php artisan up

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.

  1. Create the file app/Health/QueueBacklogHealthIndicator.php:

    app/Health/QueueBacklogHealthIndicator.php
    <?php
    namespace 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);
    }
    }
  2. Publish the configuration file:

    Terminal window
    php artisan vendor:publish --tag=actuator-config
  3. Open config/actuator.php and add the indicator to health.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,
    ],
  4. 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 } }

An indicator entry can pass arguments to the constructor. Set the threshold to 0, so that even an empty queue is too much:

config/actuator.php
'queue' => [App\Health\QueueBacklogHealthIndicator::class, ['threshold' => 0]],
Terminal window
curl -i http://127.0.0.1:8000/actuator/health
HTTP/1.1 503 Service Unavailable
Content-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.

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.