Openverse is a search engine for openly-licensed media. This monorepo includes all application code.
Log DB queries in production (#945)
Zack Krida756b521d6d57
diff --git a/api/catalog/api/middleware/force_debug_cursor_middleware.py b/api/catalog/api/middleware/force_debug_cursor_middleware.pynew file mode 100644index 000000000..142ecb634--- /dev/null+++ b/api/catalog/api/middleware/force_debug_cursor_middleware.py@@ -0,0 +1,14 @@+from django.db import connection+++# Makes DB query logging possible when debugging is disabled+# by forcing the connection to use a debug cursor.+#+# WARNING: This can have performance implications and+# should only be used in production temporarily.+def force_debug_cursor_middleware(get_response):+ def middleware(request):+ connection.force_debug_cursor = True+ return get_response(request)++ return middlewarediff --git a/api/catalog/configuration/logging.py b/api/catalog/configuration/logging.pyindex 38dd44fe2..62cf0ac40 100644--- a/api/catalog/configuration/logging.py+++ b/api/catalog/configuration/logging.py@@ -9,7 +9,7 @@ def health_check_filter(record: LogRecord) -> bool:LOG_LEVEL = config("LOG_LEVEL", default="INFO").upper()-+DJANGO_DB_LOGGING = config("DJANGO_DB_LOGGING", cast=bool, default=False)# Logging configurationLOGGING = {@@ -45,7 +45,7 @@ LOGGING = {},# Add a clause to log error messages to the console in production"console_prod": {- "level": "WARNING",+ "level": LOG_LEVEL,"filters": ["require_debug_false", "request_id"],"class": "logging.StreamHandler","formatter": "console",@@ -95,10 +95,11 @@ LOGGING = {},}-if config("DJANGO_DB_LOGGING", cast=bool, default=False):+if DJANGO_DB_LOGGING:# Behind a separate flag as it's a very noisy debug logger# and it's nice to be able to enable it conditionally within that contextLOGGING["loggers"]["django.db.backends"] = {"level": "DEBUG","handlers": ["console", "console_prod"],+ "propagate": False,}diff --git a/api/catalog/settings.py b/api/catalog/settings.pyindex 7b0e5dfce..b39835b1f 100644--- a/api/catalog/settings.py+++ b/api/catalog/settings.py@@ -24,7 +24,7 @@ from catalog.configuration.elasticsearch import ES, MEDIA_INDEX_MAPPINGfrom catalog.configuration.link_validation_cache import (LinkValidationCacheExpiryConfiguration,)-from catalog.configuration.logging import LOGGING+from catalog.configuration.logging import DJANGO_DB_LOGGING, LOGGING# Build paths inside the project like this: BASE_DIR.join('dir', 'subdir'...)@@ -109,6 +109,13 @@ MIDDLEWARE = ["oauth2_provider.middleware.OAuth2TokenMiddleware",]+# WARNING: This should not be run in production long-term as it can impact performance+if not DEBUG and DJANGO_DB_LOGGING:+ MIDDLEWARE.append(+ "catalog.api.middleware.force_debug_cursor_middleware.force_debug_cursor_middleware" # noqa: E501+ )++SWAGGER_SETTINGS = {"DEFAULT_INFO": "catalog.urls.swagger.open_api_info","SECURITY_DEFINITIONS": {},