Skip to content

Commit b108596

Browse files
authored
bugfix: Fix for byte type attributes crashing spans (#775)
1 parent 91f656f commit b108596

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

opentelemetry-sdk/CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
([#751](https://github.com/open-telemetry/opentelemetry-python/pull/751))
77
- Rename Measure to ValueRecorder in metrics
88
([#761](https://github.com/open-telemetry/opentelemetry-python/pull/761))
9+
- bugfix: byte type attributes are decoded before adding to attributes dict
10+
([#775](https://github.com/open-telemetry/opentelemetry-python/pull/775))
911

1012
## 0.8b0
1113

opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

+6
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,12 @@ def set_attribute(self, key: str, value: types.AttributeValue) -> None:
424424
# Freeze mutable sequences defensively
425425
if isinstance(value, MutableSequence):
426426
value = tuple(value)
427+
if isinstance(value, bytes):
428+
try:
429+
value = value.decode()
430+
except ValueError:
431+
logger.warning("Byte attribute could not be decoded.")
432+
return
427433
with self._lock:
428434
self.attributes[key] = value
429435

opentelemetry-sdk/tests/trace/test_trace.py

+16
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,22 @@ def test_invalid_attribute_values(self):
487487

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

490+
def test_byte_type_attribute_value(self):
491+
with self.tracer.start_as_current_span("root") as root:
492+
with self.assertLogs(level=WARNING):
493+
root.set_attribute(
494+
"invalid-byte-type-attribute",
495+
b"\xd8\xe1\xb7\xeb\xa8\xe5 \xd2\xb7\xe1",
496+
)
497+
self.assertFalse(
498+
"invalid-byte-type-attribute" in root.attributes
499+
)
500+
501+
root.set_attribute("valid-byte-type-attribute", b"valid byte")
502+
self.assertTrue(
503+
isinstance(root.attributes["valid-byte-type-attribute"], str)
504+
)
505+
490506
def test_check_attribute_helper(self):
491507
# pylint: disable=protected-access
492508
self.assertFalse(trace._is_valid_attribute_value([1, 2, 3.4, "ss", 4]))

0 commit comments

Comments
 (0)