@@ -731,7 +731,9 @@ def _instrument(self, **kwargs):
731
731
self ._original_async_client = httpx .AsyncClient
732
732
request_hook = kwargs .get ("request_hook" )
733
733
response_hook = kwargs .get ("response_hook" )
734
- async_request_hook = kwargs .get ("async_request_hook" , request_hook )
734
+ async_request_hook = kwargs .get (
735
+ "async_request_hook" , self ._wrap_async_request_hook (request_hook )
736
+ )
735
737
async_response_hook = kwargs .get ("async_response_hook" , response_hook )
736
738
if callable (request_hook ):
737
739
_InstrumentedClient ._request_hook = request_hook
@@ -749,6 +751,16 @@ def _instrument(self, **kwargs):
749
751
httpx .Client = httpx ._api .Client = _InstrumentedClient
750
752
httpx .AsyncClient = _InstrumentedAsyncClient
751
753
754
+ # Wrap a given request hook function and ensure it is asynchronous
755
+ def _wrap_async_request_hook (self , request_hook_func ):
756
+ if request_hook_func is None :
757
+ return None
758
+
759
+ async def async_request_hook (span , req ):
760
+ return request_hook_func (span , req )
761
+
762
+ return async_request_hook
763
+
752
764
def _uninstrument (self , ** kwargs ):
753
765
httpx .Client = httpx ._api .Client = self ._original_client
754
766
httpx .AsyncClient = self ._original_async_client
0 commit comments