From 0c0466e4cd2cfda986e526f833ce1c4f5310d960 Mon Sep 17 00:00:00 2001 From: Owais Lone Date: Fri, 2 Apr 2021 20:47:00 +0530 Subject: [PATCH] Remove unnecessary warning when (not) setting status description (#1721) --- CHANGELOG.md | 2 ++ .../src/opentelemetry/trace/status.py | 18 +++++++++--------- opentelemetry-api/tests/trace/test_status.py | 4 ++-- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ec84c7a5a2e..b22da9c9b95 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Adjust `B3Format` propagator to be spec compliant by not modifying context when propagation headers are not present/invalid/empty ([#1728](https://github.com/open-telemetry/opentelemetry-python/pull/1728)) +- Silence unnecessary warning when creating a new Status object without description. + ([#1721](https://github.com/open-telemetry/opentelemetry-python/pull/1721)) - Update bootstrap cmd to use exact version when installing instrumentation packages. ([#1722](https://github.com/open-telemetry/opentelemetry-python/pull/1722)) diff --git a/opentelemetry-api/src/opentelemetry/trace/status.py b/opentelemetry-api/src/opentelemetry/trace/status.py index 5e0469d71a6..ada7fa1ebda 100644 --- a/opentelemetry-api/src/opentelemetry/trace/status.py +++ b/opentelemetry-api/src/opentelemetry/trace/status.py @@ -49,15 +49,15 @@ 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") - return - - if status_code is not StatusCode.ERROR: - logger.warning( - "description should only be set when status_code is set to StatusCode.ERROR" - ) - return + if description: + if not isinstance(description, str): + logger.warning("Invalid status description type, expected str") + return + if status_code is not StatusCode.ERROR: + logger.warning( + "description should only be set when status_code is set to StatusCode.ERROR" + ) + return self._description = description diff --git a/opentelemetry-api/tests/trace/test_status.py b/opentelemetry-api/tests/trace/test_status.py index fdfcd4e83ed..74da78d6c73 100644 --- a/opentelemetry-api/tests/trace/test_status.py +++ b/opentelemetry-api/tests/trace/test_status.py @@ -30,8 +30,8 @@ def test_constructor(self): def test_invalid_description(self): with self.assertLogs(level=WARNING) as warning: - status = Status(description={"test": "val"}) # type: ignore - self.assertIs(status.status_code, StatusCode.UNSET) + status = Status(status_code=StatusCode.ERROR, description={"test": "val"}) # type: ignore + self.assertIs(status.status_code, StatusCode.ERROR) self.assertEqual(status.description, None) self.assertIn( "Invalid status description type, expected str",