From 15595dc6aff6af6fc108f26664354f1435ad92a0 Mon Sep 17 00:00:00 2001
From: Alexander Pace <alexander.pace@ligo.org>
Date: Tue, 20 Feb 2024 17:23:05 +0000
Subject: [PATCH] Re-enable Sentry

---
 config/settings/base.py                 | 14 ++++++++++++++
 config/settings/container/dev.py        |  5 +++--
 config/settings/container/playground.py | 18 ++++++++++++++++++
 config/settings/container/production.py |  3 ++-
 config/settings/container/test.py       |  3 ++-
 docker/entrypoint                       |  3 ++-
 requirements.txt                        |  2 +-
 7 files changed, 42 insertions(+), 6 deletions(-)

diff --git a/config/settings/base.py b/config/settings/base.py
index 8dab418ca..3dadabc86 100644
--- a/config/settings/base.py
+++ b/config/settings/base.py
@@ -4,6 +4,7 @@ import os, time, logging
 from os.path import abspath, dirname, join
 import socket
 from django.core.exceptions import ImproperlyConfigured
+from aws_xray_sdk.core.exceptions.exceptions import SegmentNotFoundException
 
 # Set up path to root of project
 BASE_DIR = abspath(join(dirname(__file__), "..", ".."))
@@ -24,6 +25,17 @@ def get_from_env(envvar, default_value=None, fail_if_not_found=True):
 def parse_envvar_bool(x):
     return x.lower() in ['t', 'true', '1']
 
+# a sentry before_send function that filters aws SegmentNotFoundException's.
+# these exceptions are harmless and occur when performing management tasks
+# outside of the core gracedb app. but sentry picks it up and reports it as
+# an error which is ANNOYING.
+def before_send(event, hint):
+    if "exc_info" in hint:
+        exc_type, exc_value, tb = hint["exc_info"]
+        if isinstance(exc_value, (SegmentNotFoundException,)):
+            return None
+        return event
+
 # Maintenance mode
 MAINTENANCE_MODE = False
 MAINTENANCE_MODE_MESSAGE = None
@@ -708,3 +720,5 @@ PUBLIC_PAGE_CACHING = int(get_from_env('DJANGO_PUBLIC_PAGE_CACHING',
 # Define the number of results per page on the public page:
 PUBLIC_PAGE_RESULTS = int(get_from_env('DJANGO_PUBLIC_PAGE_RESULTS',
     fail_if_not_found=False, default_value=15))
+
+
diff --git a/config/settings/container/dev.py b/config/settings/container/dev.py
index 349361d7a..215cf6722 100644
--- a/config/settings/container/dev.py
+++ b/config/settings/container/dev.py
@@ -61,9 +61,10 @@ if sentry_dsn is not None:
     import sentry_sdk
     from sentry_sdk.integrations.django import DjangoIntegration
     sentry_sdk.init(
-        environment='test',
+        environment='dev',
         dsn=sentry_dsn,
-        integrations=[DjangoIntegration()]
+        integrations=[DjangoIntegration()],
+        before_send=before_send,
     )
 
     # Turn off default admin error emails
diff --git a/config/settings/container/playground.py b/config/settings/container/playground.py
index 8bd24f01b..eed66489b 100644
--- a/config/settings/container/playground.py
+++ b/config/settings/container/playground.py
@@ -23,6 +23,24 @@ SEND_MATTERMOST_ALERTS = True
 # Add testserver to ALLOWED_HOSTS
 ALLOWED_HOSTS += ['testserver']
 
+# Set up Sentry for error logging
+sentry_dsn = get_from_env('DJANGO_SENTRY_DSN', fail_if_not_found=False)
+if sentry_dsn is not None:
+    USE_SENTRY = True
+
+    # Set up Sentry
+    import sentry_sdk
+    from sentry_sdk.integrations.django import DjangoIntegration
+    sentry_sdk.init(
+        environment='playground',
+        dsn=sentry_dsn,
+        integrations=[DjangoIntegration()],
+        before_send=before_send,
+    )
+
+    # Turn off default admin error emails
+    LOGGING['loggers']['django.request']['handlers'] = []
+
 # Home page stuff
 INSTANCE_TITLE = 'GraceDB Playground'
 
diff --git a/config/settings/container/production.py b/config/settings/container/production.py
index 80b05960e..6e6516df4 100644
--- a/config/settings/container/production.py
+++ b/config/settings/container/production.py
@@ -53,7 +53,8 @@ if sentry_dsn is not None:
     sentry_sdk.init(
         environment='production',
         dsn=sentry_dsn,
-        integrations=[DjangoIntegration()]
+        integrations=[DjangoIntegration()],
+        before_send=before_send,
     )
 
     # Turn off default admin error emails
diff --git a/config/settings/container/test.py b/config/settings/container/test.py
index 964b9292d..1c189cb71 100644
--- a/config/settings/container/test.py
+++ b/config/settings/container/test.py
@@ -63,7 +63,8 @@ if sentry_dsn is not None:
     sentry_sdk.init(
         environment='test',
         dsn=sentry_dsn,
-        integrations=[DjangoIntegration()]
+        integrations=[DjangoIntegration()],
+        before_send=before_send,
     )
 
     # Turn off default admin error emails
diff --git a/docker/entrypoint b/docker/entrypoint
index 2fd0bcbee..2326b1516 100644
--- a/docker/entrypoint
+++ b/docker/entrypoint
@@ -29,7 +29,8 @@ LIST="aws_ses_access_key_id
   igwn_alert_password
   gracedb_ldap_keytab
   egad_url
-  egad_api_key"
+  egad_api_key
+  django_sentry_dsn"
 
 for SECRET in $LIST
 do
diff --git a/requirements.txt b/requirements.txt
index c1a615538..4fc25d8f8 100644
--- a/requirements.txt
+++ b/requirements.txt
@@ -45,7 +45,7 @@ pymemcache==4.0.0
 #pyopenssl==23.0.0
 scipy==1.11.1
 scitokens==1.7.4
-sentry-sdk==0.7.10
+sentry-sdk==1.40.1
 service_identity==23.1.0
 simplejson==3.19.1
 six==1.16.0
-- 
GitLab