Skip to content

Commit 31f5726

Browse files
authored
Set status in start_as_current_span too (#381)
Fixes #377
1 parent 6aa69ae commit 31f5726

File tree

2 files changed

+37
-10
lines changed

2 files changed

+37
-10
lines changed

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

+17
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,23 @@ def use_span(
538538
self.source._current_span_slot.set( # pylint:disable=protected-access
539539
span_snapshot
540540
)
541+
542+
except Exception as error: # pylint: disable=broad-except
543+
if (
544+
span.status is None
545+
and span._set_status_on_exception # pylint:disable=protected-access # noqa
546+
):
547+
span.set_status(
548+
Status(
549+
canonical_code=StatusCanonicalCode.UNKNOWN,
550+
description="{}: {}".format(
551+
type(error).__name__, error
552+
),
553+
)
554+
)
555+
556+
raise
557+
541558
finally:
542559
if end_on_exit:
543560
span.end()

opentelemetry-sdk/tests/trace/test_trace.py

+20-10
Original file line numberDiff line numberDiff line change
@@ -641,16 +641,26 @@ def test_ended_span(self):
641641
)
642642

643643
def test_error_status(self):
644-
try:
645-
with trace.TracerSource().get_tracer(__name__).start_span(
646-
"root"
647-
) as root:
648-
raise Exception("unknown")
649-
except Exception: # pylint: disable=broad-except
650-
pass
651-
652-
self.assertIs(root.status.canonical_code, StatusCanonicalCode.UNKNOWN)
653-
self.assertEqual(root.status.description, "Exception: unknown")
644+
def error_status_test(context):
645+
with self.assertRaises(AssertionError):
646+
with context as root:
647+
raise AssertionError("unknown")
648+
649+
self.assertIs(
650+
root.status.canonical_code, StatusCanonicalCode.UNKNOWN
651+
)
652+
self.assertEqual(
653+
root.status.description, "AssertionError: unknown"
654+
)
655+
656+
error_status_test(
657+
trace.TracerSource().get_tracer(__name__).start_span("root")
658+
)
659+
error_status_test(
660+
trace.TracerSource()
661+
.get_tracer(__name__)
662+
.start_as_current_span("root")
663+
)
654664

655665

656666
def span_event_start_fmt(span_processor_name, span_name):

0 commit comments

Comments
 (0)