Skip to content

Commit b4e3e05

Browse files
authored
Post-cutover cleanups to the back-end (#286)
* add the django-health-check module and a healthz endpoint Signed-off-by: Irving Popovetsky <irving@popovetsky.com> * Add a custom_sampler to ignore all traces to the health check Signed-off-by: Irving Popovetsky <irving@popovetsky.com> * make sure to add honeycomb auto-instrumentation for dev environments or else youll have a bad day trying to troubleshoot Signed-off-by: Irving Popovetsky <irving@popovetsky.com> * Correct the ALLOWED_HOST values for prod and staging Signed-off-by: Irving Popovetsky <irving@popovetsky.com> * linter did its thing Signed-off-by: Irving Popovetsky <irving@popovetsky.com>
1 parent 4ce023b commit b4e3e05

8 files changed

Lines changed: 45 additions & 3 deletions

File tree

poetry.lock

Lines changed: 16 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ sentry-sdk = "^0.10.1"
3838
six = "^1.12"
3939
honeycomb-beeline = "^2.11.4"
4040
django-allow-cidr = "^0.3.1"
41+
django-health-check = "^3.12.1"
4142

4243
[tool.poetry.dev-dependencies]
4344
bandit = "^1.6"

src/gunicorn_config.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,21 @@ def worker_abort(worker):
218218
worker.log.info("worker received SIGABRT signal")
219219

220220

221+
def sampler(fields):
222+
request_path = fields.get("request.path")
223+
response_code = fields.get("response.status_code")
224+
225+
# never sample errors
226+
if response_code and response_code >= 500:
227+
return True, 1
228+
else:
229+
# never capture healthy health checks
230+
if request_path == "/healthz":
231+
return False, 0
232+
# catchall
233+
return True, 1
234+
235+
221236
# Added for Honeycomb instrumentation
222237
def post_worker_init(worker):
223238
worker.log.info("beeline initialization in process pid %s", worker.pid)
@@ -229,5 +244,7 @@ def post_worker_init(worker):
229244
beeline.init(
230245
writekey=os.getenv("HONEYCOMB_WRITEKEY"),
231246
dataset=os.getenv("HONEYCOMB_DATASET"),
247+
service_name="backend",
248+
sampler_hook=sampler,
232249
debug=False,
233250
)

src/operationcode_backend/urls.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
name="schema-swagger-ui",
3636
),
3737
path("redoc/", schema_view.with_ui("redoc", cache_timeout=0), name="schema-redoc"),
38+
path("healthz", include("health_check.urls")),
3839
]
3940

4041
##############################################

src/settings/components/base.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@
5757
# temp frontend apps
5858
"widget_tweaks",
5959
"snowpenguin.django.recaptcha2",
60+
# django-health-check
61+
# https://django-health-check.readthedocs.io/en/latest/
62+
"health_check", # required
63+
"health_check.db", # stock Django health checkers
6064
]
6165

6266
ROOT_URLCONF = "operationcode_backend.urls"

src/settings/environments/development.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,7 @@
1313
INSTALLED_APPS += ("debug_toolbar",)
1414
if "debug_toolbar.middleware.DebugToolbarMiddleware" not in MIDDLEWARE:
1515
MIDDLEWARE += ("debug_toolbar.middleware.DebugToolbarMiddleware",)
16+
17+
# Honeycomb beeline auto-instrumentation
18+
if "beeline.middleware.django.HoneyMiddleware" not in MIDDLEWARE: # noqa: F821
19+
MIDDLEWARE += ("beeline.middleware.django.HoneyMiddleware",) # noqa: F821

src/settings/environments/production.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from settings.components import config
44
from settings.components.base import DATABASES
55

6-
ALLOWED_HOSTS = ["operationcode.org", "pybot.operationcode.org"]
6+
ALLOWED_HOSTS = ["api.operationcode.org"]
77
DEBUG = False
88

99
if config("EXTRA_HOSTS", default=""):

src/settings/environments/staging.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from settings.components import config
44
from settings.components.base import DATABASES
55

6-
ALLOWED_HOSTS = ["operationcode.org", "api.staging.operationcode.org"]
6+
ALLOWED_HOSTS = ["api.staging.operationcode.org"]
77
DEBUG = False
88

99
if config("EXTRA_HOSTS", default=""):

0 commit comments

Comments
 (0)