opentelemetry-instrumentation-requests 0.45b0__py3-none-any.whl → 0.47b0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -31,6 +31,30 @@ Usage
31
31
  Configuration
32
32
  -------------
33
33
 
34
+ Request/Response hooks
35
+ **********************
36
+
37
+ The requests instrumentation supports extending tracing behavior with the help of
38
+ request and response hooks. These are functions that are called back by the instrumentation
39
+ right after a Span is created for a request and right before the span is finished processing a response respectively.
40
+ The hooks can be configured as follows:
41
+
42
+ .. code:: python
43
+
44
+ # `request_obj` is an instance of requests.PreparedRequest
45
+ def request_hook(span, request_obj):
46
+ pass
47
+
48
+ # `request_obj` is an instance of requests.PreparedRequest
49
+ # `response` is an instance of requests.Response
50
+ def response_hook(span, request_obj, response)
51
+ pass
52
+
53
+ RequestsInstrumentor().instrument(
54
+ request_hook=request_hook, response_hook=response_hook)
55
+ )
56
+
57
+
34
58
  Exclude lists
35
59
  *************
36
60
  To exclude certain URLs from being tracked, set the environment variable ``OTEL_PYTHON_REQUESTS_EXCLUDED_URLS``
@@ -59,22 +83,20 @@ from requests.sessions import Session
59
83
  from requests.structures import CaseInsensitiveDict
60
84
 
