opentelemetry-instrumentation-tornado 0.55b1__py3-none-any.whl → 0.56b0__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.
@@ -162,6 +162,7 @@ from timeit import default_timer
162
162
  from typing import Collection, Dict
163
163
 
164
164
  import tornado.web
165
+ import tornado.websocket
165
166
  import wrapt
166
167
  from wrapt import wrap_function_wrapper
167
168
 
@@ -182,8 +183,19 @@ from opentelemetry.instrumentation.utils import (
182
183
  from opentelemetry.metrics import get_meter
183
184
  from opentelemetry.metrics._internal.instrument import Histogram
184
185
  from opentelemetry.propagators import textmap
186
+ from opentelemetry.semconv._incubating.attributes.http_attributes import (
187
+ HTTP_CLIENT_IP,
188
+ HTTP_FLAVOR,
189
+ HTTP_HOST,
190
+ HTTP_METHOD,
191
+ HTTP_SCHEME,
192
+ HTTP_STATUS_CODE,
193
+ HTTP_TARGET,
194
+ )
195
+ from opentelemetry.semconv._incubating.attributes.net_attributes import (
196
+ NET_PEER_IP,
197
+ )
185
198
  from opentelemetry.semconv.metrics import MetricInstruments
186
- from opentelemetry.semconv.trace import SpanAttributes
187
199
  from opentelemetry.trace.status import Status, StatusCode
188
200
  from opentelemetry.util.http import (
189
201
  OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST,
@@ -351,12 +363,20 @@ def patch_handler_class(tracer, server_histograms, cls, request_hook=None):
351
363
  "prepare",
352
364
  partial(_prepare, tracer, server_histograms, request_hook),
353
365
  )
354
- _wrap(cls, "on_finish", partial(_on_finish, tracer, server_histograms))
355
366
  _wrap(
356
367
  cls,
357
368
  "log_exception",
358
369
  partial(_log_exception, tracer, server_histograms),
359
370
  )
371
+
372
+ if issubclass(cls, tornado.websocket.WebSocketHandler):
373
+ _wrap(
374
+ cls,
375
+ "on_close",
376
+ partial(_websockethandler_on_close, tracer, server_histograms),
377
+ )
378
+ else:
379
+ _wrap(cls, "on_finish", partial(_on_finish, tracer, server_histograms))
360
380
  return True
361
381
 
362
382
 
@@ -365,8 +385,11 @@ def unpatch_handler_class(cls):
365
385
  return
366
386
 
367
387
  unwrap(cls, "prepare")
368
- unwrap(cls, "on_finish")
369
388
  unwrap(cls, "log_exception")
389
+ if issubclass(cls, tornado.websocket.WebSocketHandler):
390
+ unwrap(cls, "on_close")
391
+ else:
392
+ unwrap(cls, "on_finish")
370
393
  delattr(cls, _OTEL_PATCHED_KEY)
371
394
 
372
395
 
@@ -394,13 +417,21 @@ def _prepare(
394
417
 
395
418
 
396
419
  def _on_finish(tracer, server_histograms, func, handler, args, kwargs):
397
- response = func(*args, **kwargs)
398
-
399
- _record_on_finish_metrics(server_histograms, handler)
420
+ try:
421
+ return func(*args, **kwargs)
422
+ finally:
423
+ _record_on_finish_metrics(server_histograms, handler)
424
+ _finish_span(tracer, handler)
400
425
 
401
- _finish_span(tracer, handler)
402
426
 
403
- return response
427
+ def _websockethandler_on_close(
428
+ tracer, server_histograms, func, handler, args, kwargs
429
+ ):
430
+ try:
431
+ func()
432
+ finally:
433
+ _record_on_finish_metrics(server_histograms, handler)
434
+ _finish_span(tracer, handler)
404
435
 
405
436
 
406
437
  def _log_exception(tracer, server_histograms, func, handler, args, kwargs):
@@ -442,23 +473,21 @@ def _collect_custom_response_headers_attributes(response_headers):
442
473
 
443
474
  def _get_attributes_from_request(request):
444
475
  attrs = {
445
- SpanAttributes.HTTP_METHOD: request.method,
446
- SpanAttributes.HTTP_SCHEME: request.protocol,
447
- SpanAttributes.HTTP_HOST: request.host,
448
- SpanAttributes.HTTP_TARGET: request.path,
476
+ HTTP_METHOD: request.method,
477
+ HTTP_SCHEME: request.protocol,
478
+ HTTP_HOST: request.host,
479
+ HTTP_TARGET: request.path,
449
480
  }
450
481
 
451
482
  if request.remote_ip:
452
483
  # NET_PEER_IP is the address of the network peer
453
484
  # HTTP_CLIENT_IP is the address of the client, which might be different
454
485
  # if Tornado is set to trust X-Forwarded-For headers (xheaders=True)
455
- attrs[SpanAttributes.HTTP_CLIENT_IP] = request.remote_ip
486
+ attrs[HTTP_CLIENT_IP] = request.remote_ip
456
487
  if hasattr(request.connection, "context") and getattr(
457
488
  request.connection.context, "_orig_remote_ip", None
458
489
  ):
459
- attrs[SpanAttributes.NET_PEER_IP] = (
460
- request.connection.context._orig_remote_ip
461
- )
490
+ attrs[NET_PEER_IP] = request.connection.context._orig_remote_ip
462
491
 
463
492
  return extract_attributes_from_object(
464
493
  request, _traced_request_attrs, attrs
@@ -550,7 +579,7 @@ def _finish_span(tracer, handler, error=None):
550
579
  return
551
580
 
552
581
  if ctx.span.is_recording():
553
- ctx.span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, status_code)
582
+ ctx.span.set_attribute(HTTP_STATUS_CODE, status_code)
554
583
  otel_status_code = http_status_to_status_code(
555
584
  status_code, server_span=True
556
585
  )
@@ -601,7 +630,7 @@ def _record_on_finish_metrics(server_histograms, handler, error=None):
601
630
  metric_attributes = _create_metric_attributes(handler)
602
631
 
603
632
  if isinstance(error, tornado.web.HTTPError):
604
- metric_attributes[SpanAttributes.HTTP_STATUS_CODE] = error.status_code
633
+ metric_attributes[HTTP_STATUS_CODE] = error.status_code
605
634
 
606
635
  server_histograms[MetricInstruments.HTTP_SERVER_RESPONSE_SIZE].record(
607
636
  response_size, attributes=metric_attributes
@@ -621,11 +650,11 @@ def _record_on_finish_metrics(server_histograms, handler, error=None):
621
650
 
622
651
  def _create_active_requests_attributes(request):
623
652
  metric_attributes = {
624
- SpanAttributes.HTTP_METHOD: request.method,
625
- SpanAttributes.HTTP_SCHEME: request.protocol,
626
- SpanAttributes.HTTP_FLAVOR: request.version,
627
- SpanAttributes.HTTP_HOST: request.host,
628
- SpanAttributes.HTTP_TARGET: request.path,
653
+ HTTP_METHOD: request.method,
654
+ HTTP_SCHEME: request.protocol,
655
+ HTTP_FLAVOR: request.version,
656
+ HTTP_HOST: request.host,
657
+ HTTP_TARGET: request.path,
629
658
  }
630
659
 
631
660
  return metric_attributes
@@ -633,6 +662,6 @@ def _create_active_requests_attributes(request):
633
662
 
634
663
  def _create_metric_attributes(handler):
635
664
  metric_attributes = _create_active_requests_attributes(handler.request)
636
- metric_attributes[SpanAttributes.HTTP_STATUS_CODE] = handler.get_status()
665
+ metric_attributes[HTTP_STATUS_CODE] = handler.get_status()
637
666
 
638
667
  return metric_attributes
@@ -20,9 +20,13 @@ from tornado.httpclient import HTTPError, HTTPRequest
20
20
  from opentelemetry import trace
21
21
  from opentelemetry.instrumentation.utils import http_status_to_status_code
22
22
  from opentelemetry.propagate import inject
23
- from opentelemetry.semconv.trace import SpanAttributes
23
+ from opentelemetry.semconv._incubating.attributes.http_attributes import (
24
+ HTTP_METHOD,
25
+ HTTP_STATUS_CODE,
26
+ HTTP_URL,
27
+ )
24
28
  from opentelemetry.trace.status import Status, StatusCode
25
- from opentelemetry.util.http import remove_url_credentials
29
+ from opentelemetry.util.http import redact_url
26
30
 
27
31
 
28
32
  def _normalize_request(args, kwargs):
@@ -75,8 +79,8 @@ def fetch_async(
75
79
 
76
80
  if span.is_recording():
77
81
  attributes = {
78
- SpanAttributes.HTTP_URL: remove_url_credentials(request.url),
79
- SpanAttributes.HTTP_METHOD: request.method,
82
+ HTTP_URL: redact_url(request.url),
83
+ HTTP_METHOD: request.method,
80
84
  }
81
85
  for key, value in attributes.items():
82
86
  span.set_attribute(key, value)
@@ -135,7 +139,7 @@ def _finish_tracing_callback(
135
139
  )
136
140
 
137
141
  if status_code is not None:
138
- span.set_attribute(SpanAttributes.HTTP_STATUS_CODE, status_code)
142
+ span.set_attribute(HTTP_STATUS_CODE, status_code)
139
143
  span.set_status(status)
140
144
 
141
145
  if response is not None:
@@ -160,9 +164,9 @@ def _finish_tracing_callback(
160
164
 
161
165
  def _create_metric_attributes(response):
162
166
  metric_attributes = {
163
- SpanAttributes.HTTP_STATUS_CODE: response.code,
164
- SpanAttributes.HTTP_URL: remove_url_credentials(response.request.url),
165
- SpanAttributes.HTTP_METHOD: response.request.method,
167
+ HTTP_STATUS_CODE: response.code,
168
+ HTTP_URL: redact_url(response.request.url),
169
+ HTTP_METHOD: response.request.method,
166
170
  }
167
171
 
168
172
  return metric_attributes
@@ -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.55b1"
15
+ __version__ = "0.56b0"
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: opentelemetry-instrumentation-tornado
3
- Version: 0.55b1
3
+ Version: 0.56b0
4
4
  Summary: Tornado instrumentation for OpenTelemetry
5
5
  Project-URL: Homepage, https://github.com/open-telemetry/opentelemetry-python-contrib/tree/main/instrumentation/opentelemetry-instrumentation-tornado
6
6
  Project-URL: Repository, https://github.com/open-telemetry/opentelemetry-python-contrib
@@ -18,9 +18,9 @@ Classifier: Programming Language :: Python :: 3.12
18
18
  Classifier: Programming Language :: Python :: 3.13
19
19
  Requires-Python: >=3.9
20
20
  Requires-Dist: opentelemetry-api~=1.12
21
- Requires-Dist: opentelemetry-instrumentation==0.55b1
22
- Requires-Dist: opentelemetry-semantic-conventions==0.55b1
23
- Requires-Dist: opentelemetry-util-http==0.55b1
21
+ Requires-Dist: opentelemetry-instrumentation==0.56b0
22
+ Requires-Dist: opentelemetry-semantic-conventions==0.56b0
23
+ Requires-Dist: opentelemetry-util-http==0.56b0
24
24
  Provides-Extra: instruments
25
25
  Requires-Dist: tornado>=5.1.1; extra == 'instruments'
26
26
  Description-Content-Type: text/x-rst
@@ -0,0 +1,9 @@
1
+ opentelemetry/instrumentation/tornado/__init__.py,sha256=Xo2NCi1_C4J9wK3U1EI1gNNlNPsUJDFlu97FSnjTh2I,23814
2
+ opentelemetry/instrumentation/tornado/client.py,sha256=owjoJurMaTaW3u8MGbuScAPKKtbqm-tkzY8O1eoasPQ,5031
3
+ opentelemetry/instrumentation/tornado/package.py,sha256=mv-OUOrDF54kFoP_Epb3Pv8aufliZTMOwLWL5oAV_hY,649
4
+ opentelemetry/instrumentation/tornado/version.py,sha256=Lt3ez1q5jzk4LG8aTMzoNp4bUxxWBqofY7vWXcRBByA,608
5
+ opentelemetry_instrumentation_tornado-0.56b0.dist-info/METADATA,sha256=Ae8-OpWuHMP9SOLyiHgbDKKSVK7lMNvsC_tUde_1q0Q,2072
6
+ opentelemetry_instrumentation_tornado-0.56b0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
7
+ opentelemetry_instrumentation_tornado-0.56b0.dist-info/entry_points.txt,sha256=oDTErilMYHHuN6bv9XNAU1Avoy8f_nfCx91bASSkpgg,97
8
+ opentelemetry_instrumentation_tornado-0.56b0.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
9
+ opentelemetry_instrumentation_tornado-0.56b0.dist-info/RECORD,,
@@ -1,9 +0,0 @@
1
- opentelemetry/instrumentation/tornado/__init__.py,sha256=avm1_uofS-LrkTteD7R9VLgQagBmu0cuSBqCW41yYyc,23228
2
- opentelemetry/instrumentation/tornado/client.py,sha256=GmCilVnjXQAyXqGDAZsTT8CDhVTmRtczXgFzYLmpMto,5082
3
- opentelemetry/instrumentation/tornado/package.py,sha256=mv-OUOrDF54kFoP_Epb3Pv8aufliZTMOwLWL5oAV_hY,649
4
- opentelemetry/instrumentation/tornado/version.py,sha256=9poofzNAQlIZ01Tbk-NsWviDJJKwwkXRZ-4gS8RKJ9M,608
5
- opentelemetry_instrumentation_tornado-0.55b1.dist-info/METADATA,sha256=p_YFie3JFEPUrAeoXWCOEEOIJxhKB_U-dif7D6y4yQY,2072
6
- opentelemetry_instrumentation_tornado-0.55b1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
7
- opentelemetry_instrumentation_tornado-0.55b1.dist-info/entry_points.txt,sha256=oDTErilMYHHuN6bv9XNAU1Avoy8f_nfCx91bASSkpgg,97
8
- opentelemetry_instrumentation_tornado-0.55b1.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
9
- opentelemetry_instrumentation_tornado-0.55b1.dist-info/RECORD,,