Skip to main content

A Django package to track requests with optional Redis and Celery support.

Project description

django-request-track

Python Django PyPi

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

  1. Add request_track to your INSTALLED_APPS in your Django project's settings:

    INSTALLED_APPS = [
        # ...
        'request_track',
        # ...
    ]
    
  2. Register the middleware in your MIDDLEWARE settings:

    MIDDLEWARE = [
        # ...
        'request_track.middleware.LoggingRequestMiddleware'
    ]
    
  3. 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:

  1. Django Admin Interface (with built-in filtering and search)
  2. 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:

  1. Store logs in Redis temporarily
  2. Process logs in batches using Celery
  3. Reduce database load
  4. Improve application performance

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.

Author

Support

If you encounter any issues or have questions, please open an issue on GitHub.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

django_request_track-0.8.0.tar.gz (19.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_request_track-0.8.0-py3-none-any.whl (25.1 kB view details)

Uploaded Python 3

File details

Details for the file django_request_track-0.8.0.tar.gz.

File metadata

  • Download URL: django_request_track-0.8.0.tar.gz
  • Upload date:
  • Size: 19.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for django_request_track-0.8.0.tar.gz
Algorithm Hash digest
SHA256 b2bf42489061fc532508208e4b28abe3e14de22f7af7ff729696a3c5ec55dec2
MD5 d3f4598a34da5580b040f16581534128
BLAKE2b-256 bd18603330e4e7fb0ba641e05f49f4ea3a7284bff480dfdaa62a8d132cb111d4

See more details on using hashes here.

File details

Details for the file django_request_track-0.8.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_request_track-0.8.0-py3-none-any.whl
Algorithm Hash digest
SHA256 50ae146baf5607fea105becb54e6427f12b9c5a934535657d83c4350224c6f2e
MD5 4bfbfba300195c95b801227ba16fb0ff
BLAKE2b-256 9d4860f8ec40f94e6d1ea6cd3c00fbfece6b00171dd74f427eb43cbabb6dd85d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page