mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-07-28 20:52:57 +08:00
Support PEP 561 to opentelemetry-instrumentation-urllib
(#3131)
* Support PEP 561 to `opentelemetry-instrumentation-urllib` * add future --------- Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
This commit is contained in:

committed by
GitHub

parent
c59b514cda
commit
e54256ddb7
@ -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-instrumentation-urllib` Add `py.typed` file to enable PEP 561
|
||||||
|
([#3131](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3131))
|
||||||
- `opentelemetry-opentelemetry-pymongo` Add `py.typed` file to enable PEP 561
|
- `opentelemetry-opentelemetry-pymongo` Add `py.typed` file to enable PEP 561
|
||||||
([#3136](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3136))
|
([#3136](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3136))
|
||||||
- `opentelemetry-opentelemetry-requests` Add `py.typed` file to enable PEP 561
|
- `opentelemetry-opentelemetry-requests` Add `py.typed` file to enable PEP 561
|
||||||
|
@ -43,17 +43,24 @@ The hooks can be configured as follows:
|
|||||||
|
|
||||||
.. code:: python
|
.. code:: python
|
||||||
|
|
||||||
# `request_obj` is an instance of urllib.request.Request
|
from http.client import HTTPResponse
|
||||||
def request_hook(span, request_obj):
|
from urllib.request import Request
|
||||||
|
|
||||||
|
from opentelemetry.instrumentation.urllib import URLLibInstrumentor
|
||||||
|
from opentelemetry.trace import Span
|
||||||
|
|
||||||
|
|
||||||
|
def request_hook(span: Span, request: Request):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# `request_obj` is an instance of urllib.request.Request
|
|
||||||
# `response` is an instance of http.client.HTTPResponse
|
def response_hook(span: Span, request: Request, response: HTTPResponse):
|
||||||
def response_hook(span, request_obj, response)
|
|
||||||
pass
|
pass
|
||||||
|
|
||||||
URLLibInstrumentor.instrument(
|
|
||||||
request_hook=request_hook, response_hook=response_hook)
|
URLLibInstrumentor().instrument(
|
||||||
|
request_hook=request_hook,
|
||||||
|
response_hook=response_hook
|
||||||
)
|
)
|
||||||
|
|
||||||
Exclude lists
|
Exclude lists
|
||||||
@ -74,12 +81,14 @@ API
|
|||||||
---
|
---
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import functools
|
import functools
|
||||||
import types
|
import types
|
||||||
import typing
|
import typing
|
||||||
from http import client
|
from http import client
|
||||||
from timeit import default_timer
|
from timeit import default_timer
|
||||||
from typing import Collection, Dict
|
from typing import Any, Collection
|
||||||
from urllib.request import ( # pylint: disable=no-name-in-module,import-error
|
from urllib.request import ( # pylint: disable=no-name-in-module,import-error
|
||||||
OpenerDirector,
|
OpenerDirector,
|
||||||
Request,
|
Request,
|
||||||
@ -107,7 +116,7 @@ from opentelemetry.instrumentation.utils import (
|
|||||||
is_http_instrumentation_enabled,
|
is_http_instrumentation_enabled,
|
||||||
suppress_http_instrumentation,
|
suppress_http_instrumentation,
|
||||||
)
|
)
|
||||||
from opentelemetry.metrics import Histogram, get_meter
|
from opentelemetry.metrics import Histogram, Meter, get_meter
|
||||||
from opentelemetry.propagate import inject
|
from opentelemetry.propagate import inject
|
||||||
from opentelemetry.semconv._incubating.metrics.http_metrics import (
|
from opentelemetry.semconv._incubating.metrics.http_metrics import (
|
||||||
HTTP_CLIENT_REQUEST_BODY_SIZE,
|
HTTP_CLIENT_REQUEST_BODY_SIZE,
|
||||||
@ -121,7 +130,7 @@ from opentelemetry.semconv.metrics.http_metrics import (
|
|||||||
HTTP_CLIENT_REQUEST_DURATION,
|
HTTP_CLIENT_REQUEST_DURATION,
|
||||||
)
|
)
|
||||||
from opentelemetry.semconv.trace import SpanAttributes
|
from opentelemetry.semconv.trace import SpanAttributes
|
||||||
from opentelemetry.trace import Span, SpanKind, get_tracer
|
from opentelemetry.trace import Span, SpanKind, Tracer, get_tracer
|
||||||
from opentelemetry.util.http import (
|
from opentelemetry.util.http import (
|
||||||
ExcludeList,
|
ExcludeList,
|
||||||
get_excluded_urls,
|
get_excluded_urls,
|
||||||
@ -129,6 +138,7 @@ from opentelemetry.util.http import (
|
|||||||
remove_url_credentials,
|
remove_url_credentials,
|
||||||
sanitize_method,
|
sanitize_method,
|
||||||
)
|
)
|
||||||
|
from opentelemetry.util.types import Attributes
|
||||||
|
|
||||||
_excluded_urls_from_env = get_excluded_urls("URLLIB")
|
_excluded_urls_from_env = get_excluded_urls("URLLIB")
|
||||||
|
|
||||||
@ -146,7 +156,7 @@ class URLLibInstrumentor(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 urllib module
|
"""Instruments urllib module
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@ -194,7 +204,7 @@ class URLLibInstrumentor(BaseInstrumentor):
|
|||||||
sem_conv_opt_in_mode=sem_conv_opt_in_mode,
|
sem_conv_opt_in_mode=sem_conv_opt_in_mode,
|
||||||
)
|
)
|
||||||
|
|
||||||
def _uninstrument(self, **kwargs):
|
def _uninstrument(self, **kwargs: Any):
|
||||||
_uninstrument()
|
_uninstrument()
|
||||||
|
|
||||||
def uninstrument_opener(self, opener: OpenerDirector): # pylint: disable=no-self-use
|
def uninstrument_opener(self, opener: OpenerDirector): # pylint: disable=no-self-use
|
||||||
@ -204,11 +214,11 @@ class URLLibInstrumentor(BaseInstrumentor):
|
|||||||
|
|
||||||
# pylint: disable=too-many-statements
|
# pylint: disable=too-many-statements
|
||||||
def _instrument(
|
def _instrument(
|
||||||
tracer,
|
tracer: Tracer,
|
||||||
histograms: Dict[str, Histogram],
|
histograms: dict[str, 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
|
||||||
@ -345,7 +355,7 @@ def _uninstrument():
|
|||||||
_uninstrument_from(OpenerDirector)
|
_uninstrument_from(OpenerDirector)
|
||||||
|
|
||||||
|
|
||||||
def _uninstrument_from(instr_root, restore_as_bound_func=False):
|
def _uninstrument_from(instr_root, restore_as_bound_func: bool = False):
|
||||||
instr_func_name = "open"
|
instr_func_name = "open"
|
||||||
instr_func = getattr(instr_root, instr_func_name)
|
instr_func = getattr(instr_root, instr_func_name)
|
||||||
if not getattr(
|
if not getattr(
|
||||||
@ -371,7 +381,7 @@ def _get_span_name(method: str) -> str:
|
|||||||
def _set_status_code_attribute(
|
def _set_status_code_attribute(
|
||||||
span: Span,
|
span: Span,
|
||||||
status_code: int,
|
status_code: int,
|
||||||
metric_attributes: dict = None,
|
metric_attributes: dict[str, Any] | None = None,
|
||||||
sem_conv_opt_in_mode: _StabilityMode = _StabilityMode.DEFAULT,
|
sem_conv_opt_in_mode: _StabilityMode = _StabilityMode.DEFAULT,
|
||||||
) -> None:
|
) -> None:
|
||||||
status_code_str = str(status_code)
|
status_code_str = str(status_code)
|
||||||
@ -394,8 +404,8 @@ def _set_status_code_attribute(
|
|||||||
|
|
||||||
|
|
||||||
def _create_client_histograms(
|
def _create_client_histograms(
|
||||||
meter, sem_conv_opt_in_mode=_StabilityMode.DEFAULT
|
meter: Meter, sem_conv_opt_in_mode: _StabilityMode = _StabilityMode.DEFAULT
|
||||||
) -> Dict[str, Histogram]:
|
) -> dict[str, Histogram]:
|
||||||
histograms = {}
|
histograms = {}
|
||||||
if _report_old(sem_conv_opt_in_mode):
|
if _report_old(sem_conv_opt_in_mode):
|
||||||
histograms[MetricInstruments.HTTP_CLIENT_DURATION] = (
|
histograms[MetricInstruments.HTTP_CLIENT_DURATION] = (
|
||||||
@ -436,9 +446,9 @@ def _create_client_histograms(
|
|||||||
|
|
||||||
|
|
||||||
def _record_histograms(
|
def _record_histograms(
|
||||||
histograms: Dict[str, Histogram],
|
histograms: dict[str, Histogram],
|
||||||
metric_attributes_old: dict,
|
metric_attributes_old: Attributes,
|
||||||
metric_attributes_new: dict,
|
metric_attributes_new: Attributes,
|
||||||
request_size: int,
|
request_size: int,
|
||||||
response_size: int,
|
response_size: int,
|
||||||
duration_s: float,
|
duration_s: float,
|
||||||
|
@ -12,8 +12,9 @@
|
|||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
_instruments = tuple()
|
_instruments: tuple[str, ...] = tuple()
|
||||||
|
|
||||||
_supports_metrics = True
|
_supports_metrics = True
|
||||||
|
|
||||||
|
@ -13,5 +13,3 @@
|
|||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
__version__ = "0.51b0.dev"
|
__version__ = "0.51b0.dev"
|
||||||
|
|
||||||
_instruments = tuple()
|
|
||||||
|
Reference in New Issue
Block a user