alahaxe / healthcheck-bundle
Extensible healthcheck bundle for SF
Installs: 7 663
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 0
Open Issues: 1
Requires
- php: ^8.0
- ext-json: *
- alahaxe/healthcheck-contracts: ^1.1
- psr/log: ^1||^2||^3
- symfony/config: ^6.0 || ^7.0
- symfony/console: ^6.0 || ^7.0
- symfony/dependency-injection: ^6.0 || ^7.0
- symfony/event-dispatcher-contracts: ^2 || ^3
- symfony/framework-bundle: ^6.0 || ^7.0
- symfony/http-foundation: ^6.0 || ^7.0
- symfony/http-kernel: ^6.0 || ^7.0
Requires (Dev)
- brianium/paratest: ^6.4
- php-parallel-lint/php-parallel-lint: ^1.3
- phpstan/phpstan: ^1.3
- phpunit/phpunit: ^9.5
- symfony/browser-kit: ^6.0 || ^7.0
- symfony/css-selector: ^6.0 || ^7.0
- symfony/event-dispatcher: ^6.0 || ^7.0
- symfony/phpunit-bridge: ^6.0 || ^7.0
- symfony/yaml: ^6.0 || ^7.0
README
This bundle allows to easily expose a healthcheck on a Symfony application. You can add as many checks you want and create your own checks.
Installation
composer require alahaxe/healthcheck-bundle
Quickstart
1- Router
Register package routes in your application
lahaxearnaud_healthcheck: resource: "@HealthCheckBundle/Resources/config/router.yaml"
Default route for healthcheck is /_healthcheck
2- Firewall
Allow any requests to call the healthcheck endpoint.
security: firewalls: healthcheck: pattern: ^/_healthcheck security: false
Use custom route
Do not load resource @HealthCheckBundle/Resources/config/router.yaml
file in your router but add:
lahaxearnaud_healthcheck: path: /my-healthcheck controller: Alahaxe\HealthCheckBundle\Controller\HealthCheckController
Adapte firewall pattern:
security: firewalls: healthcheck: pattern: ^/my-healthcheck security: false
Use available checks
Add a custom check
Create a custom class that implements CheckInterface
:
<?php namespace App\Service\HealthCheck; use Alahaxe\HealthCheckBundle\CheckStatus; use Alahaxe\HealthCheckBundle\Contract\CheckInterface; class AppCheck implements CheckInterface { public function check(): CheckStatus { return new CheckStatus( 'app', // the name in the final json __CLASS__, // only for debug CheckStatus::STATUS_OK, // or CheckStatus::STATUS_WARNING or CheckStatus::STATUS_INCIDENT 'An optional message, publicly exposed', 200 // an HTTP status ); } }
The output on /_healthcheck
will be:
{ "checks": { "app": { "payload": "An optional message, publicly exposed", "status": "ok" } } }
Register the service with the tag lahaxearnaud.healthcheck.check
:
App\Service\HealthCheck\AppCheck: tags: ['lahaxearnaud.healthcheck.check']
Or if you have many checks you can add the tag on a folder:
App\Service\HealthCheck\: resource: '../src/Service/HealthCheck' tags: ['lahaxearnaud.healthcheck.check']
Http verbosity
Verbosity configuration allows to redure informations exposed publicly.
If your healthcheck is protected (firewall, network rules...) you should use a full configuration.
Default verbosity is minimal
Full configuration
In your symfony configs:
health_check: http: format: full
Example of http response:
{ "context": { "environment": "dev", "datetime": "2022-01-05T17:00:53+00:00" }, "health": false, "checks": { "databaseConnectivity": { "payload": null, "status": "ok" }, "freeSpace": { "payload": null, "status": "warning" }, "cpuLoad": { "payload": null, "status": "incident" }, "redis": { "payload": null, "status": "ok" }, "app": { "payload": null, "status": "ok" } } }
Minimal configuration:
In your symfony configs:
health_check: http: format: minimal
Example of http response:
{ "health": false }
Events / Listeners
By default a log is written on each Alahaxe\HealthCheckBundle\Event\HealthCheckAllEvent::class
, take a look at Alahaxe\HealthCheckBundle\Event\Subscriber\LoggerSubscriber
for an example.
License
This bundle is under the MIT license. See the complete license in the bundle.