Skip to content

Commit 1e2ea45

Browse files
committed
(aiohttp_client) Add support for async functions for request and response hooks.
1 parent 74ff31b commit 1e2ea45

File tree

1 file changed

+17
-5
lines changed
  • instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client

1 file changed

+17
-5
lines changed

instrumentation/opentelemetry-instrumentation-aiohttp-client/src/opentelemetry/instrumentation/aiohttp_client/__init__.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ def response_hook(span: Span, params: typing.Union[
8888
---
8989
"""
9090

91+
import inspect
9192
import types
9293
import typing
94+
from collections.abc import Awaitable
9395
from timeit import default_timer
9496
from typing import Collection
9597
from urllib.parse import urlparse
@@ -139,7 +141,10 @@ def response_hook(span: Span, params: typing.Union[
139141

140142
_UrlFilterT = typing.Optional[typing.Callable[[yarl.URL], str]]
141143
_RequestHookT = typing.Optional[
142-
typing.Callable[[Span, aiohttp.TraceRequestStartParams], None]
144+
typing.Callable[
145+
[Span, aiohttp.TraceRequestStartParams],
146+
typing.Union[None, Awaitable[None]],
147+
]
143148
]
144149
_ResponseHookT = typing.Optional[
145150
typing.Callable[
@@ -150,7 +155,7 @@ def response_hook(span: Span, params: typing.Union[
150155
aiohttp.TraceRequestExceptionParams,
151156
],
152157
],
153-
None,
158+
typing.Union[None, Awaitable[None]],
154159
]
155160
]
156161

@@ -367,7 +372,9 @@ async def on_request_start(
367372
)
368373

369374
if callable(request_hook):
370-
request_hook(trace_config_ctx.span, params)
375+
call = request_hook(trace_config_ctx.span, params)
376+
if inspect.isawaitable(call):
377+
await call
371378

372379
trace_config_ctx.token = context_api.attach(
373380
trace.set_span_in_context(trace_config_ctx.span)
@@ -384,7 +391,10 @@ async def on_request_end(
384391
return
385392

386393
if callable(response_hook):
387-
response_hook(trace_config_ctx.span, params)
394+
call = response_hook(trace_config_ctx.span, params)
395+
if inspect.isawaitable(call):
396+
await call
397+
388398
_set_http_status_code_attribute(
389399
trace_config_ctx.span,
390400
params.response.status,
@@ -414,7 +424,9 @@ async def on_request_exception(
414424
trace_config_ctx.span.record_exception(params.exception)
415425

416426
if callable(response_hook):
417-
response_hook(trace_config_ctx.span, params)
427+
call = response_hook(trace_config_ctx.span, params)
428+
if inspect.isawaitable(call):
429+
await call
418430

419431
_end_trace(trace_config_ctx)
420432

0 commit comments

Comments
 (0)