Skip to content

Commit 39d3670

Browse files
committed
rebased... and add more types
1 parent c84c942 commit 39d3670

File tree

1 file changed

+28
-20
lines changed
  • instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx

1 file changed

+28
-20
lines changed

instrumentation/opentelemetry-instrumentation-httpx/src/opentelemetry/instrumentation/httpx/__init__.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,6 @@ async def async_response_hook(span, request, response):
237237

238238
_logger = logging.getLogger(__name__)
239239

240-
URL = typing.Tuple[bytes, bytes, typing.Optional[int], bytes]
241-
Headers = typing.List[typing.Tuple[bytes, bytes]]
242240
RequestHook = typing.Callable[[Span, "RequestInfo"], None]
243241
ResponseHook = typing.Callable[[Span, "RequestInfo", "ResponseInfo"], None]
244242
AsyncRequestHook = typing.Callable[
@@ -260,7 +258,7 @@ class RequestInfo(typing.NamedTuple):
260258
class ResponseInfo(typing.NamedTuple):
261259
status_code: int
262260
headers: httpx.Headers | None
263-
stream: typing.Iterable[bytes]
261+
stream: httpx.SyncByteStream | httpx.AsyncByteStream
264262
extensions: dict[str, typing.Any] | None
265263

266264

@@ -272,13 +270,19 @@ def _get_default_span_name(method: str) -> str:
272270
return method
273271

274272

275-
def _prepare_headers(headers: Headers | None) -> httpx.Headers:
273+
def _prepare_headers(headers: httpx.Headers | None) -> httpx.Headers:
276274
return httpx.Headers(headers)
277275

278276

279277
def _extract_parameters(
280278
args: tuple[typing.Any, ...], kwargs: dict[str, typing.Any]
281-
):
279+
) -> tuple[
280+
bytes,
281+
httpx.URL,
282+
httpx.Headers | None,
283+
httpx.SyncByteStream | httpx.AsyncByteStream | None,
284+
dict[str, typing.Any],
285+
]:
282286
if isinstance(args[0], httpx.Request):
283287
# In httpx >= 0.20.0, handle_request receives a Request object
284288
request: httpx.Request = args[0]
@@ -312,8 +316,14 @@ def _inject_propagation_headers(headers, args, kwargs):
312316

313317
def _extract_response(
314318
response: httpx.Response
315-
| tuple[int, Headers, httpx.SyncByteStream, dict[str, typing.Any]],
316-
) -> tuple[int, Headers, httpx.SyncByteStream, dict[str, typing.Any], str]:
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+
]:
317327
if isinstance(response, httpx.Response):
318328
status_code = response.status_code
319329
headers = response.headers
@@ -331,7 +341,7 @@ def _extract_response(
331341

332342
def _apply_request_client_attributes_to_span(
333343
span_attributes: dict[str, typing.Any],
334-
url: typing.Union[str, URL, httpx.URL],
344+
url: str | httpx.URL,
335345
method_original: str,
336346
semconv: _StabilityMode,
337347
):
@@ -443,7 +453,7 @@ def handle_request(
443453
*args: typing.Any,
444454
**kwargs: typing.Any,
445455
) -> (
446-
tuple[int, Headers, httpx.SyncByteStream, dict[str, typing.Any]]
456+
tuple[int, httpx.Headers, httpx.SyncByteStream, dict[str, typing.Any]]
447457
| httpx.Response
448458
):
449459
"""Add request info to span."""
@@ -531,9 +541,9 @@ class AsyncOpenTelemetryTransport(httpx.AsyncBaseTransport):
531541
def __init__(
532542
self,
533543
transport: httpx.AsyncBaseTransport,
534-
tracer_provider: typing.Optional[TracerProvider] = None,
535-
request_hook: typing.Optional[AsyncRequestHook] = None,
536-
response_hook: typing.Optional[AsyncResponseHook] = None,
544+
tracer_provider: TracerProvider | None = None,
545+
request_hook: AsyncRequestHook | None = None,
546+
response_hook: AsyncResponseHook | None = None,
537547
):
538548
_OpenTelemetrySemanticConventionStability._initialize()
539549
self._sem_conv_opt_in_mode = _OpenTelemetrySemanticConventionStability._get_opentelemetry_stability_opt_in_mode(
@@ -556,19 +566,17 @@ async def __aenter__(self) -> "AsyncOpenTelemetryTransport":
556566

557567
async def __aexit__(
558568
self,
559-
exc_type: typing.Optional[typing.Type[BaseException]] = None,
560-
exc_value: typing.Optional[BaseException] = None,
561-
traceback: typing.Optional[TracebackType] = None,
569+
exc_type: typing.Type[BaseException] | None = None,
570+
exc_value: BaseException | None = None,
571+
traceback: TracebackType | None = None,
562572
) -> None:
563573
await self._transport.__aexit__(exc_type, exc_value, traceback)
564574

565575
# pylint: disable=R0914
566576
async def handle_async_request(
567577
self, *args: typing.Any, **kwargs: typing.Any
568578
) -> (
569-
typing.Tuple[
570-
int, Headers, httpx.AsyncByteStream, dict[str, typing.Any]
571-
]
579+
tuple[int, httpx.Headers, httpx.AsyncByteStream, dict[str, typing.Any]]
572580
| httpx.Response
573581
):
574582
"""Add request info to span."""
@@ -728,7 +736,7 @@ def _handle_request_wrapper( # pylint: disable=too-many-locals
728736
args: tuple[typing.Any, ...],
729737
kwargs: dict[str, typing.Any],
730738
tracer: Tracer,
731-
sem_conv_opt_in_mode: _HTTPStabilityMode,
739+
sem_conv_opt_in_mode: _StabilityMode,
732740
request_hook: RequestHook,
733741
response_hook: ResponseHook,
734742
):
@@ -802,7 +810,7 @@ async def _handle_async_request_wrapper( # pylint: disable=too-many-locals
802810
args: tuple[typing.Any, ...],
803811
kwargs: dict[str, typing.Any],
804812
tracer: Tracer,
805-
sem_conv_opt_in_mode: _HTTPStabilityMode,
813+
sem_conv_opt_in_mode: _StabilityMode,
806814
async_request_hook: AsyncRequestHook,
807815
async_response_hook: AsyncResponseHook,
808816
):

0 commit comments

Comments
 (0)