Skip to content

Commit a75da2e

Browse files
committed
Set status in start_as_current_span too
Fixes open-telemetry#377
1 parent ccb97e5 commit a75da2e

File tree

2 files changed

+35
-10
lines changed

2 files changed

+35
-10
lines changed

Diff for: opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

+15
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,21 @@ 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+
541556
finally:
542557
if end_on_exit:
543558
span.end()

Diff for: 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+
try:
646+
with context as root:
647+
raise Exception("unknown")
648+
except Exception: # pylint: disable=broad-except
649+
pass
650+
651+
self.assertIs(
652+
root.status.canonical_code, StatusCanonicalCode.UNKNOWN
653+
)
654+
self.assertEqual(root.status.description, "Exception: unknown")
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)