From bdf6c4117e191d900dfe5b2de7e483248286bf18 Mon Sep 17 00:00:00 2001 From: Pamella Bezerra Date: Thu, 25 Jan 2024 14:06:36 -0300 Subject: [PATCH] Add django-guid to the project --- README.md | 1 + backend/project_name/settings/base.py | 4 +++- backend/project_name/settings/local_base.py | 13 ++++++++++++- backend/project_name/settings/production.py | 18 ++++++++++++++++-- pyproject.toml | 1 + 5 files changed, 33 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ec6705d1..149f5198 100644 --- a/README.md +++ b/README.md @@ -55,6 +55,7 @@ Also, includes a Render.com `render.yaml` and a working Django `production.py` s - `django-webpack-loader` for rendering the bundled frontend assets - `django-js-reverse` for easy handling of Django URLs on JS - `django-upgrade` for automatically upgrading Django code to the target version on pre-commit +- `django-guid` for adding a unique correlation ID to log messages from Django requests - `psycopg2` for using PostgreSQL database - `sentry-sdk` for error monitoring - `python-decouple` for reading environment variables on settings files diff --git a/backend/project_name/settings/base.py b/backend/project_name/settings/base.py index 07afabee..32a1a6cd 100644 --- a/backend/project_name/settings/base.py +++ b/backend/project_name/settings/base.py @@ -37,6 +37,7 @@ def base_dir_join(*args): "import_export", "rest_framework", "defender", + "django_guid", "common", "users", ] @@ -54,6 +55,7 @@ def base_dir_join(*args): "django.middleware.clickjacking.XFrameOptionsMiddleware", "csp.middleware.CSPMiddleware", "defender.middleware.FailedLoginMiddleware", + "django_guid.middleware.guid_middleware", ] ROOT_URLCONF = "{{project_name}}.urls" @@ -205,7 +207,7 @@ def base_dir_join(*args): ] + [f"*{host}" if host.startswith(".") else host for host in ALLOWED_HOSTS] CSP_CONNECT_SRC = [ "'self'", - "https://sentry.io", + "*.sentry.io", ] + [f"*{host}" if host.startswith(".") else host for host in ALLOWED_HOSTS] CSP_STYLE_SRC = [ "'self'", diff --git a/backend/project_name/settings/local_base.py b/backend/project_name/settings/local_base.py index 143eaa71..bc3e1675 100644 --- a/backend/project_name/settings/local_base.py +++ b/backend/project_name/settings/local_base.py @@ -37,19 +37,30 @@ LOGGING = { "version": 1, "disable_existing_loggers": False, + "filters": { + "correlation_id": {"()": "django_guid.log_filters.CorrelationId"}, + }, "formatters": { - "standard": {"format": "%(levelname)-8s [%(asctime)s] %(name)s: %(message)s"}, + "standard": { + "format": "%(levelname)-8s [%(asctime)s] [%(correlation_id)s] %(name)s: %(message)s" + }, }, "handlers": { "console": { "level": "DEBUG", "class": "logging.StreamHandler", "formatter": "standard", + "filters": ["correlation_id"], }, }, "loggers": { "": {"handlers": ["console"], "level": "INFO"}, "celery": {"handlers": ["console"], "level": "INFO"}, + "django_guid": { + "handlers": ["console"], + "level": "WARNING", + "propagate": False, + }, }, } diff --git a/backend/project_name/settings/production.py b/backend/project_name/settings/production.py index 4e3be510..b3733bf7 100644 --- a/backend/project_name/settings/production.py +++ b/backend/project_name/settings/production.py @@ -1,5 +1,6 @@ import sentry_sdk from decouple import Csv, config +from django_guid.integrations import SentryIntegration as DjangoGUIDSentryIntegration from sentry_sdk.integrations.django import DjangoIntegration from .base import * @@ -62,6 +63,13 @@ }, } +# Django GUID +DJANGO_GUID = { + "INTEGRATIONS": [ + DjangoGUIDSentryIntegration(), + ], +} + # django-log-request-id MIDDLEWARE.insert( # insert RequestIDMiddleware on the top 0, "log_request_id.middleware.RequestIDMiddleware" @@ -76,10 +84,11 @@ "filters": { "require_debug_false": {"()": "django.utils.log.RequireDebugFalse"}, "request_id": {"()": "log_request_id.filters.RequestIDFilter"}, + "correlation_id": {"()": "django_guid.log_filters.CorrelationId"}, }, "formatters": { "standard": { - "format": "%(levelname)-8s [%(asctime)s] [%(request_id)s] %(name)s: %(message)s" + "format": "%(levelname)-8s [%(asctime)s] [%(request_id)s] [%(correlation_id)s] %(name)s: %(message)s" }, }, "handlers": { @@ -94,7 +103,7 @@ "console": { "level": "DEBUG", "class": "logging.StreamHandler", - "filters": ["request_id"], + "filters": ["request_id", "correlation_id"], "formatter": "standard", }, }, @@ -114,6 +123,11 @@ "level": "DEBUG", "propagate": False, }, + "django_guid": { + "handlers": ["console"], + "level": "WARNING", + "propagate": False, + }, }, } diff --git a/pyproject.toml b/pyproject.toml index 5397ec58..d481fefd 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -31,6 +31,7 @@ setuptools = "^69.0.2" django-permissions-policy = "^4.18.0" django-csp = "^3.7" django-defender = "^0.9.7" +django-guid = "^3.4.0" [tool.poetry.group.dev.dependencies] coverage = "^7.2.7"