Closes #13645: Make Sentry integration optional (#14197)

This commit is contained in:
Jeremy Stretch 2023-11-09 16:33:35 -05:00 committed by GitHub
parent 3d20276f55
commit 840b7d804c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 29 additions and 34 deletions

View File

@ -126,10 +126,6 @@ PyYAML
# https://github.com/psf/requests/blob/main/HISTORY.md # https://github.com/psf/requests/blob/main/HISTORY.md
requests requests
# Sentry SDK
# https://github.com/getsentry/sentry-python/blob/master/CHANGELOG.md
sentry-sdk
# Social authentication framework # Social authentication framework
# https://github.com/python-social-auth/social-core/blob/master/CHANGELOG.md # https://github.com/python-social-auth/social-core/blob/master/CHANGELOG.md
social-auth-core social-auth-core

View File

@ -4,27 +4,15 @@
### Enabling Error Reporting ### 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. 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
```
### 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:
```python ```python
SENTRY_ENABLED = True SENTRY_ENABLED = True
SENTRY_DSN = "https://examplePublicKey@o0.ingest.sentry.io/0" SENTRY_DSN = "https://examplePublicKey@o0.ingest.sentry.io/0"
``` ```
Setting `SENTRY_ENABLED` to False will disable the Sentry integration.
### Assigning Tags ### Assigning Tags
You can optionally attach one or more arbitrary tags to the outgoing error reports if desired by setting the `SENTRY_TAGS` parameter: You can optionally attach one or more arbitrary tags to the outgoing error reports if desired by setting the `SENTRY_TAGS` parameter:

View File

@ -18,6 +18,9 @@ Default: False
Set to True to enable automatic error reporting via [Sentry](https://sentry.io/). 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 ## SENTRY_SAMPLE_RATE

View File

@ -227,6 +227,17 @@ sudo sh -c "echo 'boto3' >> /opt/netbox/local_requirements.txt"
!!! info !!! info
These packages were previously required in NetBox v3.5 but now are optional. 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 ## 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: 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:

View File

@ -9,12 +9,14 @@ import warnings
from urllib.parse import urlencode, urlsplit from urllib.parse import urlencode, urlsplit
import django import django
import sentry_sdk
from django.contrib.messages import constants as messages from django.contrib.messages import constants as messages
from django.core.exceptions import ImproperlyConfigured, ValidationError from django.core.exceptions import ImproperlyConfigured, ValidationError
from django.core.validators import URLValidator from django.core.validators import URLValidator
from django.utils.encoding import force_str 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.config import PARAMS
from netbox.constants import RQ_QUEUE_DEFAULT, RQ_QUEUE_HIGH, RQ_QUEUE_LOW from netbox.constants import RQ_QUEUE_DEFAULT, RQ_QUEUE_HIGH, RQ_QUEUE_LOW
@ -39,8 +41,6 @@ if sys.version_info < (3, 8):
f"NetBox requires Python 3.8 or later. (Currently installed: Python {platform.python_version()})" 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 # Configuration import
# #
@ -158,7 +158,7 @@ RQ_RETRY_MAX = getattr(configuration, 'RQ_RETRY_MAX', 0)
SCRIPTS_ROOT = getattr(configuration, 'SCRIPTS_ROOT', os.path.join(BASE_DIR, 'scripts')).rstrip('/') SCRIPTS_ROOT = getattr(configuration, 'SCRIPTS_ROOT', os.path.join(BASE_DIR, 'scripts')).rstrip('/')
SEARCH_BACKEND = getattr(configuration, 'SEARCH_BACKEND', 'netbox.search.backends.CachedValueSearchBackend') SEARCH_BACKEND = getattr(configuration, 'SEARCH_BACKEND', 'netbox.search.backends.CachedValueSearchBackend')
SECURE_SSL_REDIRECT = getattr(configuration, 'SECURE_SSL_REDIRECT', False) 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_ENABLED = getattr(configuration, 'SENTRY_ENABLED', False)
SENTRY_SAMPLE_RATE = getattr(configuration, 'SENTRY_SAMPLE_RATE', 1.0) SENTRY_SAMPLE_RATE = getattr(configuration, 'SENTRY_SAMPLE_RATE', 1.0)
SENTRY_TRACES_SAMPLE_RATE = getattr(configuration, 'SENTRY_TRACES_SAMPLE_RATE', 0) SENTRY_TRACES_SAMPLE_RATE = getattr(configuration, 'SENTRY_TRACES_SAMPLE_RATE', 0)
@ -517,12 +517,12 @@ SERIALIZATION_MODULES = {
# #
if SENTRY_ENABLED: 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: if not SENTRY_DSN:
raise ImproperlyConfigured("SENTRY_ENABLED is True but SENTRY_DSN has not been defined.") 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 # Initialize the SDK
sentry_sdk.init( sentry_sdk.init(
dsn=SENTRY_DSN, dsn=SENTRY_DSN,
@ -537,9 +537,6 @@ if SENTRY_ENABLED:
# Assign any configured tags # Assign any configured tags
for k, v in SENTRY_TAGS.items(): for k, v in SENTRY_TAGS.items():
sentry_sdk.set_tag(k, v) 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)
# #

View File

@ -9,7 +9,6 @@ from django.template.exceptions import TemplateDoesNotExist
from django.views.decorators.csrf import requires_csrf_token from django.views.decorators.csrf import requires_csrf_token
from django.views.defaults import ERROR_500_TEMPLATE_NAME, page_not_found from django.views.defaults import ERROR_500_TEMPLATE_NAME, page_not_found
from django.views.generic import View from django.views.generic import View
from sentry_sdk import capture_message
from netbox.plugins.utils import get_installed_plugins 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. 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) return page_not_found(request, exception)

View File

@ -28,7 +28,6 @@ Pillow==10.1.0
psycopg[binary,pool]==3.1.12 psycopg[binary,pool]==3.1.12
PyYAML==6.0.1 PyYAML==6.0.1
requests==2.31.0 requests==2.31.0
sentry-sdk==1.34.0
social-auth-app-django==5.4.0 social-auth-app-django==5.4.0
social-auth-core[openidconnect]==4.5.0 social-auth-core[openidconnect]==4.5.0
svgwrite==1.4.3 svgwrite==1.4.3