@@ -131,7 +131,16 @@ def response_hook(span, request, response):
131
131
# status_code, headers, stream, extensions = response
132
132
pass
133
133
134
- HTTPXClientInstrumentor().instrument(request_hook=request_hook, response_hook=response_hook)
134
+ async def async_request_hook(span, request):
135
+ # method, url, headers, stream, extensions = request
136
+ pass
137
+
138
+ async def async_response_hook(span, request, response):
139
+ # method, url, headers, stream, extensions = request
140
+ # status_code, headers, stream, extensions = response
141
+ pass
142
+
143
+ HTTPXClientInstrumentor().instrument(request_hook=request_hook, response_hook=response_hook, async_request_hook=async_request_hook, async_response_hook=async_response_hook)
135
144
136
145
137
146
Or if you are using the transport classes directly:
@@ -376,8 +385,8 @@ def __init__(
376
385
self ,
377
386
transport : httpx .AsyncBaseTransport ,
378
387
tracer_provider : typing .Optional [TracerProvider ] = None ,
379
- request_hook : typing .Optional [RequestHook ] = None ,
380
- response_hook : typing .Optional [ResponseHook ] = None ,
388
+ request_hook : typing .Optional [AsyncRequestHook ] = None ,
389
+ response_hook : typing .Optional [AsyncResponseHook ] = None ,
381
390
):
382
391
self ._transport = transport
383
392
self ._tracer = get_tracer (
@@ -509,21 +518,27 @@ def _instrument(self, **kwargs):
509
518
Args:
510
519
**kwargs: Optional arguments
511
520
``tracer_provider``: a TracerProvider, defaults to global
512
- ``request_hook``: A hook that receives the span and request that is called
513
- right after the span is created
514
- ``response_hook``: A hook that receives the span, request, and response
515
- that is called right before the span ends
521
+ ``request_hook``: A ``httpx.Client`` hook that receives the span and request
522
+ that is called right after the span is created
523
+ ``response_hook``: A ``httpx.Client`` hook that receives the span, request,
524
+ and response that is called right before the span ends
525
+ ``async_request_hook``: Async ``request_hook`` for ``httpx.AsyncClient``
526
+ ``async_response_hook``: Async``response_hook`` for ``httpx.AsyncClient``
516
527
"""
517
528
self ._original_client = httpx .Client
518
529
self ._original_async_client = httpx .AsyncClient
519
530
request_hook = kwargs .get ("request_hook" )
520
531
response_hook = kwargs .get ("response_hook" )
532
+ async_request_hook = kwargs .get ("async_request_hook" , request_hook )
533
+ async_response_hook = kwargs .get ("async_response_hook" , response_hook )
521
534
if callable (request_hook ):
522
535
_InstrumentedClient ._request_hook = request_hook
523
- _InstrumentedAsyncClient ._request_hook = request_hook
536
+ if callable (async_request_hook ):
537
+ _InstrumentedAsyncClient ._request_hook = async_request_hook
524
538
if callable (response_hook ):
525
539
_InstrumentedClient ._response_hook = response_hook
526
- _InstrumentedAsyncClient ._response_hook = response_hook
540
+ if callable (async_response_hook ):
541
+ _InstrumentedAsyncClient ._response_hook = async_response_hook
527
542
tracer_provider = kwargs .get ("tracer_provider" )
528
543
_InstrumentedClient ._tracer_provider = tracer_provider
529
544
_InstrumentedAsyncClient ._tracer_provider = tracer_provider
@@ -544,8 +559,12 @@ def _uninstrument(self, **kwargs):
544
559
def instrument_client (
545
560
client : typing .Union [httpx .Client , httpx .AsyncClient ],
546
561
tracer_provider : TracerProvider = None ,
547
- request_hook : typing .Optional [RequestHook ] = None ,
548
- response_hook : typing .Optional [ResponseHook ] = None ,
562
+ request_hook : typing .Union [
563
+ typing .Optional [RequestHook ], typing .Optional [AsyncRequestHook ]
564
+ ] = None ,
565
+ response_hook : typing .Union [
566
+ typing .Optional [ResponseHook ], typing .Optional [AsyncResponseHook ]
567
+ ] = None ,
549
568
) -> None :
550
569
"""Instrument httpx Client or AsyncClient
551
570
0 commit comments