Skip to content

Commit

Permalink
Introduce envvar support for info
Browse files Browse the repository at this point in the history
  • Loading branch information
MKodde committed Jan 24, 2024
1 parent 5aab82c commit d0f062f
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 95 deletions.
11 changes: 3 additions & 8 deletions src/Controller/HealthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,9 @@
*/
class HealthController extends AbstractController
{
/**
* @var HealthCheckChain
*/
private $healthChecker;

public function __construct(HealthCheckChain $healthChecker)
{
$this->healthChecker = $healthChecker;
public function __construct(

Check warning on line 33 in src/Controller/HealthController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/HealthController.php#L33

Added line #L33 was not covered by tests
private readonly HealthCheckChain $healthChecker
) {
}

public function __invoke(): JsonResponse
Expand Down
41 changes: 15 additions & 26 deletions src/Controller/InfoController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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;
Expand All @@ -41,34 +42,16 @@
*/
class InfoController extends AbstractController
{
/**
* @var string
*/
private $buildPath;

/**
* @var string
*/
private $environment;

/**
* @var bool
*/
private $debuggerEnabled;

/**
* @var array
*/
private $systemInfo = [];
private array $systemInfo = [];

public function __construct(
$buildPath,
$environment,
$debuggerEnabled
private readonly string $buildPath,
private readonly string $environment,
private readonly bool $debuggerEnabled,
private readonly ?string $version,
private readonly ?string $revision,
private readonly ?string $commitDate,
) {
$this->buildPath = $buildPath;
$this->environment = $environment;
$this->debuggerEnabled = $debuggerEnabled;

if (function_exists('opcache_get_status')) {
$this->systemInfo['opcache'] = opcache_get_status(false);
Expand All @@ -77,8 +60,14 @@ public function __construct(

public function __invoke(): JsonResponse
{
$buildInformation = BuildInformationFactory::build(
$this->version,
$this->revision,
$this->commitDate,
$this->buildPath
);

Check warning on line 68 in src/Controller/InfoController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/InfoController.php#L63-L68

Added lines #L63 - L68 were not covered by tests
$info = Information::buildFrom(
BuildPathFactory::buildFrom($this->buildPath),
$buildInformation,

Check warning on line 70 in src/Controller/InfoController.php

View check run for this annotation

Codecov / codecov/patch

src/Controller/InfoController.php#L70

Added line #L70 was not covered by tests
$this->environment,
$this->debuggerEnabled,
$this->systemInfo
Expand Down
2 changes: 1 addition & 1 deletion src/DependencyInjection/Compiler/HealthCheckPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function process(ContainerBuilder $container)

$definition = $container->findDefinition('openconext.monitor.health_check_chain');

// find all service IDs with the app.mail_transport tag
// find all service IDs with the openconext.monitor.health_check tag
$taggedServices = $container->findTaggedServiceIds('openconext.monitor.health_check');

foreach ($taggedServices as $id => $tags) {
Expand Down
8 changes: 5 additions & 3 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ services:
- '~'
- '%kernel.environment%'
- '%kernel.debug%'
public: true
- '%env(default::string:OPENCONEXT_APP_VERSION)%'
- '%env(default::string:OPENCONEXT_GIT_SHA)%'
- '%env(default::string:OPENCONEXT_COMMIT_DATE)%'

autowire: true
tags: ['controller.service_arguments', 'container.service_subscriber']

openconext.monitor.controller.health:
class: OpenConext\MonitorBundle\Controller\HealthController
arguments:
- '@openconext.monitor.health_check_chain'
public: true
autowire: true
tags: ['controller.service_arguments', 'container.service_subscriber']

Expand All @@ -30,4 +32,4 @@ services:
calls:
- [ setEntityManager, ['@?doctrine.orm.entity_manager']]
tags:
- { name: openconext.monitor.health_check }
- { name: openconext.monitor.health_check }
89 changes: 89 additions & 0 deletions src/Value/BuildEnvVars.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

/**
* Copyright 2024 SURFnet B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace OpenConext\MonitorBundle\Value;

/**
* Representation of the env vars that describe the build details
*
* By default, the following env vars are evaluated:
*
* OPENCONEXT_APP_VERSION maps to $version
* OPENCONEXT_GIT_SHA maps to $revision
* OPENCONEXT_COMMIT_DATE maps to $commitDate
*
* This VO is created by its factory who actually reads the env-vars
*/
class BuildEnvVars implements BuildInformation
{
public function __construct(

Check warning on line 34 in src/Value/BuildEnvVars.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVars.php#L34

Added line #L34 was not covered by tests
private readonly string $version,
private readonly string $revision,
private readonly string $commitDate,
) {
}

Check warning on line 39 in src/Value/BuildEnvVars.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVars.php#L39

Added line #L39 was not covered by tests

public function getVersion(): string

Check warning on line 41 in src/Value/BuildEnvVars.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVars.php#L41

Added line #L41 was not covered by tests
{
return $this->version;

Check warning on line 43 in src/Value/BuildEnvVars.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVars.php#L43

Added line #L43 was not covered by tests
}

public function getRevision(): string

Check warning on line 46 in src/Value/BuildEnvVars.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVars.php#L46

Added line #L46 was not covered by tests
{
return $this->revision;

Check warning on line 48 in src/Value/BuildEnvVars.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVars.php#L48

Added line #L48 was not covered by tests
}

public function getCommitDate(): string

Check warning on line 51 in src/Value/BuildEnvVars.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVars.php#L51

Added line #L51 was not covered by tests
{
return $this->commitDate;

Check warning on line 53 in src/Value/BuildEnvVars.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVars.php#L53

Added line #L53 was not covered by tests
}

public function hasRevision(): bool

Check warning on line 56 in src/Value/BuildEnvVars.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVars.php#L56

Added line #L56 was not covered by tests
{
return true;

Check warning on line 58 in src/Value/BuildEnvVars.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVars.php#L58

Added line #L58 was not covered by tests
}

public function getPath(): string

Check warning on line 61 in src/Value/BuildEnvVars.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVars.php#L61

Added line #L61 was not covered by tests
{
return '';

Check warning on line 63 in src/Value/BuildEnvVars.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVars.php#L63

Added line #L63 was not covered by tests
}

public function hasVersion(): bool

Check warning on line 66 in src/Value/BuildEnvVars.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVars.php#L66

Added line #L66 was not covered by tests
{
return true;

Check warning on line 68 in src/Value/BuildEnvVars.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVars.php#L68

Added line #L68 was not covered by tests
}

public function hasPath(): bool

Check warning on line 71 in src/Value/BuildEnvVars.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVars.php#L71

Added line #L71 was not covered by tests
{
return false;

Check warning on line 73 in src/Value/BuildEnvVars.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVars.php#L73

Added line #L73 was not covered by tests
}

public function hasCommitDate(): bool

Check warning on line 76 in src/Value/BuildEnvVars.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVars.php#L76

Added line #L76 was not covered by tests
{
return true;

Check warning on line 78 in src/Value/BuildEnvVars.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVars.php#L78

Added line #L78 was not covered by tests
}

public function jsonSerialize(): mixed

Check warning on line 81 in src/Value/BuildEnvVars.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVars.php#L81

Added line #L81 was not covered by tests
{
return [
'version' => $this->version,
'revision' => $this->revision,
'commitDate' => $this->commitDate,
];

Check warning on line 87 in src/Value/BuildEnvVars.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVars.php#L83-L87

Added lines #L83 - L87 were not covered by tests
}
}
34 changes: 34 additions & 0 deletions src/Value/BuildEnvVarsFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

/**
* Copyright 2024 SURFnet B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace OpenConext\MonitorBundle\Value;

/**
* Build instance of BuildEnvVars
*/
class BuildEnvVarsFactory
{
public static function buildFrom(string $version, string $revision, string $commitDate): BuildEnvVars

Check warning on line 26 in src/Value/BuildEnvVarsFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVarsFactory.php#L26

Added line #L26 was not covered by tests
{
return new BuildEnvVars(
$version,
$revision,
$commitDate
);

Check warning on line 32 in src/Value/BuildEnvVarsFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildEnvVarsFactory.php#L28-L32

Added lines #L28 - L32 were not covered by tests
}
}
43 changes: 43 additions & 0 deletions src/Value/BuildInformation.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

/**
* Copyright 2024 SURFnet B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace OpenConext\MonitorBundle\Value;

use JsonSerializable;

interface BuildInformation extends JsonSerializable
{
public function getVersion(): string;

public function jsonSerialize(): mixed;

public function getRevision(): string;

public function hasRevision(): bool;

public function getPath(): string;

public function getCommitDate(): string;

public function hasVersion(): bool;

public function hasPath(): bool;

public function hasCommitDate(): bool;

}
37 changes: 37 additions & 0 deletions src/Value/BuildInformationFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* Copyright 2024 SURFnet B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace OpenConext\MonitorBundle\Value;

use function is_string;

class BuildInformationFactory
{
public static function build(

Check warning on line 25 in src/Value/BuildInformationFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildInformationFactory.php#L25

Added line #L25 was not covered by tests
?string $version,
?string $revision,
?string $commitDate,
string $buildPath,
): BuildInformation
{
if (is_string($revision) && is_string($version) && is_string($commitDate)) {
return BuildEnvVarsFactory::buildFrom($version, $revision, $commitDate);

Check warning on line 33 in src/Value/BuildInformationFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildInformationFactory.php#L32-L33

Added lines #L32 - L33 were not covered by tests
}
return BuildPathFactory::buildFrom($buildPath);

Check warning on line 35 in src/Value/BuildInformationFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Value/BuildInformationFactory.php#L35

Added line #L35 was not covered by tests
}
}
Loading

0 comments on commit d0f062f

Please sign in to comment.