fivelab / diagnostic
The library for diagnostic application.
Installs: 22 944
Dependents: 0
Suggesters: 0
Security: 0
Stars: 4
Watchers: 2
Forks: 3
Open Issues: 0
Requires
- php: ^8.2
- ext-json: *
- symfony/event-dispatcher: ~5.4 | ~6.0 | ~7.0
Requires (Dev)
- ext-amqp: *
- ext-curl: *
- ext-mongodb: *
- ext-pdo: *
- ext-redis: *
- ext-sockets: *
- aws/aws-sdk-php: ~3.0
- composer/semver: ~1.0
- doctrine/annotations: ~1.13
- doctrine/dbal: ~2.2 | ~3.0
- doctrine/orm: ~2.6
- elasticsearch/elasticsearch: ~7.0
- escapestudios/symfony2-coding-standard: ~3.5.0
- fivelab/ci-rules: dev-master
- guzzlehttp/guzzle: ~7.0
- guzzlehttp/psr7: ~1.6
- http-interop/http-factory-guzzle: ~1.2
- illuminate/database: ~10.0
- opensearch-project/opensearch-php: ^2.0
- php-amqplib/php-amqplib: ^3.0
- php-http/discovery: ~1.18
- php-http/guzzle7-adapter: ~1.0
- php-http/httplug: ~2.4
- php-http/message: ~1.16
- phpmetrics/phpmetrics: ^2.0
- phpstan/phpstan: ~1.11
- phpunit/phpunit: ~10.1
- predis/predis: ~1.1
- react/promise: ~2.3
- symfony/cache: ~5.4 | ~6.0 | ~7.0
- symfony/console: ~5.4 | ~6.0 | ~7.0
- symfony/dependency-injection: ~5.4 | ~6.0 | ~7.0
- symfony/mailer: ~5.4 | ~6.0 | ~7.0
Suggests
- ext-pdo: For check connection via PDO.
- aws/aws-sdk-php: For check AWS services.
- composer/semver: For compare versions.
- doctrine/dbal: For check connect to database via DBAL connection.
- doctrine/orm: For check access to tables.
- elasticsearch/elasticsearch: For check connect and existence some indices/templates/etc... in elasticsearch.
- illuminate/database: For check connect to database via illuminate components.
- opensearch-project/opensearch-php: For check connect and existence some indices/templates/etc... in opensearch.
- psr/http-client-implementation: For HTTP checks resources
- psr/http-message-implementation:: For HTTP checks resources.
- symfony/console: For run diagnostic commands
- symfony/dependency-injection: Add diagnostic checks by tag to container.
README
#StandWithUkraineDiagnostic
The library for diagnostic application.
Why?
This library helps for developers and devops for setup any applications.
If developer correct configure all diagnostics (paths, services, etc...) the devops before run the instance with our application only run diagnostic and can sees all problems (if it exist).
Primary problem:
The developer can add any parameter or service to application. As an example - add redis service for use in runtime. And lost provide this information before release application. The devops (or developer) release new version application and the system not have any problems. But, we have a major issue because the redis only used in runtime and not processed in deploy process.
Devops add this instance to load balancer (if exist), and in next times the application not correct work ;(
Solution:
Before add the instance to load balancer (or processed scope), devops run diagnostic and can see all major problems.
Restrictions
If you want work with this mechanism you MUST add diagnostic check to all end points. As an example:
- Are you use cache? You MUST add check for verify access to cache directory.
- Are you use logs? You MUST add check for verify access to logs directory.
- Are you use DB? You MUST add check for connect to Database.
- Are you use Redis? You MUST add check for connect to Redis.
- Are you use any client for connect via HTTP? You MUST add check for connect to this endpoint.
- etc...
Attention: the devops (infrastructure team, release manager, developer) is not magic person and cannot see all required services by sky stars.
Configure and Run
For easy configure the diagnostic, you can use \FiveLab\Component\Diagnostic\Check\Definition\DefinitionCollectionBuilder
:
<?php declare(strict_types = 1); namespace Example; use FiveLab\Component\Diagnostic\Check\Definition\CheckDefinitionsBuilder; use FiveLab\Component\Diagnostic\Check\Definition\Filter\CheckDefinitionsInGroupFilter; use FiveLab\Component\Diagnostic\Check\Definition\Filter\OrXFilter; use FiveLab\Component\Diagnostic\Check\PathReadableCheck; use FiveLab\Component\Diagnostic\Check\PathWritableCheck; use FiveLab\Component\Diagnostic\Runner\Runner; $builder = new CheckDefinitionsBuilder(); $builder->addCheck('cache_dir', new PathWritableCheck('./var/cache'), 'system_dir'); $builder->addCheck('logs_dir', new PathWritableCheck('./var/logs'), 'system_dir'); $builder->addCheck('shared_dir', new PathWritableCheck('./var/shared'), 'shared_dir'); $builder->addCheck('shared_efs', new PathReadableCheck('./var/shared/.efs'), 'shared_dir'); $builder->addCheck('tmp_dir', new PathWritableCheck('./var/tmp'), ['tmp_dir', 'system_dir']); $definitions = $builder->build(); $runner = new Runner(); // Run all checks $success = $runner->run($definitions); // Run only for system dirs $definitions = $definitions->filter(new CheckDefinitionsInGroupFilter('system_dir')); $success = $runner->run($definitions); // Run tmp dir and shared dir $definitions = $definitions->filter(new OrXFilter( new CheckDefinitionsInGroupFilter('tmp_dir'), new CheckDefinitionsInGroupFilter('shared_dir') )); $success = $runner->run($definitions);
Integrate with applications
Dependency Injection
You can easy integrate this library to Symfony application, or application with DependencyInjection support.
For integrate, you can add the compiler pass to your container builder:
<?php namespace Example; use Symfony\Component\DependencyInjection\ContainerBuilder; use FiveLab\Component\Diagnostic\DependencyInjection\AddDiagnosticToBuilderCheckPass; $containerBuilder = new ContainerBuilder(); $containerBuilder->addCompilerPass(new AddDiagnosticToBuilderCheckPass()); $containerBuilder->compile();
Add add check services:
services: diagnostic.check.cache_dir: class: \FiveLab\Component\Diagnostic\Check\PathWritableCheck arguments: [ '%kernel.cache_dir%' ] tags: - { name: diagnostic.check } diagnostic.check.logs_dir: class: \FiveLab\Component\Diagnostic\Check\PathWritableCheck arguments: [ '%kernel.logs_dir%' ] tags: - { name: diagnostic.check }
The tag diagnostic.check
support next attributes:
- key - the unique key of check (default is service name).
- group - the group of this check (default is null).
- error_on_failure - if false, system not return error code from process (ignore failures, default is true).
Console commands
We provide console commands for easy integrate to any applications:
\FiveLab\Component\Diagnostic\Command\RunDiagnosticCommand
- command for run diagnostic.\FiveLab\Component\Diagnostic\Command\ListChecksCommand
- command for list all available checks.\FiveLab\Component\Diagnostic\Command\ListGroupsCommand
- command for list all available groups.
Development
For easy development you can use the Docker
and Docker compose
.
docker compose up
docker compose exec diagnostic bash
After success run and attach to container you must install vendors:
composer install
Before create the PR or merge into develop, please run next commands for validate code:
./bin/phpunit ./bin/phpcs --config-set show_warnings 0 ./bin/phpcs --standard=vendor/escapestudios/symfony2-coding-standard/Symfony/ src/ ./bin/phpcs --standard=tests/phpcs-ruleset.xml tests/