Skip to content

Commit 75a9630

Browse files
committed
Fix case where a list contains None values and make tests more clear
1 parent 18d51bb commit 75a9630

File tree

2 files changed

+46
-5
lines changed

2 files changed

+46
-5
lines changed

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ def _encode_value(
8484
if isinstance(value, Sequence):
8585
return PB2AnyValue(
8686
array_value=PB2ArrayValue(
87-
values=[_encode_value(v, allow_null=allow_null) for v in value]
87+
values=_encode_array(value, allow_null=allow_null)
8888
)
8989
)
9090
elif isinstance(value, Mapping):
@@ -99,12 +99,27 @@ def _encode_value(
9999
raise Exception(f"Invalid type {type(value)} of value {value}")
100100

101101

102-
def _encode_key_value(key: str, value: Any, allow_null: bool = False) -> PB2KeyValue:
102+
def _encode_key_value(
103+
key: str, value: Any, allow_null: bool = False
104+
) -> PB2KeyValue:
103105
return PB2KeyValue(
104106
key=key, value=_encode_value(value, allow_null=allow_null)
105107
)
106108

107109

110+
def _encode_array(
111+
array: Sequence[Any], allow_null: bool = False
112+
) -> Sequence[PB2AnyValue]:
113+
return [
114+
_encode_value(v, allow_null=allow_null)
115+
if v is not None
116+
# Use an empty AnyValue to respresent None in an array. Behavior may change pending
117+
# https://github.com/open-telemetry/opentelemetry-specification/issues/4392
118+
else PB2AnyValue()
119+
for v in array
120+
]
121+
122+
108123
def _encode_span_id(span_id: int) -> bytes:
109124
return span_id.to_bytes(length=8, byteorder="big", signed=False)
110125

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

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,16 @@
2727
ExportLogsServiceRequest,
2828
)
2929
from opentelemetry.proto.common.v1.common_pb2 import AnyValue as PB2AnyValue
30+
from opentelemetry.proto.common.v1.common_pb2 import (
31+
ArrayValue as PB2ArrayValue,
32+
)
3033
from opentelemetry.proto.common.v1.common_pb2 import (
3134
InstrumentationScope as PB2InstrumentationScope,
3235
)
3336
from opentelemetry.proto.common.v1.common_pb2 import KeyValue as PB2KeyValue
37+
from opentelemetry.proto.common.v1.common_pb2 import (
38+
KeyValueList as PB2KeyValueList,
39+
)
3440
from opentelemetry.proto.logs.v1.logs_pb2 import LogRecord as PB2LogRecord
3541
from opentelemetry.proto.logs.v1.logs_pb2 import (
3642
ResourceLogs as PB2ResourceLogs,
@@ -163,7 +169,7 @@ def _get_sdk_log_data() -> List[LogData]:
163169
trace_flags=TraceFlags(0x01),
164170
severity_text="INFO",
165171
severity_number=SeverityNumber.INFO,
166-
body={"error": None},
172+
body={"error": None, "array_with_nones": [1, None, 2]},
167173
resource=SDKResource({}),
168174
attributes={},
169175
),
@@ -326,8 +332,28 @@ def get_test_logs(
326332
flags=int(TraceFlags(0x01)),
327333
severity_text="INFO",
328334
severity_number=SeverityNumber.INFO.value,
329-
body=_encode_value(
330-
{"error": None}, allow_null=True
335+
body=PB2AnyValue(
336+
kvlist_value=PB2KeyValueList(
337+
values=[
338+
PB2KeyValue(key="error"),
339+
PB2KeyValue(
340+
key="array_with_nones",
341+
value=PB2AnyValue(
342+
array_value=PB2ArrayValue(
343+
values=[
344+
PB2AnyValue(
345+
int_value=1
346+
),
347+
PB2AnyValue(),
348+
PB2AnyValue(
349+
int_value=2
350+
),
351+
]
352+
)
353+
),
354+
),
355+
]
356+
)
331357
),
332358
attributes={},
333359
),

0 commit comments

Comments
 (0)