Skip to content

Commit 3c7bb1e

Browse files
authored
Remove nullcontext from wsgi and asgi (#3983)
mypy was complaining too so just removed it
1 parent ea06dab commit 3c7bb1e

File tree

2 files changed

+68
-71
lines changed

2 files changed

+68
-71
lines changed

sentry_sdk/integrations/asgi.py

Lines changed: 43 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
import asyncio
88
import inspect
9-
from contextlib import nullcontext
109
from copy import deepcopy
1110
from functools import partial
1211

@@ -169,20 +168,24 @@ async def _run_asgi3(self, scope, receive, send):
169168
# type: (Any, Any, Any) -> Any
170169
return await self._run_app(scope, receive, send, asgi_version=3)
171170

171+
async def _run_original_app(self, scope, receive, send, asgi_version):
172+
# type: (Any, Any, Any, Any, int) -> Any
173+
try:
174+
if asgi_version == 2:
175+
return await self.app(scope)(receive, send)
176+
else:
177+
return await self.app(scope, receive, send)
178+
179+
except Exception as exc:
180+
_capture_exception(exc, mechanism_type=self.mechanism_type)
181+
raise exc from None
182+
172183
async def _run_app(self, scope, receive, send, asgi_version):
173184
# type: (Any, Any, Any, Any, int) -> Any
174185
is_recursive_asgi_middleware = _asgi_middleware_applied.get(False)
175186
is_lifespan = scope["type"] == "lifespan"
176187
if is_recursive_asgi_middleware or is_lifespan:
177-
try:
178-
if asgi_version == 2:
179-
return await self.app(scope)(receive, send)
180-
else:
181-
return await self.app(scope, receive, send)
182-
183-
except Exception as exc:
184-
_capture_exception(exc, mechanism_type=self.mechanism_type)
185-
raise exc from None
188+
return await self._run_original_app(scope, receive, send, asgi_version)
186189

187190
_asgi_middleware_applied.set(True)
188191
try:
@@ -209,52 +212,42 @@ async def _run_app(self, scope, receive, send, asgi_version):
209212

210213
method = scope.get("method", "").upper()
211214
should_trace = method in self.http_methods_to_capture
215+
if not should_trace:
216+
return await self._run_original_app(
217+
scope, receive, send, asgi_version
218+
)
219+
212220
with sentry_sdk.continue_trace(_get_headers(scope)):
213-
with (
214-
sentry_sdk.start_span(
215-
op=(
216-
OP.WEBSOCKET_SERVER
217-
if ty == "websocket"
218-
else OP.HTTP_SERVER
219-
),
220-
name=transaction_name,
221-
source=transaction_source,
222-
origin=self.span_origin,
223-
attributes=_prepopulate_attributes(scope),
224-
)
225-
if should_trace
226-
else nullcontext()
221+
with sentry_sdk.start_span(
222+
op=(
223+
OP.WEBSOCKET_SERVER
224+
if ty == "websocket"
225+
else OP.HTTP_SERVER
226+
),
227+
name=transaction_name,
228+
source=transaction_source,
229+
origin=self.span_origin,
230+
attributes=_prepopulate_attributes(scope),
227231
) as span:
228232
if span is not None:
229233
logger.debug("[ASGI] Started transaction: %s", span)
230234
span.set_tag("asgi.type", ty)
231-
try:
232-
233-
async def _sentry_wrapped_send(event):
234-
# type: (Dict[str, Any]) -> Any
235-
is_http_response = (
236-
event.get("type") == "http.response.start"
237-
and span is not None
238-
and "status" in event
239-
)
240-
if is_http_response:
241-
span.set_http_status(event["status"])
242-
243-
return await send(event)
244-
245-
if asgi_version == 2:
246-
return await self.app(scope)(
247-
receive, _sentry_wrapped_send
248-
)
249-
else:
250-
return await self.app(
251-
scope, receive, _sentry_wrapped_send
252-
)
253-
except Exception as exc:
254-
_capture_exception(
255-
exc, mechanism_type=self.mechanism_type
235+
236+
async def _sentry_wrapped_send(event):
237+
# type: (Dict[str, Any]) -> Any
238+
is_http_response = (
239+
event.get("type") == "http.response.start"
240+
and span is not None
241+
and "status" in event
256242
)
257-
raise exc from None
243+
if is_http_response:
244+
span.set_http_status(event["status"])
245+
246+
return await send(event)
247+
248+
return await self._run_original_app(
249+
scope, receive, _sentry_wrapped_send, asgi_version
250+
)
258251
finally:
259252
_asgi_middleware_applied.set(False)
260253

sentry_sdk/integrations/wsgi.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import sys
2-
from contextlib import nullcontext
32
from functools import partial
43

54
import sentry_sdk
@@ -123,50 +122,55 @@ def __call__(self, environ, start_response):
123122
)
124123
method = environ.get("REQUEST_METHOD", "").upper()
125124
should_trace = method in self.http_methods_to_capture
126-
with sentry_sdk.continue_trace(environ):
127-
with (
128-
sentry_sdk.start_span(
125+
if should_trace:
126+
with sentry_sdk.continue_trace(environ):
127+
with sentry_sdk.start_span(
129128
op=OP.HTTP_SERVER,
130129
name=DEFAULT_TRANSACTION_NAME,
131130
source=TRANSACTION_SOURCE_ROUTE,
132131
origin=self.span_origin,
133132
attributes=_prepopulate_attributes(
134133
environ, self.use_x_forwarded_for
135134
),
136-
)
137-
if should_trace
138-
else nullcontext()
139-
) as transaction:
140-
try:
141-
response = self.app(
142-
environ,
143-
partial(
144-
_sentry_start_response,
145-
start_response,
146-
transaction,
147-
),
135+
) as span:
136+
response = self._run_original_app(
137+
environ, start_response, span
148138
)
149-
except BaseException:
150-
reraise(*_capture_exception())
139+
else:
140+
response = self._run_original_app(environ, start_response, None)
151141

152142
finally:
153143
_wsgi_middleware_applied.set(False)
154144

155145
return _ScopedResponse(scope, response)
156146

147+
def _run_original_app(self, environ, start_response, span):
148+
# type: (dict[str, str], StartResponse, Optional[Span]) -> Any
149+
try:
150+
return self.app(
151+
environ,
152+
partial(
153+
_sentry_start_response,
154+
start_response,
155+
span,
156+
),
157+
)
158+
except BaseException:
159+
reraise(*_capture_exception())
160+
157161

158162
def _sentry_start_response( # type: ignore
159163
old_start_response, # type: StartResponse
160-
transaction, # type: Optional[Span]
164+
span, # type: Optional[Span]
161165
status, # type: str
162166
response_headers, # type: WsgiResponseHeaders
163167
exc_info=None, # type: Optional[WsgiExcInfo]
164168
):
165169
# type: (...) -> WsgiResponseIter
166170
with capture_internal_exceptions():
167171
status_int = int(status.split(" ", 1)[0])
168-
if transaction is not None:
169-
transaction.set_http_status(status_int)
172+
if span is not None:
173+
span.set_http_status(status_int)
170174

171175
if exc_info is None:
172176
# The Django Rest Framework WSGI test client, and likely other

0 commit comments

Comments
 (0)