From aa712ef47780ed8b5d5a476a10af8296278b0886 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Fri, 20 Dec 2024 20:57:29 +0100 Subject: [PATCH 1/3] Support PEP 561 to `opentelemetry-instrumentation-requests` --- CHANGELOG.md | 2 ++ .../instrumentation/requests/__init__.py | 18 ++++++++++-------- .../instrumentation/requests/py.typed | 0 3 files changed, 12 insertions(+), 8 deletions(-) create mode 100644 instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/py.typed diff --git a/CHANGELOG.md b/CHANGELOG.md index a243091b1d..1c8e8af3f4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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-opentelemetry-requests` Add `py.typed` file to enable PEP 561 + ([#3134](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3134)) ### Fixed diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py index d1afa834d6..838ea85b86 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py @@ -75,7 +75,7 @@ def response_hook(span, request_obj, response) import functools import types from timeit import default_timer -from typing import Callable, Collection, Optional +from typing import Any, Callable, Collection, Optional from urllib.parse import urlparse from requests.models import PreparedRequest, Response @@ -146,7 +146,7 @@ def _instrument( duration_histogram_new: Histogram, request_hook: _RequestHookT = None, response_hook: _ResponseHookT = None, - excluded_urls: ExcludeList = None, + excluded_urls: ExcludeList | None = None, sem_conv_opt_in_mode: _StabilityMode = _StabilityMode.DEFAULT, ): """Enables tracing of all requests calls that go through @@ -164,7 +164,9 @@ def _instrument( # pylint: disable-msg=too-many-locals,too-many-branches @functools.wraps(wrapped_send) - def instrumented_send(self, request, **kwargs): + def instrumented_send( + self: Session, request: PreparedRequest, **kwargs: Any + ): if excluded_urls and excluded_urls.url_disabled(request.url): return wrapped_send(self, request, **kwargs) @@ -345,7 +347,7 @@ def _uninstrument(): _uninstrument_from(Session) -def _uninstrument_from(instr_root, restore_as_bound_func=False): +def _uninstrument_from(instr_root, restore_as_bound_func: bool = False): for instr_func_name in ("request", "send"): instr_func = getattr(instr_root, instr_func_name) if not getattr( @@ -361,7 +363,7 @@ def _uninstrument_from(instr_root, restore_as_bound_func=False): setattr(instr_root, instr_func_name, original) -def get_default_span_name(method): +def get_default_span_name(method: str) -> str: """ Default implementation for name_callback, returns HTTP {method_name}. https://opentelemetry.io/docs/reference/specification/trace/semantic_conventions/http/#name @@ -385,7 +387,7 @@ class RequestsInstrumentor(BaseInstrumentor): def instrumentation_dependencies(self) -> Collection[str]: return _instruments - def _instrument(self, **kwargs): + def _instrument(self, **kwargs: Any): """Instruments requests module Args: @@ -443,10 +445,10 @@ def _instrument(self, **kwargs): sem_conv_opt_in_mode=semconv_opt_in_mode, ) - def _uninstrument(self, **kwargs): + def _uninstrument(self, **kwargs: Any): _uninstrument() @staticmethod - def uninstrument_session(session): + def uninstrument_session(session: Session): """Disables instrumentation on the session object.""" _uninstrument_from(session, restore_as_bound_func=True) diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/py.typed b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/py.typed new file mode 100644 index 0000000000..e69de29bb2 From 6012b68c0338ccf0bce162609f46a5b220985f16 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Fri, 20 Dec 2024 21:34:24 +0100 Subject: [PATCH 2/3] add future --- .../src/opentelemetry/instrumentation/requests/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py index 838ea85b86..6c197d530e 100644 --- a/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py @@ -72,6 +72,8 @@ def response_hook(span, request_obj, response) --- """ +from __future__ import annotations + import functools import types from timeit import default_timer From b8f18bae6fad37eb89bbc1ac5706109f1ad7b2a0 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Fri, 20 Dec 2024 21:37:15 +0100 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c8e8af3f4..6bde58e775 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,7 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - 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-opentelemetry-requests` Add `py.typed` file to enable PEP 561 - ([#3134](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3134)) + ([#3135](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3135)) ### Fixed