Skip to content

Commit 9c03467

Browse files
committed
Span Status: only set description for ERROR status code.
According to the spec: > Description MUST only be used with the Error StatusCode value. > An empty Description is equivalent with a not present one. https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/api.md#set-status This commit updates the `Status` implementation to comply with the spec.
1 parent 1af9f87 commit 9c03467

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

77
## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v0.18b0...HEAD)
8+
- Status now only sets `description` when `status_code` is set to `StatusCode.ERROR`
9+
([#1673])(https://github.com/open-telemetry/opentelemetry-python/pull/1673)
810

911
### Added
1012
- Add `max_attr_value_length` support to Jaeger exporter

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

+8-2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,16 @@ def __init__(
4848
):
4949
self._status_code = status_code
5050
self._description = None
51+
5152
if description is not None and not isinstance(description, str):
5253
logger.warning("Invalid status description type, expected str")
53-
else:
54-
self._description = description
54+
return
55+
56+
if status_code != StatusCode.ERROR:
57+
logger.warning("description should only be set when status_code is set to StatusCode.ERROR")
58+
return
59+
60+
self._description = description
5561

5662
@property
5763
def status_code(self) -> StatusCode:

opentelemetry-api/tests/trace/test_status.py

+19-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,25 @@ def test_constructor(self):
2929
self.assertEqual(status.description, "unavailable")
3030

3131
def test_invalid_description(self):
32-
with self.assertLogs(level=WARNING):
32+
with self.assertLogs(level=WARNING) as warning:
3333
status = Status(description={"test": "val"}) # type: ignore
3434
self.assertIs(status.status_code, StatusCode.UNSET)
3535
self.assertEqual(status.description, None)
36+
self.assertIn("Invalid status description type, expected str", warning.output[0])
37+
38+
def test_description_and_non_error_status(self):
39+
with self.assertLogs(level=WARNING) as warning:
40+
status = Status(status_code=StatusCode.OK, description="status description")
41+
self.assertIs(status.status_code, StatusCode.OK)
42+
self.assertEqual(status.description, None)
43+
self.assertIn("description should only be set when status_code is set to StatusCode.ERROR", warning.output[0])
44+
45+
with self.assertLogs(level=WARNING) as warning:
46+
status = Status(status_code=StatusCode.UNSET, description="status description")
47+
self.assertIs(status.status_code, StatusCode.UNSET)
48+
self.assertEqual(status.description, None)
49+
self.assertIn("description should only be set when status_code is set to StatusCode.ERROR", warning.output[0])
50+
51+
status = Status(status_code=StatusCode.ERROR, description="status description")
52+
self.assertIs(status.status_code, StatusCode.ERROR)
53+
self.assertEqual(status.description, "status description")

0 commit comments

Comments
 (0)