Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions docs/settings.txt
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,11 @@ error detection system.
Default: ``('get', 'post', 'put', 'delete', 'head', 'options', 'trace')``

Any request which is not in this tuple/list will not be recorded.

``REQUEST_CORRELATION_ID``
==========================

Default: ``lambda: ''``

Function that return a string to uniquely identify request
See packages: ``django-guid`` (``django_guid.api.get_guid``) or ``django-cid``
18 changes: 18 additions & 0 deletions request/migrations/0009_request_cid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.2 on 2023-04-09 13:49

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('request', '0008_alter_request_response_choices'),
]

operations = [
migrations.AddField(
model_name='request',
name='cid',
field=models.CharField(blank=True, db_index=True, max_length=36, null=True, verbose_name='correlation id'),
),
]
4 changes: 3 additions & 1 deletion request/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Request(models.Model):
method = models.CharField(_('method'), default='GET', max_length=7)
path = models.CharField(_('path'), max_length=255)
time = models.DateTimeField(_('time'), default=timezone.now, db_index=True)
cid = models.CharField(_('correlation id'), max_length=36, blank=True, null=True, db_index=True)

is_secure = models.BooleanField(_('is secure'), default=False)
is_ajax = models.BooleanField(
Expand All @@ -45,7 +46,7 @@ class Meta:
ordering = ('-time',)

def __str__(self):
return '[{0}] {1} {2} {3}'.format(self.time, self.method, self.path, self.response)
return '[{0}] {1} {2} {3} {4}'.format(self.time, self.method, self.path, self.response, self.cid)

def get_user(self):
return get_user_model().objects.get(pk=self.user_id)
Expand All @@ -54,6 +55,7 @@ def from_http_request(self, request, response=None, commit=True):
# Request information.
self.method = request.method
self.path = request.path[:255]
self.cid = request_settings.CORRELATION_ID()[:36]

self.is_secure = request.is_secure()
self.is_ajax = request_is_ajax(request)
Expand Down
5 changes: 5 additions & 0 deletions request/settings.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from django.conf import settings
from django.core.exceptions import ImproperlyConfigured

VALID_METHOD_NAMES = getattr(
settings,
Expand All @@ -16,6 +17,10 @@
IGNORE_USERNAME = getattr(settings, 'REQUEST_IGNORE_USERNAME', tuple())
IGNORE_PATHS = getattr(settings, 'REQUEST_IGNORE_PATHS', tuple())
IGNORE_USER_AGENTS = getattr(settings, 'REQUEST_IGNORE_USER_AGENTS', tuple())
CORRELATION_ID = getattr(settings, 'REQUEST_CORRELATION_ID', lambda: '')

if not callable(CORRELATION_ID):
raise ImproperlyConfigured('REQUEST_CORRELATION_ID should be callable')

TRAFFIC_MODULES = getattr(settings, 'REQUEST_TRAFFIC_MODULES', (
'request.traffic.UniqueVisitor',
Expand Down