Skip to content

Commit fdb69e4

Browse files
committed
Remove StatusError
1 parent 1f25f3e commit fdb69e4

File tree

4 files changed

+32
-59
lines changed

4 files changed

+32
-59
lines changed

opentelemetry-api/src/opentelemetry/trace/__init__.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,7 @@ def start_span(
395395
attributes: typing.Optional[types.Attributes] = None,
396396
links: typing.Sequence[Link] = (),
397397
start_time: typing.Optional[int] = None,
398-
auto_update_status: bool = True,
398+
set_status_on_exception: bool = True,
399399
) -> "Span":
400400
"""Starts a span.
401401
@@ -427,8 +427,11 @@ def start_span(
427427
attributes: The span's attributes.
428428
links: Links span to other spans
429429
start_time: Sets the start time of a span
430-
auto_update_status: Defines if the status should be updated
431-
automatically when the span finishes
430+
set_status_on_exception: Only relevant if the returned span is used
431+
in a with/context manager. Defines wether the span status will be
432+
automatically set to UNKNOWN when an uncaught exception is raised
433+
in the span with block. The span status won't be set by this
434+
mechanism if it was previousy set manually.
432435
433436
Returns:
434437
The newly-created span.

opentelemetry-api/src/opentelemetry/trace/status.py

-22
Original file line numberDiff line numberDiff line change
@@ -183,25 +183,3 @@ def description(self) -> typing.Optional[str]:
183183
def is_ok(self) -> bool:
184184
"""Returns false if this represents an error, true otherwise."""
185185
return self._canonical_code is StatusCanonicalCode.OK
186-
187-
188-
class StatusError(Exception):
189-
def __init__(
190-
self,
191-
canonical_code: StatusCanonicalCode,
192-
description: typing.Optional[str] = None,
193-
):
194-
self._canonical_code = canonical_code
195-
self._description = description
196-
197-
super(StatusError, self).__init__()
198-
199-
@property
200-
def canonical_code(self) -> StatusCanonicalCode:
201-
"""Represents the canonical status code of a finished Span."""
202-
return self._canonical_code
203-
204-
@property
205-
def description(self) -> typing.Optional[str]:
206-
"""Status description"""
207-
return self._description

opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

+19-27
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
from opentelemetry.sdk import util
2727
from opentelemetry.sdk.util import BoundedDict, BoundedList
2828
from opentelemetry.trace import sampling
29-
from opentelemetry.trace.status import Status, StatusCanonicalCode, StatusError
29+
from opentelemetry.trace.status import Status, StatusCanonicalCode
3030
from opentelemetry.util import time_ns, types
3131

3232
logger = logging.getLogger(__name__)
@@ -136,7 +136,7 @@ def __init__(
136136
links: Sequence[trace_api.Link] = (),
137137
kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL,
138138
span_processor: SpanProcessor = SpanProcessor(),
139-
auto_update_status: bool = True,
139+
set_status_on_exception: bool = True,
140140
) -> None:
141141

142142
self.name = name
@@ -146,7 +146,7 @@ def __init__(
146146
self.trace_config = trace_config
147147
self.resource = resource
148148
self.kind = kind
149-
self._auto_update_status = auto_update_status
149+
self._set_status_on_exception = set_status_on_exception
150150

151151
self.span_processor = span_processor
152152
self.status = None
@@ -261,11 +261,11 @@ def end(self, end_time: int = None) -> None:
261261
logger.warning("Calling end() on an ended span.")
262262
return
263263

264-
self.span_processor.on_end(self)
265-
266264
if self.status is None:
267265
self.set_status(Status(canonical_code=StatusCanonicalCode.OK))
268266

267+
self.span_processor.on_end(self)
268+
269269
def update_name(self, name: str) -> None:
270270
with self._lock:
271271
has_ended = self.end_time is not None
@@ -293,26 +293,18 @@ def __exit__(
293293
) -> None:
294294
"""Ends context manager and calls `end` on the `Span`."""
295295

296-
if self.status is None and self._auto_update_status:
297-
if exc_val is not None:
298-
299-
if isinstance(exc_val, StatusError):
300-
self.set_status(
301-
Status(
302-
canonical_code=exc_val.canonical_code,
303-
description=exc_val.description,
304-
)
305-
)
306-
307-
else:
308-
self.set_status(
309-
Status(
310-
canonical_code=StatusCanonicalCode.UNKNOWN,
311-
description="{}: {}".format(
312-
exc_type.__name__, exc_val
313-
),
314-
)
315-
)
296+
if (
297+
self.status is None
298+
and self._set_status_on_exception
299+
and exc_val is not None
300+
):
301+
302+
self.set_status(
303+
Status(
304+
canonical_code=StatusCanonicalCode.UNKNOWN,
305+
description="{}: {}".format(exc_type.__name__, exc_val),
306+
)
307+
)
316308

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

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

@@ -443,7 +435,7 @@ def start_span( # pylint: disable=too-many-locals
443435
span_processor=self._active_span_processor,
444436
kind=kind,
445437
links=links,
446-
auto_update_status=auto_update_status,
438+
set_status_on_exception=set_status_on_exception,
447439
)
448440
span.start(start_time=start_time)
449441
else:

opentelemetry-sdk/tests/trace/test_trace.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
from opentelemetry import trace as trace_api
2121
from opentelemetry.sdk import trace
2222
from opentelemetry.trace import sampling
23-
from opentelemetry.trace.status import StatusCanonicalCode, StatusError
23+
from opentelemetry.trace.status import StatusCanonicalCode
2424
from opentelemetry.util import time_ns
2525

2626

@@ -514,14 +514,14 @@ def test_ended_span(self):
514514
self.assertIs(root.status, None)
515515

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

521-
self.assertIs(
522-
root.status.canonical_code, StatusCanonicalCode.CANCELLED
523-
)
524-
self.assertEqual(root.status.description, "cancelled")
523+
self.assertIs(root.status.canonical_code, StatusCanonicalCode.UNKNOWN)
524+
self.assertEqual(root.status.description, "Exception: unknown")
525525

526526

527527
def span_event_start_fmt(span_processor_name, span_name):

0 commit comments

Comments
 (0)