generated from spatie/package-skeleton-laravel
-
-
Notifications
You must be signed in to change notification settings - Fork 162
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 #154 from stfndamjanovic/redis-memory-usage
Add redis memory usage check
- Loading branch information
Showing
7 changed files
with
134 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
title: Redis memory usage | ||
weight: 17 | ||
--- | ||
|
||
This check makes sure that your Redis is not consuming too much memory. | ||
|
||
If the memory usage is larger than the specified maximum, this check will fail. | ||
|
||
## Usage | ||
|
||
Here's how you can register the check. | ||
|
||
```php | ||
use Spatie\Health\Facades\Health; | ||
use Spatie\Health\Checks\Checks\RedisMemoryUsageCheck; | ||
|
||
Health::checks([ | ||
RedisMemoryUsageCheck::new()->failWhenAboveMb(1000), | ||
]); | ||
``` | ||
|
||
### Using different connection | ||
|
||
To customize the monitored Redis connection name, call `connectionName`. | ||
|
||
```php | ||
RedisMemoryUsageCheck::new()->connectionName('other-redis-connection-name'), | ||
``` |
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
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,50 @@ | ||
<?php | ||
|
||
namespace Spatie\Health\Checks\Checks; | ||
|
||
use Illuminate\Support\Facades\Redis; | ||
use Spatie\Health\Checks\Check; | ||
use Spatie\Health\Checks\Result; | ||
|
||
class RedisMemoryUsageCheck extends Check | ||
{ | ||
protected string $connectionName = 'default'; | ||
|
||
protected float $failWhenAboveMb = 500; | ||
|
||
public function connectionName(string $connectionName): self | ||
{ | ||
$this->connectionName = $connectionName; | ||
|
||
return $this; | ||
} | ||
|
||
public function failWhenAboveMb(float $errorThresholdMb): self | ||
{ | ||
$this->failWhenAboveMb = $errorThresholdMb; | ||
|
||
return $this; | ||
} | ||
|
||
public function run(): Result | ||
{ | ||
$result = Result::make()->meta([ | ||
'connection_name' => $this->connectionName, | ||
]); | ||
|
||
$memoryUsage = $this->getMemoryUsageInMb(); | ||
|
||
if ($memoryUsage >= $this->failWhenAboveMb) { | ||
return $result->failed("Redis memory usage is {$memoryUsage} MB, which is above the threshold of {$this->failWhenAboveMb} MB."); | ||
} | ||
|
||
return $result->ok(); | ||
} | ||
|
||
protected function getMemoryUsageInMb(): float | ||
{ | ||
$memoryUsage = (int) Redis::connection($this->connectionName)->info()['Memory']['used_memory']; | ||
|
||
return round($memoryUsage / 1024 / 1024, 2); | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
namespace Spatie\Health\Tests\Checks; | ||
|
||
use Spatie\Health\Checks\Result; | ||
use Spatie\Health\Enums\Status; | ||
use Spatie\Health\Tests\TestClasses\FakeRedisMemoryUsageCheck; | ||
|
||
it('will return ok if the memory usage does not cross the threshold', function () { | ||
$result = FakeRedisMemoryUsageCheck::new() | ||
->fakeMemoryUsageInMb(999) | ||
->failWhenAboveMb(1000) | ||
->run(); | ||
|
||
expect($result) | ||
->toBeInstanceOf(Result::class) | ||
->status->toBe(Status::ok()); | ||
}); | ||
|
||
it('will return an error if the used memory does cross the threshold', function () { | ||
$result = FakeRedisMemoryUsageCheck::new() | ||
->fakeMemoryUsageInMb(1001) | ||
->failWhenAboveMb(1000) | ||
->run(); | ||
|
||
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.'); | ||
}); |
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,22 @@ | ||
<?php | ||
|
||
namespace Spatie\Health\Tests\TestClasses; | ||
|
||
use Spatie\Health\Checks\Checks\RedisMemoryUsageCheck; | ||
|
||
class FakeRedisMemoryUsageCheck extends RedisMemoryUsageCheck | ||
{ | ||
protected float $fakeMemoryUsageInMb; | ||
|
||
public function fakeMemoryUsageInMb(float $fakeMemoryUsageInMb): self | ||
{ | ||
$this->fakeMemoryUsageInMb = $fakeMemoryUsageInMb; | ||
|
||
return $this; | ||
} | ||
|
||
public function getMemoryUsageInMb(): float | ||
{ | ||
return $this->fakeMemoryUsageInMb; | ||
} | ||
} |