43
43
44
44
.. code:: python
45
45
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):
48
54
pass
49
55
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):
53
58
pass
54
59
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
57
64
)
58
65
59
66
Exclude lists
@@ -74,12 +81,14 @@ def response_hook(span, request_obj, response)
74
81
---
75
82
"""
76
83
84
+ from __future__ import annotations
85
+
77
86
import functools
78
87
import types
79
88
import typing
80
89
from http import client
81
90
from timeit import default_timer
82
- from typing import Collection , Dict
91
+ from typing import Any , Collection
83
92
from urllib .request import ( # pylint: disable=no-name-in-module,import-error
84
93
OpenerDirector ,
85
94
Request ,
@@ -107,7 +116,7 @@ def response_hook(span, request_obj, response)
107
116
is_http_instrumentation_enabled ,
108
117
suppress_http_instrumentation ,
109
118
)
110
- from opentelemetry .metrics import Histogram , get_meter
119
+ from opentelemetry .metrics import Histogram , Meter , get_meter
111
120
from opentelemetry .propagate import inject
112
121
from opentelemetry .semconv ._incubating .metrics .http_metrics import (
113
122
HTTP_CLIENT_REQUEST_BODY_SIZE ,
@@ -121,14 +130,15 @@ def response_hook(span, request_obj, response)
121
130
HTTP_CLIENT_REQUEST_DURATION ,
122
131
)
123
132
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
125
134
from opentelemetry .util .http import (
126
135
ExcludeList ,
127
136
get_excluded_urls ,
128
137
parse_excluded_urls ,
129
138
remove_url_credentials ,
130
139
sanitize_method ,
131
140
)
141
+ from opentelemetry .util .types import Attributes
132
142
133
143
_excluded_urls_from_env = get_excluded_urls ("URLLIB" )
134
144
@@ -146,7 +156,7 @@ class URLLibInstrumentor(BaseInstrumentor):
146
156
def instrumentation_dependencies (self ) -> Collection [str ]:
147
157
return _instruments
148
158
149
- def _instrument (self , ** kwargs ):
159
+ def _instrument (self , ** kwargs : Any ):
150
160
"""Instruments urllib module
151
161
152
162
Args:
@@ -194,7 +204,7 @@ def _instrument(self, **kwargs):
194
204
sem_conv_opt_in_mode = sem_conv_opt_in_mode ,
195
205
)
196
206
197
- def _uninstrument (self , ** kwargs ):
207
+ def _uninstrument (self , ** kwargs : Any ):
198
208
_uninstrument ()
199
209
200
210
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
204
214
205
215
# pylint: disable=too-many-statements
206
216
def _instrument (
207
- tracer ,
208
- histograms : Dict [str , Histogram ],
217
+ tracer : Tracer ,
218
+ histograms : dict [str , Histogram ],
209
219
request_hook : _RequestHookT = None ,
210
220
response_hook : _ResponseHookT = None ,
211
- excluded_urls : ExcludeList = None ,
221
+ excluded_urls : ExcludeList | None = None ,
212
222
sem_conv_opt_in_mode : _StabilityMode = _StabilityMode .DEFAULT ,
213
223
):
214
224
"""Enables tracing of all requests calls that go through
@@ -345,7 +355,7 @@ def _uninstrument():
345
355
_uninstrument_from (OpenerDirector )
346
356
347
357
348
- def _uninstrument_from (instr_root , restore_as_bound_func = False ):
358
+ def _uninstrument_from (instr_root , restore_as_bound_func : bool = False ):
349
359
instr_func_name = "open"
350
360
instr_func = getattr (instr_root , instr_func_name )
351
361
if not getattr (
@@ -371,7 +381,7 @@ def _get_span_name(method: str) -> str:
371
381
def _set_status_code_attribute (
372
382
span : Span ,
373
383
status_code : int ,
374
- metric_attributes : dict = None ,
384
+ metric_attributes : dict [ str , Any ] | None = None ,
375
385
sem_conv_opt_in_mode : _StabilityMode = _StabilityMode .DEFAULT ,
376
386
) -> None :
377
387
status_code_str = str (status_code )
@@ -394,8 +404,8 @@ def _set_status_code_attribute(
394
404
395
405
396
406
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 ]:
399
409
histograms = {}
400
410
if _report_old (sem_conv_opt_in_mode ):
401
411
histograms [MetricInstruments .HTTP_CLIENT_DURATION ] = (
@@ -436,9 +446,9 @@ def _create_client_histograms(
436
446
437
447
438
448
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 ,
442
452
request_size : int ,
443
453
response_size : int ,
444
454
duration_s : float ,
0 commit comments