From a75da2e48bf4793a1b83e2555b29a4c86d050f2f Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Mon, 27 Jan 2020 18:23:41 -0600 Subject: [PATCH 1/2] Set status in start_as_current_span too Fixes #377 --- .../src/opentelemetry/sdk/trace/__init__.py | 15 ++++++++++ opentelemetry-sdk/tests/trace/test_trace.py | 30 ++++++++++++------- 2 files changed, 35 insertions(+), 10 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 9829c8b33ba..4f94139e7ee 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -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() diff --git a/opentelemetry-sdk/tests/trace/test_trace.py b/opentelemetry-sdk/tests/trace/test_trace.py index 53a10518aaf..2a16e235ebf 100644 --- a/opentelemetry-sdk/tests/trace/test_trace.py +++ b/opentelemetry-sdk/tests/trace/test_trace.py @@ -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): From 13a5ef7117085068aea64c44028e50f9f67f6d73 Mon Sep 17 00:00:00 2001 From: Diego Hurtado Date: Wed, 29 Jan 2020 18:50:22 -0600 Subject: [PATCH 2/2] Raise missing exception --- .../src/opentelemetry/sdk/trace/__init__.py | 2 ++ opentelemetry-sdk/tests/trace/test_trace.py | 10 +++++----- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 4f94139e7ee..fd279e603af 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -553,6 +553,8 @@ def use_span( ) ) + raise + finally: if end_on_exit: span.end() diff --git a/opentelemetry-sdk/tests/trace/test_trace.py b/opentelemetry-sdk/tests/trace/test_trace.py index 2a16e235ebf..449fb51e8ee 100644 --- a/opentelemetry-sdk/tests/trace/test_trace.py +++ b/opentelemetry-sdk/tests/trace/test_trace.py @@ -642,16 +642,16 @@ def test_ended_span(self): def test_error_status(self): def error_status_test(context): - try: + with self.assertRaises(AssertionError): with context as root: - raise Exception("unknown") - except Exception: # pylint: disable=broad-except - pass + raise AssertionError("unknown") self.assertIs( root.status.canonical_code, StatusCanonicalCode.UNKNOWN ) - self.assertEqual(root.status.description, "Exception: unknown") + self.assertEqual( + root.status.description, "AssertionError: unknown" + ) error_status_test( trace.TracerSource().get_tracer(__name__).start_span("root")