fix: Filter 404 invalid requests from Sentry - #53
Conversation
| if status_code == 404: | ||
| return |
There was a problem hiding this comment.
Bug: The new filter in capture_invalid_request silences all 404 errors, including legitimate application errors like JobNotFoundError, not just bot-generated noise.
Severity: MEDIUM
Suggested Fix
The filtering logic should be more specific. Instead of filtering all 404s, it should distinguish between 404s from non-existent routes (e.g., from StarletteHTTPException) and application-level 404s (e.g., from JobNotFoundError). This could be done by passing more context to capture_invalid_request or by handling the filtering at the call sites in app.py.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.
Location: core/nurse_scheduling/sentry.py#L101-L102
Potential issue: The change in `sentry.py` adds a blanket filter to stop reporting any
event with a `status_code` of 404 to Sentry. While this is intended to filter out noise
from bots scanning for non-existent paths, it also unintentionally filters legitimate
application-level 404 errors. For example, when a user tries to access a deleted or
expired job, the application correctly generates a `JobNotFoundError`, which results in
a 404 status. Previously, this would be captured by Sentry, providing valuable
observability. With this change, these legitimate errors will be silently ignored,
reducing the ability to monitor and understand application behavior related to
resource-not-found scenarios.
Did we get this right? 👍 / 👎 to inform future reviews.
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## dev #53 +/- ##
==========================================
- Coverage 92.65% 92.64% -0.02%
==========================================
Files 100 100
Lines 8250 8252 +2
Branches 705 705
==========================================
+ Hits 7644 7645 +1
- Misses 460 461 +1
Partials 146 146
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
This PR addresses issue NURSE-SCHEDULING-49, where Sentry was receiving excessive 'Invalid API request' warnings due to bot vulnerability scanners probing nonexistent paths, resulting in 404 Not Found errors.
The
capture_invalid_requestfunction incore/nurse_scheduling/sentry.pywas logging all 4xx HTTP errors, including 404s, to Sentry. To reduce this noise, an early return has been added tocapture_invalid_requestto ignore requests with astatus_codeof 404. This ensures that only relevant invalid requests (e.g., 422 validation errors) are reported to Sentry, while common bot-generated 404s are filtered out.Fixes NURSE-SCHEDULING-49