61
85
  from opentelemetry.instrumentation._semconv import (
62
- _METRIC_ATTRIBUTES_CLIENT_DURATION_NAME,
63
- _SPAN_ATTRIBUTES_ERROR_TYPE,
64
- _SPAN_ATTRIBUTES_NETWORK_PEER_ADDRESS,
65
- _SPAN_ATTRIBUTES_NETWORK_PEER_PORT,
66
- _filter_duration_attrs,
86
+ _client_duration_attrs_new,
87
+ _client_duration_attrs_old,
88
+ _filter_semconv_duration_attrs,
67
89
  _get_schema_url,
90
+ _HTTPStabilityMode,
68
91
  _OpenTelemetrySemanticConventionStability,
69
- _OpenTelemetryStabilityMode,
70
92
  _OpenTelemetryStabilitySignalType,
71
93
  _report_new,
72
94
  _report_old,
73
- _set_http_hostname,
95
+ _set_http_host,
74
96
  _set_http_method,
75
- _set_http_net_peer_name,
97
+ _set_http_net_peer_name_client,
76
98
  _set_http_network_protocol_version,
77
- _set_http_port,
99
+ _set_http_peer_port_client,
78
100
  _set_http_scheme,
79
101
  _set_http_status_code,
80
102
  _set_http_url,
@@ -89,7 +111,15 @@ from opentelemetry.instrumentation.utils import (
89
111
  )
90
112
  from opentelemetry.metrics import Histogram, get_meter
91
113
  from opentelemetry.propagate import inject
114
+ from opentelemetry.semconv.attributes.error_attributes import ERROR_TYPE
115
+ from opentelemetry.semconv.attributes.network_attributes import (
116
+ NETWORK_PEER_ADDRESS,
117
+ NETWORK_PEER_PORT,
118
+ )
92
119
  from opentelemetry.semconv.metrics import MetricInstruments
120
+ from opentelemetry.semconv.metrics.http_metrics import (
121
+ HTTP_CLIENT_REQUEST_DURATION,
122
+ )
93
123
  from opentelemetry.trace import SpanKind, Tracer, get_tracer
94
124
  from opentelemetry.trace.span import Span
95
125
  from opentelemetry.trace.status import StatusCode
@@ -117,7 +147,7 @@ def _instrument(
117
147
  request_hook: _RequestHookT = None,
118
148
  response_hook: _ResponseHookT = None,
119
149
  excluded_urls: ExcludeList = None,
120
- sem_conv_opt_in_mode: _OpenTelemetryStabilityMode = _OpenTelemetryStabilityMode.DEFAULT,
150
+ sem_conv_opt_in_mode: _HTTPStabilityMode = _HTTPStabilityMode.DEFAULT,
121
151
  ):
122
152
  """Enables tracing of all requests calls that go through
123
153
  :code:`requests.session.Session.request` (this includes
@@ -158,50 +188,54 @@ def _instrument(
158
188
 
159
189
  span_attributes = {}
160
190
  _set_http_method(
161
- span_attributes, method, span_name, sem_conv_opt_in_mode
191
+ span_attributes,
192
+ method,
193
+ sanitize_method(method),
194
+ sem_conv_opt_in_mode,
162
195
  )
163
196
  _set_http_url(span_attributes, url, sem_conv_opt_in_mode)
164
197
 
165
198
  metric_labels = {}
166
199
  _set_http_method(
167
- metric_labels, method, span_name, sem_conv_opt_in_mode
200
+ metric_labels,
201
+ method,
202
+ sanitize_method(method),
203
+ sem_conv_opt_in_mode,
168
204
  )
169
205
 
170
206
  try:
171
207
  parsed_url = urlparse(url)
172
208
  if parsed_url.scheme:
173
- _set_http_scheme(
174
- metric_labels, parsed_url.scheme, sem_conv_opt_in_mode
175
- )
209
+ if _report_old(sem_conv_opt_in_mode):
210
+ # TODO: Support opt-in for url.scheme in new semconv
211
+ _set_http_scheme(
212
+ metric_labels, parsed_url.scheme, sem_conv_opt_in_mode
213
+ )
176
214
  if parsed_url.hostname:
177
- _set_http_hostname(
215
+ _set_http_host(
178
216
  metric_labels, parsed_url.hostname, sem_conv_opt_in_mode
179
217
  )
180
- _set_http_net_peer_name(
218
+ _set_http_net_peer_name_client(
181
219
  metric_labels, parsed_url.hostname, sem_conv_opt_in_mode
182
220
  )
183
221
  if _report_new(sem_conv_opt_in_mode):
184
- _set_http_hostname(
222
+ _set_http_host(
185
223
  span_attributes,
186
224
  parsed_url.hostname,
187
225
  sem_conv_opt_in_mode,
188
226
  )
189
227
  # Use semconv library when available
190
- span_attributes[
191
- _SPAN_ATTRIBUTES_NETWORK_PEER_ADDRESS
192
- ] = parsed_url.hostname
228
+ span_attributes[NETWORK_PEER_ADDRESS] = parsed_url.hostname
193
229
  if parsed_url.port:
194
- _set_http_port(
230
+ _set_http_peer_port_client(
195
231
  metric_labels, parsed_url.port, sem_conv_opt_in_mode
196
232
  )
197
233
  if _report_new(sem_conv_opt_in_mode):
198
- _set_http_port(
234
+ _set_http_peer_port_client(
199
235
  span_attributes, parsed_url.port, sem_conv_opt_in_mode
200
236
  )
201
237
  # Use semconv library when available
202
- span_attributes[
203
- _SPAN_ATTRIBUTES_NETWORK_PEER_PORT
204
- ] = parsed_url.port
238
+ span_attributes[NETWORK_PEER_PORT] = parsed_url.port
205
239
  except ValueError:
206
240
  pass
207
241
 
@@ -225,9 +259,7 @@ def _instrument(
225
259
  exception = exc
226
260
  result = getattr(exc, "response", None)
227
261
  finally:
228
- elapsed_time = max(
229
- round((default_timer() - start_time) * 1000), 0
230
- )
262
+ elapsed_time = max(default_timer() - start_time, 0)
231
263
 
232
264
  if isinstance(result, Response):
233
265
  span_attributes = {}
@@ -248,12 +280,8 @@ def _instrument(
248
280
  _report_new(sem_conv_opt_in_mode)
249
281
  and status_code is StatusCode.ERROR
250
282
  ):
251
- span_attributes[_SPAN_ATTRIBUTES_ERROR_TYPE] = str(
252
- result.status_code
253
- )
254
- metric_labels[_SPAN_ATTRIBUTES_ERROR_TYPE] = str(
255
- result.status_code
256
- )
283
+ span_attributes[ERROR_TYPE] = str(result.status_code)
284
+ metric_labels[ERROR_TYPE] = str(result.status_code)
257
285
 
258
286
  if result.raw is not None:
259
287
  version = getattr(result.raw, "version", None)
@@ -276,24 +304,26 @@ def _instrument(
276
304
  response_hook(span, request, result)
277
305
 
278
306
  if exception is not None and _report_new(sem_conv_opt_in_mode):
279
- span.set_attribute(
280
- _SPAN_ATTRIBUTES_ERROR_TYPE, type(exception).__qualname__
281
- )
282
- metric_labels[_SPAN_ATTRIBUTES_ERROR_TYPE] = type(
283
- exception
284
- ).__qualname__
307
+ span.set_attribute(ERROR_TYPE, type(exception).__qualname__)
308
+ metric_labels[ERROR_TYPE] = type(exception).__qualname__
285
309
 
286
310
  if duration_histogram_old is not None:
287
- duration_attrs_old = _filter_duration_attrs(
288
- metric_labels, _OpenTelemetryStabilityMode.DEFAULT
311
+ duration_attrs_old = _filter_semconv_duration_attrs(
312
+ metric_labels,
313
+ _client_duration_attrs_old,
314
+ _client_duration_attrs_new,
315
+ _HTTPStabilityMode.DEFAULT,
289
316
  )
290
317
  duration_histogram_old.record(
291
318
  max(round(elapsed_time * 1000), 0),
292
319
  attributes=duration_attrs_old,
293
320
  )
294
321
  if duration_histogram_new is not None:
295
- duration_attrs_new = _filter_duration_attrs(
296
- metric_labels, _OpenTelemetryStabilityMode.HTTP
322
+ duration_attrs_new = _filter_semconv_duration_attrs(
323
+ metric_labels,
324
+ _client_duration_attrs_old,
325
+ _client_duration_attrs_new,
326
+ _HTTPStabilityMode.HTTP,
297
327
  )
298
328
  duration_histogram_new.record(
299
329
  elapsed_time, attributes=duration_attrs_new
@@ -341,7 +371,10 @@ def get_default_span_name(method):
341
371
  Returns:
342
372
  span name
343
373
  """
344
- return sanitize_method(method.upper().strip())
374
+ method = sanitize_method(method.strip())
375
+ if method == "_OTHER":
376
+ return "HTTP"
377
+ return method
345
378
 
346
379
 
347
380
  class RequestsInstrumentor(BaseInstrumentor):
@@ -392,7 +425,7 @@ class RequestsInstrumentor(BaseInstrumentor):
392
425
  duration_histogram_new = None
393
426
  if _report_new(semconv_opt_in_mode):
394
427
  duration_histogram_new = meter.create_histogram(
395
- name=_METRIC_ATTRIBUTES_CLIENT_DURATION_NAME,
428
+ name=HTTP_CLIENT_REQUEST_DURATION,
396
429
  unit="s",
397
430
  description="Duration of HTTP client requests.",
398
431
  )
@@ -402,9 +435,11 @@ class RequestsInstrumentor(BaseInstrumentor):
402
435
  duration_histogram_new,
403
436
  request_hook=kwargs.get("request_hook"),
404
437
  response_hook=kwargs.get("response_hook"),
405
- excluded_urls=_excluded_urls_from_env
406
- if excluded_urls is None
407
- else parse_excluded_urls(excluded_urls),
438
+ excluded_urls=(
439
+ _excluded_urls_from_env
440
+ if excluded_urls is None
441
+ else parse_excluded_urls(excluded_urls)
442
+ ),
408
443
  sem_conv_opt_in_mode=semconv_opt_in_mode,
409
444
  )
