-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #174 from stuartmaxwell:healthcheck-app
Healthcheck-app
- Loading branch information
Showing
8 changed files
with
105 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
"""healthcheck_app package.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
"""App configuration for healthcheck_app.""" | ||
|
||
from django.apps import AppConfig | ||
|
||
|
||
class HealthcheckAppConfig(AppConfig): | ||
"""App configuration for the healthcheck app.""" | ||
|
||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "healthcheck_app" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
"""Health checks for the healthcheck_app.""" | ||
|
||
import logging | ||
|
||
from django.db import connections | ||
from django.db.utils import OperationalError | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def check_database() -> tuple[str, str | None]: | ||
"""Perform the database connectivity check. | ||
Returns a tuple of the status ('healthy' or 'unhealthy') and an optional error | ||
message. | ||
""" | ||
try: | ||
connections["default"].cursor() # You could use a specific database alias if needed | ||
except OperationalError as e: | ||
logger.debug(f"Health check found an error with the database: {e!s}") | ||
return "unhealthy", "Error connecting to the database." | ||
else: | ||
return "healthy", None |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"""URLs for the healthcheck_app.""" | ||
|
||
from django.conf import settings | ||
from django.urls import path | ||
|
||
from .views import health_check | ||
|
||
app_name = "healthcheck_app" | ||
|
||
url_path = f"{settings.HEALTHCHECK_PATH}/" if settings.HEALTHCHECK_PATH else "" | ||
|
||
urlpatterns = [ | ||
path(url_path, health_check, name="healthcheck"), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
"""Views for the healthcheck_app.""" | ||
|
||
import logging | ||
|
||
from django.http import HttpRequest, JsonResponse | ||
from django.utils import timezone | ||
|
||
from healthcheck_app.checks import check_database | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
# Checks to perform, with their corresponding check functions | ||
checks = { | ||
"database": check_database, | ||
} | ||
|
||
|
||
def health_check(_: HttpRequest) -> JsonResponse: | ||
"""Main function that returns the healthcheck. | ||
Perform a health check on the application by checking various components. | ||
Returns a JsonResponse with the health information of the components. | ||
""" | ||
# Initial health data structure | ||
health_data = { | ||
"status": "healthy", # Starts assuming everything is healthy | ||
"timestamp": timezone.now().isoformat(), | ||
"details": {}, | ||
} | ||
|
||
# Iterate over the checks and perform them | ||
for check_name, check_function in checks.items(): | ||
status, error = check_function() | ||
|
||
# Update the component's status in the health data | ||
health_data["details"][check_name] = {"status": status} | ||
|
||
# If there's an error, add it to the component's data | ||
if error: | ||
health_data["details"][check_name]["error"] = error | ||
|
||
# If any component is unhealthy, set overall status to 'unhealthy' | ||
if status == "unhealthy": | ||
health_data["status"] = "unhealthy" | ||
|
||
# Return the JsonResponse with the health data and status code | ||
return JsonResponse(health_data) |