diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 1448363..a0a1ca2 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -1038,6 +1038,22 @@ $resultClass + + + ['127.0.0.1'] + + + ['127.0.0.1'] + + + + + ['127.0.0.1'] + + + ['127.0.0.1'] + + $criticalThreshold diff --git a/src/Check/Memcache.php b/src/Check/Memcache.php index 0157947..bb7bffb 100644 --- a/src/Check/Memcache.php +++ b/src/Check/Memcache.php @@ -13,6 +13,7 @@ use function gettype; use function is_array; use function is_string; +use function microtime; use function sprintf; /** @@ -43,8 +44,8 @@ public function __construct($host = '127.0.0.1', $port = 11211) $port = (int) $port; if ($port < 1) { throw new InvalidArgumentException(sprintf( - 'Invalid port number - expecting a positive integer', - gettype($host) + 'Invalid port number %d - expecting a positive integer', + $port )); } @@ -66,13 +67,17 @@ public function check() try { $memcache = new MemcacheService(); $memcache->addServer($this->host, $this->port); - $stats = @$memcache->getExtendedStats(); - $authority = sprintf('%s:%d', $this->host, $this->port); + $startTime = microtime(true); + /** @var false|array> $stats */ + $stats = @$memcache->getExtendedStats(); + $responseTime = microtime(true) - $startTime; + + $authority = sprintf('%s:%d', $this->host, $this->port); + $serviceData = null; if ( - ! $stats - || ! is_array($stats) + ! is_array($stats) || ! isset($stats[$authority]) || false === $stats[$authority] ) { @@ -84,6 +89,12 @@ public function check() $this->port )); } + } else { + $serviceData = [ + "responseTime" => $responseTime, + "connections" => (int) $stats[$authority]['curr_connections'], + "uptime" => (int) $stats[$authority]['uptime'], + ]; } } catch (Exception $e) { return new Failure($e->getMessage()); @@ -93,6 +104,6 @@ public function check() 'Memcache server running at host %s on port %s', $this->host, $this->port - )); + ), $serviceData); } } diff --git a/src/Check/Memcached.php b/src/Check/Memcached.php index 97fd3e7..2d8659a 100644 --- a/src/Check/Memcached.php +++ b/src/Check/Memcached.php @@ -12,6 +12,7 @@ use function class_exists; use function gettype; use function is_string; +use function microtime; use function sprintf; /** @@ -43,8 +44,8 @@ public function __construct($host = '127.0.0.1', $port = 11211) $port = (int) $port; if ($port < 1) { throw new InvalidArgumentException(sprintf( - 'Invalid port number - expecting a positive integer', - gettype($host) + 'Invalid port number %d - expecting a positive integer', + $port )); } @@ -66,9 +67,14 @@ public function check() try { $memcached = new MemcachedService(); $memcached->addServer($this->host, $this->port); - $stats = @$memcached->getStats(); - $authority = sprintf('%s:%d', $this->host, $this->port); + $startTime = microtime(true); + /** @var false|array> $stats */ + $stats = @$memcached->getStats(); + $responseTime = microtime(true) - $startTime; + + $authority = sprintf('%s:%d', $this->host, $this->port); + $serviceData = null; if ( ! isset($stats[$authority]) @@ -82,6 +88,12 @@ public function check() $this->port )); } + } else { + $serviceData = [ + "responseTime" => $responseTime, + "connections" => (int) $stats[$authority]['curr_connections'], + "uptime" => (int) $stats[$authority]['uptime'], + ]; } } catch (Exception $e) { return new Failure($e->getMessage()); @@ -91,6 +103,6 @@ public function check() 'Memcached server running at host %s on port %s', $this->host, $this->port - )); + ), $serviceData); } } diff --git a/src/Check/Redis.php b/src/Check/Redis.php index c468f82..00a9839 100644 --- a/src/Check/Redis.php +++ b/src/Check/Redis.php @@ -9,6 +9,7 @@ use RuntimeException; use function class_exists; +use function microtime; /** * Validate that a Redis service is running @@ -45,9 +46,21 @@ public function __construct($host = 'localhost', $port = 6379, $auth = null) */ public function check() { - $this->createClient()->ping(); - - return new Success(); + $client = $this->createClient(); + + $startTime = microtime(true); + /** @var array $stats Redis client is not in `multi` mode, so methods will directly return there response */ + $stats = $client->info(); + $responseTime = microtime(true) - $startTime; + + return new Success( + '', + [ + "responseTime" => $responseTime, + "connections" => (int) $stats["connected_clients"], + "uptime" => (int) $stats["uptime_in_seconds"], + ] + ); } /** diff --git a/test/MemcacheTest.php b/test/MemcacheTest.php new file mode 100644 index 0000000..1ea3414 --- /dev/null +++ b/test/MemcacheTest.php @@ -0,0 +1,25 @@ +expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Cannot use array as host - expecting a string"); + new Memcache(['127.0.0.1']); + } + + public function testPortValidation(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Invalid port number -11211 - expecting a positive integer"); + new Memcache('127.0.0.1', -11211); + } +} diff --git a/test/MemcachedTest.php b/test/MemcachedTest.php new file mode 100644 index 0000000..174e396 --- /dev/null +++ b/test/MemcachedTest.php @@ -0,0 +1,25 @@ +expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Cannot use array as host - expecting a string"); + new Memcached(['127.0.0.1']); + } + + public function testPortValidation(): void + { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage("Invalid port number -11211 - expecting a positive integer"); + new Memcached('127.0.0.1', -11211); + } +}