Skip to content

Write a custom health indicator

A health indicator is a class that implements Nwidart\Actuator\Health\HealthIndicator and returns a Health value.

app/Health/PaymentGatewayHealthIndicator.php
<?php
namespace App\Health;
use App\Services\PaymentGateway;
use Nwidart\Actuator\Health\Health;
use Nwidart\Actuator\Health\HealthIndicator;
final class PaymentGatewayHealthIndicator implements HealthIndicator
{
public function __construct(private PaymentGateway $gateway) {}
public function health(): Health
{
$latency = $this->gateway->ping();
return $latency < 500
? Health::up(['latency' => $latency])
: Health::outOfService(['latency' => $latency]);
}
}

The container builds the indicator, so constructor dependencies such as PaymentGateway are injected.

Add it to health.indicators. The key is the component name:

config/actuator.php
'indicators' => [
// ...
'paymentGateway' => App\Health\PaymentGatewayHealthIndicator::class,
],
Factory Use when HTTP
Health::up($details) The component works. 200
Health::down($details) The component fails. 503
Health::outOfService($details) The component works but must not take traffic, for example during a planned outage. 503
Health::unknown($details) The indicator cannot tell, for example a check that is not configured. 200

The HTTP status applies when this status is the most severe one. The health model explains the aggregation.

Pass details to the factory, or add them one at a time. Health is immutable, so each call returns a new instance:

return Health::up()
->withDetail('region', 'eu-west-1')
->withDetail('latency', $latency);

Details are visible only when show_details allows them. Never put secrets in details.

You do not need to catch exceptions. When health() throws, the component reports DOWN with the exception class and message in the error detail, and the package logs a warning:

{ "status": "DOWN", "details": { "error": "RuntimeException: Connection refused" } }

The same happens when the container cannot build the indicator, for example when the class does not exist or a constructor dependency cannot be resolved. The other components still report.

To report a failure with your own details, catch the exception and use withException():

try {
$latency = $this->gateway->ping();
} catch (GatewayTimeout $exception) {
return Health::down(['timeout' => 2])->withException($exception);
}

Constructor parameters without a type the container can resolve become configuration arguments:

public function __construct(private PaymentGateway $gateway, private int $maxLatency = 500) {}
config/actuator.php
'paymentGateway' => [App\Health\PaymentGatewayHealthIndicator::class, ['maxLatency' => 250]],

Every health request runs every indicator, and probes call the endpoint every few seconds.

  • Set a short timeout on network calls, well below your probe timeout.
  • Avoid heavy queries. Count rows with an index, or read a cached value.
  • If a check is expensive, compute it in a scheduled job, store the result in the cache, and let the indicator read the cache.