Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -667,18 +667,24 @@ def use_span(
context_api.detach(token)

except Exception as error: # pylint: disable=broad-except
if (
isinstance(span, Span)
and span.status is None
and span._set_status_on_exception # pylint:disable=protected-access # noqa
):
span.set_status(
Status(
canonical_code=StatusCanonicalCode.UNKNOWN,
description="{}: {}".format(
type(error).__name__, error
),
try:
if (
isinstance(span, Span)
and span.status is None
and span._set_status_on_exception # pylint:disable=protected-access # noqa
):
span.set_status(
Status(
canonical_code=StatusCanonicalCode.UNKNOWN,
description="{}: {}".format(
type(error).__name__, error
),
)
)
except Exception as e:
logger.error(
"Got another exception while setting status on exception: %s"
% e
)

raise
Expand Down
11 changes: 11 additions & 0 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,3 +882,14 @@ def test_add_span_processor_after_span_creation(self):
expected_list.append(span_event_end_fmt("SP1", "foo"))

self.assertListEqual(spans_calls_list, expected_list)

def test_use_span_set_status_on_exception(self):
"""In the case where tracer sets status on exception,
it shouldn't raise another exception instead of the original one.
"""
tracer = new_tracer()
invalid_span = None
with self.assertRaises(RuntimeError) as cm:
with tracer.use_span(invalid_span, False):
raise RuntimeError("intended error")
self.assertEqual(str(cm.exception), "intended error")