From 9abaad8efe328166e4cb6f97da867e3db73a2a47 Mon Sep 17 00:00:00 2001 From: John Whitlock Date: Mon, 18 Apr 2022 17:20:02 -0500 Subject: [PATCH] Add optional django-silk Silk is a live profiling and inspection tool for Django, suitable for local development. It is disabled when DEBUG=False. It is also disabled when running tests, since it adds database queries for writing profile data to the database. --- .dockerignore | 1 + .gitignore | 1 + privaterelay/settings.py | 38 +++++++++++++++++++++++++++----------- privaterelay/urls.py | 4 +++- 4 files changed, 32 insertions(+), 12 deletions(-) diff --git a/.dockerignore b/.dockerignore index e3bf96e1b0..09f56e71d1 100644 --- a/.dockerignore +++ b/.dockerignore @@ -2,6 +2,7 @@ .env .envrc *.sqlite3 +*.prof node_modules env/ __pycache__ diff --git a/.gitignore b/.gitignore index d01f88c975..06354538c2 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,7 @@ .env .envrc *.sqlite3 +*.prof node_modules env/ __pycache__ diff --git a/privaterelay/settings.py b/privaterelay/settings.py index 95396ca153..34bbe1af0f 100644 --- a/privaterelay/settings.py +++ b/privaterelay/settings.py @@ -22,10 +22,25 @@ from hashlib import sha512 import base64 + +# This needs to be before markus, which imports pytest +IN_PYTEST = "pytest" in sys.modules +if IN_PYTEST: + assert 1==2 + + from django.conf import settings import dj_database_url +try: + # Silk is a live profiling and inspection tool for the Django framework + # https://github.com/jazzband/django-silk + import silk + HAS_SILK = True +except ImportError: + HAS_SILK = False + # Build paths inside the project like this: os.path.join(BASE_DIR, ...) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) @@ -42,6 +57,7 @@ INTERNAL_IPS = config( 'DJANGO_INTERNAL_IPS', default=[] ) +USE_SILK = DEBUG and HAS_SILK and not IN_PYTEST # Honor the 'X-Forwarded-Proto' header for request.is_secure() SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https') @@ -84,9 +100,8 @@ "'self'", 'https://www.google-analytics.com/', ) -# TODO: Add with silk -# if settings.DEBUG: -# CSP_SCRIPT_SRC += ("'unsafe-inline'",) +if USE_SILK: + CSP_SCRIPT_SRC += ("'unsafe-inline'",) csp_style_values = ["'self'"] # Next.js dynamically inserts the relevant styles when switching pages, @@ -115,9 +130,6 @@ csp_style_values.append("'sha512-%s'" % hash) CSP_STYLE_SRC = tuple(csp_style_values) -# TODO: Add with silk -# if settings.DEBUG: -# CSP_STYLE_SRC += ("'unsafe-inline'",) CSP_IMG_SRC = ["'self'"] + AVATAR_IMG_SRC REFERRER_POLICY = 'strict-origin-when-cross-origin' @@ -204,8 +216,9 @@ INSTALLED_APPS += [ 'debug_toolbar', 'drf_yasg', -# 'silk', ] +if USE_SILK: + INSTALLED_APPS.append("silk") if ADMIN_ENABLED: INSTALLED_APPS += [ @@ -233,11 +246,10 @@ def _get_initial_middleware(): MIDDLEWARE = _get_initial_middleware() +if USE_SILK: + MIDDLEWARE.append('silk.middleware.SilkyMiddleware') if DEBUG: - MIDDLEWARE += [ -# 'silk.middleware.SilkyMiddleware', - 'debug_toolbar.middleware.DebugToolbarMiddleware', - ] + MIDDLEWARE.append('debug_toolbar.middleware.DebugToolbarMiddleware') MIDDLEWARE += [ 'django.middleware.security.SecurityMiddleware', @@ -695,3 +707,7 @@ def _get_initial_middleware(): } ] ) + +if USE_SILK: + SILKY_PYTHON_PROFILER=True + SILKY_PYTHON_PROFILER_BINARY=True diff --git a/privaterelay/urls.py b/privaterelay/urls.py index 2336e17ebe..bf82694dcb 100644 --- a/privaterelay/urls.py +++ b/privaterelay/urls.py @@ -62,8 +62,10 @@ urlpatterns += [ path('__debug__/', include(debug_toolbar.urls)), path('api-auth/', include('rest_framework.urls', namespace='rest_framework')), -# path('silk/', include('silk.urls', namespace='silk')), ] +if settings.USE_SILK: + import silk + urlpatterns.append(path('silk/', include('silk.urls', namespace='silk'))) if settings.ADMIN_ENABLED: urlpatterns += [