Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions .install/symfony/config/routes/open_conext_monitor.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
open_conext_monitor:
resource: "@OpenConextMonitorBundle/Resources/config/routing.yml"
prefix: /


resource: "@OpenConextMonitorBundle/src/Controller"
type: attribute
prefix: /
47 changes: 14 additions & 33 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ A Symfony 5/6/7 bundle that adds an /internal/health and /internal/info endpoint

The endpoints return JSON responses. The `/internal/info` endpoint tries to give as much information about the currently installed
version of the application as possible. This information is based on the build path of the installation. But also
includes the Symfony environment that is currently active and whether or not the debugger is enabled.
includes the Symfony environment that is currently active and whether the debugger is enabled.

The `/internal/health` endpoint reports on the health of the application. This information could be used for example by a load
balancer. Example output:
Expand Down Expand Up @@ -46,9 +46,11 @@ When a health check failed the HTTP Response status code will be 503. And the JS
* Include the routing configuration in `config/routes.yaml` by adding:
```yaml
open_conext_monitor:
resource: "@OpenConextMonitorBundle/Resources/config/routing.yml"
prefix: /
resource: "@OpenConextMonitorBundle/src/Controller"
type: attribute
prefix: /
```
_Note: this is currently done by the bundle itself, with an external dependency called https://github.com/endroid/installer_

* Add security exceptions in `config/packages/security.yaml` (if this is required at all)
```yaml
Expand Down Expand Up @@ -78,24 +80,18 @@ use OpenConext\MonitorBundle\Value\HealthReport;

class ApiHealthCheck implements HealthCheckInterface
{
/**
* @var MyService
*/
private $testService;

public function __construct(MyService $service)
public function __construct(private readonly MyService $service)
{
$this->testService = $service;
}

public function check(HealthReportInterface $report): HealthReportInterface
{
if (!$this->testService->everythingOk()) {
if (!$this->service->everythingOk()) {
// Return a HealthReport with a DOWN status when there are indications the application is not functioning as
// intended. You can provide an optional message that is displayed alongside the DOWN status.
return HealthReport::buildStatusDown('Not everything is allright.');
}
// By default return the report that was passed along as a parameter to the check method
// By default, return the report that was passed along as a parameter to the check method
return $report;
}
}
Expand All @@ -104,35 +100,20 @@ class ApiHealthCheck implements HealthCheckInterface
registered health checkers. If everything was OK, just return the report that was passed to the method.

### Register the checker
To actually include the home made checker simply tag it with 'surfnet.monitor.health_check'

Example service definition in `services.yml`

```yaml
services:
acme.monitor.my_custom_health_check:
class: Acme\AppBundle\HealthCheck\MyCustomHealthCheck
arguments:
- @test_service
tags:
- { name: surfnet.monitor.health_check }
```
By implementing the `HealthCheckInterface` you can register your own health check.
This interface is tagged automatically, so you don't have to do it yourself.

## Overriding a default HealthCheck
To run a custom query with the DoctrineConnectionHealthCheck you will need to override it in your own project.

For example in your ACME bunde that is using the monitor bundle:
For example in your ACME bundle that is using the monitor bundle:

`services.yml`
`services.yaml`
```yaml
# Override the service, service names can be found in `/src/Resources/config/services.yml`
openconext.monitor.database_health_check:
# Override the service in `/src/config/services.yaml`
OpenConext\MonitorBundle\HealthCheck\DoctrineConnectionHealthCheck:
# Point to your own implementation of the check
class: Acme\GreatSuccessBundle\HealthCheck\DoctrineConnectionHealthCheck
# Do not forget to apply the correct tag
tags:
- { name: openconext.monitor.health_check }

