mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-07-30 21:56:07 +08:00
Support PEP 561 to opentelemetry-instrumentation-requests
(#3135)
This commit is contained in:

committed by
GitHub

parent
0ad779a5b3
commit
a606fab849
@ -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))
|
([#3100](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3100))
|
||||||
- Add support to database stability opt-in in `_semconv` utilities and add tests
|
- Add support to database stability opt-in in `_semconv` utilities and add tests
|
||||||
([#3111](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3111))
|
([#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
|
- `opentelemetry-instrumentation-system-metrics` Add `py.typed` file to enable PEP 561
|
||||||
([#3132](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3132))
|
([#3132](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3132))
|
||||||
- `opentelemetry-opentelemetry-sqlite3` Add `py.typed` file to enable PEP 561
|
- `opentelemetry-opentelemetry-sqlite3` Add `py.typed` file to enable PEP 561
|
||||||
|
@ -72,10 +72,12 @@ API
|
|||||||
---
|
---
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import functools
|
import functools
|
||||||
import types
|
import types
|
||||||
from timeit import default_timer
|
from timeit import default_timer
|
||||||
from typing import Callable, Collection, Optional
|
from typing import Any, Callable, Collection, Optional
|
||||||
from urllib.parse import urlparse
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
from requests.models import PreparedRequest, Response
|
from requests.models import PreparedRequest, Response
|
||||||
@ -146,7 +148,7 @@ def _instrument(
|
|||||||
duration_histogram_new: Histogram,
|
duration_histogram_new: Histogram,
|
||||||
request_hook: _RequestHookT = None,
|
request_hook: _RequestHookT = None,
|
||||||
response_hook: _ResponseHookT = None,
|
response_hook: _ResponseHookT = None,
|
||||||
excluded_urls: ExcludeList = None,
|
excluded_urls: ExcludeList | None = None,
|
||||||
sem_conv_opt_in_mode: _StabilityMode = _StabilityMode.DEFAULT,
|
sem_conv_opt_in_mode: _StabilityMode = _StabilityMode.DEFAULT,
|
||||||
):
|
):
|
||||||
"""Enables tracing of all requests calls that go through
|
"""Enables tracing of all requests calls that go through
|
||||||
@ -164,7 +166,9 @@ def _instrument(
|
|||||||
|
|
||||||
# pylint: disable-msg=too-many-locals,too-many-branches
|
# pylint: disable-msg=too-many-locals,too-many-branches
|
||||||
@functools.wraps(wrapped_send)
|
@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):
|
if excluded_urls and excluded_urls.url_disabled(request.url):
|
||||||
return wrapped_send(self, request, **kwargs)
|
return wrapped_send(self, request, **kwargs)
|
||||||
|
|
||||||
@ -345,7 +349,7 @@ def _uninstrument():
|
|||||||
_uninstrument_from(Session)
|
_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"):
|
for instr_func_name in ("request", "send"):
|
||||||
instr_func = getattr(instr_root, instr_func_name)
|
instr_func = getattr(instr_root, instr_func_name)
|
||||||
if not getattr(
|
if not getattr(
|
||||||
@ -361,7 +365,7 @@ def _uninstrument_from(instr_root, restore_as_bound_func=False):
|
|||||||
setattr(instr_root, instr_func_name, original)
|
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}.
|
Default implementation for name_callback, returns HTTP {method_name}.
|
||||||
https://opentelemetry.io/docs/reference/specification/trace/semantic_conventions/http/#name
|
https://opentelemetry.io/docs/reference/specification/trace/semantic_conventions/http/#name
|
||||||
@ -385,7 +389,7 @@ class RequestsInstrumentor(BaseInstrumentor):
|
|||||||
def instrumentation_dependencies(self) -> Collection[str]:
|
def instrumentation_dependencies(self) -> Collection[str]:
|
||||||
return _instruments
|
return _instruments
|
||||||
|
|
||||||
def _instrument(self, **kwargs):
|
def _instrument(self, **kwargs: Any):
|
||||||
"""Instruments requests module
|
"""Instruments requests module
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -443,10 +447,10 @@ class RequestsInstrumentor(BaseInstrumentor):
|
|||||||
sem_conv_opt_in_mode=semconv_opt_in_mode,
|
sem_conv_opt_in_mode=semconv_opt_in_mode,
|
||||||
)
|
)
|
||||||
|
|
||||||
def _uninstrument(self, **kwargs):
|
def _uninstrument(self, **kwargs: Any):
|
||||||
_uninstrument()
|
_uninstrument()
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def uninstrument_session(session):
|
def uninstrument_session(session: Session):
|
||||||
"""Disables instrumentation on the session object."""
|
"""Disables instrumentation on the session object."""
|
||||||
_uninstrument_from(session, restore_as_bound_func=True)
|
_uninstrument_from(session, restore_as_bound_func=True)
|
||||||
|
Reference in New Issue
Block a user