From 840b7d804c974bc3c1e3c2884610dd1300a37b4a Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 9 Nov 2023 16:33:35 -0500 Subject: [PATCH] Closes #13645: Make Sentry integration optional (#14197) --- base_requirements.txt | 4 ---- docs/administration/error-reporting.md | 18 +++--------------- docs/configuration/error-reporting.md | 3 +++ docs/installation/3-netbox.md | 11 +++++++++++ netbox/netbox/settings.py | 21 +++++++++------------ netbox/netbox/views/errors.py | 5 +++-- requirements.txt | 1 - 7 files changed, 29 insertions(+), 34 deletions(-) diff --git a/base_requirements.txt b/base_requirements.txt index 6e3c5ba1916..b659c9e8d45 100644 --- a/base_requirements.txt +++ b/base_requirements.txt @@ -126,10 +126,6 @@ PyYAML # https://github.com/psf/requests/blob/main/HISTORY.md requests -# Sentry SDK -# https://github.com/getsentry/sentry-python/blob/master/CHANGELOG.md -sentry-sdk - # Social authentication framework # https://github.com/python-social-auth/social-core/blob/master/CHANGELOG.md social-auth-core diff --git a/docs/administration/error-reporting.md b/docs/administration/error-reporting.md index 1629987741d..ccc0a84a557 100644 --- a/docs/administration/error-reporting.md +++ b/docs/administration/error-reporting.md @@ -4,27 +4,15 @@ ### Enabling Error Reporting -NetBox supports native integration with [Sentry](https://sentry.io/) for automatic error reporting. To enable this functionality, simply set `SENTRY_ENABLED` to True in `configuration.py`. Errors will be sent to a Sentry ingestor maintained by the NetBox team for analysis. - -```python -SENTRY_ENABLED = True -``` - -### Using a Custom DSN - -If you prefer instead to use your own Sentry ingestor, you'll need to first create a new project under your Sentry account to represent your NetBox deployment and obtain its corresponding data source name (DSN). This looks like a URL similar to the example below: - -``` -https://examplePublicKey@o0.ingest.sentry.io/0 -``` - -Once you have obtained a DSN, configure Sentry in NetBox's `configuration.py` file with the following parameters: +NetBox supports native integration with [Sentry](https://sentry.io/) for automatic error reporting. To enable this functionality, set `SENTRY_ENABLED` to True and define your unique [data source name (DSN)](https://docs.sentry.io/product/sentry-basics/concepts/dsn-explainer/) in `configuration.py`. ```python SENTRY_ENABLED = True SENTRY_DSN = "https://examplePublicKey@o0.ingest.sentry.io/0" ``` +Setting `SENTRY_ENABLED` to False will disable the Sentry integration. + ### Assigning Tags You can optionally attach one or more arbitrary tags to the outgoing error reports if desired by setting the `SENTRY_TAGS` parameter: diff --git a/docs/configuration/error-reporting.md b/docs/configuration/error-reporting.md index d1c47e2fb51..8c3526dec86 100644 --- a/docs/configuration/error-reporting.md +++ b/docs/configuration/error-reporting.md @@ -18,6 +18,9 @@ Default: False Set to True to enable automatic error reporting via [Sentry](https://sentry.io/). +!!! note + The `sentry-sdk` Python package is required to enable Sentry integration. + --- ## SENTRY_SAMPLE_RATE diff --git a/docs/installation/3-netbox.md b/docs/installation/3-netbox.md index 0713d12e392..4043416a371 100644 --- a/docs/installation/3-netbox.md +++ b/docs/installation/3-netbox.md @@ -227,6 +227,17 @@ sudo sh -c "echo 'boto3' >> /opt/netbox/local_requirements.txt" !!! info These packages were previously required in NetBox v3.5 but now are optional. +### Sentry Integration + +NetBox may be configured to send error reports to [Sentry](../administration/error-reporting.md) for analysis. This integration requires installation of the `sentry-sdk` Python library. + +```no-highlight +sudo sh -c "echo 'sentry-sdk' >> /opt/netbox/local_requirements.txt" +``` + +!!! info + Sentry integration was previously included by default in NetBox v3.6 but is now optional. + ## Run the Upgrade Script Once NetBox has been configured, we're ready to proceed with the actual installation. We'll run the packaged upgrade script (`upgrade.sh`) to perform the following actions: diff --git a/netbox/netbox/settings.py b/netbox/netbox/settings.py index 09ee38d6a84..465389a1129 100644 --- a/netbox/netbox/settings.py +++ b/netbox/netbox/settings.py @@ -9,12 +9,14 @@ from urllib.parse import urlencode, urlsplit import django -import sentry_sdk from django.contrib.messages import constants as messages from django.core.exceptions import ImproperlyConfigured, ValidationError from django.core.validators import URLValidator from django.utils.encoding import force_str -from sentry_sdk.integrations.django import DjangoIntegration +try: + import sentry_sdk +except ModuleNotFoundError: + pass from netbox.config import PARAMS from netbox.constants import RQ_QUEUE_DEFAULT, RQ_QUEUE_HIGH, RQ_QUEUE_LOW @@ -39,8 +41,6 @@ f"NetBox requires Python 3.8 or later. (Currently installed: Python {platform.python_version()})" ) -DEFAULT_SENTRY_DSN = 'https://198cf560b29d4054ab8e583a1d10ea58@o1242133.ingest.sentry.io/6396485' - # # Configuration import # @@ -158,7 +158,7 @@ SCRIPTS_ROOT = getattr(configuration, 'SCRIPTS_ROOT', os.path.join(BASE_DIR, 'scripts')).rstrip('/') SEARCH_BACKEND = getattr(configuration, 'SEARCH_BACKEND', 'netbox.search.backends.CachedValueSearchBackend') SECURE_SSL_REDIRECT = getattr(configuration, 'SECURE_SSL_REDIRECT', False) -SENTRY_DSN = getattr(configuration, 'SENTRY_DSN', DEFAULT_SENTRY_DSN) +SENTRY_DSN = getattr(configuration, 'SENTRY_DSN', None) SENTRY_ENABLED = getattr(configuration, 'SENTRY_ENABLED', False) SENTRY_SAMPLE_RATE = getattr(configuration, 'SENTRY_SAMPLE_RATE', 1.0) SENTRY_TRACES_SAMPLE_RATE = getattr(configuration, 'SENTRY_TRACES_SAMPLE_RATE', 0) @@ -517,12 +517,12 @@ def _setting(name, default=None): # if SENTRY_ENABLED: + try: + from sentry_sdk.integrations.django import DjangoIntegration + except ModuleNotFoundError: + raise ImproperlyConfigured("SENTRY_ENABLED is True but the sentry-sdk package is not installed.") if not SENTRY_DSN: raise ImproperlyConfigured("SENTRY_ENABLED is True but SENTRY_DSN has not been defined.") - # If using the default DSN, force sampling rates - if SENTRY_DSN == DEFAULT_SENTRY_DSN: - SENTRY_SAMPLE_RATE = 1.0 - SENTRY_TRACES_SAMPLE_RATE = 0 # Initialize the SDK sentry_sdk.init( dsn=SENTRY_DSN, @@ -537,9 +537,6 @@ def _setting(name, default=None): # Assign any configured tags for k, v in SENTRY_TAGS.items(): sentry_sdk.set_tag(k, v) - # If using the default DSN, append a unique deployment ID tag for error correlation - if SENTRY_DSN == DEFAULT_SENTRY_DSN: - sentry_sdk.set_tag('netbox.deployment_id', DEPLOYMENT_ID) # diff --git a/netbox/netbox/views/errors.py b/netbox/netbox/views/errors.py index d1a8ccd3656..a0f783ed6c1 100644 --- a/netbox/netbox/views/errors.py +++ b/netbox/netbox/views/errors.py @@ -9,7 +9,6 @@ from django.views.decorators.csrf import requires_csrf_token from django.views.defaults import ERROR_500_TEMPLATE_NAME, page_not_found from django.views.generic import View -from sentry_sdk import capture_message from netbox.plugins.utils import get_installed_plugins @@ -34,7 +33,9 @@ def handler_404(request, exception): """ Wrap Django's default 404 handler to enable Sentry reporting. """ - capture_message("Page not found", level="error") + if settings.SENTRY_ENABLED: + from sentry_sdk import capture_message + capture_message("Page not found", level="error") return page_not_found(request, exception) diff --git a/requirements.txt b/requirements.txt index 16bafe62fe3..45fb12f8053 100644 --- a/requirements.txt +++ b/requirements.txt @@ -28,7 +28,6 @@ Pillow==10.1.0 psycopg[binary,pool]==3.1.12 PyYAML==6.0.1 requests==2.31.0 -sentry-sdk==1.34.0 social-auth-app-django==5.4.0 social-auth-core[openidconnect]==4.5.0 svgwrite==1.4.3