Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow set_status to accept the StatusCode and optional description #2735

Merged
merged 12 commits into from Jun 11, 2022
2 changes: 2 additions & 0 deletions CHANGELOG.md
Expand Up @@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2727](https://github.com/open-telemetry/opentelemetry-python/pull/2727))
- fix: update entry point object references for metrics
([#2731](https://github.com/open-telemetry/opentelemetry-python/pull/2731))
- Allow set_status to accept the StatusCode and optional description
([#2735](https://github.com/open-telemetry/opentelemetry-python/pull/2735))

## [1.12.0rc1-0.31b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.12.0rc1-0.31b0) - 2022-05-17

Expand Down
14 changes: 11 additions & 3 deletions opentelemetry-api/src/opentelemetry/trace/span.py
Expand Up @@ -5,7 +5,7 @@
import typing
from collections import OrderedDict

from opentelemetry.trace.status import Status
from opentelemetry.trace.status import Status, StatusCode
from opentelemetry.util import types

# The key MUST begin with a lowercase letter or a digit,
Expand Down Expand Up @@ -137,7 +137,11 @@ def is_recording(self) -> bool:
"""

@abc.abstractmethod
def set_status(self, status: Status) -> None:
def set_status(
self,
status: typing.Union[Status, StatusCode],
description: typing.Optional[str] = None,
) -> None:
"""Sets the Status of the Span. If used, this will override the default
Span status.
"""
Expand Down Expand Up @@ -524,7 +528,11 @@ def add_event(
def update_name(self, name: str) -> None:
pass

def set_status(self, status: Status) -> None:
def set_status(
self,
status: typing.Union[Status, StatusCode],
description: typing.Optional[str] = None,
) -> None:
pass

def record_exception(
Expand Down
29 changes: 21 additions & 8 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Expand Up @@ -889,16 +889,29 @@ def is_recording(self) -> bool:
return self._end_time is None

@_check_span_ended
def set_status(self, status: trace_api.Status) -> None:
def set_status(
self,
status: typing.Union[Status, StatusCode],
description: typing.Optional[str] = None,
) -> None:
# Ignore future calls if status is already set to OK
# Ignore calls to set to StatusCode.UNSET
if (
self._status
and self._status.status_code is StatusCode.OK
or status.status_code is StatusCode.UNSET
):
return
self._status = status
if isinstance(status, Status):
if (
self._status
and self._status.status_code is StatusCode.OK
or status.status_code is StatusCode.UNSET
):
return
self._status = status
elif isinstance(status, StatusCode):
if (
self._status
and self._status.status_code is StatusCode.OK
or status is StatusCode.UNSET
):
return
self._status = Status(status, description)

def __exit__(
self,
Expand Down
34 changes: 33 additions & 1 deletion opentelemetry-sdk/tests/trace/test_trace.py
Expand Up @@ -45,7 +45,7 @@
get_span_with_dropped_attributes_events_links,
new_tracer,
)
from opentelemetry.trace import StatusCode
from opentelemetry.trace import Status, StatusCode
from opentelemetry.util._time import _time_ns


Expand Down Expand Up @@ -903,6 +903,38 @@ def test_span_override_start_and_end_time(self):
span.end(end_time)
self.assertEqual(end_time, span.end_time)

def test_span_set_status(self):

span1 = self.tracer.start_span("span1")
span1.set_status(Status(status_code=StatusCode.ERROR))
self.assertEqual(span1.status.status_code, StatusCode.ERROR)
self.assertEqual(span1.status.description, None)

span2 = self.tracer.start_span("span2")
span2.set_status(
Status(status_code=StatusCode.ERROR, description="desc")
)
self.assertEqual(span2.status.status_code, StatusCode.ERROR)
self.assertEqual(span2.status.description, "desc")

span3 = self.tracer.start_span("span3")
span3.set_status(StatusCode.ERROR)
self.assertEqual(span3.status.status_code, StatusCode.ERROR)
self.assertEqual(span3.status.description, None)

span4 = self.tracer.start_span("span4")
span4.set_status(StatusCode.ERROR, "span4 desc")
self.assertEqual(span4.status.status_code, StatusCode.ERROR)
self.assertEqual(span4.status.description, "span4 desc")

span5 = self.tracer.start_span("span5")
span5.set_status(
Status(status_code=StatusCode.ERROR, description="desc"),
description="ignored",
ocelotl marked this conversation as resolved.
Show resolved Hide resolved
)
self.assertEqual(span5.status.status_code, StatusCode.ERROR)
self.assertEqual(span5.status.description, "desc")

def test_ended_span(self):
"""Events, attributes are not allowed after span is ended"""

Expand Down