Skip to content

Commit 3ff1113

Browse files
fix Sync hook used as async hook
Signed-off-by: Shi, Stone <[email protected]>
1 parent 4f98519 commit 3ff1113

File tree

1 file changed

+13
-1
lines changed
  • instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx

1 file changed

+13
-1
lines changed

Diff for: instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py

+13-1
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,9 @@ def _instrument(self, **kwargs):
731731
self._original_async_client = httpx.AsyncClient
732732
request_hook = kwargs.get("request_hook")
733733
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+
)
735737
async_response_hook = kwargs.get("async_response_hook", response_hook)
736738
if callable(request_hook):
737739
_InstrumentedClient._request_hook = request_hook
@@ -749,6 +751,16 @@ def _instrument(self, **kwargs):
749751
httpx.Client = httpx._api.Client = _InstrumentedClient
750752
httpx.AsyncClient = _InstrumentedAsyncClient
751753

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+
752764
def _uninstrument(self, **kwargs):
753765
httpx.Client = httpx._api.Client = self._original_client
754766
httpx.AsyncClient = self._original_async_client

0 commit comments

Comments
 (0)