Skip to content

Commit

Permalink
Merge branch 'main' into fix-2051
Browse files Browse the repository at this point in the history
  • Loading branch information
owais authored Aug 19, 2021
2 parents 4bbfb01 + 653207d commit bb5096d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#2044](https://github.com/open-telemetry/opentelemetry-python/pull/2044))
- `opentelemetry-sdk` Add support for `OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT` env var
([#2056](https://github.com/open-telemetry/opentelemetry-python/pull/2056))
- `opentelemetry-api` Attribute keys must be non-empty strings.
([#2057](https://github.com/open-telemetry/opentelemetry-python/pull/2057))

## [0.23.1](https://github.com/open-telemetry/opentelemetry-python/pull/1987) - 2021-07-26

Expand Down
6 changes: 3 additions & 3 deletions opentelemetry-api/src/opentelemetry/attributes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ def _clean_attribute(
- It needs to be encoded/decoded e.g, bytes to strings.
"""

if key is None or key == "":
_logger.warning("invalid key `%s` (empty or null)", key)
if not (key and isinstance(key, str)):
_logger.warning("invalid key `%s`. must be non-empty string.", key)
return None

if isinstance(value, _VALID_ATTR_VALUE_TYPES):
Expand Down Expand Up @@ -118,7 +118,7 @@ def _clean_attribute_value(
if isinstance(value, bytes):
try:
value = value.decode()
except ValueError:
except UnicodeDecodeError:
_logger.warning("Byte attribute could not be decoded.")
return None

Expand Down
14 changes: 12 additions & 2 deletions opentelemetry-api/tests/attributes/test_attributes.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ def assertValid(self, value, key="k"):
def assertInvalid(self, value, key="k"):
self.assertIsNone(_clean_attribute(key, value, None))

def test_attribute_key_validation(self):
# only non-empty strings are valid keys
self.assertInvalid(1, "")
self.assertInvalid(1, 1)
self.assertInvalid(1, {})
self.assertInvalid(1, [])
self.assertInvalid(1, b"1")
self.assertValid(1, "k")
self.assertValid(1, "1")

def test_clean_attribute(self):
self.assertInvalid([1, 2, 3.4, "ss", 4])
self.assertInvalid([dict(), 1, 2, 3.4, 4])
Expand Down Expand Up @@ -142,10 +152,10 @@ def test_bounded_dict(self):
def test_no_limit_code(self):
bdict = BoundedAttributes(maxlen=None, immutable=False)
for num in range(100):
bdict[num] = num
bdict[str(num)] = num

for num in range(100):
self.assertEqual(bdict[num], num)
self.assertEqual(bdict[str(num)], num)

def test_immutable(self):
bdict = BoundedAttributes()
Expand Down

0 comments on commit bb5096d

Please sign in to comment.