Skip to content

Commit 12cd582

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

File tree

5 files changed

+50
-2
lines changed

5 files changed

+50
-2
lines changed

elasticapm/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,4 @@
2626
from elasticapm.traces import set_transaction_result # noqa: F401
2727

2828
if sys.version_info >= (3, 5):
29-
from elasticapm.contrib.asyncio.traces import async_capture_span # noqa: F401
30-
29+
from elasticapm.contrib.asyncio.traces import async_capture_span # noqa: F401

elasticapm/contrib/asyncio/traces.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import functools
2+
23
from elasticapm.traces import capture_span, error_logger, get_transaction
34
from elasticapm.utils import get_name_from_func
45

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