3slab/vdm-healthcheck-bundle
composer require 3slab/vdm-healthcheck-bundle
config/routes.yaml (or routing.yml in older Laravel versions):
vdm_healthcheck:
resource: "@VdmHealthcheckBundle/Resources/config/routing.yml"
prefix: /
/liveness (returns 200 OK if the app is running)./readiness (returns 200 OK if the app is ready to serve traffic).Add a simple database connectivity check in config/packages/vdm_healthcheck.yaml:
vdm_healthcheck:
readiness_checkers:
database:
class: "Vdm\HealthcheckBundle\Checker\DatabaseChecker"
options:
dsn: "%env(DATABASE_URL)%"
Liveness vs. Readiness:
/liveness) for Kubernetes-style "is the container alive?" checks./readiness) for "is the app ready to handle requests?" checks (e.g., DB connections, cache, queues).Secret-Protected Details:
Configure a secret in vdm_healthcheck.secret (e.g., my_secure_secret). Append ?secret=my_secure_secret to endpoints to get JSON details:
{
"status": "healthy",
"checks": {
"database": { "status": "ok", "message": "Connection successful" }
}
}
Custom Checkers:
Extend Vdm\HealthcheckBundle\Checker\AbstractChecker to create domain-specific checks (e.g., API gateways, external services):
namespace App\Healthcheck;
use Vdm\HealthcheckBundle\Checker\AbstractChecker;
class ExternalApiChecker extends AbstractChecker
{
public function check(): bool
{
$response = Http::get('https://api.example.com/health');
return $response->successful();
}
}
Register in config:
readiness_checkers:
external_api:
class: "App\Healthcheck\ExternalApiChecker"
Laravel-Specific:
config() helper to access bundle config:
$secret = config('vdm_healthcheck.secret');
Http client for external checks (injected via constructor).Kubernetes/Orchestration:
livenessProbe:
httpGet:
path: /liveness
port: 80
readinessProbe:
httpGet:
path: /readiness?secret=${SECRET}
port: 80
Monitoring:
/readiness to Prometheus or similar tools by adding a ?format=prometheus query param (extend the bundle or wrap the endpoint).Secret Handling:
vdm_healthcheck.secret means all checks return 200 OK without details.Checker Failures:
503 Service Unavailable by default. Override this in your custom checker:
public function getStatusCode(): int
{
return 429; // Custom status for rate-limiting checks
}
Caching:
->check() logic that’s stateless or resets state per invocation.Log Checker Outputs:
Enable debug mode in config/packages/vdm_healthcheck.yaml:
debug: true
Logs will appear in Laravel’s log channel.
Test Locally:
Simulate failures by mocking dependencies (e.g., use Laravel’s Mockery or Http::fake()):
Http::fake([
'https://api.example.com/health' => Http::response('500', 500),
]);
Custom Response Formats:
Override the Vdm\HealthcheckBundle\Response\HealthcheckResponseFactory to return JSON, XML, or custom formats:
namespace App\Healthcheck;
use Vdm\HealthcheckBundle\Response\HealthcheckResponseFactory;
class CustomResponseFactory extends HealthcheckResponseFactory
{
public function create(array $checks, bool $isHealthy): string
{
return json_encode(['status' => $isHealthy ? 'ok' : 'fail', 'checks' => $checks]);
}
}
Bind it in config/services.yaml:
services:
Vdm\HealthcheckBundle\Response\HealthcheckResponseFactory: '@App\Healthcheck\CustomResponseFactory'
Dynamic Checkers: Load checkers dynamically via service providers or container extensions:
public function register()
{
$this->app->extend(
'vdm_healthcheck.checkers',
fn ($checkers) => array_merge($checkers, [
new App\Healthcheck\DynamicChecker(),
])
);
}
Rate Limiting:
Protect healthcheck endpoints with Laravel’s middleware (e.g., throttle):
# config/routes.yaml
vdm_healthcheck:
resource: "@VdmHealthcheckBundle/Resources/config/routing.yml"
prefix: /
middleware: ['throttle:10,1']
How can I help you explore Laravel packages today?