|
| 1 | +ASGI-Native Logging |
| 2 | +=================== |
| 3 | + |
| 4 | +DRF API Logger supports ASGI-native logging for Django deployments that run the |
| 5 | +middleware inside Django's async request chain. The same |
| 6 | +``APILoggerMiddleware`` remains sync-capable, so existing WSGI deployments and |
| 7 | +sync deployments remain backward compatible. |
| 8 | + |
| 9 | +What Is Native |
| 10 | +-------------- |
| 11 | + |
| 12 | +The middleware declares both sync and async capability. When Django gives it an |
| 13 | +async ``get_response`` callable, it marks the middleware instance as coroutine |
| 14 | +callable and awaits the downstream handler directly. That keeps ASGI requests |
| 15 | +from being forced through the old sync-only ``__call__`` path. |
| 16 | + |
| 17 | +The async path preserves the existing public behavior: |
| 18 | + |
| 19 | +- same middleware path in ``MIDDLEWARE``; |
| 20 | +- same database and signal settings; |
| 21 | +- same masking, content-type handling, truncation markers, policies, and |
| 22 | + profiling payload shape; |
| 23 | +- same ``APILogsModel`` schema and migrations; |
| 24 | +- same signal payload keys, with correlation metadata only when correlation is |
| 25 | + enabled. |
| 26 | + |
| 27 | +Request Context Isolation |
| 28 | +------------------------- |
| 29 | + |
| 30 | +Request correlation uses Python ``contextvars``. In ASGI deployments this keeps |
| 31 | +request IDs, trace IDs, route metadata, and logging context isolated between |
| 32 | +concurrent requests. Use ``DRF_API_LOGGER_ENABLE_LOGGING_CONTEXT = True`` when |
| 33 | +application logs inside the view need access to the same request context. |
| 34 | + |
| 35 | +.. code-block:: python |
| 36 | +
|
| 37 | + DRF_API_LOGGER_SIGNAL = True |
| 38 | + DRF_API_LOGGER_ENABLE_CORRELATION = True |
| 39 | + DRF_API_LOGGER_ENABLE_LOGGING_CONTEXT = True |
| 40 | +
|
| 41 | +Database Queue Behavior |
| 42 | +----------------------- |
| 43 | + |
| 44 | +Database logging still enqueues records into the existing background worker. |
| 45 | +The request coroutine does not perform bulk database insertion. Queue failures |
| 46 | +are isolated so logging problems do not break the API response path. |
| 47 | + |
| 48 | +If database logging is enabled, continue to monitor: |
| 49 | + |
| 50 | +- ``queue_backlog`` |
| 51 | +- ``dropped_count`` |
| 52 | +- ``failed_insert_count`` |
| 53 | +- ``inserted_count`` |
| 54 | + |
| 55 | +Testing ASGI Behavior |
| 56 | +--------------------- |
| 57 | + |
| 58 | +The package tests ASGI behavior in ``tests/test_asgi_middleware.py``. Coverage |
| 59 | +includes: |
| 60 | + |
| 61 | +- middleware sync and async capability declarations; |
| 62 | +- direct async middleware calls; |
| 63 | +- Django ``AsyncClient`` integration; |
| 64 | +- signal logging and database enqueue behavior; |
| 65 | +- queue failure isolation; |
| 66 | +- concurrent request context isolation with ``contextvars``. |
| 67 | + |
| 68 | +Run the ASGI tests with: |
| 69 | + |
| 70 | +.. code-block:: bash |
| 71 | +
|
| 72 | + python -m django test tests.test_asgi_middleware --settings=tests.test_settings --verbosity=2 |
| 73 | +
|
| 74 | +Run the compatibility suite with: |
| 75 | + |
| 76 | +.. code-block:: bash |
| 77 | +
|
| 78 | + python -m django test tests.test_asgi_middleware tests.test_middleware tests.test_backward_compat tests.test_correlation tests.test_integration --settings=tests.test_settings --verbosity=1 |
| 79 | +
|
| 80 | +Application Smoke Test |
| 81 | +---------------------- |
| 82 | + |
| 83 | +Applications can smoke test the ASGI signal path with Django's ``AsyncClient``: |
| 84 | + |
| 85 | +.. code-block:: python |
| 86 | +
|
| 87 | + import json |
| 88 | + from django.test import AsyncClient, override_settings |
| 89 | + from drf_api_logger import API_LOGGER_SIGNAL |
| 90 | +
|
| 91 | + async def test_api_logging_signal_path(): |
| 92 | + events = [] |
| 93 | +
|
| 94 | + def listener(**kwargs): |
| 95 | + events.append(kwargs) |
| 96 | +
|
| 97 | + API_LOGGER_SIGNAL.listen += listener |
| 98 | + try: |
| 99 | + with override_settings( |
| 100 | + DRF_API_LOGGER_DATABASE=False, |
| 101 | + DRF_API_LOGGER_SIGNAL=True, |
| 102 | + ): |
| 103 | + response = await AsyncClient().post( |
| 104 | + "/api/test/", |
| 105 | + data=json.dumps({"password": "example-secret"}), |
| 106 | + content_type="application/json", |
| 107 | + ) |
| 108 | +
|
| 109 | + assert response.status_code == 200 |
| 110 | + assert events[0]["body"]["password"] == "***FILTERED***" |
| 111 | + finally: |
| 112 | + API_LOGGER_SIGNAL.listen -= listener |
| 113 | +
|
| 114 | +Performance Notes |
| 115 | +----------------- |
| 116 | + |
| 117 | +Measure sync and ASGI overhead in the target application before changing |
| 118 | +production traffic. Compare these modes at minimum: |
| 119 | + |
| 120 | +- application baseline with DRF API Logger disabled; |
| 121 | +- signal-only logging; |
| 122 | +- database logging with normal queue settings; |
| 123 | +- profiling enabled, if the deployment uses profiling. |
| 124 | + |
| 125 | +Use representative endpoints and report request count, concurrency, status |
| 126 | +codes, average latency, p95 latency, p99 latency, and queue backlog. For local |
| 127 | +demo validation, use ``J:\projects\drf-demo`` with the local package on |
| 128 | +``PYTHONPATH`` so the demo imports the checkout being tested. |
| 129 | + |
| 130 | +The repository includes ``scripts/measure_asgi_overhead.py`` for a local |
| 131 | +measurement pass through Django's sync ``Client`` and ASGI ``AsyncClient``. For |
| 132 | +the demo project, run it from ``J:\projects\drf-demo`` with the checkout on |
| 133 | +``PYTHONPATH``: |
| 134 | + |
| 135 | +.. code-block:: powershell |
| 136 | +
|
| 137 | + $env:PYTHONPATH='J:\projects\DRF-API-Logger' |
| 138 | + & 'J:\projects\drf-demo\venv\Scripts\python.exe' J:\projects\DRF-API-Logger\scripts\measure_asgi_overhead.py --settings config.settings --path /api/echo/ --requests 100 --concurrency 10 |
| 139 | +
|
| 140 | +Attach the JSON output to the Jira story or release notes when using it as |
| 141 | +release-readiness evidence. The script uses a synthetic payload and reports |
| 142 | +only timing and status-code aggregates. |
0 commit comments