Skip to content

Commit

Permalink
Support PEP 561 to opentelemetry-instrumentation-requests (#3135)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kludex authored Jan 10, 2025
1 parent 0ad779a commit a606fab
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
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-opentelemetry-requests` Add `py.typed` file to enable PEP 561
([#3135](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3135))
- `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 @@ -72,10 +72,12 @@ def response_hook(span, request_obj, response)
---
"""

from __future__ import annotations

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
Expand Down Expand Up @@ -146,7 +148,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
Expand All @@ -164,7 +166,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)

Expand Down Expand Up @@ -345,7 +349,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(
Expand All @@ -361,7 +365,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
Expand All @@ -385,7 +389,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:
Expand Down Expand Up @@ -443,10 +447,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)

0 comments on commit a606fab

Please sign in to comment.