From dcc3c989cf6666c9e0596cd82286c4ee75c5d71a Mon Sep 17 00:00:00 2001 From: Owais Lone Date: Sun, 28 Mar 2021 09:06:05 +0530 Subject: [PATCH] Remove unnecessary warning when (not) setting status description Currently we log a warning that status description should only be set for an error condition even when description is not being set. This commit silences such warnings. --- CHANGELOG.md | 3 +++ .../src/opentelemetry/trace/status.py | 18 +++++++++--------- opentelemetry-api/tests/trace/test_status.py | 4 ++-- 3 files changed, 14 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2eed844b5a2..d626a72e992 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.0.0...HEAD) +- Silence unnecessary warning when creating a new Status object without description. + ([#1721](https://github.com/open-telemetry/opentelemetry-python/pull/1721)) + ## [1.0.0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.0.0) - 2021-03-26 ### Added 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",