@@ -131,15 +131,29 @@ 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(
144
+ request_hook=request_hook,
145
+ response_hook=response_hook,
146
+ async_request_hook=async_request_hook,
147
+ async_response_hook=async_response_hook
148
+ )
135
149
136
150
137
151
Or if you are using the transport classes directly:
138
152
139
153
140
154
.. code-block:: python
141
155
142
- from opentelemetry.instrumentation.httpx import SyncOpenTelemetryTransport
156
+ from opentelemetry.instrumentation.httpx import SyncOpenTelemetryTransport, AsyncOpenTelemetryTransport
143
157
144
158
def request_hook(span, request):
145
159
# method, url, headers, stream, extensions = request
@@ -150,13 +164,29 @@ def response_hook(span, request, response):
150
164
# status_code, headers, stream, extensions = response
151
165
pass
152
166
167
+ async def async_request_hook(span, request):
168
+ # method, url, headers, stream, extensions = request
169
+ pass
170
+
171
+ async def async_response_hook(span, request, response):
172
+ # method, url, headers, stream, extensions = request
173
+ # status_code, headers, stream, extensions = response
174
+ pass
175
+
153
176
transport = httpx.HTTPTransport()
154
177
telemetry_transport = SyncOpenTelemetryTransport(
155
178
transport,
156
179
request_hook=request_hook,
157
180
response_hook=response_hook
158
181
)
159
182
183
+ async_transport = httpx.AsyncHTTPTransport()
184
+ async_telemetry_transport = AsyncOpenTelemetryTransport(
185
+ async_transport,
186
+ request_hook=async_request_hook,
187
+ response_hook=async_response_hook
188
+ )
189
+
160
190
API
161
191
---
162
192
"""
@@ -377,8 +407,8 @@ def __init__(
377
407
self ,
378
408
transport : httpx .AsyncBaseTransport ,
379
409
tracer_provider : typing .Optional [TracerProvider ] = None ,
380
- request_hook : typing .Optional [RequestHook ] = None ,
381
- response_hook : typing .Optional [ResponseHook ] = None ,
410
+ request_hook : typing .Optional [AsyncRequestHook ] = None ,
411
+ response_hook : typing .Optional [AsyncResponseHook ] = None ,
382
412
):
383
413
self ._transport = transport
384
414
self ._tracer = get_tracer (
@@ -511,21 +541,27 @@ def _instrument(self, **kwargs):
511
541
Args:
512
542
**kwargs: Optional arguments
513
543
``tracer_provider``: a TracerProvider, defaults to global
514
- ``request_hook``: A hook that receives the span and request that is called
515
- right after the span is created
516
- ``response_hook``: A hook that receives the span, request, and response
517
- that is called right before the span ends
544
+ ``request_hook``: A ``httpx.Client`` hook that receives the span and request
545
+ that is called right after the span is created
546
+ ``response_hook``: A ``httpx.Client`` hook that receives the span, request,
547
+ and response that is called right before the span ends
548
+ ``async_request_hook``: Async ``request_hook`` for ``httpx.AsyncClient``
549
+ ``async_response_hook``: Async``response_hook`` for ``httpx.AsyncClient``
518
550
"""
519
551
self ._original_client = httpx .Client
520
552
self ._original_async_client = httpx .AsyncClient
521
553
request_hook = kwargs .get ("request_hook" )
522
554
response_hook = kwargs .get ("response_hook" )
555
+ async_request_hook = kwargs .get ("async_request_hook" , request_hook )
556
+ async_response_hook = kwargs .get ("async_response_hook" , response_hook )
523
557
if callable (request_hook ):
524
558
_InstrumentedClient ._request_hook = request_hook
525
- _InstrumentedAsyncClient ._request_hook = request_hook
559
+ if callable (async_request_hook ):
560
+ _InstrumentedAsyncClient ._request_hook = async_request_hook
526
561
if callable (response_hook ):
527
562
_InstrumentedClient ._response_hook = response_hook
528
- _InstrumentedAsyncClient ._response_hook = response_hook
563
+ if callable (async_response_hook ):
564
+ _InstrumentedAsyncClient ._response_hook = async_response_hook
529
565
tracer_provider = kwargs .get ("tracer_provider" )
530
566
_InstrumentedClient ._tracer_provider = tracer_provider
531
567
_InstrumentedAsyncClient ._tracer_provider = tracer_provider
@@ -546,8 +582,12 @@ def _uninstrument(self, **kwargs):
546
582
def instrument_client (
547
583
client : typing .Union [httpx .Client , httpx .AsyncClient ],
548
584
tracer_provider : TracerProvider = None ,
549
- request_hook : typing .Optional [RequestHook ] = None ,
550
- response_hook : typing .Optional [ResponseHook ] = None ,
585
+ request_hook : typing .Union [
586
+ typing .Optional [RequestHook ], typing .Optional [AsyncRequestHook ]
587
+ ] = None ,
588
+ response_hook : typing .Union [
589
+ typing .Optional [ResponseHook ], typing .Optional [AsyncResponseHook ]
590
+ ] = None ,
551
591
) -> None :
552
592
"""Instrument httpx Client or AsyncClient
553
593
0 commit comments