Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ sentry-sdk = "^0.10.1"
six = "^1.12"
honeycomb-beeline = "^2.11.4"
django-allow-cidr = "^0.3.1"
django-health-check = "^3.12.1"

[tool.poetry.dev-dependencies]
bandit = "^1.6"
Expand Down
17 changes: 17 additions & 0 deletions src/gunicorn_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,21 @@ def worker_abort(worker):
worker.log.info("worker received SIGABRT signal")


def sampler(fields):
request_path = fields.get("request.path")
response_code = fields.get("response.status_code")

# never sample errors
if response_code and response_code >= 500:
Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would it be better python to instead say:

    if response_code is not None and response_code >= 500:

?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eh, I think it's fine.

return True, 1
else:
# never capture healthy health checks
if request_path == "/healthz":
return False, 0
# catchall
return True, 1


# Added for Honeycomb instrumentation
def post_worker_init(worker):
worker.log.info("beeline initialization in process pid %s", worker.pid)
Expand All @@ -229,5 +244,7 @@ def post_worker_init(worker):
beeline.init(
writekey=os.getenv("HONEYCOMB_WRITEKEY"),
dataset=os.getenv("HONEYCOMB_DATASET"),
service_name="backend",
sampler_hook=sampler,
debug=False,
)
1 change: 1 addition & 0 deletions src/operationcode_backend/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
name="schema-swagger-ui",
),
path("redoc/", schema_view.with_ui("redoc", cache_timeout=0), name="schema-redoc"),
path("healthz", include("health_check.urls")),
]

##############################################
Expand Down
4 changes: 4 additions & 0 deletions src/settings/components/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@
# temp frontend apps
"widget_tweaks",
"snowpenguin.django.recaptcha2",
# django-health-check
# https://django-health-check.readthedocs.io/en/latest/
"health_check", # required
"health_check.db", # stock Django health checkers
]

ROOT_URLCONF = "operationcode_backend.urls"
Expand Down
4 changes: 4 additions & 0 deletions src/settings/environments/development.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@
INSTALLED_APPS += ("debug_toolbar",)
if "debug_toolbar.middleware.DebugToolbarMiddleware" not in MIDDLEWARE:
MIDDLEWARE += ("debug_toolbar.middleware.DebugToolbarMiddleware",)

# Honeycomb beeline auto-instrumentation
if "beeline.middleware.django.HoneyMiddleware" not in MIDDLEWARE: # noqa: F821
MIDDLEWARE += ("beeline.middleware.django.HoneyMiddleware",) # noqa: F821
2 changes: 1 addition & 1 deletion src/settings/environments/production.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from settings.components import config
from settings.components.base import DATABASES

ALLOWED_HOSTS = ["operationcode.org", "pybot.operationcode.org"]
ALLOWED_HOSTS = ["api.operationcode.org"]
DEBUG = False

if config("EXTRA_HOSTS", default=""):
Expand Down
2 changes: 1 addition & 1 deletion src/settings/environments/staging.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from settings.components import config
from settings.components.base import DATABASES

ALLOWED_HOSTS = ["operationcode.org", "api.staging.operationcode.org"]
ALLOWED_HOSTS = ["api.staging.operationcode.org"]
DEBUG = False

if config("EXTRA_HOSTS", default=""):
Expand Down