From 2929ebd61b683c0453077de70190300874d9b449 Mon Sep 17 00:00:00 2001 From: Aaron Abbott Date: Thu, 14 Nov 2024 21:50:34 -0500 Subject: [PATCH] [release/v1.28.x-0.49bx] Support logs with no body (#4276) (#4282) * Support logs with no body (#4276) * Move cherry-picked changelog entry into Unreleased section --------- Co-authored-by: Adrian Cole <64215+codefromthecrypt@users.noreply.github.com> --- CHANGELOG.md | 5 +++++ .../proto/common/_internal/_log_encoder/__init__.py | 3 ++- .../tests/test_log_encoder.py | 12 ++++++++++++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 306ffa6c6fe..5cb1e1e0f2d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,11 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## Unreleased + +- Fix crash exporting a log record with None body + ([#4276](https://github.com/open-telemetry/opentelemetry-python/pull/4276)) + ## Version 1.28.1/0.49b1 (2024-11-08) - Fix metrics export with exemplar and no context and filtering observable instruments diff --git a/exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/_log_encoder/__init__.py b/exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/_log_encoder/__init__.py index b1f116135e5..7213f89d4a0 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/_log_encoder/__init__.py +++ b/exporter/opentelemetry-exporter-otlp-proto-common/src/opentelemetry/exporter/otlp/proto/common/_internal/_log_encoder/__init__.py @@ -48,13 +48,14 @@ def _encode_log(log_data: LogData) -> PB2LogRecord: if log_data.log_record.trace_id == 0 else _encode_trace_id(log_data.log_record.trace_id) ) + body = log_data.log_record.body return PB2LogRecord( time_unix_nano=log_data.log_record.timestamp, observed_time_unix_nano=log_data.log_record.observed_timestamp, span_id=span_id, trace_id=trace_id, flags=int(log_data.log_record.trace_flags), - body=_encode_value(log_data.log_record.body), + body=_encode_value(body) if body is not None else None, severity_text=log_data.log_record.severity_text, attributes=_encode_attributes(log_data.log_record.attributes), dropped_attributes_count=log_data.log_record.dropped_attributes, diff --git a/exporter/opentelemetry-exporter-otlp-proto-common/tests/test_log_encoder.py b/exporter/opentelemetry-exporter-otlp-proto-common/tests/test_log_encoder.py index 158940585c1..70f4c821c9e 100644 --- a/exporter/opentelemetry-exporter-otlp-proto-common/tests/test_log_encoder.py +++ b/exporter/opentelemetry-exporter-otlp-proto-common/tests/test_log_encoder.py @@ -51,6 +51,18 @@ def test_encode(self): sdk_logs, expected_encoding = self.get_test_logs() self.assertEqual(encode_logs(sdk_logs), expected_encoding) + def test_encode_no_body(self): + sdk_logs, expected_encoding = self.get_test_logs() + for log in sdk_logs: + log.log_record.body = None + + for resource_log in expected_encoding.resource_logs: + for scope_log in resource_log.scope_logs: + for log_record in scope_log.log_records: + log_record.ClearField("body") + + self.assertEqual(encode_logs(sdk_logs), expected_encoding) + def test_dropped_attributes_count(self): sdk_logs = self._get_test_logs_dropped_attributes() encoded_logs = encode_logs(sdk_logs)