Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PEP 561 to opentelemetry-instrumentation-urllib3 #3130

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#3100](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3100))
- Add support to database stability opt-in in `_semconv` utilities and add tests
([#3111](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3111))
- `opentelemetry-instrumentation-urllib3` Add `py.typed` file to enable PEP 561
([#3130](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3130))
- `opentelemetry-instrumentation-system-metrics` Add `py.typed` file to enable PEP 561
([#3132](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3132))
- `opentelemetry-opentelemetry-sqlite3` Add `py.typed` file to enable PEP 561
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,15 +83,19 @@ def response_hook(
---
"""

from __future__ import annotations

import collections.abc
import io
import typing
from dataclasses import dataclass
from timeit import default_timer
from typing import Collection
from typing import Any, Callable, Collection, Mapping, Optional

import urllib3.connectionpool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe update callers of this while we are it? It looks like we are only using urllib3.connectionpool.HTTPConnectionPool

Copy link
Contributor Author

@Kludex Kludex Dec 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean the wrap calls? I didn't want to do it because it may have some runtime unexpected behavior that I'm not aware. Should I change it as well?

But FYI, it was on purpose.

import wrapt
from urllib3.connectionpool import HTTPConnectionPool
from urllib3.response import HTTPResponse

from opentelemetry.instrumentation._semconv import (
_client_duration_attrs_new,
Expand Down Expand Up @@ -151,32 +155,16 @@ class RequestInfo:
# The type annotations here come from ``HTTPConnectionPool.urlopen()``.
method: str
url: str
headers: typing.Optional[typing.Mapping[str, str]]
body: typing.Union[
bytes, typing.IO[typing.Any], typing.Iterable[bytes], str, None
]


_UrlFilterT = typing.Optional[typing.Callable[[str], str]]
_RequestHookT = typing.Optional[
typing.Callable[
[
Span,
urllib3.connectionpool.HTTPConnectionPool,
RequestInfo,
],
None,
]
headers: Mapping[str, str] | None
body: bytes | typing.IO[typing.Any] | typing.Iterable[bytes] | str | None


_UrlFilterT = Optional[Callable[[str], str]]
xrmx marked this conversation as resolved.
Show resolved Hide resolved
_RequestHookT = Optional[
Callable[[Span, HTTPConnectionPool, RequestInfo], None]
]
_ResponseHookT = typing.Optional[
typing.Callable[
[
Span,
urllib3.connectionpool.HTTPConnectionPool,
urllib3.response.HTTPResponse,
],
None,
]
_ResponseHookT = Optional[
Callable[[Span, HTTPConnectionPool, HTTPResponse], None]
]

_URL_OPEN_ARG_TO_INDEX_MAPPING = {
Expand All @@ -190,7 +178,7 @@ class URLLib3Instrumentor(BaseInstrumentor):
def instrumentation_dependencies(self) -> Collection[str]:
return _instruments

def _instrument(self, **kwargs):
def _instrument(self, **kwargs: Any):
"""Instruments the urllib3 module
Args:
Expand Down Expand Up @@ -286,7 +274,7 @@ def _instrument(self, **kwargs):
sem_conv_opt_in_mode=sem_conv_opt_in_mode,
)

def _uninstrument(self, **kwargs):
def _uninstrument(self, **kwargs: Any):
_uninstrument()


Expand All @@ -308,10 +296,15 @@ def _instrument(
request_hook: _RequestHookT = None,
response_hook: _ResponseHookT = None,
url_filter: _UrlFilterT = None,
excluded_urls: ExcludeList = None,
excluded_urls: ExcludeList | None = None,
sem_conv_opt_in_mode: _StabilityMode = _StabilityMode.DEFAULT,
):
def instrumented_urlopen(wrapped, instance, args, kwargs):
def instrumented_urlopen(
wrapped: Callable[..., HTTPResponse],
instance: HTTPConnectionPool,
args: list[Any],
kwargs: dict[str, Any],
):
if not is_http_instrumentation_enabled():
return wrapped(*args, **kwargs)

Expand Down Expand Up @@ -397,7 +390,9 @@ def instrumented_urlopen(wrapped, instance, args, kwargs):
)


def _get_url_open_arg(name: str, args: typing.List, kwargs: typing.Mapping):
def _get_url_open_arg(
name: str, args: list[Any], kwargs: typing.Mapping[str, Any]
):
arg_idx = _URL_OPEN_ARG_TO_INDEX_MAPPING.get(name)
if arg_idx is not None:
try:
Expand All @@ -408,9 +403,9 @@ def _get_url_open_arg(name: str, args: typing.List, kwargs: typing.Mapping):


def _get_url(
instance: urllib3.connectionpool.HTTPConnectionPool,
args: typing.List,
kwargs: typing.Mapping,
instance: HTTPConnectionPool,
args: list[Any],
kwargs: typing.Mapping[str, Any],
url_filter: _UrlFilterT,
) -> str:
url_or_path = _get_url_open_arg("url", args, kwargs)
Expand All @@ -427,7 +422,7 @@ def _get_url(
return url


def _get_body_size(body: object) -> typing.Optional[int]:
def _get_body_size(body: object) -> int | None:
if body is None:
return 0
if isinstance(body, collections.abc.Sized):
Expand All @@ -437,7 +432,7 @@ def _get_body_size(body: object) -> typing.Optional[int]:
return None


def _should_append_port(scheme: str, port: typing.Optional[int]) -> bool:
def _should_append_port(scheme: str, port: int | None) -> bool:
if not port:
return False
if scheme == "http" and port == 80:
Expand All @@ -447,7 +442,7 @@ def _should_append_port(scheme: str, port: typing.Optional[int]) -> bool:
return True


def _prepare_headers(urlopen_kwargs: typing.Dict) -> typing.Dict:
def _prepare_headers(urlopen_kwargs: dict[str, Any]) -> dict[str, Any]:
headers = urlopen_kwargs.get("headers")

# avoid modifying original headers on inject
Expand All @@ -460,7 +455,7 @@ def _prepare_headers(urlopen_kwargs: typing.Dict) -> typing.Dict:
def _set_status_code_attribute(
span: Span,
status_code: int,
metric_attributes: dict = None,
metric_attributes: dict[str, Any] | None = None,
sem_conv_opt_in_mode: _StabilityMode = _StabilityMode.DEFAULT,
) -> None:
status_code_str = str(status_code)
Expand All @@ -483,9 +478,9 @@ def _set_status_code_attribute(


def _set_metric_attributes(
metric_attributes: dict,
instance: urllib3.connectionpool.HTTPConnectionPool,
response: urllib3.response.HTTPResponse,
metric_attributes: dict[str, Any],
instance: HTTPConnectionPool,
response: HTTPResponse,
method: str,
sem_conv_opt_in_mode: _StabilityMode = _StabilityMode.DEFAULT,
) -> None:
Expand Down Expand Up @@ -515,9 +510,9 @@ def _set_metric_attributes(


def _filter_attributes_semconv(
metric_attributes,
metric_attributes: dict[str, object],
sem_conv_opt_in_mode: _StabilityMode = _StabilityMode.DEFAULT,
):
) -> tuple[dict[str, object] | None, dict[str, object] | None]:
duration_attrs_old = None
duration_attrs_new = None
if _report_old(sem_conv_opt_in_mode):
Expand All @@ -539,7 +534,7 @@ def _filter_attributes_semconv(


def _record_metrics(
metric_attributes: dict,
metric_attributes: dict[str, object],
duration_histogram_old: Histogram,
duration_histogram_new: Histogram,
request_size_histogram_old: Histogram,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from re import IGNORECASE as RE_IGNORECASE
from re import compile as re_compile
from re import search
from typing import Callable, Iterable, Optional
from typing import Callable, Iterable, Optional, overload
from urllib.parse import urlparse, urlunparse

from opentelemetry.semconv.trace import SpanAttributes
Expand Down Expand Up @@ -193,6 +193,14 @@ def normalise_response_header_name(header: str) -> str:
return f"http.response.header.{key}"


@overload
def sanitize_method(method: str) -> str: ...


@overload
def sanitize_method(method: None) -> None: ...


def sanitize_method(method: Optional[str]) -> Optional[str]:
if method is None:
return None
Expand Down
Loading