Installation:
composer require devexploris/myaku-health-check
Add to config/bundles.php:
return [
Devexploris\MyakuHealthCheck\MyakuHealthCheckBundle::class => ['all' => true],
];
Route Registration:
Add to config/routes.yaml:
myaku_health_check:
resource: Devexploris\MyakuHealthCheck\Controller\HealthCheckController
type: attribute
First Use Case:
Access GET /health to verify the endpoint works. The response will include disk space, memory (Linux only), database, and cache statuses.
config/packages/myaku_health_check.yaml (create if missing).APP_MYAKU_TOKEN in .env (generate with openssl rand -hex 24).threshold.space and threshold.memory if needed (e.g., threshold: { space: 80 }).For a Laravel developer migrating from Symfony, replace Symfony’s health_check bundle with this package. The /health endpoint replaces Laravel’s php artisan down or custom health checks. Use it in:
Basic Health Check:
// In a Laravel controller or service
$response = Http::get('http://localhost/health');
$health = json_decode($response->body(), true);
if ($health['database']['connected'] === false) {
throw new \RuntimeException('Database unavailable');
}
Threshold-Based Alerts:
Configure thresholds in myaku_health_check.yaml:
myaku_health_check:
threshold:
space: 90
memory: 95
threshold_targeted: true in the response indicates a breach.IP Whitelisting: Restrict access to specific IPs:
security:
whitelist:
- "127.0.0.1"
- "192.168.1.100"
Token Authentication: Include the token in requests:
curl -H "x-myaku-token: YOUR_TOKEN_HERE" http://localhost/health
Laravel-Specific:
Use the endpoint in Laravel’s AppServiceProvider for bootstrapping checks:
public function boot()
{
$health = Http::get('http://localhost/health')->json();
if ($health['database']['connected'] === false) {
Log::error('Database unavailable during boot');
}
}
Monitoring Integration: Parse the JSON response in tools like:
/health and extract metrics (e.g., space_used_percent).Custom Checks: Extend the bundle by creating a custom checker (see Extension Points).
Environment-Specific Config:
Use Laravel’s .env for dynamic thresholds:
threshold:
space: "%env(int:MYAKU_SPACE_THRESHOLD, 80)%"
Memory Check on Non-Linux:
The memory check fails silently on macOS/Windows (returns connected: false). Document this in your monitoring alerts.
Database/Cache Auto-Detection:
If doctrine.dbal.default_connection or cache.app are missing, the checks are skipped. Verify these services are registered in Symfony’s container.
Token Security:
.env (e.g., APP_MYAKU_TOKEN).openssl rand -hex 24).Threshold Logic:
space: 80 = 80% usage).threshold_targeted: true triggers a 503 response.IP Whitelist Strictness:
whitelist is empty, all IPs are allowed.503 Responses:
Check the JSON response for threshold_targeted: true or error fields in database/cache.
403 Forbidden:
x-myaku-token header matches .env.Missing Checks:
doctrine.dbal.default_connection and cache.app are available in Symfony’s container.Custom Checkers:
Extend the bundle by implementing Devexploris\MyakuHealthCheck\Checker\CheckerInterface:
namespace App\HealthChecks;
use Devexploris\MyakuHealthCheck\Checker\CheckerInterface;
class CustomChecker implements CheckerInterface
{
public function check(): array
{
return [
'status' => 'ok',
'data' => ['custom_metric' => 42],
];
}
}
Register it in services.yaml:
services:
App\HealthChecks\CustomChecker:
tags: ['myaku_health_check.checker']
Latency Metrics:
Use the latency field in database/cache for performance monitoring.
Testing:
Mock the /health endpoint in Laravel tests:
$response = Http::fake([
'http://localhost/health' => Http::response(['space' => ['free' => '100%']], 200),
]);
Laravel-Symfony Bridge: If using Laravel, wrap the Symfony bundle in a Laravel service provider to handle route registration and configuration:
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Devexploris\MyakuHealthCheck\MyakuHealthCheckBundle;
class MyakuHealthCheckProvider extends ServiceProvider
{
public function register()
{
$this->app->register(MyakuHealthCheckBundle::class, ['all' => true]);
}
}
Log Critical Failures:
In a Laravel event listener or service, log 503 responses:
if ($health['database']['connected'] === false) {
Log::critical('Database connection failed', ['error' => $health['database']['error']]);
}
How can I help you explore Laravel packages today?