-
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from bram123/add-elasticsearch-check
Add new ElasticSearch healthcheck
- Loading branch information
Showing
4 changed files
with
113 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?php | ||
|
||
namespace Laminas\Diagnostics\Check; | ||
|
||
use Exception; | ||
use GuzzleHttp\ClientInterface as GuzzleClientInterface; | ||
use Laminas\Diagnostics\Result\Failure; | ||
use Laminas\Diagnostics\Result\ResultInterface; | ||
use Laminas\Diagnostics\Result\Success; | ||
use Laminas\Diagnostics\Result\Warning; | ||
|
||
use function array_merge; | ||
use function microtime; | ||
use function trim; | ||
|
||
/** | ||
* Ensures a connection to ElasticSearch is possible and the cluster health is 'green' | ||
*/ | ||
class ElasticSearch extends GuzzleHttpService | ||
{ | ||
/** | ||
* @param array $headers An array of headers used to create the request | ||
* @param array $options An array of guzzle options used to create the request | ||
* @param null|GuzzleClientInterface $guzzle Instance of guzzle to use | ||
*/ | ||
public function __construct(string $elasticSearchUrl, array $headers = [], array $options = [], $guzzle = null) | ||
{ | ||
$elasticSearchUrl .= '/_cat/health?h=status'; | ||
|
||
parent::__construct($elasticSearchUrl, $headers, $options, 200, null, $guzzle); | ||
} | ||
|
||
public function check(): ResultInterface | ||
{ | ||
try { | ||
$startTime = microtime(true); | ||
$response = $this->guzzle->send($this->request, array_merge($this->options)); | ||
$health = trim((string) $response->getBody()); | ||
$responseTime = microtime(true) - $startTime; | ||
} catch (Exception $e) { | ||
return new Failure("Unable to connect to elasticsearch: " . $e->getMessage()); | ||
} | ||
|
||
$serviceData = [ | ||
"responseTime" => $responseTime, | ||
]; | ||
|
||
if ($health === 'green') { | ||
return new Success("Cluster status green", $serviceData); | ||
} | ||
|
||
if ($health === 'yellow') { | ||
return new Warning("Cluster status yellow", $serviceData); | ||
} | ||
|
||
return new Failure("Cluster status red", $serviceData); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace LaminasTest\Diagnostics; | ||
|
||
use Exception; | ||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Handler\MockHandler; | ||
use GuzzleHttp\Psr7\Response; | ||
use Laminas\Diagnostics\Check\ElasticSearch; | ||
use Laminas\Diagnostics\Result\FailureInterface; | ||
use Laminas\Diagnostics\Result\ResultInterface; | ||
use Laminas\Diagnostics\Result\SuccessInterface; | ||
use Laminas\Diagnostics\Result\WarningInterface; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** @coversDefaultClass \Laminas\Diagnostics\Check\ElasticSearch */ | ||
class ElasticSearchTest extends TestCase | ||
{ | ||
/** | ||
* @dataProvider healthStatusProvider | ||
* @param class-string<ResultInterface> $expectedResult | ||
* @throws Exception | ||
*/ | ||
public function testElasticSearch(string $clusterStatus, string $expectedResult): void | ||
{ | ||
$mockHandler = new MockHandler([new Response(200, [], $clusterStatus)]); | ||
$mockClient = new Client(['handler' => $mockHandler]); | ||
$check = new ElasticSearch('localhost:9200', [], [], $mockClient); | ||
|
||
// Assert the ElasticSearch check converts the API response to the correct ResultInterface implementation | ||
$checkResult = $check->check(); | ||
static::assertInstanceOf($expectedResult, $checkResult); | ||
|
||
// Assert ElasticSearch check returns extra data | ||
$resultData = $checkResult->getData(); | ||
static::assertIsArray($resultData); | ||
static::assertArrayHasKey('responseTime', $resultData); | ||
static::assertIsNumeric($resultData['responseTime']); | ||
} | ||
|
||
/** | ||
* @return array{array{string, class-string<ResultInterface>}} | ||
*/ | ||
public function healthStatusProvider(): array | ||
{ | ||
return [ | ||
["green\n", SuccessInterface::class], | ||
["yellow\n", WarningInterface::class], | ||
["red\n", FailureInterface::class], | ||
["unknown\n", FailureInterface::class], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters