44
55import sentry_sdk
66from sentry_sdk .api import continue_trace
7- from sentry_sdk .consts import OP , SPANSTATUS , SPANDATA
7+ from sentry_sdk .consts import OP , SPANDATA , SPANSTATUS
88from sentry_sdk .integrations import (
99 _DEFAULT_FAILED_REQUEST_STATUS_CODES ,
10- _check_minimum_version ,
11- Integration ,
1210 DidNotEnable ,
11+ Integration ,
12+ _check_minimum_version ,
1313)
14- from sentry_sdk .integrations .logging import ignore_logger
15- from sentry_sdk .scope import should_send_default_pii
16- from sentry_sdk .sessions import track_session
1714from sentry_sdk .integrations ._wsgi_common import (
1815 _filter_headers ,
1916 request_body_within_bounds ,
2017)
18+ from sentry_sdk .integrations .logging import ignore_logger
19+ from sentry_sdk .scope import should_send_default_pii
20+ from sentry_sdk .sessions import track_session
21+ from sentry_sdk .traces import (
22+ SOURCE_FOR_STYLE as SEGMENT_SOURCE_FOR_STYLE ,
23+ )
2124from sentry_sdk .traces import (
2225 NoOpStreamedSpan ,
2326 SegmentSource ,
2427 SpanStatus ,
2528 StreamedSpan ,
26- SOURCE_FOR_STYLE as SEGMENT_SOURCE_FOR_STYLE ,
2729)
2830from sentry_sdk .tracing import (
2931 BAGGAGE_HEADER_NAME ,
3638 should_propagate_trace ,
3739)
3840from sentry_sdk .utils import (
41+ CONTEXTVARS_ERROR_MESSAGE ,
42+ HAS_REAL_CONTEXTVARS ,
43+ SENSITIVE_DATA_SUBSTITUTE ,
44+ AnnotatedValue ,
3945 capture_internal_exceptions ,
4046 ensure_integration_enabled ,
4147 event_from_exception ,
4450 parse_version ,
4551 reraise ,
4652 transaction_from_function ,
47- HAS_REAL_CONTEXTVARS ,
48- CONTEXTVARS_ERROR_MESSAGE ,
49- SENSITIVE_DATA_SUBSTITUTE ,
50- AnnotatedValue ,
5153)
5254
5355try :
5456 import asyncio
5557
56- from aiohttp import __version__ as AIOHTTP_VERSION
5758 from aiohttp import ClientSession , TraceConfig
59+ from aiohttp import __version__ as AIOHTTP_VERSION
5860 from aiohttp .web import Application , HTTPException , UrlDispatcher
5961except ImportError :
6062 raise DidNotEnable ("AIOHTTP not installed" )
6163
6264from typing import TYPE_CHECKING
6365
6466if TYPE_CHECKING :
65- from aiohttp .web_request import Request
66- from aiohttp .web_urldispatcher import UrlMappingMatchInfo
67- from aiohttp import TraceRequestStartParams , TraceRequestEndParams
68-
6967 from collections .abc import Set
7068 from types import SimpleNamespace
71- from typing import Any
72- from typing import ContextManager
73- from typing import Optional
74- from typing import Tuple
75- from typing import Union
69+ from typing import Any , ContextManager , Optional , Tuple , Union
70+
71+ from aiohttp import TraceRequestEndParams , TraceRequestStartParams
72+ from aiohttp . web_request import Request
73+ from aiohttp . web_urldispatcher import UrlMappingMatchInfo
7674
75+ from sentry_sdk ._types import Attributes , Event , EventProcessor
7776 from sentry_sdk .tracing import Span
7877 from sentry_sdk .utils import ExcInfo
79- from sentry_sdk ._types import Attributes , Event , EventProcessor
8078
8179
8280TRANSACTION_STYLE_VALUES = ("handler_name" , "method_and_path_pattern" )
@@ -145,22 +143,24 @@ async def sentry_app_handle(
145143 {"aiohttp_request" : request }
146144 )
147145
148- header_dict : "dict[str, Any]" = {}
146+ header_attributes : "dict[str, Any]" = {}
149147 for header , header_value in _filter_headers (
150148 headers , use_annotated_value = False
151149 ).items ():
152- header_dict [f"http.request.header.{ header .lower ()} " ] = (
150+ header_attributes [
151+ f"http.request.header.{ header .lower ()} "
152+ ] = (
153153 # header_value will always be a string because we set `use_annotated_value` to false above
154154 header_value
155155 )
156156
157- url_query = (
157+ url_query_attribute = (
158158 {"url.query" : request .query_string }
159159 if request .query_string
160160 else {}
161161 )
162162
163- client_address = (
163+ client_address_attributes = (
164164 {
165165 "client.address" : request .remote ,
166166 "user.ip_address" : request .remote ,
@@ -180,9 +180,9 @@ async def sentry_app_handle(
180180 "url.full" : "%s://%s%s"
181181 % (request .scheme , request .host , request .path ),
182182 "http.request.method" : request .method ,
183- ** url_query ,
184- ** client_address ,
185- ** header_dict ,
183+ ** url_query_attribute ,
184+ ** client_address_attributes ,
185+ ** header_attributes ,
186186 },
187187 )
188188 else :
@@ -212,8 +212,8 @@ async def sentry_app_handle(
212212 "http.response.status_code" , e .status_code
213213 )
214214
215- # There are a number of sub-exceptions that should have an "ok" status, but will
216- # actually have a status of error once the `raise` happens below
215+ # There are a number of sub-exceptions that should have an "ok" status,
216+ # but will actually have a status of error once the `raise` happens below
217217 # and we pass through the `should_be_treated_as_error` method invoked
218218 # within a span's `__exit__` method.
219219 #
@@ -224,6 +224,8 @@ async def sentry_app_handle(
224224 # approach as well, so this matches what we do today.
225225 span .status = SpanStatus .ERROR .value
226226 else :
227+ # Since a NoOpStreamedSpan can end up here, we have to guard against it
228+ # so this only gets set in the legacy transaction approach.
227229 if not isinstance (span , NoOpStreamedSpan ):
228230 span .set_http_status (e .status_code )
229231
0 commit comments