Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Django runtime system check
When creating a Django website, you sometimes need a URL endpoint to monitor your database, cache, and storage systems. The django-watchman package provides runtime system checks that help you monitor your application's health in both development and production environments.
In this article, we'll create a URL endpoint that provides comprehensive system health checks for your Django application.
Installation
First, install the django-watchman package using pip ?
pip install django-watchman
Configuration
Adding to INSTALLED_APPS
In your settings.py file, add watchman to your installed apps ?
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
# Add watchman
'watchman',
]
URL Configuration
In your main urls.py file, include the watchman URLs ?
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('watchman/', include('watchman.urls')),
]
Available Endpoints
Django-watchman provides several endpoints for monitoring different aspects of your application ?
| Endpoint | Purpose | Response Format |
|---|---|---|
| /watchman/ | Basic health check | JSON |
| /watchman/dashboard/ | Web dashboard | HTML |
| /watchman/?check=databases | Database connectivity | JSON |
| /watchman/?check=caches | Cache system status | JSON |
Example Usage
Once configured, you can access the system dashboard. The dashboard provides a visual overview of your application's health status, including database connections, cache systems, and custom checks.
JSON Response Example
When you visit the basic endpoint, you'll receive a JSON response like this ?
{
"databases": [
{
"default": {
"ok": true
}
}
],
"caches": [
{
"default": {
"ok": true
}
}
]
}
Custom Health Checks
You can also create custom health checks by defining them in your settings.py ?
WATCHMAN_CHECKS = (
'watchman.checks.caches',
'watchman.checks.databases',
'myapp.checks.custom_check',
)
Conclusion
Django-watchman provides an easy way to monitor your application's health through simple URL endpoints. Use the dashboard for visual monitoring and JSON endpoints for programmatic health checks in production systems.
