Skip to content

Commit

Permalink
Set up Sentry profiling for the API (#5264)
Browse files Browse the repository at this point in the history
  • Loading branch information
krysal authored Dec 11, 2024
1 parent 7679d55 commit 0a92dc0
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 14 deletions.
16 changes: 8 additions & 8 deletions api/conf/settings/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -118,15 +118,15 @@ 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"],
},
},
"root": {
"handlers": ["console_structured"],
"level": LOG_LEVEL,
"level": DJANGO_LOG_LEVEL,
"propagate": False,
},
"loggers": {
Expand All @@ -139,7 +139,7 @@ def suppress_unwanted_logs(record: LogRecord) -> bool:
},
"uvicorn": {
"handlers": ["console_structured"],
"level": LOG_LEVEL,
"level": DJANGO_LOG_LEVEL,
},
},
}
Expand All @@ -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"],
Expand Down
2 changes: 1 addition & 1 deletion api/conf/settings/security.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down
8 changes: 6 additions & 2 deletions api/conf/settings/sentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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,
)
Expand Down
8 changes: 5 additions & 3 deletions api/env.template
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand All @@ -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
Expand Down

0 comments on commit 0a92dc0

Please sign in to comment.