```

The rest of the service configuration is up to your own needs. You can inject arguments, factory calls and other service features as need be.
Expand Down
24 changes: 10 additions & 14 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,21 @@
{
"name": "openconext/monitor-bundle",
"type": "symfony-bundle",
"description": "A Symfony 5/6/7 bundle that facilitates health and info endpoints to a Symfony application.",
"description": "A Symfony 6/7 bundle that facilitates health and info endpoints to a Symfony application.",
"keywords": ["SURFnet", "StepUp", "OpenConext", "monitoring", "health"],
"license": "Apache-2.0",
"minimum-stability": "stable",
"require": {
"php": ">=8.2, <9.0-dev",
"doctrine/orm": "^2.9",
"doctrine/dbal": "^3.1",
"doctrine/dbal": "^3.1|^4.0",
"endroid/installer": "^1.4",
"symfony/dependency-injection": "^5.4|^6.3|^7.0",
"symfony/framework-bundle": "^5.4|^6.3|^7.0",
"symfony/dependency-injection": "^6.3|^7.0",
"symfony/framework-bundle": "^6.3|^7.0",
"webmozart/assert": "^1.10"
},
"require-dev": {
"php-parallel-lint/php-parallel-lint": "^1.3",
"phpmd/phpmd": "^2.13",
"matthiasnoback/symfony-config-test": "^4.3",
"phpdocumentor/reflection-docblock": "^5.2",
"phpunit/php-token-stream": "^3.1.3|^4.0.4",
"phpunit/phpunit": "^9.6|^10.4",
"sebastian/phpcpd": "^4.1|^5.0|^6.0",
"squizlabs/php_codesniffer": "^3.6",
Expand All @@ -28,21 +24,21 @@
},
"autoload": {
"psr-4": {
"OpenConext\\MonitorBundle\\": "src"
"OpenConext\\MonitorBundle\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"App\\Tests\\": "src/Tests/"
"App\\Tests\\": "tests/"
}
},
"scripts": {
"tests": {
"docheader": "vendor/bin/docheader check src/",
"phpcs": "vendor/bin/phpcs src --report=full --standard=phpcs.xml --extensions=php --warning-severity=0",
"phpcpd": "vendor/bin/phpcpd src --exclude=src/Tests/*",
"docheader": "vendor/bin/docheader check src/ tests/",
"phpcs": "vendor/bin/phpcs src tests --report=full --standard=phpcs.xml --extensions=php --warning-severity=0",
"phpcpd": "vendor/bin/phpcpd src",
"phpunit": "vendor/bin/phpunit --coverage-text",
"phpmd": "vendor/bin/phpmd src text phpmd.xml --exclude 'src/Tests/*'"
"phpmd": "vendor/bin/phpmd src text phpmd.xml"
},
"post-update-cmd": [
"@tests"
Expand Down
8 changes: 8 additions & 0 deletions config/services.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
services:
_defaults:
autowire: true # Automatically injects dependencies in your services.
autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

OpenConext\MonitorBundle\:
resource: '../src/'
exclude: '../src/{Entity,Repository,Tests,Resources,DependencyInjection,OpenConextMonitorBundle.php}'
49 changes: 23 additions & 26 deletions phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,29 @@
>

<php>
<ini name="error_reporting" value="-1" />
<server name="APP_ENV" value="test" force="true" />
<server name="SHELL_VERBOSITY" value="-1" />
<server name="SYMFONY_PHPUNIT_REMOVE" value="" />
<server name="SYMFONY_PHPUNIT_VERSION" value="9" />

<server name="KERNEL_CLASS" value="App\Tests\App\AppKernel"/>
<server name="KERNEL_DIR" value="./Tests/App"/>
<ini name="zend.enable_gc" value="0"/>
<ini name="error_reporting" value="-1"/>
<server name="APP_ENV" value="test" force="true"/>
<server name="SHELL_VERBOSITY" value="-1"/>
<server name="SYMFONY_PHPUNIT_REMOVE" value=""/>
<server name="SYMFONY_PHPUNIT_VERSION" value="9"/>
<ini name="zend.enable_gc" value="0"/>
</php>

<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<directory>.github</directory>
<directory>src/Tests</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="Test Suite">
<directory>src/Tests</directory>
</testsuite>
</testsuites>
<listeners>
<listener class="\Mockery\Adapter\Phpunit\TestListener"/>
</listeners>
<coverage processUncoveredFiles="true">
<include>
<directory suffix=".php">src</directory>
</include>
<exclude>
<directory>.github</directory>
<directory>tests</directory>
</exclude>
</coverage>
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<listeners>
<listener class="\Mockery\Adapter\Phpunit\TestListener"/>
</listeners>
</phpunit>
3 changes: 3 additions & 0 deletions src/Controller/HealthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use OpenConext\MonitorBundle\HealthCheck\HealthCheckChain;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\Routing\Attribute\Route;

/**
* Display the health state of the application.
Expand All @@ -35,6 +36,8 @@ public function __construct(
) {
}

#[Route('/health', name: 'monitor.health', methods: ['GET'])]
#[Route('/internal/health', name: 'monitor.internal_health', methods: ['GET'])]
public function __invoke(): JsonResponse
{
$statusResponse = $this->healthChecker->check();
Expand Down
16 changes: 13 additions & 3 deletions src/Controller/InfoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
namespace OpenConext\MonitorBundle\Controller;

use OpenConext\MonitorBundle\Value\BuildInformationFactory;
use OpenConext\MonitorBundle\Value\BuildPathFactory;
use OpenConext\MonitorBundle\Value\Information;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;

/**
* Display specific information about the application.
Expand All @@ -43,21 +43,31 @@
class InfoController extends AbstractController
{
private array $systemInfo = [];
private readonly string $buildPath;

public function __construct(
private readonly string $buildPath,
#[Autowire(param: 'kernel.project_dir')]
Comment thread
MKodde marked this conversation as resolved.
private readonly string $projectDir,
#[Autowire(param: 'kernel.environment')]
private readonly string $environment,
#[Autowire(param: 'kernel.debug')]
private readonly bool $debuggerEnabled,
#[Autowire(env: 'default::string:OPENCONEXT_APP_VERSION')]
private readonly ?string $version,
#[Autowire(env: 'default::string:OPENCONEXT_GIT_SHA')]
private readonly ?string $revision,
#[Autowire(env: 'default::string:OPENCONEXT_COMMIT_DATE')]
private readonly ?string $commitDate,
) {
$this->buildPath = basename(realpath($this->projectDir));

if (function_exists('opcache_get_status')) {
$this->systemInfo['opcache'] = opcache_get_status(false);
}
}

#[Route('/info', name: 'monitor.info', methods: ['GET'])]
#[Route('/internal/info', name: 'monitor.internal_info', methods: ['GET'])]
public function __invoke(): JsonResponse
{
$buildInformation = BuildInformationFactory::build(
Expand Down
48 changes: 0 additions & 48 deletions src/DependencyInjection/Compiler/HealthCheckPass.php

This file was deleted.

56 changes: 0 additions & 56 deletions src/DependencyInjection/OpenConextMonitorExtension.php

This file was deleted.

Loading