Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restrict attribute keys to non-empty strings #2057

Merged
merged 1 commit into from
Aug 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,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` Fixed bugs (#2041, #2042 & #2045) in Span Limits
([#2044](https://github.com/open-telemetry/opentelemetry-python/pull/2044))
- `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