Skip to content

Commit

Permalink
Set status in start_as_current_span too (#381)
Browse files Browse the repository at this point in the history
Fixes #377
  • Loading branch information
ocelotl authored Jan 30, 2020
1 parent 6aa69ae commit 31f5726
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 10 deletions.
17 changes: 17 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,23 @@ 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
),
)
)

raise

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):
with self.assertRaises(AssertionError):
with context as root:
raise AssertionError("unknown")

self.assertIs(
root.status.canonical_code, StatusCanonicalCode.UNKNOWN
)
self.assertEqual(
root.status.description, "AssertionError: 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 31f5726

Please sign in to comment.