Skip to content

Commit

Permalink
Add optional django-silk
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
jwhitlock committed Apr 19, 2022
1 parent ac9a4b3 commit 1fa4c72
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 12 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.env
.envrc
*.sqlite3
*.prof
node_modules
env/
__pycache__
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.env
.envrc
*.sqlite3
*.prof
node_modules
env/
__pycache__
Expand Down
34 changes: 23 additions & 11 deletions privaterelay/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import os, sys
from datetime import datetime

# This needs to be before markus, which imports pytest
IN_PYTEST = "pytest" in sys.modules

from decouple import config
import markus
import sentry_sdk
Expand All @@ -26,6 +29,14 @@

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__)))

Expand All @@ -42,6 +53,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')
Expand Down Expand Up @@ -84,9 +96,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,
Expand Down Expand Up @@ -115,9 +126,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'
Expand Down Expand Up @@ -204,8 +212,9 @@
INSTALLED_APPS += [
'debug_toolbar',
'drf_yasg',
# 'silk',
]
if USE_SILK:
INSTALLED_APPS.append("silk")

if ADMIN_ENABLED:
INSTALLED_APPS += [
Expand Down Expand Up @@ -233,11 +242,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',
Expand Down Expand Up @@ -695,3 +703,7 @@ def _get_initial_middleware():
}
]
)

if USE_SILK:
SILKY_PYTHON_PROFILER=True
SILKY_PYTHON_PROFILER_BINARY=True
4 changes: 3 additions & 1 deletion privaterelay/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 += [
Expand Down

0 comments on commit 1fa4c72

Please sign in to comment.