feat(wsgi): Migrate to span first #5988
5 issues
High
Circular import between wsgi.py and _wsgi_common.py will cause ImportError at runtime - `sentry_sdk/integrations/_wsgi_common.py:8`
The new import from sentry_sdk.integrations.wsgi import get_client_ip, get_request_url creates a circular dependency. wsgi.py imports _get_request_attributes from _wsgi_common.py (line 12 in wsgi.py), while _wsgi_common.py now imports from wsgi.py. When wsgi.py is loaded first, it will fail because _wsgi_common.py will try to import symbols from wsgi.py before they're defined. This will cause an ImportError or AttributeError at runtime.
Circular import between _wsgi_common.py and wsgi.py will cause ImportError - `sentry_sdk/integrations/_wsgi_common.py:8`
The new import from sentry_sdk.integrations.wsgi import get_client_ip, get_request_url at module level creates a circular dependency. When _wsgi_common.py is imported first (e.g., via flask.py, bottle.py, or other integrations that import from _wsgi_common before wsgi), Python tries to load wsgi.py which in turn imports _get_request_attributes from the partially-initialized _wsgi_common module. This results in ImportError: cannot import name '_get_request_attributes' from partially initialized module 'sentry_sdk.integrations._wsgi_common'.
Medium
AttributeError when span is None for non-captured HTTP methods - `sentry_sdk/integrations/wsgi.py:167`
When the HTTP method is not in http_methods_to_capture, span_ctx remains None and becomes nullcontext(), which yields None as the context value. The code then calls span.set_attribute(attr, value) on this None value, causing an AttributeError. While wrapped in capture_internal_exceptions(), this will log an internal error on every request with non-captured methods (like HEAD or OPTIONS by default).
AttributeError when HTTP method not in http_methods_to_capture - `sentry_sdk/integrations/wsgi.py:167`
When the HTTP request method is not in http_methods_to_capture (e.g., HEAD, OPTIONS), span_ctx defaults to nullcontext() which yields None as the span value. The code then attempts to call span.set_attribute(attr, value) on None, causing an AttributeError. While this exception is caught by capture_internal_exceptions(), it will log an internal SDK error for every HEAD/OPTIONS request and silently fail to set request attributes.
Low
server.port attribute set as string instead of integer - `sentry_sdk/integrations/_wsgi_common.py:287-288`
The server.port attribute is set as a string from environ.get("SERVER_PORT") at line 288, but OpenTelemetry semantic conventions expect this to be an integer. Other integrations in this codebase (e.g., clickhouse_driver, asyncpg) use integer values for server.port. This type inconsistency could cause issues with backend data processing or filtering.
4 skills analyzed
| Skill | Findings | Duration | Cost |
|---|---|---|---|
| code-review | 3 | 2m 29s | $2.45 |
| find-bugs | 2 | 4m 41s | $3.96 |
| skill-scanner | 0 | 4m 3s | $0.38 |
| security-review | 0 | 4m 43s | $0.85 |
Duration: 15m 56s · Tokens: 5.7M in / 56.7k out · Cost: $7.67 (+extraction: $0.01, +merge: $0.00, +fix_gate: $0.01, +dedup: $0.00)