@@ -162,6 +162,7 @@ def response_hook(span, request, response):
162
162
"""
163
163
import logging
164
164
import typing
165
+ from types import TracebackType
165
166
166
167
import httpx
167
168
@@ -293,6 +294,18 @@ def __init__(
293
294
self ._request_hook = request_hook
294
295
self ._response_hook = response_hook
295
296
297
+ def __enter__ (self ) -> "SyncOpenTelemetryTransport" :
298
+ self ._transport .__enter__ ()
299
+ return self
300
+
301
+ def __exit__ (
302
+ self ,
303
+ exc_type : typing .Optional [typing .Type [BaseException ]] = None ,
304
+ exc_value : typing .Optional [BaseException ] = None ,
305
+ traceback : typing .Optional [TracebackType ] = None ,
306
+ ) -> None :
307
+ self ._transport .__exit__ (exc_type , exc_value , traceback )
308
+
296
309
def handle_request (
297
310
self ,
298
311
* args ,
@@ -343,6 +356,9 @@ def handle_request(
343
356
344
357
return response
345
358
359
+ def close (self ) -> None :
360
+ self ._transport .close ()
361
+
346
362
347
363
class AsyncOpenTelemetryTransport (httpx .AsyncBaseTransport ):
348
364
"""Async transport class that will trace all requests made with a client.
@@ -372,6 +388,18 @@ def __init__(
372
388
self ._request_hook = request_hook
373
389
self ._response_hook = response_hook
374
390
391
+ async def __aenter__ (self : A ) -> A : # Use generics for subclass support.
392
+ await self ._transport .__aenter__ ()
393
+ return self
394
+
395
+ async def __aexit__ (
396
+ self ,
397
+ exc_type : typing .Optional [typing .Type [BaseException ]] = None ,
398
+ exc_value : typing .Optional [BaseException ] = None ,
399
+ traceback : typing .Optional [TracebackType ] = None ,
400
+ ) -> None :
401
+ await self ._transport .__aexit__ (exc_type , exc_value , traceback )
402
+
375
403
async def handle_async_request (
376
404
self , * args , ** kwargs
377
405
) -> typing .Union [
@@ -423,6 +451,9 @@ async def handle_async_request(
423
451
424
452
return response
425
453
454
+ async def aclose (self ) -> None :
455
+ await self ._transport .aclose ()
456
+
426
457
427
458
class _InstrumentedClient (httpx .Client ):
428
459
_tracer_provider = None
0 commit comments