Skip to content

Commit 674a0fe

Browse files
committed
add instrumentation for aiohttp.client
Signed-off-by: Benjamin Wohlwend <[email protected]>
1 parent 0b62ade commit 674a0fe

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
from elasticapm.instrumentation.packages.asyncio_base import AsyncAbstractInstrumentedModule
2+
from elasticapm import async_capture_span
3+
from elasticapm.utils import default_ports
4+
from elasticapm.utils.compat import urlparse
5+
6+
7+
def get_host_from_url(url):
8+
parsed_url = urlparse.urlparse(url)
9+
host = parsed_url.hostname or " "
10+
11+
if (
12+
parsed_url.port and
13+
default_ports.get(parsed_url.scheme) != parsed_url.port
14+
):
15+
host += ":" + str(parsed_url.port)
16+
17+
return host
18+
19+
20+
class AioHttpClientInstrumentation(AsyncAbstractInstrumentedModule):
21+
name = 'aiohttp_client'
22+
23+
instrument_list = [
24+
("aiohttp.client", "ClientSession._request"),
25+
]
26+
27+
async def call(self, module, method, wrapped, instance, args, kwargs):
28+
method = kwargs['method'] if 'method' in kwargs else args[0]
29+
url = kwargs['method'] if 'method' in kwargs else args[1]
30+
31+
signature = " ".join([method.upper(), get_host_from_url(url)])
32+
33+
async with async_capture_span(signature, "ext.http.aiohttp", {'url': url}, leaf=True):
34+
return await wrapped(*args, **kwargs)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from elasticapm.instrumentation.packages.base import AbstractInstrumentedModule
2+
3+
4+
class AsyncAbstractInstrumentedModule(AbstractInstrumentedModule):
5+
async def call(self, module, method, wrapped, instance, args, kwargs):
6+
raise NotImplementedError()

elasticapm/instrumentation/register.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import sys
2+
13
from elasticapm.utils.module_import import import_string
24

35
_cls_register = {
@@ -25,6 +27,11 @@
2527
'elasticapm.instrumentation.packages.django.template.DjangoTemplateSourceInstrumentation',
2628
}
2729

30+
if sys.version_info >= (3, 5):
31+
_cls_register.update([
32+
'elasticapm.instrumentation.packages.aiohttp.AioHttpClientInstrumentation',
33+
])
34+
2835

2936
def register(cls):
3037
_cls_register.add(cls)

0 commit comments

Comments
 (0)