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

Span.set_status priority #1902

Merged
merged 3 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Updated `opentelemetry-opencensus-exporter` to use `service_name` of spans instead of resource
([#1897](https://github.com/open-telemetry/opentelemetry-python/pull/1897))
- Ignore calls to `Span.set_status` with `StatusCode.UNSET` and also if previous status already
had `StatusCode.OK`.
([#1902](https://github.com/open-telemetry/opentelemetry-python/pull/1902))

## [1.3.0-0.22b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.3.0-0.22b0) - 2021-06-01

Expand Down
8 changes: 8 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,14 @@ def is_recording(self) -> bool:

@_check_span_ended
def set_status(self, status: trace_api.Status) -> 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

def __exit__(
Expand Down
38 changes: 36 additions & 2 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,6 @@ def error_status_test(context):
with self.assertRaises(AssertionError):
with context as root:
raise AssertionError("unknown")

self.assertIs(root.status.status_code, StatusCode.ERROR)
self.assertEqual(
root.status.description, "AssertionError: unknown"
Expand All @@ -928,18 +927,53 @@ def error_status_test(context):
.start_as_current_span("root")
)

def test_last_status_wins(self):
def test_status_cannot_override_ok(self):
def error_status_test(context):
with self.assertRaises(AssertionError):
with context as root:
root.set_status(trace_api.status.Status(StatusCode.OK))
raise AssertionError("unknown")
self.assertIs(root.status.status_code, StatusCode.OK)
self.assertIsNone(root.status.description)

error_status_test(
trace.TracerProvider().get_tracer(__name__).start_span("root")
)
error_status_test(
trace.TracerProvider()
.get_tracer(__name__)
.start_as_current_span("root")
)

def test_status_cannot_set_unset(self):
def unset_status_test(context):
with self.assertRaises(AssertionError):
with context as root:
raise AssertionError("unknown")
root.set_status(trace_api.status.Status(StatusCode.UNSET))
self.assertIs(root.status.status_code, StatusCode.ERROR)
self.assertEqual(
root.status.description, "AssertionError: unknown"
)

unset_status_test(
trace.TracerProvider().get_tracer(__name__).start_span("root")
)
unset_status_test(
trace.TracerProvider()
.get_tracer(__name__)
.start_as_current_span("root")
)

def test_last_status_wins(self):
def error_status_test(context):
with self.assertRaises(AssertionError):
with context as root:
raise AssertionError("unknown")
root.set_status(trace_api.status.Status(StatusCode.OK))
self.assertIs(root.status.status_code, StatusCode.OK)
self.assertIsNone(root.status.description)

error_status_test(
trace.TracerProvider().get_tracer(__name__).start_span("root")
)
Expand Down