Skip to content

Commit

Permalink
Merge pull request #55 from awarrenlove/degraded-status
Browse files Browse the repository at this point in the history
Support DEGRADED status in HealthChecks
  • Loading branch information
Gman98ish authored May 17, 2021
2 parents 35fe6de + 56af564 commit b6d5f26
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/Controllers/HealthCheckController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Container\Container;
use Illuminate\Http\Response;
use Illuminate\Support\Collection;
use UKFast\HealthCheck\Status;

class HealthCheckController
{
Expand All @@ -19,16 +20,20 @@ public function __invoke(Container $container)
return $check->status();
});

$isOkay = $statuses->filter(function ($status) {
$isProblem = $statuses->contains(function ($status) {
return $status->isProblem();
})->isEmpty();
});

$isDegraded = $statuses->contains(function ($status) {
return $status->isDegraded();
});

$body = ['status' => $isOkay ? 'OK' : 'PROBLEM'];
$body = ['status' => ($isProblem ? Status::PROBLEM : ($isDegraded ? Status::DEGRADED : Status::OKAY))];
foreach ($statuses as $status) {
$body[$status->name()] = [];
$body[$status->name()]['status'] = $status->isOkay() ? 'OK' : 'PROBLEM';
$body[$status->name()]['status'] = $status->getStatus();

if ($status->isProblem()) {
if (!$status->isOkay()) {
$body[$status->name()]['message'] = $status->message();
}

Expand All @@ -37,6 +42,6 @@ public function __invoke(Container $container)
}
}

return new Response($body, $isOkay ? 200 : 500);
return new Response($body, $isProblem ? 500 : 200);
}
}
8 changes: 8 additions & 0 deletions src/HealthCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ public function problem($message = '', $context = [])
->withName($this->name());
}

public function degraded($message = '', $context = [])
{
return (new Status)
->degraded($message)
->withContext($context)
->withName($this->name());
}

public function okay($context = [])
{
return (new Status)
Expand Down
35 changes: 35 additions & 0 deletions src/Status.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ class Status
{
const PROBLEM = 'PROBLEM';

const DEGRADED = 'DEGRADED';

const OKAY = 'OK';

/** @var string */
Expand All @@ -30,6 +32,19 @@ public function problem($message = '')
return $this;
}

/**
* Marks the status as degraded
*
* @param string? $message Degraded message
* @return self
*/
public function degraded($message = '')
{
$this->status = Status::DEGRADED;
$this->message = $message;
return $this;
}

/**
* Marks status as okay
*
Expand All @@ -41,6 +56,16 @@ public function okay()
return $this;
}

/**
* Returns the status string
*
* @return string|null
*/
public function getStatus()
{
return $this->status;
}

/**
* Returns if the status is a problem
*
Expand All @@ -51,6 +76,16 @@ public function isProblem()
return $this->status == Status::PROBLEM;
}

/**
* Returns if the status is degraded
*
* @return bool
*/
public function isDegraded()
{
return $this->status == Status::DEGRADED;
}

/**
* Returns if the status is okay
*
Expand Down
57 changes: 57 additions & 0 deletions tests/Controllers/HealthCheckControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Controllers;

use Illuminate\Http\Response;
use Tests\TestCase;
use UKFast\HealthCheck\Controllers\HealthCheckController;
use UKFast\HealthCheck\HealthCheck;
Expand All @@ -27,6 +28,26 @@ public function returns_overall_status_of_okay_when_everything_is_up()
], json_decode($response->getContent(), true));
}

/**
* @test
*/
public function returns_degraded_status_with_response_code_200_when_service_is_degraded()
{
$this->setChecks([AlwaysUpCheck::class, AlwaysDegradedCheck::class]);
$response = (new HealthCheckController())->__invoke($this->app);

$this->assertSame(Response::HTTP_OK, $response->getStatusCode());
$this->assertSame([
'status' => 'DEGRADED',
'always-up' => ['status' => 'OK'],
'always-degraded' => [
'status' => 'DEGRADED',
'message' => 'Something went wrong',
'context' => ['debug' => 'info'],
],
], json_decode($response->getContent(), true));
}

/**
* @test
*/
Expand All @@ -46,6 +67,30 @@ public function returns_status_of_problem_when_a_problem_occurs()
], json_decode($response->getContent(), true));
}

/**
* @test
*/
public function returns_status_of_problem_when_both_degraded_and_problem_statuses_occur()
{
$this->setChecks([AlwaysUpCheck::class, AlwaysDegradedCheck::class, AlwaysDownCheck::class]);
$response = (new HealthCheckController)->__invoke($this->app);

$this->assertSame([
'status' => 'PROBLEM',
'always-up' => ['status' => 'OK'],
'always-degraded' => [
'status' => 'DEGRADED',
'message' => 'Something went wrong',
'context' => ['debug' => 'info'],
],
'always-down' => [
'status' => 'PROBLEM',
'message' => 'Something went wrong',
'context' => ['debug' => 'info'],
],
], json_decode($response->getContent(), true));
}

protected function setChecks($checks)
{
config(['healthcheck.checks' => $checks]);
Expand All @@ -62,6 +107,18 @@ public function status()
}
}

class AlwaysDegradedCheck extends HealthCheck
{
protected $name = 'always-degraded';

public function status()
{
return $this->degraded('Something went wrong', [
'debug' => 'info',
]);
}
}

class AlwaysDownCheck extends HealthCheck
{
protected $name = 'always-down';
Expand Down

0 comments on commit b6d5f26

Please sign in to comment.