@@ -230,15 +230,13 @@ async def async_response_hook(span, request, response):
230
230
NETWORK_PEER_ADDRESS ,
231
231
NETWORK_PEER_PORT ,
232
232
)
233
- from opentelemetry .trace import SpanKind , TracerProvider , get_tracer
233
+ from opentelemetry .trace import SpanKind , Tracer , TracerProvider , get_tracer
234
234
from opentelemetry .trace .span import Span
235
235
from opentelemetry .trace .status import StatusCode
236
236
from opentelemetry .util .http import remove_url_credentials , sanitize_method
237
237
238
238
_logger = logging .getLogger (__name__ )
239
239
240
- URL = typing .Tuple [bytes , bytes , typing .Optional [int ], bytes ]
241
- Headers = typing .List [typing .Tuple [bytes , bytes ]]
242
240
RequestHook = typing .Callable [[Span , "RequestInfo" ], None ]
243
241
ResponseHook = typing .Callable [[Span , "RequestInfo" , "ResponseInfo" ], None ]
244
242
AsyncRequestHook = typing .Callable [
@@ -253,17 +251,15 @@ class RequestInfo(typing.NamedTuple):
253
251
method : bytes
254
252
url : httpx .URL
255
253
headers : httpx .Headers | None
256
- stream : typing .Optional [
257
- typing .Union [httpx .SyncByteStream , httpx .AsyncByteStream ]
258
- ]
259
- extensions : typing .Optional [dict ]
254
+ stream : httpx .SyncByteStream | httpx .AsyncByteStream | None
255
+ extensions : dict [str , typing .Any ] | None
260
256
261
257
262
258
class ResponseInfo (typing .NamedTuple ):
263
259
status_code : int
264
260
headers : httpx .Headers | None
265
- stream : typing . Iterable [ bytes ]
266
- extensions : typing .Optional [ dict ]
261
+ stream : httpx . SyncByteStream | httpx . AsyncByteStream
262
+ extensions : dict [ str , typing .Any ] | None
267
263
268
264
269
265
def _get_default_span_name (method : str ) -> str :
@@ -274,11 +270,19 @@ def _get_default_span_name(method: str) -> str:
274
270
return method
275
271
276
272
277
- def _prepare_headers (headers : typing . Optional [ Headers ] ) -> httpx .Headers :
273
+ def _prepare_headers (headers : httpx . Headers | None ) -> httpx .Headers :
278
274
return httpx .Headers (headers )
279
275
280
276
281
- def _extract_parameters (args , kwargs ):
277
+ def _extract_parameters (
278
+ args : tuple [typing .Any , ...], kwargs : dict [str , typing .Any ]
279
+ ) -> tuple [
280
+ bytes ,
281
+ httpx .URL ,
282
+ httpx .Headers | None ,
283
+ httpx .SyncByteStream | httpx .AsyncByteStream | None ,
284
+ dict [str , typing .Any ],
285
+ ]:
282
286
if isinstance (args [0 ], httpx .Request ):
283
287
# In httpx >= 0.20.0, handle_request receives a Request object
284
288
request : httpx .Request = args [0 ]
@@ -311,10 +315,15 @@ def _inject_propagation_headers(headers, args, kwargs):
311
315
312
316
313
317
def _extract_response (
314
- response : typing .Union [
315
- httpx .Response , typing .Tuple [int , Headers , httpx .SyncByteStream , dict ]
316
- ],
317
- ) -> typing .Tuple [int , Headers , httpx .SyncByteStream , dict , str ]:
318
+ response : httpx .Response
319
+ | tuple [int , httpx .Headers , httpx .SyncByteStream , dict [str , typing .Any ]],
320
+ ) -> tuple [
321
+ int ,
322
+ httpx .Headers ,
323
+ httpx .SyncByteStream | httpx .AsyncByteStream ,
324
+ dict [str , typing .Any ],
325
+ str ,
326
+ ]:
318
327
if isinstance (response , httpx .Response ):
319
328
status_code = response .status_code
320
329
headers = response .headers
@@ -331,8 +340,8 @@ def _extract_response(
331
340
332
341
333
342
def _apply_request_client_attributes_to_span (
334
- span_attributes : dict ,
335
- url : typing . Union [ str , URL , httpx .URL ] ,
343
+ span_attributes : dict [ str , typing . Any ] ,
344
+ url : str | httpx .URL ,
336
345
method_original : str ,
337
346
semconv : _StabilityMode ,
338
347
):
@@ -407,9 +416,9 @@ class SyncOpenTelemetryTransport(httpx.BaseTransport):
407
416
def __init__ (
408
417
self ,
409
418
transport : httpx .BaseTransport ,
410
- tracer_provider : typing . Optional [ TracerProvider ] = None ,
411
- request_hook : typing . Optional [ RequestHook ] = None ,
412
- response_hook : typing . Optional [ ResponseHook ] = None ,
419
+ tracer_provider : TracerProvider | None = None ,
420
+ request_hook : RequestHook | None = None ,
421
+ response_hook : ResponseHook | None = None ,
413
422
):
414
423
_OpenTelemetrySemanticConventionStability ._initialize ()
415
424
self ._sem_conv_opt_in_mode = _OpenTelemetrySemanticConventionStability ._get_opentelemetry_stability_opt_in_mode (
@@ -426,27 +435,27 @@ def __init__(
426
435
self ._request_hook = request_hook
427
436
self ._response_hook = response_hook
428
437
429
- def __enter__ (self ) -> " SyncOpenTelemetryTransport" :
438
+ def __enter__ (self ) -> SyncOpenTelemetryTransport :
430
439
self ._transport .__enter__ ()
431
440
return self
432
441
433
442
def __exit__ (
434
443
self ,
435
- exc_type : typing . Optional [ typing . Type [ BaseException ]] = None ,
436
- exc_value : typing . Optional [ BaseException ] = None ,
437
- traceback : typing . Optional [ TracebackType ] = None ,
444
+ exc_type : type [ BaseException ] | None = None ,
445
+ exc_value : BaseException | None = None ,
446
+ traceback : TracebackType | None = None ,
438
447
) -> None :
439
448
self ._transport .__exit__ (exc_type , exc_value , traceback )
440
449
441
450
# pylint: disable=R0914
442
451
def handle_request (
443
452
self ,
444
- * args ,
445
- ** kwargs ,
446
- ) -> typing . Union [
447
- typing . Tuple [int , " Headers" , httpx .SyncByteStream , dict ],
448
- httpx .Response ,
449
- ] :
453
+ * args : typing . Any ,
454
+ ** kwargs : typing . Any ,
455
+ ) -> (
456
+ tuple [int , httpx . Headers , httpx .SyncByteStream , dict [ str , typing . Any ]]
457
+ | httpx .Response
458
+ ) :
450
459
"""Add request info to span."""
451
460
if not is_http_instrumentation_enabled ():
452
461
return self ._transport .handle_request (* args , ** kwargs )
@@ -532,9 +541,9 @@ class AsyncOpenTelemetryTransport(httpx.AsyncBaseTransport):
532
541
def __init__ (
533
542
self ,
534
543
transport : httpx .AsyncBaseTransport ,
535
- tracer_provider : typing . Optional [ TracerProvider ] = None ,
536
- request_hook : typing . Optional [ AsyncRequestHook ] = None ,
537
- response_hook : typing . Optional [ AsyncResponseHook ] = None ,
544
+ tracer_provider : TracerProvider | None = None ,
545
+ request_hook : AsyncRequestHook | None = None ,
546
+ response_hook : AsyncResponseHook | None = None ,
538
547
):
539
548
_OpenTelemetrySemanticConventionStability ._initialize ()
540
549
self ._sem_conv_opt_in_mode = _OpenTelemetrySemanticConventionStability ._get_opentelemetry_stability_opt_in_mode (
@@ -557,19 +566,19 @@ async def __aenter__(self) -> "AsyncOpenTelemetryTransport":
557
566
558
567
async def __aexit__ (
559
568
self ,
560
- exc_type : typing .Optional [ typing . Type [BaseException ]] = None ,
561
- exc_value : typing . Optional [ BaseException ] = None ,
562
- traceback : typing . Optional [ TracebackType ] = None ,
569
+ exc_type : typing .Type [BaseException ] | None = None ,
570
+ exc_value : BaseException | None = None ,
571
+ traceback : TracebackType | None = None ,
563
572
) -> None :
564
573
await self ._transport .__aexit__ (exc_type , exc_value , traceback )
565
574
566
575
# pylint: disable=R0914
567
576
async def handle_async_request (
568
- self , * args , ** kwargs
569
- ) -> typing . Union [
570
- typing . Tuple [int , " Headers" , httpx .AsyncByteStream , dict ],
571
- httpx .Response ,
572
- ] :
577
+ self , * args : typing . Any , ** kwargs : typing . Any
578
+ ) -> (
579
+ tuple [int , httpx . Headers , httpx .AsyncByteStream , dict [ str , typing . Any ]]
580
+ | httpx .Response
581
+ ) :
573
582
"""Add request info to span."""
574
583
if not is_http_instrumentation_enabled ():
575
584
return await self ._transport .handle_async_request (* args , ** kwargs )
@@ -653,7 +662,7 @@ class HTTPXClientInstrumentor(BaseInstrumentor):
653
662
def instrumentation_dependencies (self ) -> typing .Collection [str ]:
654
663
return _instruments
655
664
656
- def _instrument (self , ** kwargs ):
665
+ def _instrument (self , ** kwargs : typing . Any ):
657
666
"""Instruments httpx Client and AsyncClient
658
667
659
668
Args:
@@ -716,20 +725,20 @@ def _instrument(self, **kwargs):
716
725
),
717
726
)
718
727
719
- def _uninstrument (self , ** kwargs ):
728
+ def _uninstrument (self , ** kwargs : typing . Any ):
720
729
unwrap (httpx .HTTPTransport , "handle_request" )
721
730
unwrap (httpx .AsyncHTTPTransport , "handle_async_request" )
722
731
723
732
@staticmethod
724
733
def _handle_request_wrapper ( # pylint: disable=too-many-locals
725
- wrapped ,
726
- instance ,
727
- args ,
728
- kwargs ,
729
- tracer ,
730
- sem_conv_opt_in_mode ,
731
- request_hook ,
732
- response_hook ,
734
+ wrapped : typing . Callable [..., typing . Any ] ,
735
+ instance : httpx . HTTPTransport ,
736
+ args : tuple [ typing . Any , ...] ,
737
+ kwargs : dict [ str , typing . Any ] ,
738
+ tracer : Tracer ,
739
+ sem_conv_opt_in_mode : _StabilityMode ,
740
+ request_hook : RequestHook ,
741
+ response_hook : ResponseHook ,
733
742
):
734
743
if not is_http_instrumentation_enabled ():
735
744
return wrapped (* args , ** kwargs )
@@ -796,14 +805,14 @@ def _handle_request_wrapper( # pylint: disable=too-many-locals
796
805
797
806
@staticmethod
798
807
async def _handle_async_request_wrapper ( # pylint: disable=too-many-locals
799
- wrapped ,
800
- instance ,
801
- args ,
802
- kwargs ,
803
- tracer ,
804
- sem_conv_opt_in_mode ,
805
- async_request_hook ,
806
- async_response_hook ,
808
+ wrapped : typing . Callable [..., typing . Awaitable [ typing . Any ]] ,
809
+ instance : httpx . AsyncHTTPTransport ,
810
+ args : tuple [ typing . Any , ...] ,
811
+ kwargs : dict [ str , typing . Any ] ,
812
+ tracer : Tracer ,
813
+ sem_conv_opt_in_mode : _StabilityMode ,
814
+ async_request_hook : AsyncRequestHook ,
815
+ async_response_hook : AsyncResponseHook ,
807
816
):
808
817
if not is_http_instrumentation_enabled ():
809
818
return await wrapped (* args , ** kwargs )
@@ -872,14 +881,10 @@ async def _handle_async_request_wrapper( # pylint: disable=too-many-locals
872
881
@classmethod
873
882
def instrument_client (
874
883
cls ,
875
- client : typing .Union [httpx .Client , httpx .AsyncClient ],
876
- tracer_provider : TracerProvider = None ,
877
- request_hook : typing .Union [
878
- typing .Optional [RequestHook ], typing .Optional [AsyncRequestHook ]
879
- ] = None ,
880
- response_hook : typing .Union [
881
- typing .Optional [ResponseHook ], typing .Optional [AsyncResponseHook ]
882
- ] = None ,
884
+ client : httpx .Client | httpx .AsyncClient ,
885
+ tracer_provider : TracerProvider | None = None ,
886
+ request_hook : RequestHook | AsyncRequestHook | None = None ,
887
+ response_hook : ResponseHook | AsyncResponseHook | None = None ,
883
888
) -> None :
884
889
"""Instrument httpx Client or AsyncClient
885
890
@@ -977,9 +982,7 @@ def instrument_client(
977
982
client ._is_instrumented_by_opentelemetry = True
978
983
979
984
@staticmethod
980
- def uninstrument_client (
981
- client : typing .Union [httpx .Client , httpx .AsyncClient ],
982
- ):
985
+ def uninstrument_client (client : httpx .Client | httpx .AsyncClient ) -> None :
983
986
"""Disables instrumentation for the given client instance
984
987
985
988
Args:
0 commit comments