Skip to content

Commit b37b395

Browse files
committed
Filter out invalid HTTP_HOST header errors from Sentry
1 parent 7cbd984 commit b37b395

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

approval_polls/settings.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,31 @@
4242
if not DEBUG:
4343
COMPRESS_OFFLINE = True
4444
LIBSASS_OUTPUT_STYLE = "compressed"
45+
46+
def filter_invalid_host_errors(event, hint):
47+
"""Filter out invalid HTTP_HOST header errors from Sentry."""
48+
# Check exception type in hint
49+
if "exc_info" in hint:
50+
exc_type, exc_value, tb = hint["exc_info"]
51+
if exc_type.__name__ == "DisallowedHost":
52+
return None
53+
# Check log record message
54+
if "log_record" in hint:
55+
msg = str(hint.get("log_record", {}).get("msg", ""))
56+
if "Invalid HTTP_HOST header" in msg:
57+
return None
58+
# Check event exception data directly
59+
if event.get("exception"):
60+
for exc in event["exception"].get("values", []):
61+
exc_type = exc.get("type", "")
62+
exc_value = exc.get("value", "")
63+
if (
64+
exc_type == "DisallowedHost"
65+
or "Invalid HTTP_HOST header" in exc_value
66+
):
67+
return None
68+
return event
69+
4570
sentry_sdk.init(
4671
dsn="https://78856604267db99554868743d5eb61e5@o4506681396625408.ingest.sentry.io/4506681396756480",
4772
# Set traces_sample_rate to 1.0 to capture 100%
@@ -51,6 +76,7 @@
5176
# of sampled transactions.
5277
# We recommend adjusting this value in production.
5378
profiles_sample_rate=1.0,
79+
before_send=filter_invalid_host_errors,
5480
)
5581
CSRF_TRUSTED_ORIGINS = [
5682
"https://vote.electionscience.org",

0 commit comments

Comments
 (0)