From cf61ac96fbd410fb570f1b22b53487dba16939f2 Mon Sep 17 00:00:00 2001 From: Bram Leeda Date: Thu, 10 Nov 2022 17:13:18 +0100 Subject: [PATCH 1/3] Add responsetime/uptime/connection-count to memcache + redis Signed-off-by: Bram Leeda --- src/Check/Memcache.php | 23 +++++++++++++++++------ src/Check/Memcached.php | 20 ++++++++++++++++---- src/Check/Redis.php | 19 ++++++++++++++++--- 3 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/Check/Memcache.php b/src/Check/Memcache.php index 0157947..5b83e76 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,7 +44,7 @@ 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', + 'Invalid port number %s - expecting a positive integer', gettype($host) )); } @@ -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..abb95e2 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,7 +44,7 @@ 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', + 'Invalid port number %s - expecting a positive integer', gettype($host) )); } @@ -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"], + ] + ); } /** From 3ccaadd89a46e939be53f3b99598818343da939b Mon Sep 17 00:00:00 2001 From: Bram Leeda Date: Fri, 11 Nov 2022 12:03:27 +0100 Subject: [PATCH 2/3] Fix Memcache(d) port number validation error Added unittest for Memcache(d) constructor's host/port validation Had to update psalm-baseline because we are testing invalid input for $host Signed-off-by: Bram Leeda --- psalm-baseline.xml | 34 ++++++++++++++++------------------ src/Check/Memcache.php | 4 ++-- src/Check/Memcached.php | 4 ++-- test/MemcacheTest.php | 25 +++++++++++++++++++++++++ test/MemcachedTest.php | 25 +++++++++++++++++++++++++ 5 files changed, 70 insertions(+), 22 deletions(-) create mode 100644 test/MemcacheTest.php create mode 100644 test/MemcachedTest.php diff --git a/psalm-baseline.xml b/psalm-baseline.xml index 1448363..a516fbd 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -440,15 +440,6 @@ is_string($host) - - $memcache - $stats - - - addServer - connect - getExtendedStats - Memcache @@ -460,15 +451,6 @@ is_string($host) - - $memcached - $stats - - - addServer - getLastDisconnectedServer - getStats - Memcached @@ -1038,6 +1020,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 5b83e76..bb7bffb 100644 --- a/src/Check/Memcache.php +++ b/src/Check/Memcache.php @@ -44,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 %s - expecting a positive integer', - gettype($host) + 'Invalid port number %d - expecting a positive integer', + $port )); } diff --git a/src/Check/Memcached.php b/src/Check/Memcached.php index abb95e2..2d8659a 100644 --- a/src/Check/Memcached.php +++ b/src/Check/Memcached.php @@ -44,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 %s - expecting a positive integer', - gettype($host) + 'Invalid port number %d - expecting a positive integer', + $port )); } 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); + } +} From faac44df008e62f630a6a5841aad5ebcfe6e01af Mon Sep 17 00:00:00 2001 From: Bram Leeda Date: Fri, 11 Nov 2022 12:30:49 +0100 Subject: [PATCH 3/3] Update psalm-baseline, add extension baselines that were removed after locally installing the extensions Signed-off-by: Bram Leeda --- psalm-baseline.xml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/psalm-baseline.xml b/psalm-baseline.xml index a516fbd..a0a1ca2 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -440,6 +440,15 @@ is_string($host) + + $memcache + $stats + + + addServer + connect + getExtendedStats + Memcache @@ -451,6 +460,15 @@ is_string($host) + + $memcached + $stats + + + addServer + getLastDisconnectedServer + getStats + Memcached