Skip to content

Commit 056c552

Browse files
kartarismichaelyaakoby
authored andcommitted
adds AioRedisHealthProvider
1 parent 5f2439a commit 056c552

File tree

3 files changed

+80
-0
lines changed

3 files changed

+80
-0
lines changed

README.md

+10
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,16 @@ pyctuator = Pyctuator(...) # arguments removed for brevity
255255
pyctuator.register_health_provider(RedisHealthProvider(r))
256256
```
257257

258+
Or if your service is using asynchronous Redis, to monitor the connection initialize a `AioRedisHealthProvider`:
259+
260+
```python
261+
from redis import asyncio as aioredis
262+
263+
r = aioredis.Redis()
264+
pyctuator = Pyctuator(...) # arguments removed for brevity
265+
pyctuator.register_health_provider(AioRedisHealthProvider(r))
266+
```
267+
258268
### Custom Environment
259269
Out of the box, Pyctuator exposes Python's environment variables to Spring Boot Admin.
260270

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import asyncio
2+
3+
from pyctuator.health.health_provider import Status
4+
from pyctuator.health.redis_health_provider import RedisHealthStatus, RedisHealthDetails, RedisHealthProvider
5+
6+
7+
class AioRedisHealthProvider(RedisHealthProvider):
8+
def get_health(self) -> RedisHealthStatus:
9+
try:
10+
info = asyncio.run(self.redis.info())
11+
12+
return RedisHealthStatus(
13+
status=Status.UP,
14+
details=RedisHealthDetails(
15+
version=info["redis_version"],
16+
mode=info["redis_mode"],
17+
))
18+
except Exception as e: # pylint: disable=broad-except
19+
return RedisHealthStatus(
20+
status=Status.DOWN,
21+
details=RedisHealthDetails(
22+
failure=str(e)
23+
))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# pylint: disable=import-outside-toplevel
2+
3+
import importlib.util
4+
import os
5+
6+
import pytest
7+
8+
@pytest.fixture
9+
def require_redis() -> None:
10+
if not importlib.util.find_spec("redis"):
11+
pytest.skip("redis is missing, skipping")
12+
13+
14+
@pytest.mark.usefixtures("require_redis")
15+
@pytest.fixture
16+
def require_redis_server() -> None:
17+
should_test_with_redis = os.getenv("TEST_REDIS_SERVER", None)
18+
if not should_test_with_redis:
19+
pytest.skip("No Redis server (env TEST_REDIS_SERVER isn't True), skipping")
20+
21+
@pytest.fixture
22+
def redis_host() -> str:
23+
return os.getenv("REDIS_HOST", "localhost")
24+
25+
@pytest.mark.usefixtures("require_redis", "require_redis_server")
26+
def test_aioredis_health(redis_host: str) -> None:
27+
from redis.asyncio import Redis as AioRedis
28+
from pyctuator.health.health_provider import Status
29+
from pyctuator.health.aioredis_health_provider import AioRedisHealthProvider, RedisHealthStatus, RedisHealthDetails
30+
31+
redis_instance = AioRedis(host=redis_host)
32+
33+
health = AioRedisHealthProvider(redis_instance).get_health()
34+
assert health == RedisHealthStatus(Status.UP, RedisHealthDetails("5.0.3", "standalone"))
35+
36+
37+
@pytest.mark.usefixtures("require_redis", "require_redis_server")
38+
def test_aioredis_bad_password(redis_host: str) -> None:
39+
from redis.asyncio import Redis as AioRedis
40+
from pyctuator.health.health_provider import Status
41+
from pyctuator.health.aioredis_health_provider import AioRedisHealthProvider
42+
43+
redis_instance = AioRedis(host=redis_host, password="blabla")
44+
45+
health = AioRedisHealthProvider(redis_instance).get_health()
46+
assert health.status == Status.DOWN
47+
assert "Client sent AUTH, but no password is set" in str(health.details.failure)

0 commit comments

Comments
 (0)