Skip to content

Commit 915643c

Browse files
authored
sdk: Fix DefaultSpan raising an exception in use_span (#577)
Fixes the error caused by span.status in use_span. As status is not included in opentelemetry.trace.Span, span.status in use_span may cause an error. For example, when use_span is called with DefaultSpan, it causes the error below. 'DefaultSpan' object has no attribute 'status'
1 parent 44b592c commit 915643c

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,8 @@ def use_span(
605605

606606
except Exception as error: # pylint: disable=broad-except
607607
if (
608-
span.status is None
608+
isinstance(span, Span)
609+
and span.status is None
609610
and span._set_status_on_exception # pylint:disable=protected-access # noqa
610611
):
611612
span.set_status(

opentelemetry-sdk/tests/trace/test_trace.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@ def run_general_code(shutdown_on_exit, explicit_shutdown):
107107
out = run_general_code(False, False)
108108
self.assertTrue(out.startswith(b"0"))
109109

110+
def test_use_span_exception(self):
111+
class TestUseSpanException(Exception):
112+
pass
113+
114+
default_span = trace_api.DefaultSpan(trace_api.INVALID_SPAN_CONTEXT)
115+
tracer = new_tracer()
116+
with self.assertRaises(TestUseSpanException):
117+
with tracer.use_span(default_span):
118+
raise TestUseSpanException()
119+
110120

111121
class TestTracerSampling(unittest.TestCase):
112122
def test_default_sampler(self):

0 commit comments

Comments
 (0)