Skip to content

Commit

Permalink
fix: sync response hooks being used on httpx.AsyncClient
Browse files Browse the repository at this point in the history
  • Loading branch information
samypr100 committed Aug 25, 2023
1 parent 0871dd4 commit 24f4b7e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,16 @@ def response_hook(span, request, response):
# status_code, headers, stream, extensions = response
pass
HTTPXClientInstrumentor().instrument(request_hook=request_hook, response_hook=response_hook)
async def async_request_hook(span, request):
# method, url, headers, stream, extensions = request
pass
async def async_response_hook(span, request, response):
# method, url, headers, stream, extensions = request
# status_code, headers, stream, extensions = response
pass
HTTPXClientInstrumentor().instrument(request_hook=request_hook, response_hook=response_hook, async_request_hook=async_request_hook, async_response_hook=async_response_hook)
Or if you are using the transport classes directly:
Expand Down Expand Up @@ -376,8 +385,8 @@ def __init__(
self,
transport: httpx.AsyncBaseTransport,
tracer_provider: typing.Optional[TracerProvider] = None,
request_hook: typing.Optional[RequestHook] = None,
response_hook: typing.Optional[ResponseHook] = None,
request_hook: typing.Optional[AsyncRequestHook] = None,
response_hook: typing.Optional[AsyncResponseHook] = None,
):
self._transport = transport
self._tracer = get_tracer(
Expand Down Expand Up @@ -509,21 +518,27 @@ def _instrument(self, **kwargs):
Args:
**kwargs: Optional arguments
``tracer_provider``: a TracerProvider, defaults to global
``request_hook``: A hook that receives the span and request that is called
right after the span is created
``response_hook``: A hook that receives the span, request, and response
that is called right before the span ends
``request_hook``: A ``httpx.Client`` hook that receives the span and request
that is called right after the span is created
``response_hook``: A ``httpx.Client`` hook that receives the span, request,
and response that is called right before the span ends
``async_request_hook``: Async ``request_hook`` for ``httpx.AsyncClient``
``async_response_hook``: Async``response_hook`` for ``httpx.AsyncClient``
"""
self._original_client = httpx.Client
self._original_async_client = httpx.AsyncClient
request_hook = kwargs.get("request_hook")
response_hook = kwargs.get("response_hook")
async_request_hook = kwargs.get("async_request_hook", request_hook)
async_response_hook = kwargs.get("async_response_hook", response_hook)
if callable(request_hook):
_InstrumentedClient._request_hook = request_hook
_InstrumentedAsyncClient._request_hook = request_hook
if callable(async_request_hook):
_InstrumentedAsyncClient._request_hook = async_request_hook
if callable(response_hook):
_InstrumentedClient._response_hook = response_hook
_InstrumentedAsyncClient._response_hook = response_hook
if callable(async_response_hook):
_InstrumentedAsyncClient._response_hook = async_response_hook
tracer_provider = kwargs.get("tracer_provider")
_InstrumentedClient._tracer_provider = tracer_provider
_InstrumentedAsyncClient._tracer_provider = tracer_provider
Expand All @@ -544,8 +559,12 @@ def _uninstrument(self, **kwargs):
def instrument_client(
client: typing.Union[httpx.Client, httpx.AsyncClient],
tracer_provider: TracerProvider = None,
request_hook: typing.Optional[RequestHook] = None,
response_hook: typing.Optional[ResponseHook] = None,
request_hook: typing.Union[
typing.Optional[RequestHook], typing.Optional[AsyncRequestHook]
] = None,
response_hook: typing.Union[
typing.Optional[ResponseHook], typing.Optional[AsyncResponseHook]
] = None,
) -> None:
"""Instrument httpx Client or AsyncClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,28 @@ def test_response_hook(self):
)
HTTPXClientInstrumentor().uninstrument()

def test_response_hook_sync_async_kwargs(self):
HTTPXClientInstrumentor().instrument(
tracer_provider=self.tracer_provider,
response_hook=_response_hook,
async_response_hook=_async_response_hook,
)
client = self.create_client()
result = self.perform_request(self.URL, client=client)

self.assertEqual(result.text, "Hello!")
span = self.assert_span()
self.assertEqual(
span.attributes,
{
SpanAttributes.HTTP_METHOD: "GET",
SpanAttributes.HTTP_URL: self.URL,
SpanAttributes.HTTP_STATUS_CODE: 200,
HTTP_RESPONSE_BODY: "Hello!",
},
)
HTTPXClientInstrumentor().uninstrument()

def test_request_hook(self):
HTTPXClientInstrumentor().instrument(
tracer_provider=self.tracer_provider,
Expand All @@ -434,6 +456,20 @@ def test_request_hook(self):
self.assertEqual(span.name, "GET" + self.URL)
HTTPXClientInstrumentor().uninstrument()

def test_request_hook_sync_async_kwargs(self):
HTTPXClientInstrumentor().instrument(
tracer_provider=self.tracer_provider,
request_hook=_request_hook,
async_request_hook=_async_request_hook,
)
client = self.create_client()
result = self.perform_request(self.URL, client=client)

self.assertEqual(result.text, "Hello!")
span = self.assert_span()
self.assertEqual(span.name, "GET" + self.URL)
HTTPXClientInstrumentor().uninstrument()

def test_request_hook_no_span_update(self):
HTTPXClientInstrumentor().instrument(
tracer_provider=self.tracer_provider,
Expand Down

0 comments on commit 24f4b7e

Please sign in to comment.