Skip to content

Commit

Permalink
IDPF-92 Added Logging & Deployment Fixes (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
0RWilliams authored Nov 18, 2024
1 parent 905da4b commit ac9af23
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Procfile
Original file line number Diff line number Diff line change
@@ -1 +1 @@
web: python manage.py check --deploy && python manage.py migrate --noinput && python manage.py clearcache && granian --interface wsgi config.wsgi:application --host 0.0.0.0 --port $PORT
web: python manage.py check --deploy && python manage.py migrate --noinput && granian --interface wsgi config.wsgi:application --host 0.0.0.0 --port $PORT
41 changes: 41 additions & 0 deletions config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
https://docs.djangoproject.com/en/5.1/ref/settings/
"""

import os
import sys
from pathlib import Path
from typing import Any

Expand Down Expand Up @@ -60,6 +62,7 @@
"django.contrib.staticfiles",
"django.contrib.postgres",
"core.apps.CoreConfig",
"pingdom.apps.PingdomConfig",
]

MIDDLEWARE: list[str] = [
Expand Down Expand Up @@ -88,6 +91,44 @@
},
]

LOGGING = {
"version": 1,
"disable_existing_loggers": False,
"handlers": {
"stdout": {
"class": "logging.StreamHandler",
"stream": sys.stdout,
},
},
"root": {
"handlers": ["stdout"],
"level": os.getenv("ROOT_LOG_LEVEL", "INFO"),
},
"loggers": {
"django": {
"handlers": [
"stdout",
],
"level": os.getenv("DJANGO_LOG_LEVEL", "INFO"),
"propagate": True,
},
"django.request": {
"handlers": [
"stdout",
],
"level": os.getenv("DJANGO_LOG_LEVEL", "INFO"),
"propagate": True,
},
"requestlogs": {
"handlers": [
"stdout",
],
"level": os.getenv("DJANGO_LOG_LEVEL", "INFO"),
"propagate": False,
},
},
}

# Sentry
# https://docs.sentry.io/platforms/python/integrations/django/
SENTRY_DSN: str = env.str("SENTRY_DSN")
Expand Down
1 change: 1 addition & 0 deletions config/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@

urlpatterns = [
path("", include("core.urls")),
path("pingdom/", include("pingdom.urls")),
path("admin/", admin.site.urls),
]
4 changes: 4 additions & 0 deletions package-lock.json

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

Empty file added pingdom/__init__.py
Empty file.
6 changes: 6 additions & 0 deletions pingdom/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class PingdomConfig(AppConfig):
name = "pingdom"
verbose_name = "Pingdom config"
17 changes: 17 additions & 0 deletions pingdom/services.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from django.contrib.auth import get_user_model
from django.db import DatabaseError


class CheckDatabase:
name = "database"

def check(self):
try:
user = get_user_model()
user.objects.all().exists()
return True, ""
except DatabaseError as e:
return False, e


services_to_check = (CheckDatabase,)
12 changes: 12 additions & 0 deletions pingdom/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from django.shortcuts import reverse
from django.test import TestCase


class PingdomTest(TestCase):
def test_ping_response(self):
url = reverse("pingdom")
response = self.client.get(url)

self.assertEqual(response.status_code, 200)
assert "<status>OK</status>" in str(response.content)
assert response.headers["content-type"] == "text/xml"
8 changes: 8 additions & 0 deletions pingdom/urls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.urls import path

from .views import pingdom


urlpatterns = [
path("ping.xml", pingdom, name="pingdom"),
]
27 changes: 27 additions & 0 deletions pingdom/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from django.http import HttpResponse

from .services import services_to_check


PINGDOM_TEMPLATE = """<?xml version="1.0" encoding="UTF-8"?>
<pingdom_http_custom_check>
<status>{status}</status>
</pingdom_http_custom_check>\n"""

COMMENT_TEMPLATE = "<!--{comment}-->\n"


def pingdom(request):
checked = {}
for service in services_to_check:
checked[service.name] = service().check()

if all(item[0] for item in checked.values()):
return HttpResponse(
PINGDOM_TEMPLATE.format(status="OK"), content_type="text/xml"
)
else:
body = PINGDOM_TEMPLATE.format(status="FALSE")
for service_result in filter(lambda x: x[0] is False, checked.values()):
body += COMMENT_TEMPLATE.format(comment=service_result[1])
return HttpResponse(body, status=500, content_type="text/xml")
3 changes: 1 addition & 2 deletions poetry.lock

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

0 comments on commit ac9af23

Please sign in to comment.