Skip to content

Commit 632452a

Browse files
committed
extract response from exception and record its info into span
1 parent 8256c46 commit 632452a

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

instrumentation/opentelemetry-instrumentation-tornado/src/opentelemetry/instrumentation/tornado/client.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -105,17 +105,17 @@ def _finish_tracing_callback(
105105
request_size_histogram,
106106
response_size_histogram,
107107
):
108+
response = None
108109
status_code = None
109110
description = None
110-
exc = future.exception()
111111

112-
if span.is_recording() and exc:
113-
if isinstance(exc, HTTPError):
114-
status_code = exc.code
112+
exc = future.exception()
113+
if exc:
115114
description = f"{type(exc).__name__}: {exc}"
116-
117-
response = None
118-
if not exc:
115+
if isinstance(exc, HTTPError):
116+
response = exc.response
117+
status_code = response.code
118+
else:
119119
response = future.result()
120120
status_code = response.code
121121

instrumentation/opentelemetry-instrumentation-tornado/tests/test_instrumentation.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
from opentelemetry.semconv.trace import SpanAttributes
3434
from opentelemetry.test.test_base import TestBase
3535
from opentelemetry.test.wsgitestutil import WsgiTestBase
36-
from opentelemetry.trace import SpanKind
36+
from opentelemetry.trace import SpanKind, StatusCode
3737
from opentelemetry.util.http import (
3838
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_REQUEST,
3939
OTEL_INSTRUMENTATION_HTTP_CAPTURE_HEADERS_SERVER_RESPONSE,
@@ -494,7 +494,6 @@ def test_response_headers(self):
494494
self.assertEqual(len(spans), 3)
495495
self.assertTraceResponseHeaderMatchesSpan(response.headers, spans[1])
496496

497-
self.memory_exporter.clear()
498497
set_global_response_propagator(orig)
499498

500499
def test_credential_removal(self):
@@ -614,6 +613,7 @@ def test_http_client_success_response(self):
614613
self.assertEqual(manual.name, "manual")
615614
self.assertEqual(server.name, "GET /")
616615
self.assertEqual(client.name, "GET")
616+
self.assertEqual(client.status.status_code, StatusCode.UNSET)
617617
self.memory_exporter.clear()
618618

619619
def test_http_client_failed_response(self):
@@ -626,20 +626,22 @@ def test_http_client_failed_response(self):
626626
server, client = self.sorted_spans(spans)
627627
self.assertEqual(server.name, "GET /some-404")
628628
self.assertEqual(client.name, "GET")
629+
self.assertEqual(client.status.status_code, StatusCode.ERROR)
629630
self.memory_exporter.clear()
630631

631632
# when an exception is thrown
632633
try:
633634
response = self.fetch("/some-404", raise_error=True)
634635
self.assertEqual(response.code, 404)
635636
except HTTPClientError:
636-
pass
637+
pass # expected exception - continue
637638

638639
spans = self.memory_exporter.get_finished_spans()
639640
self.assertEqual(len(spans), 2)
640641
server, client = self.sorted_spans(spans)
641642
self.assertEqual(server.name, "GET /some-404")
642643
self.assertEqual(client.name, "GET")
644+
self.assertEqual(client.status.status_code, StatusCode.ERROR)
643645
self.memory_exporter.clear()
644646

645647

0 commit comments

Comments
 (0)