Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,10 @@ class AioHttpClientInstrumentation(AsyncAbstractInstrumentedModule):
instrument_list = [("aiohttp.client", "ClientSession._request")]

async def call(self, module, method, wrapped, instance, args, kwargs):
method = kwargs["method"] if "method" in kwargs else args[0]
url = kwargs["url"] if "url" in kwargs else args[1]
url = str(url)
method = kwargs.get("method", args[0])
url = str(kwargs.get("url", kwargs.get("str_or_url", None)))
if url is None:
url = args[1]

signature = " ".join([method.upper(), get_host_from_url(url)])
url = sanitize_url(url)
Expand Down
32 changes: 32 additions & 0 deletions tests/instrumentation/asyncio_tests/aiohttp_client_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,38 @@ async def test_http_get(instrument, event_loop, elasticapm_client, waiting_https
assert spans[0]["outcome"] == "success"


@pytest.mark.parametrize("use_yarl", [True, False])
async def test_http_get_via_request(instrument, event_loop, elasticapm_client, waiting_httpserver, use_yarl):
assert event_loop.is_running()
elasticapm_client.begin_transaction("test")

url = waiting_httpserver.url
url = yarl.URL(url) if use_yarl else url

async with aiohttp.ClientSession() as session:
await session._request("GET", str_or_url=waiting_httpserver.url)

elasticapm_client.end_transaction()
transaction = elasticapm_client.events[constants.TRANSACTION][0]
spans = elasticapm_client.spans_for_transaction(transaction)
assert len(spans) == 1
span = spans[0]
assert span["name"] == "GET %s:%s" % waiting_httpserver.server_address
assert span["type"] == "external"
assert span["subtype"] == "http"
assert span["sync"] is False
assert span["context"]["http"]["url"] == waiting_httpserver.url
assert span["context"]["http"]["status_code"] == 204
assert spans[0]["context"]["destination"]["service"] == {
"name": "",
"resource": "127.0.0.1:%d" % waiting_httpserver.server_address[1],
"type": "",
}
assert spans[0]["context"]["service"]["target"]["type"] == "http"
assert spans[0]["context"]["service"]["target"]["name"] == f"127.0.0.1:{waiting_httpserver.server_address[1]}"
assert spans[0]["outcome"] == "success"


@pytest.mark.parametrize("status_code", [400, 500])
async def test_http_get_error(instrument, event_loop, elasticapm_client, waiting_httpserver, status_code):
assert event_loop.is_running()
Expand Down