diff --git a/CHANGELOG.md b/CHANGELOG.md index 81c56db3798..bb00fce8d4c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v0.18b0...HEAD) +- Status now only sets `description` when `status_code` is set to `StatusCode.ERROR` + ([#1673])(https://github.com/open-telemetry/opentelemetry-python/pull/1673) ### Added - Add `max_attr_value_length` support to Jaeger exporter diff --git a/opentelemetry-api/src/opentelemetry/trace/status.py b/opentelemetry-api/src/opentelemetry/trace/status.py index 822d3ca83d6..0ab6a7c8d54 100644 --- a/opentelemetry-api/src/opentelemetry/trace/status.py +++ b/opentelemetry-api/src/opentelemetry/trace/status.py @@ -48,10 +48,16 @@ def __init__( ): self._status_code = status_code self._description = None + if description is not None and not isinstance(description, str): logger.warning("Invalid status description type, expected str") - else: - self._description = description + return + + if status_code != StatusCode.ERROR: + logger.warning("description should only be set when status_code is set to StatusCode.ERROR") + return + + self._description = description @property def status_code(self) -> StatusCode: diff --git a/opentelemetry-api/tests/trace/test_status.py b/opentelemetry-api/tests/trace/test_status.py index cd0f678d13b..f97aabe819d 100644 --- a/opentelemetry-api/tests/trace/test_status.py +++ b/opentelemetry-api/tests/trace/test_status.py @@ -29,7 +29,25 @@ def test_constructor(self): self.assertEqual(status.description, "unavailable") def test_invalid_description(self): - with self.assertLogs(level=WARNING): + with self.assertLogs(level=WARNING) as warning: status = Status(description={"test": "val"}) # type: ignore self.assertIs(status.status_code, StatusCode.UNSET) self.assertEqual(status.description, None) + self.assertIn("Invalid status description type, expected str", warning.output[0]) + + def test_description_and_non_error_status(self): + with self.assertLogs(level=WARNING) as warning: + status = Status(status_code=StatusCode.OK, description="status description") + self.assertIs(status.status_code, StatusCode.OK) + self.assertEqual(status.description, None) + self.assertIn("description should only be set when status_code is set to StatusCode.ERROR", warning.output[0]) + + with self.assertLogs(level=WARNING) as warning: + status = Status(status_code=StatusCode.UNSET, description="status description") + self.assertIs(status.status_code, StatusCode.UNSET) + self.assertEqual(status.description, None) + self.assertIn("description should only be set when status_code is set to StatusCode.ERROR", warning.output[0]) + + status = Status(status_code=StatusCode.ERROR, description="status description") + self.assertIs(status.status_code, StatusCode.ERROR) + self.assertEqual(status.description, "status description")