diff --git a/app/app/settings.py b/app/app/settings.py index 6d0e314c7e3..3384f87e9a3 100644 --- a/app/app/settings.py +++ b/app/app/settings.py @@ -24,6 +24,9 @@ import environ import raven +import sentry_sdk +from sentry_sdk.integrations.django import DjangoIntegration + from boto3.session import Session from easy_thumbnails.conf import Settings as easy_thumbnails_defaults @@ -75,7 +78,6 @@ 'autotranslate', 'django_extensions', 'easy_thumbnails', - 'raven.contrib.django.raven_compat', 'health_check', 'health_check.db', 'health_check.cache', @@ -131,7 +133,6 @@ MIDDLEWARE = [ 'corsheaders.middleware.CorsMiddleware', 'django.middleware.security.SecurityMiddleware', - 'raven.contrib.django.raven_compat.middleware.SentryResponseErrorIdMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', 'app.middleware.drop_accept_langauge', 'django.middleware.locale.LocaleMiddleware', @@ -255,67 +256,93 @@ RELEASE = raven.fetch_git_sha(os.path.abspath(os.pardir)) if ENV == 'prod' else '' RAVEN_JS_VERSION = env.str('RAVEN_JS_VERSION', default='3.26.4') if SENTRY_DSN: + sentry_sdk.init( + SENTRY_DSN, + integrations=[DjangoIntegration()] + ) RAVEN_CONFIG = { 'dsn': SENTRY_DSN, } if RELEASE: RAVEN_CONFIG['release'] = RELEASE + +LOGGING = { + 'version': 1, + 'disable_existing_loggers': False, + 'filters': { + 'host_filter': { + '()': 'app.log_filters.HostFilter', + } + }, + 'root': { + 'level': 'INFO', + 'handlers': ['console'], + }, + 'formatters': { + 'simple': { + 'format': '%(asctime)s %(name)-12s [%(levelname)-8s] %(message)s', + 'datefmt': '%Y-%m-%d %H:%M:%S' + }, + 'verbose': { + 'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s' + }, + }, + 'handlers': { + 'console': { + 'level': 'DEBUG', + 'class': 'logging.StreamHandler', + 'formatter': 'verbose', + }, + }, + 'loggers': { + 'django.db.backends': { + 'level': 'INFO', + 'handlers': ['console'], + 'propagate': False, + }, + 'django.security.*': { + 'handlers': ['console'], + 'level': DEBUG and 'DEBUG' or 'INFO', + }, + 'django.request': { + 'handlers': ['console'], + 'level': DEBUG and 'DEBUG' or 'INFO', + }, + }, +} + +# Production logging if ENV not in ['local', 'test', 'staging', 'preview']: + # add AWS monitoring boto3_session = Session( aws_access_key_id=AWS_ACCESS_KEY_ID, aws_secret_access_key=AWS_SECRET_ACCESS_KEY, region_name=AWS_DEFAULT_REGION ) - LOGGING = { - 'version': 1, - 'disable_existing_loggers': True, - 'filters': { - 'host_filter': { - '()': 'app.log_filters.HostFilter', - } - }, - 'root': { - 'level': 'INFO', - 'handlers': ['console', 'watchtower', ], - }, - 'formatters': { - 'simple': { - 'format': '%(asctime)s %(name)-12s [%(levelname)-8s] %(message)s', - 'datefmt': '%Y-%m-%d %H:%M:%S' - }, - 'cloudwatch': { - 'format': '%(hostname)s %(name)-12s [%(levelname)-8s] %(message)s', - }, - 'verbose': { - 'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s' - }, - }, - 'handlers': { - 'console': { - 'level': 'DEBUG', - 'class': 'loggging.StreamHandler', - 'formatter': 'verbose', - }, - 'watchtower': { - 'level': AWS_LOG_LEVEL, - 'class': 'watchtower.django.DjangoCloudWatchLogHandler', - 'boto3_session': boto3_session, - 'log_group': AWS_LOG_GROUP, - 'stream_name': AWS_LOG_STREAM, - 'filters': ['host_filter'], - 'formatter': 'cloudwatch', - }, - }, - 'loggers': { - 'django.db.backends': { - 'level': AWS_LOG_LEVEL, - 'handlers': ['console', 'watchtower'], - 'propagate': False, - }, - }, + + LOGGING['formatters']['cloudwatch'] = { + 'format': '%(hostname)s %(name)-12s [%(levelname)-8s] %(message)s', } + LOGGING['handlers']['watchtower'] = { + 'level': AWS_LOG_LEVEL, + 'class': 'watchtower.django.DjangoCloudWatchLogHandler', + 'boto3_session': boto3_session, + 'log_group': AWS_LOG_GROUP, + 'stream_name': AWS_LOG_STREAM, + 'filters': ['host_filter'], + 'formatter': 'cloudwatch', + } + LOGGING['root']['handlers'].push('watchtower') + LOGGING['loggers']['django.db.backends']['handlers'].push('watchtower') + LOGGING['loggers']['django.db.backends']['level'] = AWS_LOG_LEVEL + + LOGGING['loggers']['django.request'] = LOGGING['loggers']['django.db.backends'] + LOGGING['loggers']['django.security.*'] = LOGGING['loggers']['django.db.backends'] + for ia in INSTALLED_APPS: + LOGGING['loggers'][ia] = LOGGING['loggers']['django.db.backends'] + # add elasticsearch monitoring if ENABLE_APM: LOGGING['handlers']['elasticapm'] = { 'level': 'WARNING', @@ -328,66 +355,6 @@ } LOGGING['root']['handlers'] = ['sentry', 'elasticapm'] - LOGGING['loggers']['django.request'] = LOGGING['loggers']['django.db.backends'] - LOGGING['loggers']['django.security.*'] = LOGGING['loggers']['django.db.backends'] - for ia in INSTALLED_APPS: - LOGGING['loggers'][ia] = LOGGING['loggers']['django.db.backends'] -else: - LOGGING = {} - -if SENTRY_DSN: - if ENV == 'prod': - LOGGING = { - 'version': 1, - 'disable_existing_loggers': True, - 'filters': { - 'host_filter': { - '()': 'app.log_filters.HostFilter', - } - }, - 'formatters': { - 'simple': { - 'format': '%(asctime)s %(name)-12s [%(levelname)-8s] %(message)s', - 'datefmt': '%Y-%m-%d %H:%M:%S' - }, - 'verbose': { - 'format': '%(levelname)s %(asctime)s %(module)s %(process)d %(thread)d %(message)s' - }, - }, - 'handlers': { - 'console': { - 'level': 'INFO', - 'class': 'logging.StreamHandler', - 'formatter': 'verbose', - }, - 'sentry': { - 'level': 'INFO', # To capture more than ERROR, change to WARNING, INFO, etc. - 'class': 'raven.contrib.django.raven_compat.handlers.SentryHandler', - }, - }, - 'loggers': { - 'django.db.backends': { - 'level': 'DEBUG', - 'handlers': ['console'], - 'propagate': False, - }, - 'root': { - 'level': 'WARNING', - 'handlers': ['sentry'], - }, - 'raven': { - 'level': 'DEBUG', - 'handlers': ['console'], - 'propagate': False, - }, - 'sentry.errors': { - 'level': 'DEBUG', - 'handlers': ['console'], - 'propagate': False, - }, - }, - } - GEOIP_PATH = env('GEOIP_PATH', default='/usr/share/GeoIP/') # Static files (CSS, JavaScript, Images) diff --git a/docs/ENVIRONMENT_VARIABLES.md b/docs/ENVIRONMENT_VARIABLES.md index 0179a72f9b7..8046cb1b538 100644 --- a/docs/ENVIRONMENT_VARIABLES.md +++ b/docs/ENVIRONMENT_VARIABLES.md @@ -79,9 +79,7 @@ For further information, please check out the [Gitcoin Bot Documentation](https: | Variable | Description | Type | Default | | --- | --- | --- | --- | | SENTRY_DSN | The [Sentry](https://sentry.io) DSN. | `str` | '' | -| SENTRY_TOKEN | A bearer token with project:releases permissions. | `str` | '' | -| SENTRY_ORG | The name of your Sentry organization. | `str` | '' | -| SENTRY_PROJECT | The name of your Sentry project. | `str` | '' | +| SENTRY_JS_DSN | The DSN for Javascript Sentry reporting (defaults to SENTRY_DSN). | `str` | '' | ## SendGrid diff --git a/docs/THIRD_PARTY_SETUP.md b/docs/THIRD_PARTY_SETUP.md index afe9bf11f4c..558e7bc1ab4 100644 --- a/docs/THIRD_PARTY_SETUP.md +++ b/docs/THIRD_PARTY_SETUP.md @@ -99,8 +99,5 @@ If you would like to track errors of your local environment, setup an account at Once you have access to your project secrets, you can enable Sentry error tracking for both the backend and frontend by adding the following environment variables to `web/app/app/.env`: ```shell -SENTRY_USER=xxx -SENTRY_PASSWORD=xxx -SENTRY_ADDRESS=https://sentry.example.xxx -SENTRY_PROJECT=2 +SENTRY_DSN=xxx ``` diff --git a/requirements/base.txt b/requirements/base.txt index e1a2c116147..4188619300d 100644 --- a/requirements/base.txt +++ b/requirements/base.txt @@ -64,7 +64,6 @@ ecdsa pysha3 oyaml pygithub==1.43.2 -raven==6.9.0 django-cacheops==4.0.7 django-redis==4.10.0 collectfast==0.6.2 @@ -80,3 +79,5 @@ redis-semaphore watchtower==0.5.4 Wand==0.4.4 wagtail==2.3 +raven==6.9.0 +sentry-sdk