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
5966Exclude lists
@@ -74,12 +81,14 @@ def response_hook(span, request_obj, response)
7481---
7582"""
7683
84+ from __future__ import annotations
85+
7786import functools
7887import types
7988import typing
8089from http import client
8190from timeit import default_timer
82- from typing import Collection , Dict
91+ from typing import Any , Collection
8392from 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
111120from opentelemetry .propagate import inject
112121from 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)
123132from opentelemetry .semconv .trace import SpanAttributes
124- from opentelemetry .trace import Span , SpanKind , get_tracer
133+ from opentelemetry .trace import Span , SpanKind , Tracer , get_tracer
125134from 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
206216def _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:
371381def _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
396406def _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
438448def _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 ,
0 commit comments