feat(flask): Add http.route attribute - #7344
Conversation
| from typing import TYPE_CHECKING | ||
|
|
||
| import sentry_sdk | ||
| from sentry_sdk.consts import SPANDATA |
There was a problem hiding this comment.
http.route access crashes when request.url_rule is None
In _request_started, request.url_rule.rule is used without a null check; unmatched routes leave url_rule as None and will raise AttributeError under span streaming. Guard like FastAPI (route_path is not None) or wrap with capture_internal_exceptions, matching _set_transaction_name_and_source.
Evidence
- Hunk adds
SPANDATAimport used by the newhttp.routewrite in_request_started. - That path does
server_span.set_attribute(SPANDATA.HTTP_ROUTE, request.url_rule.rule)with only aserver_span is not Noneguard. - Nearby
_set_transaction_name_and_sourcealready try/excepts the samerequest.url_ruleaccess because it can be missing. - FastAPI only sets
HTTP_ROUTEwhenroute_path is not None; Flask does not. test_http_routeonly covers a matched/message/123456path, not 404/unmatched routing.
Also found at 2 additional locations
sentry_sdk/integrations/flask.py:154-156tests/integrations/flask/test_flask.py:162-162
Identified by Warden · code-review, find-bugs · FE7-QHG
Codecov Results 📊✅ 130423 passed | ❌ 1 failed | ⏭️ 7110 skipped | Total: 137534 | Pass Rate: 94.83% | Execution Time: 471m 33s 📊 Comparison with Base Branch
➕ New Tests (1)View new tests
❌ Failed Tests
|
Description
Set the
http.routeattribute on the server span in patches for Flask endpoints.Issues
Reminders
uv run ruff.feat:,fix:,ref:,meta:)