Skip to content

Commit 2e053d7

Browse files
Kludexxrmx
andcommitted
Support PEP 561 to opentelemetry-instrumentation-urllib (open-telemetry#3131)
* Support PEP 561 to `opentelemetry-instrumentation-urllib` * add future --------- Co-authored-by: Riccardo Magliocchetti <[email protected]>
1 parent 595d206 commit 2e053d7

File tree

5 files changed

+36
-25
lines changed

5 files changed

+36
-25
lines changed

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-instrumentation-urllib` Add `py.typed` file to enable PEP 561
21+
([#3131](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3131))
2022
- `opentelemetry-opentelemetry-pymongo` Add `py.typed` file to enable PEP 561
2123
([#3136](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3136))
2224
- `opentelemetry-opentelemetry-requests` Add `py.typed` file to enable PEP 561

instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/__init__.py

+32-22
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,24 @@
4343
4444
.. code:: python
4545
46-
# `request_obj` is an instance of urllib.request.Request
47-
def request_hook(span, request_obj):
46+
from http.client import HTTPResponse
47+
from urllib.request import Request
48+
49+
from opentelemetry.instrumentation.urllib import URLLibInstrumentor
50+
from opentelemetry.trace import Span
51+
52+
53+
def request_hook(span: Span, request: Request):
4854
pass
4955
50-
# `request_obj` is an instance of urllib.request.Request
51-
# `response` is an instance of http.client.HTTPResponse
52-
def response_hook(span, request_obj, response)
56+
57+
def response_hook(span: Span, request: Request, response: HTTPResponse):
5358
pass
5459
55-
URLLibInstrumentor.instrument(
56-
request_hook=request_hook, response_hook=response_hook)
60+
61+
URLLibInstrumentor().instrument(
62+
request_hook=request_hook,
63+
response_hook=response_hook
5764
)
5865
5966
Exclude lists
@@ -74,12 +81,14 @@ def response_hook(span, request_obj, response)
7481
---
7582
"""
7683

84+
from __future__ import annotations
85+
7786
import functools
7887
import types
7988
import typing
8089
from http import client
8190
from timeit import default_timer
82-
from typing import Collection, Dict
91+
from typing import Any, Collection
8392
from urllib.request import ( # pylint: disable=no-name-in-module,import-error
8493
OpenerDirector,
8594
Request,
@@ -107,7 +116,7 @@ def response_hook(span, request_obj, response)
107116
is_http_instrumentation_enabled,
108117
suppress_http_instrumentation,
109118
)
110-
from opentelemetry.metrics import Histogram, get_meter
119+
from opentelemetry.metrics import Histogram, Meter, get_meter
111120
from opentelemetry.propagate import inject
112121
from opentelemetry.semconv._incubating.metrics.http_metrics import (
113122
HTTP_CLIENT_REQUEST_BODY_SIZE,
@@ -121,14 +130,15 @@ def response_hook(span, request_obj, response)
121130
HTTP_CLIENT_REQUEST_DURATION,
122131
)
123132
from opentelemetry.semconv.trace import SpanAttributes
124-
from opentelemetry.trace import Span, SpanKind, get_tracer
133+
from opentelemetry.trace import Span, SpanKind, Tracer, get_tracer
125134
from opentelemetry.util.http import (
126135
ExcludeList,
127136
get_excluded_urls,
128137
parse_excluded_urls,
129138
remove_url_credentials,
130139
sanitize_method,
131140
)
141+
from opentelemetry.util.types import Attributes
132142

133143
_excluded_urls_from_env = get_excluded_urls("URLLIB")
134144

@@ -146,7 +156,7 @@ class URLLibInstrumentor(BaseInstrumentor):
146156
def instrumentation_dependencies(self) -> Collection[str]:
147157
return _instruments
148158

149-
def _instrument(self, **kwargs):
159+
def _instrument(self, **kwargs: Any):
150160
"""Instruments urllib module
151161
152162
Args:
@@ -194,7 +204,7 @@ def _instrument(self, **kwargs):
194204
sem_conv_opt_in_mode=sem_conv_opt_in_mode,
195205
)
196206

197-
def _uninstrument(self, **kwargs):
207+
def _uninstrument(self, **kwargs: Any):
198208
_uninstrument()
199209

200210
def uninstrument_opener(self, opener: OpenerDirector): # pylint: disable=no-self-use
@@ -204,11 +214,11 @@ def uninstrument_opener(self, opener: OpenerDirector): # pylint: disable=no-sel
204214

205215
# pylint: disable=too-many-statements
206216
def _instrument(
207-
tracer,
208-
histograms: Dict[str, Histogram],
217+
tracer: Tracer,
218+
histograms: dict[str, Histogram],
209219
request_hook: _RequestHookT = None,
210220
response_hook: _ResponseHookT = None,
211-
excluded_urls: ExcludeList = None,
221+
excluded_urls: ExcludeList | None = None,
212222
sem_conv_opt_in_mode: _StabilityMode = _StabilityMode.DEFAULT,
213223
):
214224
"""Enables tracing of all requests calls that go through
@@ -345,7 +355,7 @@ def _uninstrument():
345355
_uninstrument_from(OpenerDirector)
346356

347357

348-
def _uninstrument_from(instr_root, restore_as_bound_func=False):
358+
def _uninstrument_from(instr_root, restore_as_bound_func: bool = False):
349359
instr_func_name = "open"
350360
instr_func = getattr(instr_root, instr_func_name)
351361
if not getattr(
@@ -371,7 +381,7 @@ def _get_span_name(method: str) -> str:
371381
def _set_status_code_attribute(
372382
span: Span,
373383
status_code: int,
374-
metric_attributes: dict = None,
384+
metric_attributes: dict[str, Any] | None = None,
375385
sem_conv_opt_in_mode: _StabilityMode = _StabilityMode.DEFAULT,
376386
) -> None:
377387
status_code_str = str(status_code)
@@ -394,8 +404,8 @@ def _set_status_code_attribute(
394404

395405

396406
def _create_client_histograms(
397-
meter, sem_conv_opt_in_mode=_StabilityMode.DEFAULT
398-
) -> Dict[str, Histogram]:
407+
meter: Meter, sem_conv_opt_in_mode: _StabilityMode = _StabilityMode.DEFAULT
408+
) -> dict[str, Histogram]:
399409
histograms = {}
400410
if _report_old(sem_conv_opt_in_mode):
401411
histograms[MetricInstruments.HTTP_CLIENT_DURATION] = (
@@ -436,9 +446,9 @@ def _create_client_histograms(
436446

437447

438448
def _record_histograms(
439-
histograms: Dict[str, Histogram],
440-
metric_attributes_old: dict,
441-
metric_attributes_new: dict,
449+
histograms: dict[str, Histogram],
450+
metric_attributes_old: Attributes,
451+
metric_attributes_new: Attributes,
442452
request_size: int,
443453
response_size: int,
444454
duration_s: float,

instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/package.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
from __future__ import annotations
1516

16-
_instruments = tuple()
17+
_instruments: tuple[str, ...] = tuple()
1718

1819
_supports_metrics = True
1920

instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/py.typed

Whitespace-only changes.

instrumentation/opentelemetry-instrumentation-urllib/src/opentelemetry/instrumentation/urllib/version.py

-2
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,3 @@
1313
# limitations under the License.
1414

1515
__version__ = "0.51b0.dev"
16-
17-
_instruments = tuple()

0 commit comments

Comments
 (0)