Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add redis memory usage check #154

Merged
merged 1 commit into from
Feb 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions docs/available-checks/redis-memory-usage.md
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'),
```
2 changes: 1 addition & 1 deletion docs/available-checks/schedule.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Schedule
weight: 17
weight: 18
---

This check will make sure the schedule is running. If the check detects that the schedule is not run every minute, it will fail.
Expand Down
2 changes: 1 addition & 1 deletion docs/available-checks/security-advisories.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Security advisories
weight: 18
weight: 19
---

This check will check if the PHP packages installed in your project have known security vulnerabilities. This check works using [Packagist's security vulnerability API](https://php.watch/articles/composer-audit#packagist-vuln-list-api).
Expand Down
2 changes: 1 addition & 1 deletion docs/available-checks/used-disk-space.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
title: Used disk space
weight: 19
weight: 20
---

This check will monitor the percentage of available disk space.
Expand Down
50 changes: 50 additions & 0 deletions src/Checks/Checks/RedisMemoryUsageCheck.php
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);
}
}
30 changes: 30 additions & 0 deletions tests/Checks/RedisMemoryUsageTest.php
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.');
});
22 changes: 22 additions & 0 deletions tests/TestClasses/FakeRedisMemoryUsageCheck.php
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;
}
}