Skip to content

Commit

Permalink
Closes #13645: Make Sentry integration optional (#14197)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremystretch authored Nov 9, 2023
1 parent 3d20276 commit 840b7d8
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 34 deletions.
4 changes: 0 additions & 4 deletions base_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 3 additions & 15 deletions docs/administration/error-reporting.md
Original file line number Diff line number Diff line change
Expand Up @@ -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://[email protected]/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://[email protected]/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:
Expand Down
3 changes: 3 additions & 0 deletions docs/configuration/error-reporting.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 11 additions & 0 deletions docs/installation/3-netbox.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
21 changes: 9 additions & 12 deletions netbox/netbox/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,8 +41,6 @@
f"NetBox requires Python 3.8 or later. (Currently installed: Python {platform.python_version()})"
)

DEFAULT_SENTRY_DSN = 'https://[email protected]/6396485'

#
# Configuration import
#
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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,
Expand All @@ -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)


#
Expand Down
5 changes: 3 additions & 2 deletions netbox/netbox/views/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand Down
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 840b7d8

Please sign in to comment.