410
445
 
@@ -16,3 +16,5 @@
16
16
  _instruments = ("requests ~= 2.0",)
17
17
 
18
18
  _supports_metrics = True
19
+
20
+ _semconv_status = "migration"
@@ -12,4 +12,4 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- __version__ = "0.45b0"
15
+ __version__ = "0.47b0"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: opentelemetry-instrumentation-requests
3
- Version: 0.45b0
3
+ Version: 0.47b0
4
4
  Summary: OpenTelemetry requests instrumentation
5
5
  Project-URL: Homepage, https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-requests
6
6
  Author-email: OpenTelemetry Authors <cncf-opentelemetry-contributors@lists.cncf.io>
@@ -15,11 +15,12 @@ Classifier: Programming Language :: Python :: 3.8
15
15
  Classifier: Programming Language :: Python :: 3.9
16
16
  Classifier: Programming Language :: Python :: 3.10
17
17
  Classifier: Programming Language :: Python :: 3.11
18
+ Classifier: Programming Language :: Python :: 3.12
18
19
  Requires-Python: >=3.8
19
20
  Requires-Dist: opentelemetry-api~=1.12
20
- Requires-Dist: opentelemetry-instrumentation==0.45b0
21
- Requires-Dist: opentelemetry-semantic-conventions==0.45b0
22
- Requires-Dist: opentelemetry-util-http==0.45b0
21
+ Requires-Dist: opentelemetry-instrumentation==0.47b0
22
+ Requires-Dist: opentelemetry-semantic-conventions==0.47b0
23
+ Requires-Dist: opentelemetry-util-http==0.47b0
23
24
  Provides-Extra: instruments
