spatie/laravel-health
Monitor your Laravel app’s health by registering checks (disk space, etc.) with warning/fail thresholds. Get notified via mail or Slack when checks degrade, and extend with custom checks for proactive alerting.
This check will send a request to a given URL. It will report a failure when that URL doesn't respond with a successful response code within a second.
This check relies on cache.
Here's how you can register the check.
use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\PingCheck;
Health::checks([
PingCheck::new()->url('https://example.com'),
]);
You can use timeout() to set the maximum number of seconds the HTTP request should run for before the PingCheck fails.
use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\PingCheck;
Health::checks([
PingCheck::new()->url('https://example.com')->timeout(2),
]);
If you need more precise control, you can use timeoutMs() to set the timeout in milliseconds.
use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\PingCheck;
Health::checks([
PingCheck::new()->url('https://example.com')->timeoutMs(500),
]);
You can use name() to change the title of the PingCheck. This is useful when you have multiple PingChecks and you want to distinguish them from each other easily.
use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\PingCheck;
Health::checks([
PingCheck::new()->url('https://example.com')->name('Example'),
PingCheck::new()->url('https://spatie.be')->name('Spatie'),
]);
You can use retryTimes() to set the number of times to retry the PingCheck before failing.
use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\PingCheck;
Health::checks([
PingCheck::new()->url('https://example.com')->retryTimes(3),
]);
By default, the check will immediately report a failure when the URL is unreachable. You can use failAfterMinutes() to delay the failure status. When this option is set, the check will first report a warning status when the URL becomes unreachable. Only if the URL remains unreachable for the specified number of minutes, the check will transition to a failed status.
This is useful to avoid false alarms caused by temporary network issues or brief service interruptions.
use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\PingCheck;
Health::checks([
PingCheck::new()
->url('https://example.com')
->failAfterMinutes(5),
]);
In this example, the check will return a warning for the first 5 minutes of unreachability, and only after that will it report a failure.
The failAfterMinutes feature relies on cache to work. It will write a timestamp of the first failure into the cache that will be verified on subsequent checks. You can use useCacheStore() to specify a different cache store.
use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\PingCheck;
Health::checks([
PingCheck::new()
->url('https://example.com')
->failAfterMinutes(5)
->useCacheStore('your-custom-store-name'),
]);
Optionally, you can use cacheKeyPrefix() to customize the prefix used for the cache key. The default prefix is health:checks:ping.
PingCheck::new()
->url('https://example.com')
->failAfterMinutes(5)
->cacheKeyPrefix('my-app:health:ping'),
How can I help you explore Laravel packages today?