Skip to content

Commit 3bb6bdc

Browse files
authored
Span.set_status priority (open-telemetry#1902)
1 parent fcde911 commit 3bb6bdc

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Changed
1010
- Updated `opentelemetry-opencensus-exporter` to use `service_name` of spans instead of resource
1111
([#1897](https://github.com/open-telemetry/opentelemetry-python/pull/1897))
12+
- Ignore calls to `Span.set_status` with `StatusCode.UNSET` and also if previous status already
13+
had `StatusCode.OK`.
14+
([#1902](https://github.com/open-telemetry/opentelemetry-python/pull/1902))
1215

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

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,14 @@ def is_recording(self) -> bool:
806806

807807
@_check_span_ended
808808
def set_status(self, status: trace_api.Status) -> None:
809+
# Ignore future calls if status is already set to OK
810+
# Ignore calls to set to StatusCode.UNSET
811+
if (
812+
self._status
813+
and self._status.status_code is StatusCode.OK
814+
or status.status_code is StatusCode.UNSET
815+
):
816+
return
809817
self._status = status
810818

811819
def __exit__(

opentelemetry-sdk/tests/trace/test_trace.py

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,6 @@ def error_status_test(context):
913913
with self.assertRaises(AssertionError):
914914
with context as root:
915915
raise AssertionError("unknown")
916-
917916
self.assertIs(root.status.status_code, StatusCode.ERROR)
918917
self.assertEqual(
919918
root.status.description, "AssertionError: unknown"
@@ -928,18 +927,53 @@ def error_status_test(context):
928927
.start_as_current_span("root")
929928
)
930929

931-
def test_last_status_wins(self):
930+
def test_status_cannot_override_ok(self):
932931
def error_status_test(context):
933932
with self.assertRaises(AssertionError):
934933
with context as root:
935934
root.set_status(trace_api.status.Status(StatusCode.OK))
936935
raise AssertionError("unknown")
936+
self.assertIs(root.status.status_code, StatusCode.OK)
937+
self.assertIsNone(root.status.description)
938+
939+
error_status_test(
940+
trace.TracerProvider().get_tracer(__name__).start_span("root")
941+
)
942+
error_status_test(
943+
trace.TracerProvider()
944+
.get_tracer(__name__)
945+
.start_as_current_span("root")
946+
)
937947

948+
def test_status_cannot_set_unset(self):
949+
def unset_status_test(context):
950+
with self.assertRaises(AssertionError):
951+
with context as root:
952+
raise AssertionError("unknown")
953+
root.set_status(trace_api.status.Status(StatusCode.UNSET))
938954
self.assertIs(root.status.status_code, StatusCode.ERROR)
939955
self.assertEqual(
940956
root.status.description, "AssertionError: unknown"
941957
)
942958

959+
unset_status_test(
960+
trace.TracerProvider().get_tracer(__name__).start_span("root")
961+
)
962+
unset_status_test(
963+
trace.TracerProvider()
964+
.get_tracer(__name__)
965+
.start_as_current_span("root")
966+
)
967+
968+
def test_last_status_wins(self):
969+
def error_status_test(context):
970+
with self.assertRaises(AssertionError):
971+
with context as root:
972+
raise AssertionError("unknown")
973+
root.set_status(trace_api.status.Status(StatusCode.OK))
974+
self.assertIs(root.status.status_code, StatusCode.OK)
975+
self.assertIsNone(root.status.description)
976+
943977
error_status_test(
944978
trace.TracerProvider().get_tracer(__name__).start_span("root")
945979
)

0 commit comments

Comments
 (0)