Skip to content

Openverse is a search engine for openly-licensed media. This monorepo includes all application code.

Set up Sentry profiling for the API (#5264)

Krystle Salazar0a92dc0b0da6
diff --git a/api/conf/settings/logging.py b/api/conf/settings/logging.py
index 0e1b598a6..3593ab7bc 100644
--- a/api/conf/settings/logging.py
+++ b/api/conf/settings/logging.py
@@ -20,8 +20,8 @@ if "django_structlog" not in INSTALLED_APPS:
MIDDLEWARE.insert(0, "django_structlog.middlewares.RequestMiddleware")
-LOG_LEVEL = config("LOG_LEVEL", default="INFO").upper()
-DJANGO_DB_LOGGING = config("DJANGO_DB_LOGGING", cast=bool, default=False)
+DJANGO_LOG_LEVEL = config("DJANGO_LOG_LEVEL", default="INFO").upper()
+DJANGO_DATABASE_LOGGING = config("DJANGO_DATABASE_LOGGING", cast=bool, default=False)
LOG_PROCESSOR = config(
"LOG_PROCESSOR",
default="console" if ENVIRONMENT == "local" else "json",
@@ -118,7 +118,7 @@ LOGGING = {
},
"handlers": {
"console_structured": {
- "level": LOG_LEVEL,
+ "level": DJANGO_LOG_LEVEL,
"class": "logging.StreamHandler",
"formatter": "structured",
"filters": ["suppress_unwanted_logs"],
@@ -126,7 +126,7 @@ LOGGING = {
},
"root": {
"handlers": ["console_structured"],
- "level": LOG_LEVEL,
+ "level": DJANGO_LOG_LEVEL,
"propagate": False,
},
"loggers": {
@@ -139,7 +139,7 @@ LOGGING = {
},
"uvicorn": {
"handlers": ["console_structured"],
- "level": LOG_LEVEL,
+ "level": DJANGO_LOG_LEVEL,
},
},
}
@@ -158,9 +158,9 @@ structlog.configure(
cache_logger_on_first_use=(ENVIRONMENT == "production"),
)
-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 context
+if DJANGO_DATABASE_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 context
LOGGING["loggers"]["django.db.backends"] = {
"level": "DEBUG",
"handlers": ["console_structured"],
diff --git a/api/conf/settings/security.py b/api/conf/settings/security.py
index f14c97f22..9700a2639 100644
--- a/api/conf/settings/security.py
+++ b/api/conf/settings/security.py
@@ -67,7 +67,7 @@ CORS_EXPOSE_HEADERS = [
]
# Proxy handling, for production
-if config("IS_PROXIED", default=True, cast=bool):
+if config("DJANGO_IS_PROXIED", default=True, cast=bool):
# https://docs.djangoproject.com/en/4.0/ref/settings/#secure-proxy-ssl-header
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
diff --git a/api/conf/settings/sentry.py b/api/conf/settings/sentry.py
index dfaf7db7c..06d390a9b 100644
--- a/api/conf/settings/sentry.py
+++ b/api/conf/settings/sentry.py
@@ -8,7 +8,10 @@ from conf.settings.base import ENVIRONMENT
SENTRY_DSN = config("SENTRY_DSN", default="")
-SENTRY_SAMPLE_RATE = config("SENTRY_SAMPLE_RATE", default=0, cast=float)
+SENTRY_TRACES_SAMPLE_RATE = config("SENTRY_TRACES_SAMPLE_RATE", default=0, cast=float)
+SENTRY_PROFILES_SAMPLE_RATE = config(
+ "SENTRY_PROFILES_SAMPLE_RATE", default=0, cast=float
+)
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = config("DJANGO_DEBUG_ENABLED", default=False, cast=bool)
@@ -26,7 +29,8 @@ if not DEBUG and SENTRY_DSN:
sentry_sdk.init(
dsn=SENTRY_DSN,
integrations=INTEGRATIONS,
- traces_sample_rate=SENTRY_SAMPLE_RATE,
+ traces_sample_rate=SENTRY_TRACES_SAMPLE_RATE,
+ profiles_sample_rate=SENTRY_PROFILES_SAMPLE_RATE,
send_default_pii=False,
environment=ENVIRONMENT,
)
diff --git a/api/env.template b/api/env.template
index aa920195a..4021ed5f3 100644
--- a/api/env.template
+++ b/api/env.template
@@ -3,7 +3,9 @@ PYTHONUNBUFFERED=0
DJANGO_SETTINGS_MODULE=conf.settings
DJANGO_SECRET_KEY=example_key
DJANGO_DEBUG_ENABLED=True
+DJANGO_LOG_LEVEL=INFO
+DJANGO_IS_PROXIED=False
CANONICAL_DOMAIN=localhost:50280
ENVIRONMENT=local
@@ -25,6 +27,7 @@ ENVIRONMENT=local
#DJANGO_DATABASE_PASSWORD=deploy
#DJANGO_DATABASE_NAME=openledger
#DJANGO_DATABASE_APPLICATION_NAME=openverse-api
+#DJANGO_DATABASE_LOGGING=False
SEMANTIC_VERSION=1.0.0
@@ -49,10 +52,9 @@ SEMANTIC_VERSION=1.0.0
#DEBUG_SCORES=False
#USE_RANK_FEATURES=True
-IS_PROXIED=False
-
-#SENTRY_SAMPLE_RATE=1.0
#SENTRY_DSN=
+#SENTRY_TRACES_SAMPLE_RATE=0
+#SENTRY_PROFILES_SAMPLE_RATE=0
FILTER_DEAD_LINKS_BY_DEFAULT=False
ENABLE_FILTERED_INDEX_QUERIES=True