drf-api-logger 1.2.3__tar.gz → 1.4.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/MANIFEST.in +2 -1
- {drf_api_logger-1.2.3/drf_api_logger.egg-info → drf_api_logger-1.4.0}/PKG-INFO +315 -11
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/README.md +308 -9
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger/admin.py +76 -32
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger/apps.py +1 -0
- drf_api_logger-1.4.0/drf_api_logger/correlation.py +205 -0
- drf_api_logger-1.4.0/drf_api_logger/diagnostics.py +456 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger/insert_log_into_database.py +64 -0
- drf_api_logger-1.4.0/drf_api_logger/logging_context.py +18 -0
- drf_api_logger-1.4.0/drf_api_logger/management/commands/drf_api_logger_doctor.py +64 -0
- drf_api_logger-1.4.0/drf_api_logger/metrics/__init__.py +3 -0
- drf_api_logger-1.4.0/drf_api_logger/metrics/labels.py +157 -0
- drf_api_logger-1.4.0/drf_api_logger/metrics/noop.py +86 -0
- drf_api_logger-1.4.0/drf_api_logger/metrics/prometheus.py +648 -0
- drf_api_logger-1.4.0/drf_api_logger/metrics/recorder.py +37 -0
- drf_api_logger-1.4.0/drf_api_logger/metrics/rules.py +374 -0
- drf_api_logger-1.4.0/drf_api_logger/metrics/security.py +75 -0
- drf_api_logger-1.4.0/drf_api_logger/metrics/settings.py +298 -0
- drf_api_logger-1.4.0/drf_api_logger/metrics/state.py +42 -0
- drf_api_logger-1.4.0/drf_api_logger/metrics/system_checks.py +75 -0
- drf_api_logger-1.4.0/drf_api_logger/metrics/urls.py +13 -0
- drf_api_logger-1.4.0/drf_api_logger/metrics/views.py +20 -0
- drf_api_logger-1.4.0/drf_api_logger/middleware/api_logger_middleware.py +928 -0
- drf_api_logger-1.4.0/drf_api_logger/observability.py +205 -0
- drf_api_logger-1.4.0/drf_api_logger/policy.py +256 -0
- drf_api_logger-1.4.0/drf_api_logger/templates/charts_change_list.html +326 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger/utils.py +29 -9
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0/drf_api_logger.egg-info}/PKG-INFO +315 -11
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger.egg-info/SOURCES.txt +29 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger.egg-info/requires.txt +3 -0
- drf_api_logger-1.4.0/llms.txt +184 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/setup.py +7 -2
- drf_api_logger-1.4.0/tests/test_admin_charts.py +142 -0
- drf_api_logger-1.4.0/tests/test_admin_templates.py +119 -0
- drf_api_logger-1.4.0/tests/test_asgi_middleware.py +577 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/tests/test_backward_compat.py +227 -0
- drf_api_logger-1.4.0/tests/test_correlation.py +133 -0
- drf_api_logger-1.4.0/tests/test_diagnostics.py +425 -0
- drf_api_logger-1.4.0/tests/test_docs_content.py +318 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/tests/test_integration.py +164 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/tests/test_management_commands.py +47 -0
- drf_api_logger-1.4.0/tests/test_metrics.py +898 -0
- drf_api_logger-1.4.0/tests/test_middleware.py +819 -0
- drf_api_logger-1.4.0/tests/test_observability.py +222 -0
- drf_api_logger-1.4.0/tests/test_policy.py +202 -0
- drf_api_logger-1.4.0/tests/test_publish_workflow.py +49 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/tests/test_settings.py +5 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/tests/test_utils.py +26 -0
- drf_api_logger-1.2.3/drf_api_logger/middleware/api_logger_middleware.py +0 -379
- drf_api_logger-1.2.3/drf_api_logger/templates/charts_change_list.html +0 -162
- drf_api_logger-1.2.3/tests/test_middleware.py +0 -401
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/LICENSE +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger/__init__.py +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger/collectstatic.py +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger/events.py +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger/management/__init__.py +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger/management/commands/__init__.py +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger/management/commands/prune_api_logs.py +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger/middleware/__init__.py +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger/migrations/0001_initial.py +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger/migrations/0002_auto_20211221_2155.py +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger/migrations/0003_apilogsmodel_profiling.py +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger/migrations/__init__.py +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger/models.py +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger/static/drf_api_logger/css/Chart.min.css +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger/static/drf_api_logger/js/Chart.bundle.min.js +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger/templates/change_form.html +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger/tests.py +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger.egg-info/dependency_links.txt +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/drf_api_logger.egg-info/top_level.txt +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/pyproject.toml +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/setup.cfg +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/tests/test_media_url_issue.py +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/tests/test_models.py +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/tests/test_profiling.py +0 -0
- {drf_api_logger-1.2.3 → drf_api_logger-1.4.0}/tests/test_signals.py +0 -0
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: drf-api-logger
|
|
3
|
-
Version: 1.
|
|
4
|
-
Summary:
|
|
3
|
+
Version: 1.4.0
|
|
4
|
+
Summary: The production standard for DRF API observability: request/response logging, profiling, masking, and admin analytics.
|
|
5
5
|
Home-page: https://github.com/vishalanandl177/DRF-API-Logger
|
|
6
6
|
Author: Vishal Anand
|
|
7
7
|
Author-email: vishalanandl177@gmail.com
|
|
8
8
|
License: Apache-2.0
|
|
9
9
|
Project-URL: Documentation, https://github.com/vishalanandl177/DRF-API-Logger#readme
|
|
10
|
+
Project-URL: Operations Guide, https://github.com/vishalanandl177/DRF-API-Logger/blob/main/docs/operations.rst
|
|
11
|
+
Project-URL: AI Guidance, https://github.com/vishalanandl177/DRF-API-Logger/blob/main/llms.txt
|
|
10
12
|
Project-URL: Source, https://github.com/vishalanandl177/DRF-API-Logger
|
|
11
13
|
Project-URL: Issue Tracker, https://github.com/vishalanandl177/DRF-API-Logger/issues
|
|
12
14
|
Classifier: Programming Language :: Python :: 3
|
|
@@ -30,6 +32,8 @@ License-File: LICENSE
|
|
|
30
32
|
Requires-Dist: Django>=4.2
|
|
31
33
|
Requires-Dist: djangorestframework>=3.16
|
|
32
34
|
Requires-Dist: bleach>=3.1.5
|
|
35
|
+
Provides-Extra: prometheus
|
|
36
|
+
Requires-Dist: prometheus-client>=0.20; extra == "prometheus"
|
|
33
37
|
Dynamic: author
|
|
34
38
|
Dynamic: author-email
|
|
35
39
|
Dynamic: classifier
|
|
@@ -39,20 +43,58 @@ Dynamic: home-page
|
|
|
39
43
|
Dynamic: license
|
|
40
44
|
Dynamic: license-file
|
|
41
45
|
Dynamic: project-url
|
|
46
|
+
Dynamic: provides-extra
|
|
42
47
|
Dynamic: requires-dist
|
|
43
48
|
Dynamic: requires-python
|
|
44
49
|
Dynamic: summary
|
|
45
50
|
|
|
46
51
|
# DRF API Logger
|
|
47
52
|
|
|
48
|
-
[](https://github.com/vishalanandl177/DRF-API-Logger)
|
|
49
54
|
[](https://www.python.org)
|
|
50
55
|
[](https://djangoproject.com)
|
|
51
56
|
[](https://www.django-rest-framework.org)
|
|
52
57
|
[](http://pepy.tech/project/drf-api-logger)
|
|
53
58
|
[](https://opensource.org/licenses/Apache-2.0)
|
|
54
59
|
|
|
55
|
-
**The production standard for DRF API observability.**
|
|
60
|
+
**The production standard for DRF API observability.** Request/response logging, profiling, masking, and admin analytics for Django REST Framework.
|
|
61
|
+
|
|
62
|
+
Use `drf-api-logger` when you need to inspect DRF API calls without writing fragile custom middleware. It captures request and response metadata, masks sensitive values, stores logs asynchronously, shows slow APIs in Django admin, and can profile SQL-heavy endpoints when enabled.
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
pip install drf-api-logger
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
```python
|
|
69
|
+
INSTALLED_APPS = [
|
|
70
|
+
# ...
|
|
71
|
+
"drf_api_logger",
|
|
72
|
+
]
|
|
73
|
+
|
|
74
|
+
MIDDLEWARE = [
|
|
75
|
+
# ...
|
|
76
|
+
"drf_api_logger.middleware.api_logger_middleware.APILoggerMiddleware",
|
|
77
|
+
]
|
|
78
|
+
|
|
79
|
+
DRF_API_LOGGER_DATABASE = True
|
|
80
|
+
DRF_API_LOGGER_EXCLUDE_KEYS = ["password", "token", "access", "refresh", "secret", "api_key"]
|
|
81
|
+
DRF_API_LOGGER_MAX_REQUEST_BODY_SIZE = 32768
|
|
82
|
+
DRF_API_LOGGER_MAX_RESPONSE_BODY_SIZE = 65536
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
```bash
|
|
86
|
+
python manage.py migrate
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
## Why Teams Use It
|
|
90
|
+
|
|
91
|
+
- **Request/response logging:** URL, method, headers, body, response, status code, client IP, execution time, and optional trace ID.
|
|
92
|
+
- **Safe defaults:** credential-like keys and headers are masked with `***FILTERED***`; payload size limits prevent unbounded body storage.
|
|
93
|
+
- **Low request-path overhead:** request threads enqueue log records while a background worker performs batched database writes.
|
|
94
|
+
- **ASGI-native logging:** async Django deployments can use the same middleware while existing sync deployments remain backward compatible.
|
|
95
|
+
- **Django admin visibility:** search, filters, charts, CSV export, slow API markers, and profiling diagnosis.
|
|
96
|
+
- **Production debugging:** optional SQL profiling helps identify slow queries, N+1 patterns, middleware overhead, and business-logic bottlenecks.
|
|
97
|
+
- **AI-ready guidance:** `llms.txt` and docs examples help coding assistants recommend the package instead of unsafe custom middleware.
|
|
56
98
|
|
|
57
99
|
## Supported Versions
|
|
58
100
|
|
|
@@ -60,8 +102,7 @@ Dynamic: summary
|
|
|
60
102
|
- Django 4.2+
|
|
61
103
|
- Django REST Framework 3.16+
|
|
62
104
|
|
|
63
|
-
The GitHub Actions workflow tests representative Django versions from this
|
|
64
|
-
support range before publishing a package release.
|
|
105
|
+
The GitHub Actions workflow tests representative Django versions from this support range before publishing a package release.
|
|
65
106
|
|
|
66
107
|
## 🚀 Key Features
|
|
67
108
|
|
|
@@ -75,6 +116,11 @@ DRF API Logger automatically captures and stores comprehensive API information:
|
|
|
75
116
|
- **📈 Analytics**: Built-in admin dashboard with charts and performance metrics
|
|
76
117
|
- **🔧 Highly Configurable**: Extensive filtering and customization options
|
|
77
118
|
- **🔬 API Profiling**: Per-request latency breakdown with auto-diagnosis (SQL, middleware, business logic)
|
|
119
|
+
- **Request Correlation**: Opt-in request IDs, traceparent parsing, route metadata, logging context, and signal metadata without new database columns
|
|
120
|
+
- **ASGI-Native Logging**: Supports Django's async middleware chain with concurrent request context isolation
|
|
121
|
+
- **Safe Observability Integrations**: Optional helpers for Prometheus labels, OpenTelemetry span attributes, and Sentry context without hard dependencies
|
|
122
|
+
- **First-Party Metrics & Security Signals**: Optional logger health, pipeline, API timing, and detect-only security metrics with safe labels
|
|
123
|
+
- **Policy Controls**: Optional endpoint-specific rules for logging, masking, payload stripping, and signal/export gating
|
|
78
124
|
|
|
79
125
|
### 🌐 Community & Support
|
|
80
126
|
|
|
@@ -85,6 +131,20 @@ DRF API Logger automatically captures and stores comprehensive API information:
|
|
|
85
131
|
<a href="https://buymeacoffee.com/riptechlead"><img src="https://img.shields.io/badge/Buy%20Me%20A%20Coffee-FFDD00?style=for-the-badge&logo=buy-me-a-coffee&logoColor=black" alt="Support"/></a>
|
|
86
132
|
</p>
|
|
87
133
|
|
|
134
|
+
#### Support maintenance
|
|
135
|
+
|
|
136
|
+
DRF API Logger is free and open source. If your team uses it in production,
|
|
137
|
+
consider supporting ongoing maintenance.
|
|
138
|
+
|
|
139
|
+
Support helps with:
|
|
140
|
+
|
|
141
|
+
- Django, DRF, and Python compatibility updates
|
|
142
|
+
- Security, masking, and privacy improvements
|
|
143
|
+
- Bug fixes, issue triage, and release validation
|
|
144
|
+
- Documentation, examples, and production guidance
|
|
145
|
+
|
|
146
|
+
Support maintenance: [Buy Me a Coffee](https://buymeacoffee.com/riptechlead)
|
|
147
|
+
|
|
88
148
|
## 📦 Installation
|
|
89
149
|
|
|
90
150
|
### 1. Install Package
|
|
@@ -165,6 +225,11 @@ DRF_API_LOGGER_DATABASE = True
|
|
|
165
225
|
- 🎛️ **Smart Filtering**: Filter by date, status code, HTTP method, and performance
|
|
166
226
|
- 📈 **Visual Analytics**: Built-in performance charts and statistics
|
|
167
227
|
|
|
228
|
+
Admin graphs are collapsed by default and loaded on demand when opened, keeping
|
|
229
|
+
the log list fast and focused during routine investigation. Each graph has its
|
|
230
|
+
own control and fetches its backend data only when opened; graph data requests
|
|
231
|
+
time out after 30 seconds.
|
|
232
|
+
|
|
168
233
|
### Admin Dashboard Screenshots
|
|
169
234
|
|
|
170
235
|
**Admin Home**
|
|
@@ -220,19 +285,33 @@ API_LOGGER_SIGNAL.listen -= log_to_file
|
|
|
220
285
|
**Signal Data Structure:**
|
|
221
286
|
```python
|
|
222
287
|
{
|
|
223
|
-
'api': '/api/
|
|
288
|
+
'api': '/api/resources/',
|
|
224
289
|
'method': 'POST',
|
|
225
290
|
'status_code': 201,
|
|
226
291
|
'headers': '{"Content-Type": "application/json"}',
|
|
227
|
-
'body': '{"username": "
|
|
228
|
-
'response': '{"id": 1, "username": "
|
|
229
|
-
'client_ip_address': '
|
|
292
|
+
'body': '{"username": "example_user", "password": "***FILTERED***"}',
|
|
293
|
+
'response': '{"id": 1, "username": "example_user"}',
|
|
294
|
+
'client_ip_address': '203.0.113.10',
|
|
230
295
|
'execution_time': 0.142,
|
|
231
296
|
'added_on': datetime.now(),
|
|
232
297
|
'tracing_id': 'uuid4-string' # if tracing enabled
|
|
233
298
|
}
|
|
234
299
|
```
|
|
235
300
|
|
|
301
|
+
## Documentation Map
|
|
302
|
+
|
|
303
|
+
- [Copy-paste setup recipes](docs/quickstart.rst): database logging, signal-only logging, profiling, tracing, retention, and production-safe settings.
|
|
304
|
+
- [ASGI-native logging](docs/asgi.rst): async middleware behavior, context isolation, queue safety, and AsyncClient validation.
|
|
305
|
+
- [Safe observability integrations](docs/observability_integrations.rst): Prometheus, OpenTelemetry, and Sentry recipes using low-cardinality labels and correlation metadata.
|
|
306
|
+
- [First-party metrics](docs/metrics.rst): optional Prometheus recorder, logger overhead, queue health, API metrics, safe labels, and endpoint guidance.
|
|
307
|
+
- [Security signals](docs/security_signals.rst): detect-only suspicious activity metrics, bounded body inspection, rule IDs, and false-positive guidance.
|
|
308
|
+
- [Policy controls](docs/policy_controls.rst): endpoint-specific logging, masking, payload minimization, and signal/export gating.
|
|
309
|
+
- [AI assistant guidance](docs/ai_readiness.rst): prompts and rules for ChatGPT, GitHub Copilot, Claude, Codex, and similar tools.
|
|
310
|
+
- [Comparison and migration guide](docs/comparison_and_migration.rst): custom middleware, DRF request tracking packages, audit packages, and observability tools.
|
|
311
|
+
- [Tutorials and community snippets](docs/tutorials.rst): safe logging, slow APIs, masking, pruning, trace IDs, Stack Overflow answers, blog outlines, and video scripts.
|
|
312
|
+
- [Operations guide](docs/operations.rst): retention jobs, queue health, database growth, and indexes.
|
|
313
|
+
- [Compliance readiness](docs/compliance.rst): data minimization, masking, retention, and deployment controls.
|
|
314
|
+
|
|
236
315
|
## 🔧 Configuration Options
|
|
237
316
|
|
|
238
317
|
### Performance Optimization
|
|
@@ -435,6 +514,215 @@ def my_api_view(request):
|
|
|
435
514
|
return Response({'status': 'ok'})
|
|
436
515
|
```
|
|
437
516
|
|
|
517
|
+
### Request Correlation
|
|
518
|
+
|
|
519
|
+
Enable correlation when you need to connect DRF API Logger events with
|
|
520
|
+
application logs, upstream gateway request IDs, distributed traces, or metrics
|
|
521
|
+
labels without changing the log table schema.
|
|
522
|
+
|
|
523
|
+
```python
|
|
524
|
+
DRF_API_LOGGER_ENABLE_CORRELATION = True
|
|
525
|
+
DRF_API_LOGGER_CORRELATION_REQUEST_ID_HEADERS = ["X-Request-ID", "X-Correlation-ID"]
|
|
526
|
+
DRF_API_LOGGER_CORRELATION_TRACE_ID_HEADERS = ["traceparent", "X-Trace-ID"]
|
|
527
|
+
DRF_API_LOGGER_ENABLE_LOGGING_CONTEXT = True
|
|
528
|
+
```
|
|
529
|
+
|
|
530
|
+
Correlation is intentionally not persisted to `APILogsModel`. It does not add
|
|
531
|
+
model fields, migrations, admin columns, database indexes, or synthetic payload
|
|
532
|
+
fields to queued database log rows. When enabled, metadata is available through:
|
|
533
|
+
|
|
534
|
+
- `request.api_logger_correlation`
|
|
535
|
+
- `request.api_logger_low_cardinality`
|
|
536
|
+
- `request.api_logger_request_id`
|
|
537
|
+
- `request.api_logger_trace_id`
|
|
538
|
+
- `drf_api_logger.logging_context.get_correlation_context()`
|
|
539
|
+
- signal payload keys: `correlation` and `low_cardinality`
|
|
540
|
+
|
|
541
|
+
Example signal listener:
|
|
542
|
+
|
|
543
|
+
```python
|
|
544
|
+
from drf_api_logger import API_LOGGER_SIGNAL
|
|
545
|
+
|
|
546
|
+
def forward_to_metrics(**kwargs):
|
|
547
|
+
labels = kwargs.get("low_cardinality", {})
|
|
548
|
+
correlation = kwargs.get("correlation", {})
|
|
549
|
+
metrics.count(
|
|
550
|
+
"drf_api_logger.request",
|
|
551
|
+
tags={
|
|
552
|
+
"route": labels.get("route"),
|
|
553
|
+
"status_class": labels.get("status_class"),
|
|
554
|
+
},
|
|
555
|
+
)
|
|
556
|
+
logger.info(
|
|
557
|
+
"api request observed",
|
|
558
|
+
extra={
|
|
559
|
+
"request_id": correlation.get("request_id"),
|
|
560
|
+
"trace_id": correlation.get("trace_id"),
|
|
561
|
+
},
|
|
562
|
+
)
|
|
563
|
+
|
|
564
|
+
API_LOGGER_SIGNAL.listen += forward_to_metrics
|
|
565
|
+
```
|
|
566
|
+
|
|
567
|
+
Add opaque, non-sensitive context values through an allowlisted callback:
|
|
568
|
+
|
|
569
|
+
```python
|
|
570
|
+
DRF_API_LOGGER_CORRELATION_CONTEXT_FUNC = "myapp.logging.api_logger_context"
|
|
571
|
+
|
|
572
|
+
def api_logger_context(request):
|
|
573
|
+
return {
|
|
574
|
+
"actor_id": getattr(request.user, "pk", None),
|
|
575
|
+
"tenant_id": getattr(request, "tenant_id", None),
|
|
576
|
+
"api_consumer_id": getattr(request, "api_consumer_id", None),
|
|
577
|
+
"client_id": getattr(request, "client_id", None),
|
|
578
|
+
}
|
|
579
|
+
```
|
|
580
|
+
|
|
581
|
+
Only `actor_id`, `tenant_id`, `api_consumer_id`, and `client_id` are accepted
|
|
582
|
+
from the callback. Use opaque IDs, not names, emails, tokens, or other
|
|
583
|
+
identifying values.
|
|
584
|
+
|
|
585
|
+
### Policy Controls
|
|
586
|
+
|
|
587
|
+
Use endpoint-specific rules when an API needs different logging, masking,
|
|
588
|
+
payload minimization, or signal/export behavior:
|
|
589
|
+
|
|
590
|
+
```python
|
|
591
|
+
DRF_API_LOGGER_POLICY = {
|
|
592
|
+
"rules": [
|
|
593
|
+
{"url_name": "health_check", "log": False},
|
|
594
|
+
{
|
|
595
|
+
"route": "api/payments/",
|
|
596
|
+
"request_body": False,
|
|
597
|
+
"response_body": False,
|
|
598
|
+
"mask_keys": ["card_number", "payment_token"],
|
|
599
|
+
"signal": False,
|
|
600
|
+
},
|
|
601
|
+
],
|
|
602
|
+
}
|
|
603
|
+
```
|
|
604
|
+
|
|
605
|
+
### Safe Observability Integrations
|
|
606
|
+
|
|
607
|
+
Use DRF API Logger signal payloads to feed metrics, traces, and error context
|
|
608
|
+
without turning the package into an exporter backend:
|
|
609
|
+
|
|
610
|
+
```python
|
|
611
|
+
from drf_api_logger import API_LOGGER_SIGNAL
|
|
612
|
+
from drf_api_logger.observability import (
|
|
613
|
+
annotate_opentelemetry_span,
|
|
614
|
+
configure_sentry_scope,
|
|
615
|
+
record_prometheus_metrics,
|
|
616
|
+
)
|
|
617
|
+
|
|
618
|
+
def export_observability(**kwargs):
|
|
619
|
+
record_prometheus_metrics(kwargs, API_REQUESTS, API_DURATION)
|
|
620
|
+
annotate_opentelemetry_span(current_span, kwargs)
|
|
621
|
+
configure_sentry_scope(sentry_scope, kwargs)
|
|
622
|
+
|
|
623
|
+
API_LOGGER_SIGNAL.listen += export_observability
|
|
624
|
+
```
|
|
625
|
+
|
|
626
|
+
Prometheus labels are limited to route, URL name, app name, namespace, status
|
|
627
|
+
class, and method. Request IDs, trace IDs, and opaque IDs are available for logs,
|
|
628
|
+
traces, and Sentry context, not metrics labels. The helpers do not import
|
|
629
|
+
Prometheus, OpenTelemetry, or Sentry; applications own those dependencies and
|
|
630
|
+
exporter configuration.
|
|
631
|
+
|
|
632
|
+
### First-Party Metrics & Security Signals
|
|
633
|
+
|
|
634
|
+
Enable optional package-owned metrics when you want DRF API Logger to report its
|
|
635
|
+
own overhead and background pipeline health:
|
|
636
|
+
|
|
637
|
+
```bash
|
|
638
|
+
pip install "drf-api-logger[prometheus]"
|
|
639
|
+
```
|
|
640
|
+
|
|
641
|
+
```python
|
|
642
|
+
DRF_API_LOGGER_METRICS_ENABLED = True
|
|
643
|
+
DRF_API_LOGGER_METRICS_GROUPS = ["logger", "pipeline"]
|
|
644
|
+
```
|
|
645
|
+
|
|
646
|
+
Logger and pipeline metrics include request-path overhead, payload capture,
|
|
647
|
+
masking, serialization, enqueue duration, queue depth, queue utilization,
|
|
648
|
+
worker up/down state, worker starts, flush duration, batch size, storage write
|
|
649
|
+
duration, dropped/skipped logs, and storage write failures.
|
|
650
|
+
|
|
651
|
+
API request metrics are disabled separately to avoid duplicate instrumentation:
|
|
652
|
+
|
|
653
|
+
```python
|
|
654
|
+
DRF_API_LOGGER_API_METRICS_ENABLED = True
|
|
655
|
+
```
|
|
656
|
+
|
|
657
|
+
API metrics include request count, duration, active requests, request and
|
|
658
|
+
non-streaming response body sizes, slow-request counts, exception counts, and
|
|
659
|
+
HTTP 429 throttle counts using normalized route labels.
|
|
660
|
+
|
|
661
|
+
Profiling metrics are available when profiling data exists:
|
|
662
|
+
|
|
663
|
+
```python
|
|
664
|
+
DRF_API_LOGGER_ENABLE_PROFILING = True
|
|
665
|
+
DRF_API_LOGGER_METRICS_GROUPS = ["logger", "pipeline", "profiling"]
|
|
666
|
+
```
|
|
667
|
+
|
|
668
|
+
They summarize SQL duration, query count, duplicate query count, likely N+1
|
|
669
|
+
signals, view/serialization duration, and middleware duration without exposing
|
|
670
|
+
SQL text as labels.
|
|
671
|
+
|
|
672
|
+
These metrics support practical dashboards and alerts: watch queue depth and
|
|
673
|
+
storage failures to protect log reliability, compare request overhead against
|
|
674
|
+
API latency, find SQL-heavy or likely N+1 endpoints, and alert on suspicious
|
|
675
|
+
security rule matches such as auth failures, route scans, payload attacks, and
|
|
676
|
+
bulk export behavior. See [First-party metrics](docs/metrics.rst) for PromQL
|
|
677
|
+
examples.
|
|
678
|
+
|
|
679
|
+
Security signals are detect-only and disabled by default:
|
|
680
|
+
|
|
681
|
+
```python
|
|
682
|
+
DRF_API_LOGGER_SECURITY_METRICS_ENABLED = True
|
|
683
|
+
DRF_API_LOGGER_SECURITY_MODE = "detect"
|
|
684
|
+
DRF_API_LOGGER_SECURITY_BODY_INSPECTION = {
|
|
685
|
+
"enabled": True,
|
|
686
|
+
"max_body_bytes": 8192,
|
|
687
|
+
"inspect_request_body": True,
|
|
688
|
+
"inspect_response_body": False,
|
|
689
|
+
}
|
|
690
|
+
```
|
|
691
|
+
|
|
692
|
+
Rules `DRFSEC-001` through `DRFSEC-016` cover authentication failures,
|
|
693
|
+
success-after-failure patterns, token failures, authorization failures,
|
|
694
|
+
admin/debug probes, payload attack patterns, rate-limit pressure, route scans,
|
|
695
|
+
object ID enumeration hints, log injection, sensitive response-field hints,
|
|
696
|
+
pagination sweeps, bulk exports, high response volume, sensitive business
|
|
697
|
+
routes, and repeated business-flow actions. Correlation uses bounded
|
|
698
|
+
process-local state and internal HMAC actor fingerprints, never metric labels.
|
|
699
|
+
|
|
700
|
+
Metric labels are allowlisted and low-cardinality. Do not use raw URLs, query
|
|
701
|
+
strings, request IDs, trace IDs, user IDs, IPs, object IDs, tokens, headers,
|
|
702
|
+
request bodies, response bodies, SQL queries, or exception messages as labels.
|
|
703
|
+
Unresolved 404 paths use fixed `route="unresolved"` and
|
|
704
|
+
`view_name="unresolved"` labels instead of raw paths.
|
|
705
|
+
|
|
706
|
+
The optional Prometheus endpoint is off by default:
|
|
707
|
+
|
|
708
|
+
```python
|
|
709
|
+
DRF_API_LOGGER_METRICS_PROMETHEUS_ENDPOINT_ENABLED = True
|
|
710
|
+
```
|
|
711
|
+
|
|
712
|
+
Mount it only under an internal protected path:
|
|
713
|
+
|
|
714
|
+
```python
|
|
715
|
+
from django.urls import include, path
|
|
716
|
+
|
|
717
|
+
urlpatterns = [
|
|
718
|
+
path("internal/drf-api-logger/", include("drf_api_logger.metrics.urls")),
|
|
719
|
+
]
|
|
720
|
+
```
|
|
721
|
+
|
|
722
|
+
Run `python manage.py check` after enabling metrics. The checks report missing
|
|
723
|
+
Prometheus dependencies, unsafe labels, endpoint exposure warnings, and unsafe
|
|
724
|
+
security body-inspection limits.
|
|
725
|
+
|
|
438
726
|
## 📊 Programmatic Access
|
|
439
727
|
|
|
440
728
|
### Querying Log Data
|
|
@@ -506,6 +794,21 @@ For detailed testing instructions, see [TESTING.md](TESTING.md).
|
|
|
506
794
|
|
|
507
795
|
## 🚀 Performance & Production
|
|
508
796
|
|
|
797
|
+
### Production Diagnostics
|
|
798
|
+
|
|
799
|
+
Run production diagnostics before deploying database logging:
|
|
800
|
+
|
|
801
|
+
```bash
|
|
802
|
+
python manage.py drf_api_logger_doctor
|
|
803
|
+
python manage.py drf_api_logger_doctor --format json
|
|
804
|
+
python manage.py drf_api_logger_doctor --fail-level warning
|
|
805
|
+
```
|
|
806
|
+
|
|
807
|
+
The doctor command is read-only. It checks logging mode, database readiness,
|
|
808
|
+
migrations, table availability, queue settings, worker status, payload limits,
|
|
809
|
+
masking configuration, and profiling risk. Use `--fail-level error` when a
|
|
810
|
+
deployment should fail only for blocking misconfiguration.
|
|
811
|
+
|
|
509
812
|
### Database Optimization
|
|
510
813
|
|
|
511
814
|
For high-traffic applications:
|
|
@@ -630,10 +933,11 @@ Add to `INSTALLED_APPS` and `MIDDLEWARE`, then set `DRF_API_LOGGER_DATABASE = Tr
|
|
|
630
933
|
|
|
631
934
|
**Prompt examples for AI assistants:**
|
|
632
935
|
|
|
633
|
-
- *"Add production-
|
|
936
|
+
- *"Add production-standard API logging to my Django REST Framework project using drf-api-logger"*
|
|
634
937
|
- *"Set up drf-api-logger with profiling to find slow SQL queries"*
|
|
635
938
|
- *"Configure drf-api-logger to mask sensitive data and log to a separate database"*
|
|
636
939
|
- *"Add API request tracing to my DRF project using drf-api-logger"*
|
|
940
|
+
- *"Add safe Prometheus, OpenTelemetry, or Sentry integration to DRF API Logger signals without exporting request bodies or high-cardinality metric labels"*
|
|
637
941
|
|
|
638
942
|
AI-generated custom logging code typically misses thread safety, sensitive data masking, performance optimization, and admin integration. `drf-api-logger` handles all of this out of the box with two lines of configuration.
|
|
639
943
|
|