-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Sentry error monitoring (#253)
- Loading branch information
Showing
8 changed files
with
156 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,7 @@ | ||
FLASK_APP=eligibility_server/app.py | ||
|
||
# this sample uses an odd relative path because the value is | ||
# used by code under the eligibility_server directory | ||
# and thus the config folder is one level up | ||
|
||
ELIGIBILITY_SERVER_SETTINGS=../config/sample.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import logging | ||
import os | ||
import subprocess | ||
|
||
import sentry_sdk | ||
from sentry_sdk.integrations.flask import FlaskIntegration | ||
from sentry_sdk.scrubber import EventScrubber, DEFAULT_DENYLIST | ||
|
||
from eligibility_server.settings import Configuration | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
SENTRY_ENVIRONMENT = os.environ.get("SENTRY_ENVIRONMENT", "local") | ||
|
||
|
||
# https://stackoverflow.com/a/21901260/358804 | ||
def get_git_revision_hash(): | ||
return subprocess.check_output(["git", "rev-parse", "HEAD"]).decode("ascii").strip() | ||
|
||
|
||
def get_release() -> str: | ||
return get_git_revision_hash() | ||
|
||
|
||
def get_denylist(): | ||
# custom denylist | ||
denylist = DEFAULT_DENYLIST + ["sub", "name"] | ||
return denylist | ||
|
||
|
||
def get_traces_sample_rate(config: Configuration): | ||
rate = config.sentry_traces_sample_rate | ||
if rate < 0.0 or rate > 1.0: | ||
logger.warning("SENTRY_TRACES_SAMPLE_RATE was not in the range [0.0, 1.0], defaulting to 0.0") | ||
rate = 0.0 | ||
else: | ||
logger.info(f"SENTRY_TRACES_SAMPLE_RATE set to: {rate}") | ||
|
||
return rate | ||
|
||
|
||
def configure(config: Configuration): | ||
SENTRY_DSN = config.sentry_dsn | ||
if SENTRY_DSN: | ||
release = get_release() | ||
logger.info(f"Enabling Sentry for environment '{SENTRY_ENVIRONMENT}', release '{release}'...") | ||
|
||
sentry_sdk.init( | ||
dsn=SENTRY_DSN, | ||
integrations=[ | ||
FlaskIntegration(), | ||
], | ||
traces_sample_rate=get_traces_sample_rate(config), | ||
environment=SENTRY_ENVIRONMENT, | ||
release=release, | ||
in_app_include=["eligibility_server"], | ||
# send_default_pii must be False (the default) for a custom EventScrubber/denylist | ||
# https://docs.sentry.io/platforms/python/data-management/sensitive-data/#event_scrubber | ||
send_default_pii=False, | ||
event_scrubber=EventScrubber(denylist=get_denylist()), | ||
) | ||
|
||
sentry_sdk.set_tag("agency_name", config.agency_name) | ||
else: | ||
logger.warning("SENTRY_DSN not set, so won't send events") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters