Skip to content

Commit

Permalink
Remove StatusError
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Dec 9, 2019
1 parent 1f25f3e commit fdb69e4
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 59 deletions.
9 changes: 6 additions & 3 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def start_span(
attributes: typing.Optional[types.Attributes] = None,
links: typing.Sequence[Link] = (),
start_time: typing.Optional[int] = None,
auto_update_status: bool = True,
set_status_on_exception: bool = True,
) -> "Span":
"""Starts a span.
Expand Down Expand Up @@ -427,8 +427,11 @@ def start_span(
attributes: The span's attributes.
links: Links span to other spans
start_time: Sets the start time of a span
auto_update_status: Defines if the status should be updated
automatically when the span finishes
set_status_on_exception: Only relevant if the returned span is used
in a with/context manager. Defines wether the span status will be
automatically set to UNKNOWN when an uncaught exception is raised
in the span with block. The span status won't be set by this
mechanism if it was previousy set manually.
Returns:
The newly-created span.
Expand Down
22 changes: 0 additions & 22 deletions opentelemetry-api/src/opentelemetry/trace/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,25 +183,3 @@ def description(self) -> typing.Optional[str]:
def is_ok(self) -> bool:
"""Returns false if this represents an error, true otherwise."""
return self._canonical_code is StatusCanonicalCode.OK


class StatusError(Exception):
def __init__(
self,
canonical_code: StatusCanonicalCode,
description: typing.Optional[str] = None,
):
self._canonical_code = canonical_code
self._description = description

super(StatusError, self).__init__()

@property
def canonical_code(self) -> StatusCanonicalCode:
"""Represents the canonical status code of a finished Span."""
return self._canonical_code

@property
def description(self) -> typing.Optional[str]:
"""Status description"""
return self._description
46 changes: 19 additions & 27 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from opentelemetry.sdk import util
from opentelemetry.sdk.util import BoundedDict, BoundedList
from opentelemetry.trace import sampling
from opentelemetry.trace.status import Status, StatusCanonicalCode, StatusError
from opentelemetry.trace.status import Status, StatusCanonicalCode
from opentelemetry.util import time_ns, types

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -136,7 +136,7 @@ def __init__(
links: Sequence[trace_api.Link] = (),
kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL,
span_processor: SpanProcessor = SpanProcessor(),
auto_update_status: bool = True,
set_status_on_exception: bool = True,
) -> None:

self.name = name
Expand All @@ -146,7 +146,7 @@ def __init__(
self.trace_config = trace_config
self.resource = resource
self.kind = kind
self._auto_update_status = auto_update_status
self._set_status_on_exception = set_status_on_exception

self.span_processor = span_processor
self.status = None
Expand Down Expand Up @@ -261,11 +261,11 @@ def end(self, end_time: int = None) -> None:
logger.warning("Calling end() on an ended span.")
return

self.span_processor.on_end(self)

if self.status is None:
self.set_status(Status(canonical_code=StatusCanonicalCode.OK))

self.span_processor.on_end(self)

def update_name(self, name: str) -> None:
with self._lock:
has_ended = self.end_time is not None
Expand Down Expand Up @@ -293,26 +293,18 @@ def __exit__(
) -> None:
"""Ends context manager and calls `end` on the `Span`."""

if self.status is None and self._auto_update_status:
if exc_val is not None:

if isinstance(exc_val, StatusError):
self.set_status(
Status(
canonical_code=exc_val.canonical_code,
description=exc_val.description,
)
)

else:
self.set_status(
Status(
canonical_code=StatusCanonicalCode.UNKNOWN,
description="{}: {}".format(
exc_type.__name__, exc_val
),
)
)
if (
self.status is None
and self._set_status_on_exception
and exc_val is not None
):

self.set_status(
Status(
canonical_code=StatusCanonicalCode.UNKNOWN,
description="{}: {}".format(exc_type.__name__, exc_val),
)
)

super().__exit__(exc_type, exc_val, exc_tb)

Expand Down Expand Up @@ -383,7 +375,7 @@ def start_span( # pylint: disable=too-many-locals
attributes: Optional[types.Attributes] = None,
links: Sequence[trace_api.Link] = (),
start_time: Optional[int] = None,
auto_update_status: bool = True,
set_status_on_exception: bool = True,
) -> "Span":
"""See `opentelemetry.trace.Tracer.start_span`."""

Expand Down Expand Up @@ -443,7 +435,7 @@ def start_span( # pylint: disable=too-many-locals
span_processor=self._active_span_processor,
kind=kind,
links=links,
auto_update_status=auto_update_status,
set_status_on_exception=set_status_on_exception,
)
span.start(start_time=start_time)
else:
Expand Down
14 changes: 7 additions & 7 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from opentelemetry import trace as trace_api
from opentelemetry.sdk import trace
from opentelemetry.trace import sampling
from opentelemetry.trace.status import StatusCanonicalCode, StatusError
from opentelemetry.trace.status import StatusCanonicalCode
from opentelemetry.util import time_ns


Expand Down Expand Up @@ -514,14 +514,14 @@ def test_ended_span(self):
self.assertIs(root.status, None)

def test_error_status(self):
with self.assertRaises(StatusError):
try:
with trace.Tracer("test_error_status").start_span("root") as root:
raise StatusError(StatusCanonicalCode.CANCELLED, "cancelled")
raise Exception("unknown")
except Exception: # pylint: disable=broad-except
pass

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


def span_event_start_fmt(span_processor_name, span_name):
Expand Down

0 comments on commit fdb69e4

Please sign in to comment.