Skip to content

Commit 1e61922

Browse files
committed
exporter/otlp: allow export of none values in logs attributes
1 parent 879ab21 commit 1e61922

File tree

3 files changed

+80
-11
lines changed

3 files changed

+80
-11
lines changed

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
)
4646
from opentelemetry.sdk.trace import Resource
4747
from opentelemetry.sdk.util.instrumentation import InstrumentationScope
48-
from opentelemetry.util.types import Attributes
48+
from opentelemetry.util.types import _ExtendedAttributes
4949

5050
_logger = logging.getLogger(__name__)
5151

@@ -136,14 +136,17 @@ def _encode_trace_id(trace_id: int) -> bytes:
136136

137137

138138
def _encode_attributes(
139-
attributes: Attributes,
139+
attributes: _ExtendedAttributes,
140+
allow_null: bool = False,
140141
) -> Optional[List[PB2KeyValue]]:
141142
if attributes:
142143
pb2_attributes = []
143144
for key, value in attributes.items():
144145
# pylint: disable=broad-exception-caught
145146
try:
146-
pb2_attributes.append(_encode_key_value(key, value))
147+
pb2_attributes.append(
148+
_encode_key_value(key, value, allow_null=allow_null)
149+
)
147150
except Exception as error:
148151
_logger.exception("Failed to encode key %s: %s", key, error)
149152
else:

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ def _encode_log(log_data: LogData) -> PB2LogRecord:
5757
flags=int(log_data.log_record.trace_flags),
5858
body=_encode_value(body, allow_null=True),
5959
severity_text=log_data.log_record.severity_text,
60-
attributes=_encode_attributes(log_data.log_record.attributes),
60+
attributes=_encode_attributes(
61+
log_data.log_record.attributes, allow_null=True
62+
),
6163
dropped_attributes_count=log_data.log_record.dropped_attributes,
6264
severity_number=log_data.log_record.severity_number.value,
6365
)

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

+71-7
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,28 @@ def _get_sdk_log_data() -> List[LogData]:
225225
),
226226
)
227227

228-
return [log1, log2, log3, log4, log5, log6, log7]
228+
log8 = LogData(
229+
log_record=SDKLogRecord(
230+
timestamp=1644650584292683044,
231+
observed_timestamp=1644650584292683044,
232+
trace_id=212592107417388365804938480559624925566,
233+
span_id=6077757853989569466,
234+
trace_flags=TraceFlags(0x01),
235+
severity_text="INFO",
236+
severity_number=SeverityNumber.INFO,
237+
body="Test export of extended attributes",
238+
resource=SDKResource({}),
239+
attributes={
240+
"extended": {
241+
"sequence": [{"inner": "mapping", "none": None}]
242+
}
243+
},
244+
),
245+
instrumentation_scope=InstrumentationScope(
246+
"extended_name", "extended_version"
247+
),
248+
)
249+
return [log1, log2, log3, log4, log5, log6, log7, log8]
229250

230251
def get_test_logs(
231252
self,
@@ -265,7 +286,8 @@ def get_test_logs(
265286
"Do not go gentle into that good night. Rage, rage against the dying of the light"
266287
),
267288
attributes=_encode_attributes(
268-
{"a": 1, "b": "c"}
289+
{"a": 1, "b": "c"},
290+
allow_null=True,
269291
),
270292
)
271293
],
@@ -295,7 +317,8 @@ def get_test_logs(
295317
{
296318
"filename": "model.py",
297319
"func_name": "run_method",
298-
}
320+
},
321+
allow_null=True,
299322
),
300323
)
301324
],
@@ -326,7 +349,8 @@ def get_test_logs(
326349
{
327350
"filename": "model.py",
328351
"func_name": "run_method",
329-
}
352+
},
353+
allow_null=True,
330354
),
331355
)
332356
],
@@ -336,7 +360,8 @@ def get_test_logs(
336360
name="scope_with_attributes",
337361
version="scope_with_attributes_version",
338362
attributes=_encode_attributes(
339-
{"one": 1, "two": "2"}
363+
{"one": 1, "two": "2"},
364+
allow_null=True,
340365
),
341366
),
342367
schema_url="instrumentation_schema_url",
@@ -360,7 +385,8 @@ def get_test_logs(
360385
{
361386
"filename": "model.py",
362387
"func_name": "run_method",
363-
}
388+
},
389+
allow_null=True,
364390
),
365391
)
366392
],
@@ -416,7 +442,8 @@ def get_test_logs(
416442
severity_number=SeverityNumber.DEBUG.value,
417443
body=_encode_value("To our galaxy"),
418444
attributes=_encode_attributes(
419-
{"a": 1, "b": "c"}
445+
{"a": 1, "b": "c"},
446+
allow_null=True,
420447
),
421448
),
422449
],
@@ -471,6 +498,43 @@ def get_test_logs(
471498
),
472499
],
473500
),
501+
PB2ScopeLogs(
502+
scope=PB2InstrumentationScope(
503+
name="extended_name",
504+
version="extended_version",
505+
),
506+
log_records=[
507+
PB2LogRecord(
508+
time_unix_nano=1644650584292683044,
509+
observed_time_unix_nano=1644650584292683044,
510+
trace_id=_encode_trace_id(
511+
212592107417388365804938480559624925566
512+
),
513+
span_id=_encode_span_id(
514+
6077757853989569466,
515+
),
516+
flags=int(TraceFlags(0x01)),
517+
severity_text="INFO",
518+
severity_number=SeverityNumber.INFO.value,
519+
body=_encode_value(
520+
"Test export of extended attributes"
521+
),
522+
attributes=_encode_attributes(
523+
{
524+
"extended": {
525+
"sequence": [
526+
{
527+
"inner": "mapping",
528+
"none": None,
529+
}
530+
]
531+
}
532+
},
533+
allow_null=True,
534+
),
535+
),
536+
],
537+
),
474538
],
475539
),
476540
]

0 commit comments

Comments
 (0)