From 90aa7b8444d9f51e5b02e9afed19881a065a0152 Mon Sep 17 00:00:00 2001 From: Matthias Mair Date: Sat, 11 Jun 2022 15:13:13 +0200 Subject: [PATCH] Sentry (#3174) * Add sentry for optional error reporting Closes https://github.com/inventreedb/org/issues/3 Heavily inspired by https://github.com/netbox-community/netbox/issues/9340 * do not consider optional stuff in coverage * Add DSN for inventree org Co-authored-by: Oliver Walters --- InvenTree/InvenTree/settings.py | 29 +++++++++++++++++++++++++++-- InvenTree/config_template.yaml | 8 ++++++++ requirements.txt | 1 + 3 files changed, 36 insertions(+), 2 deletions(-) diff --git a/InvenTree/InvenTree/settings.py b/InvenTree/InvenTree/settings.py index 9a6b81d16f..059ae72909 100644 --- a/InvenTree/InvenTree/settings.py +++ b/InvenTree/InvenTree/settings.py @@ -23,7 +23,9 @@ from django.core.files.storage import default_storage from django.utils.translation import gettext_lazy as _ import moneyed +import sentry_sdk import yaml +from sentry_sdk.integrations.django import DjangoIntegration from .config import get_base_dir, get_config_file, get_plugin_file, get_setting @@ -33,6 +35,9 @@ def _is_true(x): return str(x).strip().lower() in ['1', 'y', 'yes', 't', 'true'] +# Default Sentry DSN (can be overriden if user wants custom sentry integration) +INVENTREE_DSN = 'https://3928ccdba1d34895abde28031fd00100@o378676.ingest.sentry.io/6494600' + # Determine if we are running in "test" mode e.g. "manage.py test" TESTING = 'test' in sys.argv # Are enviroment variables manipulated by tests? Needs to be set by testing code @@ -546,7 +551,7 @@ db_config['TEST'] = { # Set collation option for mysql test database if 'mysql' in db_engine: - db_config['TEST']['COLLATION'] = 'utf8_general_ci' + db_config['TEST']['COLLATION'] = 'utf8_general_ci' # pragma: no cover DATABASES = { 'default': db_config @@ -882,6 +887,26 @@ MARKDOWNIFY_WHITELIST_ATTRS = [ MARKDOWNIFY_BLEACH = False +# Error reporting +SENTRY_ENABLED = get_setting('INVENTREE_SENTRY_ENABLED', CONFIG.get('sentry_enabled', False)) +SENTRY_DSN = get_setting('INVENTREE_SENTRY_DSN', CONFIG.get('sentry_dsn', INVENTREE_DSN)) + +if SENTRY_ENABLED and SENTRY_DSN: # pragma: no cover + sentry_sdk.init( + dsn=SENTRY_DSN, + integrations=[DjangoIntegration(), ], + traces_sample_rate=1.0 if DEBUG else 0.15, + send_default_pii=True + ) + inventree_tags = { + 'testing': TESTING, + 'docker': DOCKER, + 'debug': DEBUG, + 'remote': REMOTE_LOGIN, + } + for key, val in inventree_tags.items(): + sentry_sdk.set_tag(f'inventree_{key}', val) + # Maintenance mode MAINTENANCE_MODE_RETRY_AFTER = 60 MAINTENANCE_MODE_STATE_BACKEND = 'maintenance_mode.backends.DefaultStorageBackend' @@ -925,6 +950,6 @@ CUSTOM_LOGO = get_setting( ) # check that the logo-file exsists in media -if CUSTOM_LOGO and not default_storage.exists(CUSTOM_LOGO): +if CUSTOM_LOGO and not default_storage.exists(CUSTOM_LOGO): # pragma: no cover CUSTOM_LOGO = False logger.warning("The custom logo file could not be found in the default media storage") diff --git a/InvenTree/config_template.yaml b/InvenTree/config_template.yaml index 6b0d3d29ae..af975e7267 100644 --- a/InvenTree/config_template.yaml +++ b/InvenTree/config_template.yaml @@ -102,6 +102,14 @@ debug: True # and only if InvenTree is accessed from a local IP (127.0.0.1) debug_toolbar: False +# Set sentry_enabled to True to report errors back to the maintainers +# Use the environment variable INVENTREE_SENTRY_ENABLED +# sentry_enabled: True + +# Set sentry_dsn to your custom DSN if you want to use your own instance for error reporting +# Use the environment variable INVENTREE_SENTRY_DSN +# sentry_dsn: https://custom@custom.ingest.sentry.io/custom + # Set this variable to True to enable InvenTree Plugins # Alternatively, use the environment variable INVENTREE_PLUGINS_ENABLED plugins_enabled: False diff --git a/requirements.txt b/requirements.txt index 564b92d4f1..26bfca213b 100644 --- a/requirements.txt +++ b/requirements.txt @@ -47,5 +47,6 @@ pygments==2.7.4 # Syntax highlighting python-barcode[images]==0.13.1 # Barcode generator qrcode[pil]==6.1 # QR code generator rapidfuzz==0.7.6 # Fuzzy string matching +sentry-sdk==1.5.12 # Error reporting (optional) tablib[xls,xlsx,yaml] # Support for XLS and XLSX formats weasyprint==55.0 # PDF generation library