From bd753c9cb07a047eb711c4fcc382da181817293f Mon Sep 17 00:00:00 2001 From: Mike Zou Date: Fri, 7 Feb 2020 15:54:24 -0800 Subject: [PATCH 1/7] Add conversion from sequence to immutable tuple --- .../src/opentelemetry/sdk/trace/__init__.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index e429467061..4c8c1d4e34 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -236,6 +236,9 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None: if error_message is not None: logger.warning("%s in attribute value sequence", error_message) return + else: + # convert value : sequence into immutable tuple lest client changes it. + value_tuple = tuple(value) elif not isinstance(value, (bool, str, Number, Sequence)): logger.warning("invalid type for attribute value") return @@ -258,9 +261,9 @@ def _check_attribute_value_sequence(sequence: Sequence) -> Optional[str]: if first_element_type not in (bool, str, Number): return "invalid type" - for element in sequence: - if not isinstance(element, first_element_type): - return "different type" + if any(not isinstance(element, first_element_type) for element in sequence): + return "different type" + return None def add_event( From 701bb1a11dd865a379bd94c64f5a93ab9daadf29 Mon Sep 17 00:00:00 2001 From: Mike Zou Date: Fri, 7 Feb 2020 16:24:10 -0800 Subject: [PATCH 2/7] remove space --- opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py | 1 - 1 file changed, 1 deletion(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 4c8c1d4e34..4f74a0b7d5 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -242,7 +242,6 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None: elif not isinstance(value, (bool, str, Number, Sequence)): logger.warning("invalid type for attribute value") return - self.attributes[key] = value @staticmethod From 6dc1e0a0de4c4dcf62124c7b08c720856916bb77 Mon Sep 17 00:00:00 2001 From: Mike Zou Date: Fri, 7 Feb 2020 16:28:00 -0800 Subject: [PATCH 3/7] remove style changes for checking all types in sequence are same --- opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 4f74a0b7d5..98dcea9340 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -238,10 +238,11 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None: return else: # convert value : sequence into immutable tuple lest client changes it. - value_tuple = tuple(value) + value = tuple(value) elif not isinstance(value, (bool, str, Number, Sequence)): logger.warning("invalid type for attribute value") return + self.attributes[key] = value @staticmethod @@ -260,8 +261,9 @@ def _check_attribute_value_sequence(sequence: Sequence) -> Optional[str]: if first_element_type not in (bool, str, Number): return "invalid type" - if any(not isinstance(element, first_element_type) for element in sequence): - return "different type" + for element in sequence: + if not isinstance(element, first_element_type): + return "different type" return None From ad1f0bbdb4cfd7fc81a3b3eb265194eab03e875e Mon Sep 17 00:00:00 2001 From: Mike Zou Date: Fri, 7 Feb 2020 16:48:47 -0800 Subject: [PATCH 4/7] Fix bug where str gets converted to tuple as a sequence --- opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 98dcea9340..f9c7863529 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -236,7 +236,7 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None: if error_message is not None: logger.warning("%s in attribute value sequence", error_message) return - else: + elif not isinstance(value, str): # convert value : sequence into immutable tuple lest client changes it. value = tuple(value) elif not isinstance(value, (bool, str, Number, Sequence)): From 897865a0e16bd1de5a0c73c9dc36f77d94d1c7c5 Mon Sep 17 00:00:00 2001 From: Mike Zou Date: Fri, 7 Feb 2020 17:14:04 -0800 Subject: [PATCH 5/7] Update test_attributes to return list values as tuples after conversion --- opentelemetry-sdk/tests/trace/test_trace.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/opentelemetry-sdk/tests/trace/test_trace.py b/opentelemetry-sdk/tests/trace/test_trace.py index 449fb51e8e..8fba182d5e 100644 --- a/opentelemetry-sdk/tests/trace/test_trace.py +++ b/opentelemetry-sdk/tests/trace/test_trace.py @@ -386,12 +386,12 @@ def test_attributes(self): self.assertEqual(root.attributes["http.status_text"], "OK") self.assertEqual(root.attributes["misc.pi"], 3.14) self.assertEqual(root.attributes["attr-key"], "attr-value2") - self.assertEqual(root.attributes["empty-list"], []) + self.assertEqual(root.attributes["empty-list"], ()) self.assertEqual( - root.attributes["list-of-bools"], [True, True, False] + root.attributes["list-of-bools"], (True, True, False) ) self.assertEqual( - root.attributes["list-of-numerics"], [123, 3.14, 0] + root.attributes["list-of-numerics"], (123, 3.14, 0) ) attributes = { From a41eb3d19802881325840ea09799ef341b2cb6d2 Mon Sep 17 00:00:00 2001 From: Chris Kleinknecht Date: Mon, 10 Feb 2020 12:03:24 -0800 Subject: [PATCH 6/7] Lint --- opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index f9c7863529..31f0ca2d41 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -264,7 +264,7 @@ def _check_attribute_value_sequence(sequence: Sequence) -> Optional[str]: for element in sequence: if not isinstance(element, first_element_type): return "different type" - + return None def add_event( From d3ff178416d1e25097b67c89784e9a44ef3f1986 Mon Sep 17 00:00:00 2001 From: Chris Kleinknecht Date: Thu, 13 Feb 2020 13:45:53 -0800 Subject: [PATCH 7/7] Lint --- opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 31f0ca2d41..5b00370766 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -236,7 +236,7 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None: if error_message is not None: logger.warning("%s in attribute value sequence", error_message) return - elif not isinstance(value, str): + if not isinstance(value, str): # convert value : sequence into immutable tuple lest client changes it. value = tuple(value) elif not isinstance(value, (bool, str, Number, Sequence)):