Skip to content

Commit a606fab

Browse files
authored
Support PEP 561 to opentelemetry-instrumentation-requests (#3135)
1 parent 0ad779a commit a606fab

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
([#3100](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3100))
1818
- Add support to database stability opt-in in `_semconv` utilities and add tests
1919
([#3111](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3111))
20+
- `opentelemetry-opentelemetry-requests` Add `py.typed` file to enable PEP 561
21+
([#3135](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3135))
2022
- `opentelemetry-instrumentation-system-metrics` Add `py.typed` file to enable PEP 561
2123
([#3132](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3132))
2224
- `opentelemetry-opentelemetry-sqlite3` Add `py.typed` file to enable PEP 561

Diff for: instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/__init__.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,12 @@ def response_hook(span, request_obj, response)
7272
---
7373
"""
7474

75+
from __future__ import annotations
76+
7577
import functools
7678
import types
7779
from timeit import default_timer
78-
from typing import Callable, Collection, Optional
80+
from typing import Any, Callable, Collection, Optional
7981
from urllib.parse import urlparse
8082

8183
from requests.models import PreparedRequest, Response
@@ -146,7 +148,7 @@ def _instrument(
146148
duration_histogram_new: Histogram,
147149
request_hook: _RequestHookT = None,
148150
response_hook: _ResponseHookT = None,
149-
excluded_urls: ExcludeList = None,
151+
excluded_urls: ExcludeList | None = None,
150152
sem_conv_opt_in_mode: _StabilityMode = _StabilityMode.DEFAULT,
151153
):
152154
"""Enables tracing of all requests calls that go through
@@ -164,7 +166,9 @@ def _instrument(
164166

165167
# pylint: disable-msg=too-many-locals,too-many-branches
166168
@functools.wraps(wrapped_send)
167-
def instrumented_send(self, request, **kwargs):
169+
def instrumented_send(
170+
self: Session, request: PreparedRequest, **kwargs: Any
171+
):
168172
if excluded_urls and excluded_urls.url_disabled(request.url):
169173
return wrapped_send(self, request, **kwargs)
170174

@@ -345,7 +349,7 @@ def _uninstrument():
345349
_uninstrument_from(Session)
346350

347351

348-
def _uninstrument_from(instr_root, restore_as_bound_func=False):
352+
def _uninstrument_from(instr_root, restore_as_bound_func: bool = False):
349353
for instr_func_name in ("request", "send"):
350354
instr_func = getattr(instr_root, instr_func_name)
351355
if not getattr(
@@ -361,7 +365,7 @@ def _uninstrument_from(instr_root, restore_as_bound_func=False):
361365
setattr(instr_root, instr_func_name, original)
362366

363367

364-
def get_default_span_name(method):
368+
def get_default_span_name(method: str) -> str:
365369
"""
366370
Default implementation for name_callback, returns HTTP {method_name}.
367371
https://opentelemetry.io/docs/reference/specification/trace/semantic_conventions/http/#name
@@ -385,7 +389,7 @@ class RequestsInstrumentor(BaseInstrumentor):
385389
def instrumentation_dependencies(self) -> Collection[str]:
386390
return _instruments
387391

388-
def _instrument(self, **kwargs):
392+
def _instrument(self, **kwargs: Any):
389393
"""Instruments requests module
390394
391395
Args:
@@ -443,10 +447,10 @@ def _instrument(self, **kwargs):
443447
sem_conv_opt_in_mode=semconv_opt_in_mode,
444448
)
445449

446-
def _uninstrument(self, **kwargs):
450+
def _uninstrument(self, **kwargs: Any):
447451
_uninstrument()
448452

449453
@staticmethod
450-
def uninstrument_session(session):
454+
def uninstrument_session(session: Session):
451455
"""Disables instrumentation on the session object."""
452456
_uninstrument_from(session, restore_as_bound_func=True)

Diff for: instrumentation/opentelemetry-instrumentation-requests/src/opentelemetry/instrumentation/requests/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)