@@ -88,8 +88,10 @@ def response_hook(span: Span, params: typing.Union[
8888---
8989"""
9090
91+ import inspect
9192import types
9293import typing
94+ from collections .abc import Awaitable
9395from timeit import default_timer
9496from typing import Collection
9597from 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