Skip to content

Commit 6919e35

Browse files
authored
feat: add monitor:health --json option (#317)
* fix: configured label was not being used in ping check * feat: output in json format option * fix: cleanup and added test * fix: phpstan fixes * fix: reduced test to only check if json flag works * fix: ignore result code in json test * chore: php-cs fixes * fix: added formated json output * fix: added formated json output
1 parent d9c0969 commit 6919e35

2 files changed

Lines changed: 48 additions & 18 deletions

File tree

src/Command/HealthCheckCommand.php

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,29 +45,58 @@ protected function configure(): void
4545
->addOption('fail-on-warning', description: 'Fail command if any checks have a warning result')
4646
->addOption('fail-on-skip', description: 'Fail command if any checks are skipped')
4747
->addOption('fail-on-unknown', description: 'Fail command if any checks have an unknown result')
48+
->addOption('json', description: 'Output in JSON format')
4849
;
4950
}
5051

52+
/**
53+
* @throws \JsonException
54+
*/
5155
protected function execute(InputInterface $input, OutputInterface $output): int
5256
{
57+
$isJsonOutput = $input->getOption('json');
5358
$io = new SymfonyStyle($input, $output);
54-
$subscriber = $io->isVerbose() ? new ConsoleCheckVerboseSubscriber($io) : new ConsoleCheckListSubscriber($input, $output);
5559

56-
$this->eventDispatcher->addSubscriber($subscriber);
60+
if (!$isJsonOutput) {
61+
$subscriber = $io->isVerbose() ? new ConsoleCheckVerboseSubscriber($io) : new ConsoleCheckListSubscriber($input, $output);
62+
$this->eventDispatcher->addSubscriber($subscriber);
63+
}
5764

5865
$suite = $this->checkRegistry->suite($input->getOption('suite'));
5966

6067
if (!$suite->count()) {
6168
throw new \RuntimeException(\sprintf('No checks found for suite "%s"', $suite));
6269
}
6370

71+
$results = $suite->run(!$input->getOption('no-cache'));
72+
$failureStatuses = [];
73+
74+
if ($input->getOption('fail-on-warning')) {
75+
$failureStatuses[] = Status::WARNING;
76+
}
77+
78+
if ($input->getOption('fail-on-skip')) {
79+
$failureStatuses[] = Status::SKIP;
80+
}
81+
82+
if ($input->getOption('fail-on-unknown')) {
83+
$failureStatuses[] = Status::UNKNOWN;
84+
}
85+
86+
$isFail = $results->defects(...$failureStatuses)->count() > 0;
87+
88+
if ($isJsonOutput) {
89+
$output->write(\json_encode($results, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT, 512));
90+
91+
return $isFail ? self::FAILURE : self::SUCCESS;
92+
}
93+
6494
$io->section($input->getOption('suite') ? \sprintf('Running Check Suite "%s"', $suite) : 'Running All Checks');
6595

6696
if ($input->getOption('no-cache')) {
6797
$io->note('Running with cache disabled');
6898
}
6999

70-
$results = $suite->run(!$input->getOption('no-cache'));
71100
$warnings = $results->warnings();
72101
$unknowns = $results->unknowns();
73102
$summary = \array_filter([
@@ -79,21 +108,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
79108
$unknowns->count() ? \sprintf('%d unknown%s', $unknowns->count(), $unknowns->count() > 2 ? 's' : '') : null,
80109
]);
81110
$message = \sprintf('%d check executed (%s)', $results->count(), \implode(', ', $summary));
82-
$failureStatuses = [];
83-
84-
if ($input->getOption('fail-on-warning')) {
85-
$failureStatuses[] = Status::WARNING;
86-
}
87-
88-
if ($input->getOption('fail-on-skip')) {
89-
$failureStatuses[] = Status::SKIP;
90-
}
91-
92-
if ($input->getOption('fail-on-unknown')) {
93-
$failureStatuses[] = Status::UNKNOWN;
94-
}
95-
96-
$isFail = $results->defects(...$failureStatuses)->count() > 0;
97111

98112
$io->newLine();
99113
$io->{$isFail ? 'error' : 'success'}($message);

tests/LiipMonitorBundleTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,22 @@ public function execute_health_command(): void
9898
$logger->hasInfoThatContains('Health check "Check Service 1": Success');
9999
}
100100

101+
/**
102+
* @test
103+
* @group slow
104+
* @throws \JsonException
105+
*/
106+
public function execute_health_command_with_json_output(): void
107+
{
108+
$output = $this->executeConsoleCommand('monitor:health --json')
109+
->output()
110+
;
111+
112+
// Verify that the output is valid JSON
113+
$jsonData = \json_decode($output, true, 512, \JSON_THROW_ON_ERROR);
114+
$this->assertIsArray($jsonData);
115+
}
116+
101117
/**
102118
* @test
103119
* @group slow

0 commit comments

Comments
 (0)