From 0a92dc0b0da6cbbe92efb9bc6399e3c37e49161c Mon Sep 17 00:00:00 2001 From: Krystle Salazar Date: Wed, 11 Dec 2024 10:44:00 -0400 Subject: [PATCH] Set up Sentry profiling for the API (#5264) --- api/conf/settings/logging.py | 16 ++++++++-------- api/conf/settings/security.py | 2 +- api/conf/settings/sentry.py | 8 ++++++-- api/env.template | 8 +++++--- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/api/conf/settings/logging.py b/api/conf/settings/logging.py index 0e1b598a625..3593ab7bcb5 100644 --- a/api/conf/settings/logging.py +++ b/api/conf/settings/logging.py @@ -20,8 +20,8 @@ def health_check_filter(record: LogRecord) -> bool: 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 @@ def suppress_unwanted_logs(record: LogRecord) -> bool: }, "handlers": { "console_structured": { - "level": LOG_LEVEL, + "level": DJANGO_LOG_LEVEL, "class": "logging.StreamHandler", "formatter": "structured", "filters": ["suppress_unwanted_logs"], @@ -126,7 +126,7 @@ def suppress_unwanted_logs(record: LogRecord) -> bool: }, "root": { "handlers": ["console_structured"], - "level": LOG_LEVEL, + "level": DJANGO_LOG_LEVEL, "propagate": False, }, "loggers": { @@ -139,7 +139,7 @@ def suppress_unwanted_logs(record: LogRecord) -> bool: }, "uvicorn": { "handlers": ["console_structured"], - "level": LOG_LEVEL, + "level": DJANGO_LOG_LEVEL, }, }, } @@ -158,9 +158,9 @@ def suppress_unwanted_logs(record: LogRecord) -> bool: 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 f14c97f2240..9700a26396c 100644 --- a/api/conf/settings/security.py +++ b/api/conf/settings/security.py @@ -67,7 +67,7 @@ ] # 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 dfaf7db7c7e..06d390a9b5b 100644 --- a/api/conf/settings/sentry.py +++ b/api/conf/settings/sentry.py @@ -8,7 +8,10 @@ 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 @@ 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 aa920195a27..4021ed5f361 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