Skip to content

Commit 1570b48

Browse files
committed
Keep behavior the same for None values in list with allow_null=False
1 parent 08ead05 commit 1570b48

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/__init__.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ def _encode_key_value(
110110
def _encode_array(
111111
array: Sequence[Any], allow_null: bool = False
112112
) -> Sequence[PB2AnyValue]:
113+
if not allow_null:
114+
# Let the exception get raised by _encode_value()
115+
return [_encode_value(v, allow_null=allow_null) for v in array]
116+
113117
return [
114118
_encode_value(v, allow_null=allow_null)
115119
if v is not None

exporter/opentelemetry-exporter-otlp-proto-common/tests/test_attribute_encoder.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,24 @@ def test_encode_attributes_all_kinds(self):
8888
],
8989
)
9090

91+
def test_encode_attributes_error_list_none(self):
92+
with self.assertLogs(level=ERROR) as error:
93+
result = _encode_attributes(
94+
{"a": 1, "bad_key": ["test", None, "test"], "b": 2}
95+
)
96+
97+
self.assertEqual(len(error.records), 1)
98+
self.assertEqual(error.records[0].msg, "Failed to encode key %s: %s")
99+
self.assertEqual(error.records[0].args[0], "bad_key")
100+
self.assertIsInstance(error.records[0].args[1], Exception)
101+
self.assertEqual(
102+
result,
103+
[
104+
PB2KeyValue(key="a", value=PB2AnyValue(int_value=1)),
105+
PB2KeyValue(key="b", value=PB2AnyValue(int_value=2)),
106+
],
107+
)
108+
91109
def test_encode_attributes_error_logs_key(self):
92110
with self.assertLogs(level=ERROR) as error:
93111
result = _encode_attributes({"a": 1, "bad_key": None, "b": 2})

0 commit comments

Comments
 (0)