Skip to content

Commit 4da1fd9

Browse files
authored
Add ability to define and run a subset health checks (#390)
* feat: add ability to define health check subsets * feat: adjust default health check to conform with new structure * chore: add usage instruction in readme * feat: allow subset argument to be specify using Django Command * feat: fix broken tests with missing subset argument * feat: move getattr from django settings to conf * feat: remove unused variable * feat: add tests * feat: add subset not found should return HTTP 404 * feat: ensure plugins are sorted by name * feat: ensure plugins are sorted by name * feat: adjust health check config structure * feat: add more tests * feat: add commmand tests * feat: fix merge conflicts * feat: fix merge conflicts
1 parent 7328eaa commit 4da1fd9

File tree

20 files changed

+215
-54
lines changed

20 files changed

+215
-54
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,24 @@ one of these checks, set its value to `None`.
9090
}
9191
```
9292

93+
To use Health Check Subsets, Specify a subset name and associate it with the relevant health check services to utilize Health Check Subsets.
94+
```python
95+
HEALTH_CHECK = {
96+
# .....
97+
"SUBSETS": {
98+
"startup-probe": ["MigrationsHealthCheck", "DatabaseBackend"],
99+
"liveness-probe": ["DatabaseBackend"],
100+
"<SUBSET_NAME>": ["<Health_Check_Service_Name"]
101+
},
102+
# .....
103+
}
104+
```
105+
106+
To only execute specific subset of health check
107+
```shell
108+
curl -X GET -H "Accept: application/json" http://www.example.com/ht/startup-probe/
109+
```
110+
93111
If using the DB check, run migrations:
94112

95113
```shell

health_check/backends.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@ class BaseHealthCheckBackend:
2020
def __init__(self):
2121
self.errors = []
2222

23-
def check_status(self):
23+
def check_status(self, subset=None):
2424
raise NotImplementedError
2525

26-
def run_check(self):
26+
def run_check(self, subset=None):
2727
start = timer()
2828
self.errors = []
2929
try:
30-
self.check_status()
30+
self.check_status(subset=subset)
3131
except HealthCheckException as e:
3232
self.add_error(e, e)
3333
except BaseException:

health_check/cache/backends.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def __init__(self, backend="default"):
2828
def identifier(self):
2929
return f"Cache backend: {self.backend}"
3030

31-
def check_status(self):
31+
def check_status(self, subset=None):
3232
cache = caches[self.backend]
3333

3434
try:

health_check/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@
44
HEALTH_CHECK.setdefault("DISK_USAGE_MAX", 90)
55
HEALTH_CHECK.setdefault("MEMORY_MIN", 100)
66
HEALTH_CHECK.setdefault("WARNINGS_AS_ERRORS", True)
7+
HEALTH_CHECK.setdefault("SUBSETS", {})
78
HEALTH_CHECK.setdefault("DISABLE_THREADING", False)

health_check/contrib/celery/backends.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99

1010
class CeleryHealthCheck(BaseHealthCheckBackend):
11-
def check_status(self):
11+
def check_status(self, subset=None):
1212
timeout = getattr(settings, "HEALTHCHECK_CELERY_TIMEOUT", 3)
1313
result_timeout = getattr(settings, "HEALTHCHECK_CELERY_RESULT_TIMEOUT", timeout)
1414
queue_timeout = getattr(settings, "HEALTHCHECK_CELERY_QUEUE_TIMEOUT", timeout)

health_check/contrib/celery_ping/backends.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
class CeleryPingHealthCheck(BaseHealthCheckBackend):
99
CORRECT_PING_RESPONSE = {"ok": "pong"}
1010

11-
def check_status(self):
11+
def check_status(self, subset=None):
1212
timeout = getattr(settings, "HEALTHCHECK_CELERY_PING_TIMEOUT", 1)
1313

1414
try:

health_check/contrib/migrations/backends.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class MigrationsHealthCheck(BaseHealthCheckBackend):
1414
def get_migration_plan(self, executor):
1515
return executor.migration_plan(executor.loader.graph.leaf_nodes())
1616

17-
def check_status(self):
17+
def check_status(self, subset=None):
1818
db_alias = getattr(settings, "HEALTHCHECK_MIGRATIONS_DB", DEFAULT_DB_ALIAS)
1919
try:
2020
executor = MigrationExecutor(connections[db_alias])

health_check/contrib/psutil/backends.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
class DiskUsage(BaseHealthCheckBackend):
17-
def check_status(self):
17+
def check_status(self, subset=None):
1818
try:
1919
du = psutil.disk_usage("/")
2020
if DISK_USAGE_MAX and du.percent >= DISK_USAGE_MAX:
@@ -28,7 +28,7 @@ def check_status(self):
2828

2929

3030
class MemoryUsage(BaseHealthCheckBackend):
31-
def check_status(self):
31+
def check_status(self, subset=None):
3232
try:
3333
memory = psutil.virtual_memory()
3434
if MEMORY_MIN and memory.available < (MEMORY_MIN * 1024 * 1024):

health_check/contrib/rabbitmq/backends.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class RabbitMQHealthCheck(BaseHealthCheckBackend):
1515

1616
namespace = None
1717

18-
def check_status(self):
18+
def check_status(self, subset=None):
1919
"""Check RabbitMQ service by opening and closing a broker channel."""
2020
logger.debug("Checking for a broker_url on django settings...")
2121

health_check/contrib/redis/backends.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class RedisHealthCheck(BaseHealthCheckBackend):
1414

1515
redis_url = getattr(settings, "REDIS_URL", "redis://localhost/1")
1616

17-
def check_status(self):
17+
def check_status(self, subset=None):
1818
"""Check Redis service by pinging the redis instance with a redis connection."""
1919
logger.debug("Got %s as the redis_url. Connecting to redis...", self.redis_url)
2020

0 commit comments

Comments
 (0)