-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
health.py
40 lines (33 loc) · 1.46 KB
/
health.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import itertools
import orjson
from django.http import HttpResponse
from django.utils.deprecation import MiddlewareMixin
from rest_framework.request import Request
class HealthCheck(MiddlewareMixin):
def process_request(self, request: Request):
# Our health check can't be a done as a view, because we need
# to bypass the ALLOWED_HOSTS check. We need to do this
# since not all load balancers can send the expected Host header
# which would cause a 400 BAD REQUEST, marking the node dead.
# Instead, we just intercept the request at this point, and return
# our success/failure immediately.
if request.path != "/_health/":
return
if "full" not in request.GET:
return HttpResponse("ok", content_type="text/plain")
from sentry.status_checks import Problem, check_all
threshold = Problem.threshold(Problem.SEVERITY_CRITICAL)
results = {
check: list(filter(threshold, problems)) for check, problems in check_all().items()
}
problems = list(itertools.chain.from_iterable(results.values()))
return HttpResponse(
orjson.dumps(
{
"problems": [str(p) for p in problems],
"healthy": {type(check).__name__: not p for check, p in results.items()},
}
),
content_type="application/json",
status=(500 if problems else 200),
)