24
25
  Requires-Dist: requests~=2.0; extra == 'instruments'
25
26
  Description-Content-Type: text/x-rst
@@ -0,0 +1,8 @@
1
+ opentelemetry/instrumentation/requests/__init__.py,sha256=tqqY6InvdlsCVSbpTdrp1nl-pd8FVVJQBAxYWpkIDtg,16602
2
+ opentelemetry/instrumentation/requests/package.py,sha256=84lK70NyCoRefASXKjU5f4byJhf5qWDL6IdYjch-UTM,679
3
+ opentelemetry/instrumentation/requests/version.py,sha256=6FmWdr2KRW0MGpqEO5NBzLrm6URCtz_6ixXi_ALh6JA,608
4
+ opentelemetry_instrumentation_requests-0.47b0.dist-info/METADATA,sha256=r5UaSUez1DA-FmJY8XlCZEhk-4j_-YE1gRgmkO4cFTQ,2531
5
+ opentelemetry_instrumentation_requests-0.47b0.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
6
+ opentelemetry_instrumentation_requests-0.47b0.dist-info/entry_points.txt,sha256=w_cFVp9h9IXzWm2YeBaOEUANSn9iuUlNAl0QC3PDf94,100
7
+ opentelemetry_instrumentation_requests-0.47b0.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
8
+ opentelemetry_instrumentation_requests-0.47b0.dist-info/RECORD,,
@@ -1,4 +1,4 @@
1
1
  Wheel-Version: 1.0
2
- Generator: hatchling 1.22.4
2
+ Generator: hatchling 1.25.0
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
@@ -186,7 +186,7 @@
186
186
  same "printed page" as the copyright notice for easier
187
187
  identification within third-party archives.
188
188
 
189
- Copyright The OpenTelemetry Authors
189
+ Copyright [yyyy] [name of copyright owner]
190
190
 
191
191
  Licensed under the Apache License, Version 2.0 (the "License");
192
192
  you may not use this file except in compliance with the License.
@@ -1,8 +0,0 @@
1
- opentelemetry/instrumentation/requests/__init__.py,sha256=bHMyUOO5x7CW8Tlrs34RB4SuM0BZC6bKMogCufkEB10,15466
2
- opentelemetry/instrumentation/requests/package.py,sha256=ULOZ8NBcPPzs-WzXMXwwoMJcc2GmNARO-nzRR1gEAMU,648
3
- opentelemetry/instrumentation/requests/version.py,sha256=HyCKkZGb11xsSYPWI9CHcfWAYU5IqDVTDpTmCwMvfu0,608
4
- opentelemetry_instrumentation_requests-0.45b0.dist-info/METADATA,sha256=QTXSZBRHuxiJnWXIGn3lmzA8j2Ka8tw7hGYCd6TaAUI,2480
5
- opentelemetry_instrumentation_requests-0.45b0.dist-info/WHEEL,sha256=uNdcs2TADwSd5pVaP0Z_kcjcvvTUklh2S7bxZMF8Uj0,87
6
- opentelemetry_instrumentation_requests-0.45b0.dist-info/entry_points.txt,sha256=w_cFVp9h9IXzWm2YeBaOEUANSn9iuUlNAl0QC3PDf94,100
7
- opentelemetry_instrumentation_requests-0.45b0.dist-info/licenses/LICENSE,sha256=h8jwqxShIeVkc8vOo9ynxGYW16f4fVPxLhZKZs0H5U8,11350
8
- opentelemetry_instrumentation_requests-0.45b0.dist-info/RECORD,,