From 84623ee91de59a99800e8cdaee990b2e747978d6 Mon Sep 17 00:00:00 2001 From: Ankur Agrawal Date: Thu, 12 Nov 2020 10:27:00 +0530 Subject: [PATCH 1/2] Add for support for sentry exception tracking for the repo --- chatbot_ner/config.py | 30 ++++++++++++++++++++++++++++++ requirements.txt | 3 +++ 2 files changed, 33 insertions(+) diff --git a/chatbot_ner/config.py b/chatbot_ner/config.py index 0720b40c8..92a0c86ed 100644 --- a/chatbot_ner/config.py +++ b/chatbot_ner/config.py @@ -2,6 +2,7 @@ import logging.handlers import os +import sys import dotenv from elasticsearch import RequestsHttpConnection @@ -39,6 +40,35 @@ ner_logger.addHandler(handler) ner_logger.addHandler(handler_stdout) +# HAPTIK Environment and CAS name +CLIENT_APPLICATIONS_SETUP_NAME = os.environ.get('CLIENT_APPLICATIONS_SETUP_NAME') +HAPTIK_ENV = os.environ.get('HAPTIK_ENV') + +# 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 +SENTRY_ALLOWED_LOGGERS = os.environ.get('SENTRY_ALLOWED_LOGGERS') +SENTRY_ALLOWED_LOGGERS = [sentry_allowed_logger.strip() for sentry_allowed_logger + in SENTRY_ALLOWED_LOGGERS.split(',')] if SENTRY_ALLOWED_LOGGERS else [] + +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=HAPTIK_ENV, + sample_rate=0.1, + before_send=before_sentry_send + ) + # SETUP NLP LIB LOGGING NLP_LIB_LOG_FILENAME = os.path.join(LOG_PATH, 'nlp_log.log') # Set up a specific logger with our desired output level diff --git a/requirements.txt b/requirements.txt index f155161a5..21e12730f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -24,3 +24,6 @@ flake8==3.4.1 pyaml==19.4.1 coverage==4.5.3 nose-exclude==0.5.0 +sentry-sdk==0.14.1 +structlog-sentry==1.2.2 +django-structlog==1.5.2 From 46d5ad1e85ae29f66334465e02d1dce5327dab53 Mon Sep 17 00:00:00 2001 From: Ankur Agrawal Date: Tue, 17 Nov 2020 10:17:54 +0530 Subject: [PATCH 2/2] - Move setup sentry inside new module setup_sentry, within a function - Remove not required packages from requirements --- chatbot_ner/config.py | 30 ------------------------------ chatbot_ner/settings.py | 6 ++++++ chatbot_ner/setup_sentry.py | 35 +++++++++++++++++++++++++++++++++++ requirements.txt | 2 -- 4 files changed, 41 insertions(+), 32 deletions(-) create mode 100644 chatbot_ner/setup_sentry.py diff --git a/chatbot_ner/config.py b/chatbot_ner/config.py index 92a0c86ed..0720b40c8 100644 --- a/chatbot_ner/config.py +++ b/chatbot_ner/config.py @@ -2,7 +2,6 @@ import logging.handlers import os -import sys import dotenv from elasticsearch import RequestsHttpConnection @@ -40,35 +39,6 @@ ner_logger.addHandler(handler) ner_logger.addHandler(handler_stdout) -# HAPTIK Environment and CAS name -CLIENT_APPLICATIONS_SETUP_NAME = os.environ.get('CLIENT_APPLICATIONS_SETUP_NAME') -HAPTIK_ENV = os.environ.get('HAPTIK_ENV') - -# 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 -SENTRY_ALLOWED_LOGGERS = os.environ.get('SENTRY_ALLOWED_LOGGERS') -SENTRY_ALLOWED_LOGGERS = [sentry_allowed_logger.strip() for sentry_allowed_logger - in SENTRY_ALLOWED_LOGGERS.split(',')] if SENTRY_ALLOWED_LOGGERS else [] - -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=HAPTIK_ENV, - sample_rate=0.1, - before_send=before_sentry_send - ) - # SETUP NLP LIB LOGGING NLP_LIB_LOG_FILENAME = os.path.join(LOG_PATH, 'nlp_log.log') # Set up a specific logger with our desired output level 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 21e12730f..bebdb513f 100644 --- a/requirements.txt +++ b/requirements.txt @@ -25,5 +25,3 @@ pyaml==19.4.1 coverage==4.5.3 nose-exclude==0.5.0 sentry-sdk==0.14.1 -structlog-sentry==1.2.2 -django-structlog==1.5.2