diff --git a/chatbot_ner/settings.py b/chatbot_ner/settings.py index 5a517bd51..fd75dfa0e 100755 --- a/chatbot_ner/settings.py +++ b/chatbot_ner/settings.py @@ -13,6 +13,8 @@ import os import sys +from chatbot_ner.setup_sentry import setup_sentry + BASE_DIR = os.path.dirname(os.path.dirname(__file__)) # Quick-start development settings - unsuitable for production @@ -28,6 +30,10 @@ ALLOWED_HOSTS = ['*'] +# setup sentry + +setup_sentry() + # Application definition INSTALLED_APPS = [ diff --git a/chatbot_ner/setup_sentry.py b/chatbot_ner/setup_sentry.py new file mode 100644 index 000000000..9446464a0 --- /dev/null +++ b/chatbot_ner/setup_sentry.py @@ -0,0 +1,35 @@ +from __future__ import absolute_import + +import os +import sys + +# HAPTIK Environment and CAS name +ENVIRONMENT = os.environ.get('ENVIRONMENT') or os.environ.get('HAPTIK_ENV') +CLIENT_APPLICATIONS_SETUP_NAME = os.environ.get('CLIENT_APPLICATIONS_SETUP_NAME') + +# Support for Sentry DSN +SENTRY_DSN = os.environ.get('SENTRY_DSN') +SENTRY_ENABLED = os.environ.get('SENTRY_ENABLED') +SENTRY_ENABLED = True if SENTRY_ENABLED == 'True' and 'test' not in sys.argv else False + + +def setup_sentry(): + """ + Setup sentry if enabled in the environment + """ + if SENTRY_ENABLED: + import sentry_sdk + from sentry_sdk.integrations.django import DjangoIntegration + from sentry_sdk.integrations.logging import LoggingIntegration + + def before_sentry_send(event, hint): + event.setdefault("tags", {})["cas_name"] = CLIENT_APPLICATIONS_SETUP_NAME + return event + + sentry_sdk.init( + dsn=SENTRY_DSN, + integrations=[DjangoIntegration(), LoggingIntegration()], + environment=ENVIRONMENT, + sample_rate=0.1, + before_send=before_sentry_send + ) diff --git a/requirements.txt b/requirements.txt index f155161a5..bebdb513f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -24,3 +24,4 @@ flake8==3.4.1 pyaml==19.4.1 coverage==4.5.3 nose-exclude==0.5.0 +sentry-sdk==0.14.1