Skip to content

Commit

Permalink
Merge pull request #57 from bram123/add-redismemcache-server-status
Browse files Browse the repository at this point in the history
Add responsetime/uptime/connection-count to memcache + redis
  • Loading branch information
Ocramius authored Nov 11, 2022
2 parents 2e94f1a + faac44d commit 2d19d23
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 15 deletions.
16 changes: 16 additions & 0 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1038,6 +1038,22 @@
<code>$resultClass</code>
</PossiblyNullArgument>
</file>
<file src="test/MemcacheTest.php">
<InvalidArgument occurrences="1">
<code>['127.0.0.1']</code>
</InvalidArgument>
<InvalidCast occurrences="1">
<code>['127.0.0.1']</code>
</InvalidCast>
</file>
<file src="test/MemcachedTest.php">
<InvalidArgument occurrences="1">
<code>['127.0.0.1']</code>
</InvalidArgument>
<InvalidCast occurrences="1">
<code>['127.0.0.1']</code>
</InvalidCast>
</file>
<file src="test/OpCacheMemoryTest.php">
<InvalidScalarArgument occurrences="2">
<code>$criticalThreshold</code>
Expand Down
25 changes: 18 additions & 7 deletions src/Check/Memcache.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use function gettype;
use function is_array;
use function is_string;
use function microtime;
use function sprintf;

/**
Expand Down Expand Up @@ -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
));
}

Expand All @@ -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<string, false|array<string, int|string>> $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]
) {
Expand All @@ -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());
Expand All @@ -93,6 +104,6 @@ public function check()
'Memcache server running at host %s on port %s',
$this->host,
$this->port
));
), $serviceData);
}
}
22 changes: 17 additions & 5 deletions src/Check/Memcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use function class_exists;
use function gettype;
use function is_string;
use function microtime;
use function sprintf;

/**
Expand Down Expand Up @@ -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
));
}

Expand All @@ -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<string, false|array<string, int|string>> $stats */
$stats = @$memcached->getStats();
$responseTime = microtime(true) - $startTime;

$authority = sprintf('%s:%d', $this->host, $this->port);
$serviceData = null;

if (
! isset($stats[$authority])
Expand All @@ -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());
Expand All @@ -91,6 +103,6 @@ public function check()
'Memcached server running at host %s on port %s',
$this->host,
$this->port
));
), $serviceData);
}
}
19 changes: 16 additions & 3 deletions src/Check/Redis.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use RuntimeException;

use function class_exists;
use function microtime;

/**
* Validate that a Redis service is running
Expand Down Expand Up @@ -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"],
]
);
}

/**
Expand Down
25 changes: 25 additions & 0 deletions test/MemcacheTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace LaminasTest\Diagnostics;

use InvalidArgumentException;
use Laminas\Diagnostics\Check\Memcache;
use PHPUnit\Framework\TestCase;

/** @covers \Laminas\Diagnostics\Check\Memcache */
class MemcacheTest extends TestCase
{
public function testHostValidation(): void
{
$this->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);
}
}
25 changes: 25 additions & 0 deletions test/MemcachedTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace LaminasTest\Diagnostics;

use InvalidArgumentException;
use Laminas\Diagnostics\Check\Memcached;
use PHPUnit\Framework\TestCase;

/** @covers \Laminas\Diagnostics\Check\Memcached */
class MemcachedTest extends TestCase
{
public function testHostValidation(): void
{
$this->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);
}
}

0 comments on commit 2d19d23

Please sign in to comment.