From de62c390223c269a97d73eb828263f1ed3f5e0c5 Mon Sep 17 00:00:00 2001 From: Caitlin Barnard Date: Wed, 2 Oct 2024 13:28:38 +0100 Subject: [PATCH 1/5] Set up sentry monitoring --- config/settings/base.py | 43 +++++++++++++++++++++++++++-------------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/config/settings/base.py b/config/settings/base.py index 128b380b1..e2ee44217 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -15,11 +15,14 @@ import dj_database_url import environ +import sentry_sdk from dbt_copilot_python.database import database_url_from_env from dbt_copilot_python.utility import is_copilot from django.urls import reverse_lazy from django_log_formatter_asim import ASIMFormatter from django_log_formatter_ecs import ECSFormatter +from sentry_sdk.integrations.django import DjangoIntegration +from sentry_sdk.integrations.redis import RedisIntegration BASE_DIR = Path(__file__).resolve().parent.parent.parent @@ -111,9 +114,7 @@ if is_copilot(): DATABASES = { - "default": dj_database_url.config( - default=database_url_from_env("DATABASE_CREDENTIALS") - ) + "default": dj_database_url.config(default=database_url_from_env("DATABASE_CREDENTIALS")) } else: if "postgres" in VCAP_SERVICES: @@ -129,9 +130,7 @@ # https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ - { - "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator" - }, # noqa + {"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator"}, # noqa {"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator"}, {"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"}, {"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"}, @@ -153,9 +152,7 @@ def FILTERS_VERBOSE_LOOKUPS(): from django_filters.conf import DEFAULTS verbose_lookups = DEFAULTS["VERBOSE_LOOKUPS"].copy() - verbose_lookups.update( - {"icontains": "", "contains": "", "startswith": "", "istartswith": ""} - ) + verbose_lookups.update({"icontains": "", "contains": "", "startswith": "", "istartswith": ""}) return verbose_lookups @@ -196,9 +193,7 @@ def FILTERS_VERBOSE_LOOKUPS(): # If "temp" is in instance name it means it's the temp files bucket if "temp" in bucket["instance_name"]: TEMP_FILE_AWS_ACCESS_KEY_ID = app_bucket_credentials["aws_access_key_id"] - TEMP_FILE_AWS_SECRET_ACCESS_KEY = app_bucket_credentials[ - "aws_secret_access_key" - ] + TEMP_FILE_AWS_SECRET_ACCESS_KEY = app_bucket_credentials["aws_secret_access_key"] TEMP_FILE_AWS_REGION = app_bucket_credentials["aws_region"] TEMP_FILE_AWS_S3_REGION_NAME = app_bucket_credentials["aws_region"] TEMP_FILE_AWS_STORAGE_BUCKET_NAME = app_bucket_credentials["bucket_name"] @@ -219,9 +214,7 @@ def FILTERS_VERBOSE_LOOKUPS(): TEMP_FILE_AWS_SECRET_ACCESS_KEY = env("TEMP_FILE_AWS_SECRET_ACCESS_KEY", default="") TEMP_FILE_AWS_REGION = env("TEMP_FILE_AWS_REGION", default="") TEMP_FILE_AWS_S3_REGION_NAME = env("TEMP_FILE_AWS_REGION", default="") - TEMP_FILE_AWS_STORAGE_BUCKET_NAME = env( - "TEMP_FILE_AWS_STORAGE_BUCKET_NAME", default="" - ) + TEMP_FILE_AWS_STORAGE_BUCKET_NAME = env("TEMP_FILE_AWS_STORAGE_BUCKET_NAME", default="") AWS_S3_CUSTOM_DOMAIN = "%s.s3.amazonaws.com" % AWS_STORAGE_BUCKET_NAME AWS_S3_OBJECT_PARAMETERS = {"CacheControl": "max-age=86400"} @@ -384,3 +377,23 @@ def FILTERS_VERBOSE_LOOKUPS(): }, }, } + + +# Sentry +# https://docs.sentry.io/platforms/python/guides/django/ +SENTRY_ENVIRONMENT = env("SENTRY_ENVIRONMENT", None) +SENTRY_DSN = env("SENTRY_DSN", None) + +# Configure sentry if a DSN is set +if SENTRY_DSN: + # AWS Prefix needs to be removed once migration is complete + sentry_environment = f"aws-{SENTRY_ENVIRONMENT}" if is_copilot() else SENTRY_ENVIRONMENT + sentry_sdk.init( + dsn=SENTRY_DSN, + environment=sentry_environment, + release=GIT_COMMIT, + integrations=[DjangoIntegration(), RedisIntegration()], + enable_tracing=env.bool("SENTRY_ENABLE_TRACING", False), + traces_sample_rate=env.float("SENTRY_TRACES_SAMPLE_RATE", 0.0), + send_default_pii=True, + ) From 241918852f5cf925d0674f37e4e13967056b9913 Mon Sep 17 00:00:00 2001 From: Caitlin Barnard Date: Wed, 2 Oct 2024 15:20:47 +0100 Subject: [PATCH 2/5] Revert formatting --- config/settings/base.py | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/config/settings/base.py b/config/settings/base.py index e2ee44217..421471967 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -114,7 +114,9 @@ if is_copilot(): DATABASES = { - "default": dj_database_url.config(default=database_url_from_env("DATABASE_CREDENTIALS")) + "default": dj_database_url.config( + default=database_url_from_env("DATABASE_CREDENTIALS") + ) } else: if "postgres" in VCAP_SERVICES: @@ -130,7 +132,9 @@ # https://docs.djangoproject.com/en/2.0/ref/settings/#auth-password-validators AUTH_PASSWORD_VALIDATORS = [ - {"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator"}, # noqa + { + "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator" + }, # noqa {"NAME": "django.contrib.auth.password_validation.MinimumLengthValidator"}, {"NAME": "django.contrib.auth.password_validation.CommonPasswordValidator"}, {"NAME": "django.contrib.auth.password_validation.NumericPasswordValidator"}, @@ -152,7 +156,9 @@ def FILTERS_VERBOSE_LOOKUPS(): from django_filters.conf import DEFAULTS verbose_lookups = DEFAULTS["VERBOSE_LOOKUPS"].copy() - verbose_lookups.update({"icontains": "", "contains": "", "startswith": "", "istartswith": ""}) + verbose_lookups.update( + {"icontains": "", "contains": "", "startswith": "", "istartswith": ""} + ) return verbose_lookups @@ -193,7 +199,9 @@ def FILTERS_VERBOSE_LOOKUPS(): # If "temp" is in instance name it means it's the temp files bucket if "temp" in bucket["instance_name"]: TEMP_FILE_AWS_ACCESS_KEY_ID = app_bucket_credentials["aws_access_key_id"] - TEMP_FILE_AWS_SECRET_ACCESS_KEY = app_bucket_credentials["aws_secret_access_key"] + TEMP_FILE_AWS_SECRET_ACCESS_KEY = app_bucket_credentials[ + "aws_secret_access_key" + ] TEMP_FILE_AWS_REGION = app_bucket_credentials["aws_region"] TEMP_FILE_AWS_S3_REGION_NAME = app_bucket_credentials["aws_region"] TEMP_FILE_AWS_STORAGE_BUCKET_NAME = app_bucket_credentials["bucket_name"] @@ -214,7 +222,9 @@ def FILTERS_VERBOSE_LOOKUPS(): TEMP_FILE_AWS_SECRET_ACCESS_KEY = env("TEMP_FILE_AWS_SECRET_ACCESS_KEY", default="") TEMP_FILE_AWS_REGION = env("TEMP_FILE_AWS_REGION", default="") TEMP_FILE_AWS_S3_REGION_NAME = env("TEMP_FILE_AWS_REGION", default="") - TEMP_FILE_AWS_STORAGE_BUCKET_NAME = env("TEMP_FILE_AWS_STORAGE_BUCKET_NAME", default="") + TEMP_FILE_AWS_STORAGE_BUCKET_NAME = env( + "TEMP_FILE_AWS_STORAGE_BUCKET_NAME", default="" + ) AWS_S3_CUSTOM_DOMAIN = "%s.s3.amazonaws.com" % AWS_STORAGE_BUCKET_NAME AWS_S3_OBJECT_PARAMETERS = {"CacheControl": "max-age=86400"} @@ -387,7 +397,9 @@ def FILTERS_VERBOSE_LOOKUPS(): # Configure sentry if a DSN is set if SENTRY_DSN: # AWS Prefix needs to be removed once migration is complete - sentry_environment = f"aws-{SENTRY_ENVIRONMENT}" if is_copilot() else SENTRY_ENVIRONMENT + sentry_environment = ( + f"aws-{SENTRY_ENVIRONMENT}" if is_copilot() else SENTRY_ENVIRONMENT + ) sentry_sdk.init( dsn=SENTRY_DSN, environment=sentry_environment, From 47a71c69d8ce939b3f2cc18334270f8ed5e30111 Mon Sep 17 00:00:00 2001 From: Caitlin Barnard Date: Wed, 2 Oct 2024 15:22:19 +0100 Subject: [PATCH 3/5] Add sentry environment to ci envs --- .env.ci | 1 + 1 file changed, 1 insertion(+) diff --git a/.env.ci b/.env.ci index 35544d616..ca355542e 100644 --- a/.env.ci +++ b/.env.ci @@ -7,3 +7,4 @@ CACHE_ENDPOINT=redis://redis:6379 AUTHBROKER_CLIENT_ID= AUTHBROKER_CLIENT_SECRET= AUTHBROKER_URL= +SENTRY_ENVIRONMENT=ci \ No newline at end of file From 270ef251875eb46c6ede24c6b16c960805952c38 Mon Sep 17 00:00:00 2001 From: Caitlin Barnard Date: Wed, 2 Oct 2024 15:27:07 +0100 Subject: [PATCH 4/5] Add sentry dsn to ci envs --- .env.ci | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.env.ci b/.env.ci index ca355542e..9fbfc44f9 100644 --- a/.env.ci +++ b/.env.ci @@ -7,4 +7,5 @@ CACHE_ENDPOINT=redis://redis:6379 AUTHBROKER_CLIENT_ID= AUTHBROKER_CLIENT_SECRET= AUTHBROKER_URL= -SENTRY_ENVIRONMENT=ci \ No newline at end of file +SENTRY_ENVIRONMENT=ci +SENTRY_DSN= \ No newline at end of file From 4a32a98bd9f65e3dd7ae0a7193fd70ab247964d0 Mon Sep 17 00:00:00 2001 From: Sam Dudley Date: Thu, 3 Oct 2024 10:05:10 +0100 Subject: [PATCH 5/5] finishing touches --- config/settings/base.py | 6 +++--- config/settings/dev.py | 6 ------ config/settings/prod.py | 6 ------ 3 files changed, 3 insertions(+), 15 deletions(-) diff --git a/config/settings/base.py b/config/settings/base.py index 421471967..2fa2e73a4 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -391,12 +391,12 @@ def FILTERS_VERBOSE_LOOKUPS(): # Sentry # https://docs.sentry.io/platforms/python/guides/django/ -SENTRY_ENVIRONMENT = env("SENTRY_ENVIRONMENT", None) -SENTRY_DSN = env("SENTRY_DSN", None) +SENTRY_ENVIRONMENT = env.str("SENTRY_ENVIRONMENT", None) +SENTRY_DSN = env.str("SENTRY_DSN", None) # Configure sentry if a DSN is set if SENTRY_DSN: - # AWS Prefix needs to be removed once migration is complete + # TODO: AWS Prefix needs to be removed once migration is complete. sentry_environment = ( f"aws-{SENTRY_ENVIRONMENT}" if is_copilot() else SENTRY_ENVIRONMENT ) diff --git a/config/settings/dev.py b/config/settings/dev.py index 1598f641e..f671b17c3 100644 --- a/config/settings/dev.py +++ b/config/settings/dev.py @@ -24,12 +24,6 @@ # Set async file uploading ASYNC_FILE_UPLOAD = True -sentry_sdk.init( - os.environ.get("SENTRY_DSN"), - environment=os.environ.get("SENTR Y_ENVIRONMENT"), - integrations=[DjangoIntegration()], -) - # HSTS (https://man.uktrade.io/docs/procedures/1st-go-live.html) SECURE_HSTS_SECONDS = 3600 SECURE_HSTS_PRELOAD = True diff --git a/config/settings/prod.py b/config/settings/prod.py index ae3abc1b6..4415299b0 100644 --- a/config/settings/prod.py +++ b/config/settings/prod.py @@ -27,12 +27,6 @@ # Set async file uploading ASYNC_FILE_UPLOAD = True -sentry_sdk.init( - os.environ.get("SENTRY_DSN"), - environment=os.environ.get("SENTRY_ENVIRONMENT"), - integrations=[DjangoIntegration()], -) - # Django staff SSO user migration process requries the following MIGRATE_EMAIL_USER_ON_LOGIN = True