Skip to content

Commit

Permalink
Set status in start_as_current_span too
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Jan 28, 2020
1 parent ccb97e5 commit a75da2e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
15 changes: 15 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,21 @@ def use_span(
self.source._current_span_slot.set( # pylint:disable=protected-access
span_snapshot
)

except Exception as error: # pylint: disable=broad-except
if (
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
),
)
)

finally:
if end_on_exit:
span.end()
Expand Down
30 changes: 20 additions & 10 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -641,16 +641,26 @@ def test_ended_span(self):
)

def test_error_status(self):
try:
with trace.TracerSource().get_tracer(__name__).start_span(
"root"
) as root:
raise Exception("unknown")
except Exception: # pylint: disable=broad-except
pass

self.assertIs(root.status.canonical_code, StatusCanonicalCode.UNKNOWN)
self.assertEqual(root.status.description, "Exception: unknown")
def error_status_test(context):
try:
with context as root:
raise Exception("unknown")
except Exception: # pylint: disable=broad-except
pass

self.assertIs(
root.status.canonical_code, StatusCanonicalCode.UNKNOWN
)
self.assertEqual(root.status.description, "Exception: unknown")

error_status_test(
trace.TracerSource().get_tracer(__name__).start_span("root")
)
error_status_test(
trace.TracerSource()
.get_tracer(__name__)
.start_as_current_span("root")
)


def span_event_start_fmt(span_processor_name, span_name):
Expand Down

0 comments on commit a75da2e

Please sign in to comment.