A Django package to track requests with optional Redis and Celery support.
Project description
django-request-track
Overview
'django-request-track' is a powerful Django package designed to capture and store HTTP requests essential information, including IP address, user model, user agent, HTTP method, timestamp, and more. It supports both synchronous and asynchronous operations, providing flexible logging options with Redis buffer and Celery integration for efficient request tracking.
Features
- Track HTTP requests with detailed information
- Support for both sync and async operations
- Redis buffer with Celery integration for efficient logging
- Customizable request sampling
- Flexible user logging modes
- IP address tracking with optional separate model
- Customizable header logging
- Support for Django 5.0+
Installation
Install the package using pip:
pip install django-request-track
Quick Start
-
Add request_track to your INSTALLED_APPS in your Django project's settings:
INSTALLED_APPS = [ # ... 'request_track', # ... ]
-
Register the middleware in your MIDDLEWARE settings:
MIDDLEWARE = [ # ... 'request_track.middleware.LoggingRequestMiddleware' ]
-
Run migrations:
python manage.py migrate
Configuration
You can customize the behavior of django-request-track through your Django settings:
REQUEST_TRACK_SETTINGS = {
# Specify which HTTP headers to log
"HEADERS_TO_LOG": ["sec-ch-ua-platform"],
# Paths that should always be logged (ignoring sampling)
"FORCE_PATHS": ["/admin", "/critical-action"],
# Paths to exclude from logging
"EXCLUDE_PATHS": ["/admin/jsi18n/"],
# User logging mode: 'all', 'authenticated', or 'anonymous'
"USER_LOGGING_MODE": "all",
# Sampling rate (0: no logging, 1: log all, 0.5: log 50% randomly)
"SAMPLING_RATE": 1,
# Whether FORCE_PATHS should respect sampling rate
"FORCE_PATHS_SAMPLING": False,
# Store IP addresses in a separate model
"USE_IP_ADDRESS_MODEL": True,
# Use Redis as a buffer for logging (recommended for production)
"USE_REDIS_BUFFER": False,
# Redis key for storing logs (required if USE_REDIS_BUFFER is True)
"REDIS_KEY": "req_logs",
# Redis connection URL (required if USE_REDIS_BUFFER is True)
"REDIS_URL": "redis://localhost:6379/2",
}
Usage Examples
Basic Usage
The package will automatically start tracking requests once installed and configured. You can access the logs through:
- Django Admin Interface (with built-in filtering and search)
- RequestLog model:
from request_track.models import RequestLog
# Get all logs
logs = RequestLog.objects.all()
# Get logs for a specific user
user_logs = RequestLog.objects.filter(user=user)
# Get logs for a specific IP
ip_logs = RequestLog.objects.filter(ip_address='192.168.1.1')
Using Redis Buffer with Celery
For production environments, it's recommended to use Redis as a buffer with Celery for batch processing:
REQUEST_TRACK_SETTINGS = {
"USE_REDIS_BUFFER": True,
"REDIS_KEY": "req_logs",
"REDIS_URL": "redis://localhost:6379/2",
}
This configuration will:
- Store logs temporarily in Redis
- Process logs in batches using Celery
- Reduce database load
- Improve application performance
Setting Up Celery Beat for Periodic Log Processing
To enable periodic processing of buffered logs, you need to configure Celery Beat. This allows request_track.tasks.process_request_logs to run at fixed intervals.
Requirements
Install the following packages:
pip install celery redis msgpack
Configuration
In your project's celery.py file, add the following:
from datetime import timedelta
app.conf.beat_schedule = {
'flush-request-logs-every-interval': {
'task': 'request_track.tasks.process_request_logs',
'schedule': timedelta(seconds=10), # adjust as needed
},
}
Make sure the task name is exactly as shown above.
Running Celery and Celery Beat
You must start both the Celery worker and the beat scheduler:
# Start the Celery worker
celery -A your_project_name worker --loglevel=info
# Start Celery Beat
celery -A your_project_name beat --loglevel=info
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_request_track-0.8.2.tar.gz.
File metadata
- Download URL: django_request_track-0.8.2.tar.gz
- Upload date:
- Size: 19.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eef5508be295047af3ebec3b9cae3716a1a46ceb6dfa897f0f77d3808017b132
|
|
| MD5 |
a25cbad5171fed1b910862f28ca43b41
|
|
| BLAKE2b-256 |
1b25416ba8aba93b167f9fbf1c8fff63aaf6690183e7a2d810daf6b282a2e77b
|
File details
Details for the file django_request_track-0.8.2-py3-none-any.whl.
File metadata
- Download URL: django_request_track-0.8.2-py3-none-any.whl
- Upload date:
- Size: 25.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ee5b253abc813954d1414aa47a6996f9bab0bfbd23c1e14a5a381191a7f67792
|
|
| MD5 |
94c13d4cf9c0a046788f49553add7bbd
|
|
| BLAKE2b-256 |
60832b71b77b8a339733ab9c4b4fbcd7c6088d2ac89b0f20046e6c140b3a1fe1
|