From 3a66708ac5493e6c972371bd5d354545b62a235d Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Sat, 4 Sep 2021 16:25:53 +0530 Subject: [PATCH 1/3] Do not count invalid attributes for dropped --- .../src/opentelemetry/attributes/__init__.py | 12 ++++++------ .../tests/attributes/test_attributes.py | 3 +++ 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/attributes/__init__.py b/opentelemetry-api/src/opentelemetry/attributes/__init__.py index a266839326a..8acd44e7e5b 100644 --- a/opentelemetry-api/src/opentelemetry/attributes/__init__.py +++ b/opentelemetry-api/src/opentelemetry/attributes/__init__.py @@ -172,14 +172,14 @@ def __setitem__(self, key, value): self.dropped += 1 return - if key in self._dict: - del self._dict[key] - elif self.maxlen is not None and len(self._dict) == self.maxlen: - del self._dict[next(iter(self._dict.keys()))] - self.dropped += 1 - value = _clean_attribute(key, value, self.max_value_len) if value is not None: + if key in self._dict: + del self._dict[key] + elif self.maxlen is not None and len(self._dict) == self.maxlen: + self._dict.popitem(last=False) + self.dropped += 1 + self._dict[key] = value def __delitem__(self, key): diff --git a/opentelemetry-api/tests/attributes/test_attributes.py b/opentelemetry-api/tests/attributes/test_attributes.py index fa0606b347c..2af2a0ff5b9 100644 --- a/opentelemetry-api/tests/attributes/test_attributes.py +++ b/opentelemetry-api/tests/attributes/test_attributes.py @@ -137,6 +137,9 @@ def test_bounded_dict(self): self.assertEqual(len(bdict), dic_len) self.assertEqual(bdict.dropped, dic_len) + # Invalid values shouldn't be considered for `dropped` + bdict["invalid-seq"] = [None, 1, "2"] + self.assertEqual(bdict.dropped, dic_len) # test that elements in the dict are the new ones for key in self.base: From 49d590c54bd2f911c30645c939c874234a9a1280 Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Sat, 4 Sep 2021 16:33:53 +0530 Subject: [PATCH 2/3] Fix lint --- opentelemetry-api/src/opentelemetry/attributes/__init__.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/opentelemetry-api/src/opentelemetry/attributes/__init__.py b/opentelemetry-api/src/opentelemetry/attributes/__init__.py index 8acd44e7e5b..79abcd4d4a6 100644 --- a/opentelemetry-api/src/opentelemetry/attributes/__init__.py +++ b/opentelemetry-api/src/opentelemetry/attributes/__init__.py @@ -176,7 +176,9 @@ def __setitem__(self, key, value): if value is not None: if key in self._dict: del self._dict[key] - elif self.maxlen is not None and len(self._dict) == self.maxlen: + elif ( + self.maxlen is not None and len(self._dict) == self.maxlen + ): self._dict.popitem(last=False) self.dropped += 1 From 06d144a75905ab014f5fa11959cf511425a5e92a Mon Sep 17 00:00:00 2001 From: Srikanth Chekuri Date: Sat, 4 Sep 2021 16:34:06 +0530 Subject: [PATCH 3/3] Add CHANGELOG --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8bee8bd3cca..8dd5578a693 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - `opentelemetry-semantic-conventions` Update to semantic conventions v1.6.1 ([#2077](https://github.com/open-telemetry/opentelemetry-python/pull/2077)) +- Do not count invalid attributes for dropped + ([#2096](https://github.com/open-telemetry/opentelemetry-python/pull/2096)) - Fix propagation bug caused by counting skipped entries ([#2071](https://github.com/open-telemetry/opentelemetry-python/pull/2071))