Skip to content

Commit

Permalink
Add Sentry exception monitoring
Browse files Browse the repository at this point in the history
Why these changes are being introduced:
We need to centralize exception monitoring and receive notifications for
exceptions in staging and prod environments. Sentry is our standard
service for this.

How this addresses that need:
* Adds SENTRY_DSN value to config with appropriate setting from SSM/env
  based on workspace
* Initializes sentry in the Submitter app initialization
* Adds a check_sentry() method to Config class to allow local testing
  of the Sentry integration
* Updates relevant tests

Side effects of this change:
None. The necessary DSS project and appropriate alerts have been set up
in Sentry, and the relevant SSM parameter has been added to Parameter
Store.

Relevant ticket(s):
* https://mitlibraries.atlassian.net/browse/ETD-443
  • Loading branch information
hakbailey committed Nov 18, 2021
1 parent 5027e62 commit b23f706
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 147 deletions.
3 changes: 2 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ name = "pypi"
[packages]
click = "*"
boto3 = "*"
smart-open = "*"
dspace-python-client = {git = "https://github.com/mitlibraries/dspace-python-client.git"}
sentry-sdk = "*"
smart-open = "*"

[dev-packages]
flake8 = "*"
Expand Down
299 changes: 156 additions & 143 deletions Pipfile.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ def mocked_ssm(aws_credentials):
Value="info",
Type="String",
)
ssm.put_parameter(
Name="/test/example/sentry_dsn",
Value="http://12345.6789.sentry",
Type="String",
)
yield ssm


Expand Down
4 changes: 4 additions & 0 deletions submitter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"""
import logging

import sentry_sdk

from submitter.config import Config

CONFIG = Config()
Expand All @@ -14,3 +16,5 @@
logger.info(
"Logging configured with level=%s, filter=%s", CONFIG.LOG_LEVEL, CONFIG.LOG_FILTER
)
sentry_sdk.init(CONFIG.SENTRY_DSN, environment=CONFIG.ENV)
logger.info("Sentry initialized with DSN=%s and env=%s", CONFIG.SENTRY_DSN, CONFIG.ENV)
14 changes: 11 additions & 3 deletions submitter/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ def __init__(self):
print("Env variable 'WORKSPACE' is required, please set it and try again.")
raise e
self.AWS_REGION_NAME = "us-east-1"
logger.info(
"Configuring dspace-submission-service for current env: %s", self.ENV
)
print(f"Configuring dspace-submission-service for env={self.ENV}")
self.load_config_variables(self.ENV)

def load_config_variables(self, env: str):
Expand Down Expand Up @@ -49,6 +47,7 @@ def load_config_variables(self, env: str):
self.LOG_LEVEL = ssm.get_parameter_value(
self.SSM_PATH + "dss_log_level"
).upper()
self.SENTRY_DSN = ssm.get_parameter_value(self.SSM_PATH + "sentry_dsn")
self.SKIP_PROCESSING = "false"
self.SQS_ENDPOINT_URL = "https://sqs.us-east-1.amazonaws.com/"
elif env == "test":
Expand All @@ -59,6 +58,7 @@ def load_config_variables(self, env: str):
self.INPUT_QUEUE = "test_queue_with_messages"
self.LOG_FILTER = "true"
self.LOG_LEVEL = os.getenv("DSS_LOG_LEVEL", "INFO").upper()
self.SENTRY_DSN = None
self.SKIP_PROCESSING = "false"
self.SQS_ENDPOINT_URL = "https://sqs.us-east-1.amazonaws.com/"
else:
Expand All @@ -69,5 +69,13 @@ def load_config_variables(self, env: str):
self.INPUT_QUEUE = os.getenv("DSS_INPUT_QUEUE")
self.LOG_FILTER = os.getenv("DSS_LOG_FILTER", "true").lower()
self.LOG_LEVEL = os.getenv("DSS_LOG_LEVEL", "INFO").upper()
self.SENTRY_DSN = os.getenv("DSS_SENTRY_DSN")
self.SKIP_PROCESSING = os.environ.get("SKIP_PROCESSING", "false").lower()
self.SQS_ENDPOINT_URL = os.environ.get("SQS_ENDPOINT_URL")

def check_sentry(self):
if self.SENTRY_DSN:
logger.info("Sending a Zero Division Error to Sentry")
1 / 0
else:
logger.info("No Sentry DSN found")
3 changes: 3 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def test_prod_stage_config_success(mocked_ssm):
assert config.INPUT_QUEUE == "empty_input_queue"
assert config.LOG_FILTER == "false"
assert config.LOG_LEVEL == "INFO"
assert config.SENTRY_DSN == "http://12345.6789.sentry"
assert config.SKIP_PROCESSING == "false"
assert config.SQS_ENDPOINT_URL == "https://sqs.us-east-1.amazonaws.com/"

Expand All @@ -42,6 +43,7 @@ def test_dev_config_success():
os.environ.pop("DSS_INPUT_QUEUE", None)
os.environ["DSS_LOG_FILTER"] = "False"
os.environ["DSS_LOG_LEVEL"] = "debug"
os.environ.pop("SENTRY_DSN", None)
os.environ["SKIP_PROCESSING"] = "True"
os.environ.pop("SQS_ENDPOINT_URL", None)
config = Config()
Expand All @@ -52,5 +54,6 @@ def test_dev_config_success():
assert config.INPUT_QUEUE is None
assert config.LOG_FILTER == "false"
assert config.LOG_LEVEL == "DEBUG"
assert config.SENTRY_DSN is None
assert config.SKIP_PROCESSING == "true"
assert config.SQS_ENDPOINT_URL is None

0 comments on commit b23f706

Please sign in to comment.