diff --git a/sentry_sdk/integrations/aiohttp.py b/sentry_sdk/integrations/aiohttp.py index 58fe09bf1e..e51bdeeac3 100644 --- a/sentry_sdk/integrations/aiohttp.py +++ b/sentry_sdk/integrations/aiohttp.py @@ -213,9 +213,10 @@ async def on_request_start(session, trace_config_ctx, params): % (method, parsed_url.url if parsed_url else SENSITIVE_DATA_SUBSTITUTE), ) span.set_data(SPANDATA.HTTP_METHOD, method) - span.set_data("url", parsed_url.url) - span.set_data(SPANDATA.HTTP_QUERY, parsed_url.query) - span.set_data(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment) + if parsed_url is not None: + span.set_data("url", parsed_url.url) + span.set_data(SPANDATA.HTTP_QUERY, parsed_url.query) + span.set_data(SPANDATA.HTTP_FRAGMENT, parsed_url.fragment) if should_propagate_trace(hub, str(params.url)): for key, value in hub.iter_trace_propagation_headers(span): diff --git a/tests/integrations/aiohttp/test_aiohttp.py b/tests/integrations/aiohttp/test_aiohttp.py index 8068365334..de5cf19f44 100644 --- a/tests/integrations/aiohttp/test_aiohttp.py +++ b/tests/integrations/aiohttp/test_aiohttp.py @@ -256,6 +256,36 @@ async def hello(request): assert event["transaction_info"] == {"source": expected_source} +@pytest.mark.tests_internal_exceptions +@pytest.mark.asyncio +async def test_tracing_unparseable_url(sentry_init, aiohttp_client, capture_events): + sentry_init(integrations=[AioHttpIntegration()], traces_sample_rate=1.0) + + async def hello(request): + return web.Response(text="hello") + + app = web.Application() + app.router.add_get("/", hello) + + events = capture_events() + + client = await aiohttp_client(app) + with mock.patch( + "sentry_sdk.integrations.aiohttp.parse_url", side_effect=ValueError + ): + resp = await client.get("/") + + assert resp.status == 200 + + (event,) = events + + assert event["type"] == "transaction" + assert ( + event["transaction"] + == "tests.integrations.aiohttp.test_aiohttp.test_tracing_unparseable_url..hello" + ) + + @pytest.mark.asyncio async def test_traces_sampler_gets_request_object_in_sampling_context( sentry_init,