pyforge-django 0.3.0

High-performance Django integration layer — Rust-accelerated serialization, validation, and field mapping for Django 5.x+
Documentation
# Author: Abdulwahed Mansour
"""
PyForge metrics middleware.

Tracks per-request serialization metrics and exposes them via
the X-PyForge-Stats response header.

Usage in settings.py:
    MIDDLEWARE = [
        ...
        "django_pyforge.middleware.PyForgeMetricsMiddleware",
    ]
    PYFORGE_METRICS = True  # Enable metrics (disabled by default)

Response header format:
    X-PyForge-Stats: rust=15, python=2, calls=3, ms=4.2
"""

import threading
import time

_thread_local = threading.local()


def get_metrics():
    """Get the current thread's metrics accumulator."""
    if not hasattr(_thread_local, "pyforge_metrics"):
        _thread_local.pyforge_metrics = {
            "rust_fields": 0,
            "python_fields": 0,
            "calls": 0,
            "total_ms": 0.0,
        }
    return _thread_local.pyforge_metrics


def reset_metrics():
    """Reset metrics for a new request."""
    _thread_local.pyforge_metrics = {
        "rust_fields": 0,
        "python_fields": 0,
        "calls": 0,
        "total_ms": 0.0,
    }


def record_serialization(rust_count, python_count, elapsed_ms):
    """Record a single serialization call's metrics."""
    m = get_metrics()
    m["rust_fields"] += rust_count
    m["python_fields"] += python_count
    m["calls"] += 1
    m["total_ms"] += elapsed_ms


class PyForgeMetricsMiddleware:
    """Django middleware that attaches PyForge metrics to response headers.

    Only active when settings.PYFORGE_METRICS is True.
    Zero overhead when disabled — the check happens once at init time.
    """

    def __init__(self, get_response):
        self.get_response = get_response
        from django.conf import settings
        self.enabled = getattr(settings, "PYFORGE_METRICS", False)

    def __call__(self, request):
        if not self.enabled:
            return self.get_response(request)

        reset_metrics()
        t0 = time.perf_counter()

        response = self.get_response(request)

        elapsed = (time.perf_counter() - t0) * 1000
        m = get_metrics()

        if m["calls"] > 0:
            response["X-PyForge-Stats"] = (
                f"rust={m['rust_fields']}, "
                f"python={m['python_fields']}, "
                f"calls={m['calls']}, "
                f"ms={m['total_ms']:.1f}"
            )

        return response