Skip to content

Commit

Permalink
bugfix: Fix for byte type attributes crashing spans (#775)
Browse files Browse the repository at this point in the history
  • Loading branch information
sethmaxwl authored Jun 5, 2020
1 parent 91f656f commit b108596
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
2 changes: 2 additions & 0 deletions opentelemetry-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
([#751](https://github.com/open-telemetry/opentelemetry-python/pull/751))
- Rename Measure to ValueRecorder in metrics
([#761](https://github.com/open-telemetry/opentelemetry-python/pull/761))
- bugfix: byte type attributes are decoded before adding to attributes dict
([#775](https://github.com/open-telemetry/opentelemetry-python/pull/775))

## 0.8b0

Expand Down
6 changes: 6 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,12 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None:
# Freeze mutable sequences defensively
if isinstance(value, MutableSequence):
value = tuple(value)
if isinstance(value, bytes):
try:
value = value.decode()
except ValueError:
logger.warning("Byte attribute could not be decoded.")
return
with self._lock:
self.attributes[key] = value

Expand Down
16 changes: 16 additions & 0 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,22 @@ def test_invalid_attribute_values(self):

self.assertEqual(len(root.attributes), 0)

def test_byte_type_attribute_value(self):
with self.tracer.start_as_current_span("root") as root:
with self.assertLogs(level=WARNING):
root.set_attribute(
"invalid-byte-type-attribute",
b"\xd8\xe1\xb7\xeb\xa8\xe5 \xd2\xb7\xe1",
)
self.assertFalse(
"invalid-byte-type-attribute" in root.attributes
)

root.set_attribute("valid-byte-type-attribute", b"valid byte")
self.assertTrue(
isinstance(root.attributes["valid-byte-type-attribute"], str)
)

def test_check_attribute_helper(self):
# pylint: disable=protected-access
self.assertFalse(trace._is_valid_attribute_value([1, 2, 3.4, "ss", 4]))
Expand Down

0 comments on commit b108596

Please sign in to comment.