Skip to content

Commit d34ce77

Browse files
authored
Merge pull request #20381 from netbox-community/20380-sentry_config
Closes #20380: Introduce the `SENTRY_CONFIG` config parameter
2 parents f45a11d + 8e33205 commit d34ce77

File tree

2 files changed

+77
-9
lines changed

2 files changed

+77
-9
lines changed

docs/configuration/error-reporting.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,32 @@
11
# Error Reporting Settings
22

3+
## SENTRY_CONFIG
4+
5+
A dictionary mapping keyword arguments to values, to be passed to `sentry_sdk.init()`. See the [Sentry Python SDK documentation](https://docs.sentry.io/platforms/python/) for more information on supported parameters.
6+
7+
The default configuration is shown below:
8+
9+
```python
10+
{
11+
"sample_rate": 1.0,
12+
"send_default_pii": False,
13+
"traces_sample_rate": 0,
14+
}
15+
```
16+
17+
Additionally, `http_proxy` and `https_proxy` are set to the HTTP and HTTPS proxies, respectively, configured for NetBox (if any).
18+
319
## SENTRY_DSN
420

21+
!!! warning "This parameter will be removed in NetBox v4.5."
22+
Set this using `SENTRY_CONFIG` instead:
23+
24+
```
25+
SENTRY_CONFIG = {
26+
"dsn": "https://[email protected]/0",
27+
}
28+
```
29+
530
Default: `None`
631

732
Defines a Sentry data source name (DSN) for automated error reporting. `SENTRY_ENABLED` must be `True` for this parameter to take effect. For example:
@@ -25,6 +50,15 @@ Set to `True` to enable automatic error reporting via [Sentry](https://sentry.io
2550

2651
## SENTRY_SAMPLE_RATE
2752

53+
!!! warning "This parameter will be removed in NetBox v4.5."
54+
Set this using `SENTRY_CONFIG` instead:
55+
56+
```
57+
SENTRY_CONFIG = {
58+
"sample_rate": 0.2,
59+
}
60+
```
61+
2862
Default: `1.0` (all)
2963

3064
The sampling rate for errors. Must be a value between 0 (disabled) and 1.0 (report on all errors).
@@ -33,6 +67,15 @@ The sampling rate for errors. Must be a value between 0 (disabled) and 1.0 (repo
3367

3468
## SENTRY_SEND_DEFAULT_PII
3569

70+
!!! warning "This parameter will be removed in NetBox v4.5."
71+
Set this using `SENTRY_CONFIG` instead:
72+
73+
```
74+
SENTRY_CONFIG = {
75+
"send_default_pii": True,
76+
}
77+
```
78+
3679
Default: `False`
3780

3881
Maps to the Sentry SDK's [`send_default_pii`](https://docs.sentry.io/platforms/python/configuration/options/#send-default-pii) parameter. If enabled, certain personally identifiable information (PII) is added.
@@ -60,6 +103,15 @@ SENTRY_TAGS = {
60103

61104
## SENTRY_TRACES_SAMPLE_RATE
62105

106+
!!! warning "This parameter will be removed in NetBox v4.5."
107+
Set this using `SENTRY_CONFIG` instead:
108+
109+
```
110+
SENTRY_CONFIG = {
111+
"traces_sample_rate": 0.2,
112+
}
113+
```
114+
63115
Default: `0` (disabled)
64116

65117
The sampling rate for transactions. Must be a value between 0 (disabled) and 1.0 (report on all transactions).

netbox/netbox/settings.py

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,16 @@
176176
SECURE_HSTS_PRELOAD = getattr(configuration, 'SECURE_HSTS_PRELOAD', False)
177177
SECURE_HSTS_SECONDS = getattr(configuration, 'SECURE_HSTS_SECONDS', 0)
178178
SECURE_SSL_REDIRECT = getattr(configuration, 'SECURE_SSL_REDIRECT', False)
179+
SENTRY_CONFIG = getattr(configuration, 'SENTRY_CONFIG', {})
180+
# TODO: Remove in NetBox v4.5
179181
SENTRY_DSN = getattr(configuration, 'SENTRY_DSN', None)
180182
SENTRY_ENABLED = getattr(configuration, 'SENTRY_ENABLED', False)
183+
# TODO: Remove in NetBox v4.5
181184
SENTRY_SAMPLE_RATE = getattr(configuration, 'SENTRY_SAMPLE_RATE', 1.0)
185+
# TODO: Remove in NetBox v4.5
182186
SENTRY_SEND_DEFAULT_PII = getattr(configuration, 'SENTRY_SEND_DEFAULT_PII', False)
183187
SENTRY_TAGS = getattr(configuration, 'SENTRY_TAGS', {})
188+
# TODO: Remove in NetBox v4.5
184189
SENTRY_TRACES_SAMPLE_RATE = getattr(configuration, 'SENTRY_TRACES_SAMPLE_RATE', 0)
185190
SESSION_COOKIE_NAME = getattr(configuration, 'SESSION_COOKIE_NAME', 'sessionid')
186191
SESSION_COOKIE_PATH = CSRF_COOKIE_PATH
@@ -598,18 +603,29 @@ def _setting(name, default=None):
598603
import sentry_sdk
599604
except ModuleNotFoundError:
600605
raise ImproperlyConfigured("SENTRY_ENABLED is True but the sentry-sdk package is not installed.")
601-
if not SENTRY_DSN:
602-
raise ImproperlyConfigured("SENTRY_ENABLED is True but SENTRY_DSN has not been defined.")
606+
607+
# Construct default Sentry initialization parameters from legacy SENTRY_* config parameters
608+
sentry_config = {
609+
'dsn': SENTRY_DSN,
610+
'sample_rate': SENTRY_SAMPLE_RATE,
611+
'send_default_pii': SENTRY_SEND_DEFAULT_PII,
612+
'traces_sample_rate': SENTRY_TRACES_SAMPLE_RATE,
613+
# TODO: Support proxy routing
614+
'http_proxy': HTTP_PROXIES.get('http') if HTTP_PROXIES else None,
615+
'https_proxy': HTTP_PROXIES.get('https') if HTTP_PROXIES else None,
616+
}
617+
# Override/extend the default parameters with any provided via SENTRY_CONFIG
618+
sentry_config.update(SENTRY_CONFIG)
619+
# Check for a DSN
620+
if not sentry_config.get('dsn'):
621+
raise ImproperlyConfigured(
622+
"Sentry is enabled but a DSN has not been specified. Set one under the SENTRY_CONFIG parameter."
623+
)
624+
603625
# Initialize the SDK
604626
sentry_sdk.init(
605-
dsn=SENTRY_DSN,
606627
release=RELEASE.full_version,
607-
sample_rate=SENTRY_SAMPLE_RATE,
608-
traces_sample_rate=SENTRY_TRACES_SAMPLE_RATE,
609-
send_default_pii=SENTRY_SEND_DEFAULT_PII,
610-
# TODO: Support proxy routing
611-
http_proxy=HTTP_PROXIES.get('http') if HTTP_PROXIES else None,
612-
https_proxy=HTTP_PROXIES.get('https') if HTTP_PROXIES else None
628+
**sentry_config
613629
)
614630
# Assign any configured tags
615631
for k, v in SENTRY_TAGS.items():

0 commit comments

Comments
 (0)