Skip to content

Commit d418c60

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 d418c60

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-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

+10-2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,18 @@ 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(
58+
"description should only be set when status_code is set to StatusCode.ERROR"
59+
)
60+
return
61+
62+
self._description = description
5563

5664
@property
5765
def status_code(self) -> StatusCode:

opentelemetry-api/tests/trace/test_status.py

+34-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,40 @@ 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(
37+
"Invalid status description type, expected str",
38+
warning.output[0],
39+
)
40+
41+
def test_description_and_non_error_status(self):
42+
with self.assertLogs(level=WARNING) as warning:
43+
status = Status(
44+
status_code=StatusCode.OK, description="status description"
45+
)
46+
self.assertIs(status.status_code, StatusCode.OK)
47+
self.assertEqual(status.description, None)
48+
self.assertIn(
49+
"description should only be set when status_code is set to StatusCode.ERROR",
50+
warning.output[0],
51+
)
52+
53+
with self.assertLogs(level=WARNING) as warning:
54+
status = Status(
55+
status_code=StatusCode.UNSET, description="status description"
56+
)
57+
self.assertIs(status.status_code, StatusCode.UNSET)
58+
self.assertEqual(status.description, None)
59+
self.assertIn(
60+
"description should only be set when status_code is set to StatusCode.ERROR",
61+
warning.output[0],
62+
)
63+
64+
status = Status(
65+
status_code=StatusCode.ERROR, description="status description"
66+
)
67+
self.assertIs(status.status_code, StatusCode.ERROR)
68+
self.assertEqual(status.description, "status description")

0 commit comments

Comments
 (0)