Skip to content

Commit 491f4c0

Browse files
committed
CommonHealth Metric Impl. / CLI command json output
1 parent 90f0833 commit 491f4c0

File tree

4 files changed

+50
-6
lines changed

4 files changed

+50
-6
lines changed

health_check/backends.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,13 @@ def add_error(self, error, cause=None):
7777
else:
7878
msg = _("unknown error")
7979
error = HealthCheckException(msg)
80-
if isinstance(cause, BaseException):
81-
logger.exception(str(error))
82-
else:
83-
logger.error(str(error))
80+
81+
if HEALTH_CHECK['VERBOSE']:
82+
if isinstance(cause, BaseException):
83+
logger.exception(str(error))
84+
else:
85+
logger.error(str(error))
86+
8487
self.errors.append(error)
8588

8689
def pretty_status(self):
@@ -94,3 +97,10 @@ def status(self):
9497

9598
def identifier(self):
9699
return self.__class__.__name__
100+
101+
102+
class CommonHealth(BaseHealthCheckBackend):
103+
104+
def check_status(self):
105+
if self.errors:
106+
raise HealthCheckException("Found errors!")

health_check/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
HEALTH_CHECK.setdefault('WARNINGS_AS_ERRORS', True)
77
HEALTH_CHECK.setdefault('USE_PROMETHEUS', False)
88
HEALTH_CHECK.setdefault('PROMETHEUS_METRIC_NAMESPACE', 'app')
9+
HEALTH_CHECK.setdefault('VERBOSE', True)
Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
11
import sys
2+
import json
23

34
from django.core.management.base import BaseCommand
45

6+
from health_check.conf import HEALTH_CHECK
57
from health_check.mixins import CheckMixin
68

79

810
class Command(CheckMixin, BaseCommand):
911
help = "Run health checks and exit 0 if everything went well."
1012

13+
def add_arguments(self, parser):
14+
parser.add_argument("--json-output", action="store_true", required=False)
15+
parser.add_argument("--verbose", action="store_true", required=False)
16+
1117
def handle(self, *args, **options):
18+
if not options["verbose"]:
19+
HEALTH_CHECK["VERBOSE"] = False
20+
1221
# perform all checks
1322
errors = self.errors
23+
if options["json_output"]:
24+
25+
self.json_output()
26+
else:
27+
self.plain_output()
28+
if errors:
29+
sys.exit(1)
1430

31+
def plain_output(self):
1532
for plugin in self.plugins:
1633
style_func = self.style.SUCCESS if not plugin.errors else self.style.ERROR
1734
self.stdout.write(
@@ -21,5 +38,9 @@ def handle(self, *args, **options):
2138
)
2239
)
2340

24-
if errors:
25-
sys.exit(1)
41+
def json_output(self):
42+
metrics = {
43+
p.identifier(): p.status
44+
for p in self.plugins
45+
}
46+
self.stdout.write(json.dumps(metrics))

health_check/mixins.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import copy
22
from concurrent.futures import ThreadPoolExecutor
33

4+
from health_check.backends import CommonHealth
45
from health_check.conf import HEALTH_CHECK
56
from health_check.exceptions import ServiceWarning
67
from health_check.plugins import plugin_dir
@@ -9,6 +10,7 @@
910
class CheckMixin:
1011
_errors = None
1112
_plugins = None
13+
_common_health = None
1214

1315
@property
1416
def errors(self):
@@ -25,6 +27,13 @@ def plugins(self):
2527
), key=lambda plugin: plugin.identifier())
2628
return self._plugins
2729

30+
@property
31+
def common_health(self):
32+
if not self._common_health:
33+
self._common_health = CommonHealth()
34+
self._plugins.append(self._common_health)
35+
return self._common_health
36+
2837
def run_check(self):
2938
errors = []
3039

@@ -47,4 +56,7 @@ def _run(plugin):
4756
else:
4857
errors.extend(plugin.errors)
4958

59+
self.common_health.errors = errors
60+
_run(self.common_health)
61+
5062
return errors

0 commit comments

Comments
 (0)