Skip to content

Commit 6f4fda5

Browse files
authored
fix(aiohttp): parsed_url can be None (#2734)
1 parent c53fbac commit 6f4fda5

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

sentry_sdk/integrations/aiohttp.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -213,9 +213,10 @@ async def on_request_start(session, trace_config_ctx, params):
213213
% (method, parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE),
214214
)
215215
span.set_data(SPANDATA.HTTP_METHOD, method)
216-
span.set_data("url", parsed_url.url)
217-
span.set_data(SPANDATA.HTTP_QUERY, parsed_url.query)
218-
span.set_data(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment)
216+
if parsed_url is not None:
217+
span.set_data("url", parsed_url.url)
218+
span.set_data(SPANDATA.HTTP_QUERY, parsed_url.query)
219+
span.set_data(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment)
219220

220221
if should_propagate_trace(hub, str(params.url)):
221222
for key, value in hub.iter_trace_propagation_headers(span):

tests/integrations/aiohttp/test_aiohttp.py

+30
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,36 @@ async def hello(request):
256256
assert event["transaction_info"] == {"source": expected_source}
257257

258258

259+
@pytest.mark.tests_internal_exceptions
260+
@pytest.mark.asyncio
261+
async def test_tracing_unparseable_url(sentry_init, aiohttp_client, capture_events):
262+
sentry_init(integrations=[AioHttpIntegration()], traces_sample_rate=1.0)
263+
264+
async def hello(request):
265+
return web.Response(text="hello")
266+
267+
app = web.Application()
268+
app.router.add_get("/", hello)
269+
270+
events = capture_events()
271+
272+
client = await aiohttp_client(app)
273+
with mock.patch(
274+
"sentry_sdk.integrations.aiohttp.parse_url", side_effect=ValueError
275+
):
276+
resp = await client.get("/")
277+
278+
assert resp.status == 200
279+
280+
(event,) = events
281+
282+
assert event["type"] == "transaction"
283+
assert (
284+
event["transaction"]
285+
== "tests.integrations.aiohttp.test_aiohttp.test_tracing_unparseable_url.<locals>.hello"
286+
)
287+
288+
259289
@pytest.mark.asyncio
260290
async def test_traces_sampler_gets_request_object_in_sampling_context(
261291
sentry_init,

0 commit comments

Comments
 (0)