Skip to content

Commit

Permalink
Merge pull request #239 from pelmered/feature/improve-redis-memory-us…
Browse files Browse the repository at this point in the history
…age-check

Improve redis memory usage check
  • Loading branch information
freekmurze authored Jul 26, 2024
2 parents 696dad4 + b5cb6e2 commit eeadb69
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 6 deletions.
4 changes: 3 additions & 1 deletion docs/available-checks/redis-memory-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ use Spatie\Health\Facades\Health;
use Spatie\Health\Checks\Checks\RedisMemoryUsageCheck;

Health::checks([
RedisMemoryUsageCheck::new()->failWhenAboveMb(1000),
RedisMemoryUsageCheck::new()
->warnWhenAboveMb(900)
->failWhenAboveMb(1000),
]);
```

Expand Down
29 changes: 26 additions & 3 deletions src/Checks/Checks/RedisMemoryUsageCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class RedisMemoryUsageCheck extends Check
{
protected string $connectionName = 'default';

protected ?float $warnWhenAboveMb = null;

protected float $failWhenAboveMb = 500;

public function connectionName(string $connectionName): self
Expand All @@ -21,6 +23,13 @@ public function connectionName(string $connectionName): self
return $this;
}

public function warnWhenAboveMb(float $errorThresholdMb): self
{
$this->warnWhenAboveMb = $errorThresholdMb;

return $this;
}

public function failWhenAboveMb(float $errorThresholdMb): self
{
$this->failWhenAboveMb = $errorThresholdMb;
Expand All @@ -30,14 +39,28 @@ public function failWhenAboveMb(float $errorThresholdMb): self

public function run(): Result
{
$result = Result::make()->meta([
$memoryUsage = $this->getMemoryUsageInMb();

$result = Result::make()->shortSummary("{$memoryUsage} MB used");

$result->meta([
'connection_name' => $this->connectionName,
'memory_usage' => $memoryUsage,
]);

$memoryUsage = $this->getMemoryUsageInMb();
$message = "Redis memory usage is {$memoryUsage} MB. The {threshold_type} threshold is {memory_threshold} MB.";

if ($memoryUsage >= $this->failWhenAboveMb) {
return $result->failed("Redis memory usage is {$memoryUsage} MB, which is above the threshold of {$this->failWhenAboveMb} MB.");
return $result->failed(strtr($message, [
'{threshold_type}' => 'fail',
'{memory_threshold}' => $this->failWhenAboveMb,
]));
}
if ($this->warnWhenAboveMb && $memoryUsage >= $this->warnWhenAboveMb) {
return $result->warning(strtr($message, [
'{threshold_type}' => 'warning',
'{memory_threshold}' => $this->warnWhenAboveMb,
]));
}

return $result->ok();
Expand Down
19 changes: 17 additions & 2 deletions tests/Checks/RedisMemoryUsageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
it('will return ok if the memory usage does not cross the threshold', function () {
$result = FakeRedisMemoryUsageCheck::new()
->fakeMemoryUsageInMb(999)
->failWhenAboveMb(1000)
->warnWhenAboveMb(1000)
->failWhenAboveMb(1100)
->run();

expect($result)
Expand All @@ -26,5 +27,19 @@
expect($result)
->toBeInstanceOf(Result::class)
->status->toEqual(Status::failed())
->getNotificationMessage()->toEqual('Redis memory usage is 1001 MB, which is above the threshold of 1000 MB.');
->getNotificationMessage()->toEqual('Redis memory usage is 1001 MB. The fail threshold is 1000 MB.');
});


it('will return a warning if the used memory does cross the threshold', function () {
$result = FakeRedisMemoryUsageCheck::new()
->fakeMemoryUsageInMb(700)
->warnWhenAboveMb(600)
->failWhenAboveMb(1000)
->run();

expect($result)
->toBeInstanceOf(Result::class)
->status->toEqual(Status::warning())
->getNotificationMessage()->toEqual('Redis memory usage is 700 MB. The warning threshold is 600 MB.');
});

0 comments on commit eeadb69

Please sign in to comment.