Skip to content

Commit b0ff6dc

Browse files
fix(exporter): convert body to str as fallback
1 parent db2dd1e commit b0ff6dc

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
([#4494](https://github.com/open-telemetry/opentelemetry-python/pull/4494))
1818
- Improve CI by cancelling stale runs and setting timeouts
1919
([#4498](https://github.com/open-telemetry/opentelemetry-python/pull/4498))
20+
- Fix logging of objects which are convertible to strings
21+
([#4510](https://github.com/open-telemetry/opentelemetry-python/pull/4510))
2022

2123
## Version 1.31.0/0.52b0 (2025-03-12)
2224

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,9 @@ def _encode_resource(resource: Resource) -> PB2Resource:
7070

7171

7272
def _encode_value(
73-
value: Any, allow_null: bool = False
73+
value: Any,
74+
allow_null: bool = False,
75+
fallback: Optional[Callable[[Any], Any]] = None,
7476
) -> Optional[PB2AnyValue]:
7577
if allow_null is True and value is None:
7678
return None
@@ -99,6 +101,8 @@ def _encode_value(
99101
]
100102
)
101103
)
104+
elif fallback is not None:
105+
return _encode_value(fallback(value), allow_null)
102106
raise Exception(f"Invalid type {type(value)} of value {value}")
103107

104108

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ def _encode_log(log_data: LogData) -> PB2LogRecord:
5555
span_id=span_id,
5656
trace_id=trace_id,
5757
flags=int(log_data.log_record.trace_flags),
58-
body=_encode_value(body, allow_null=True),
58+
body=_encode_value(body, allow_null=True, fallback=str),
5959
severity_text=log_data.log_record.severity_text,
6060
attributes=_encode_attributes(log_data.log_record.attributes),
6161
dropped_attributes_count=log_data.log_record.dropped_attributes,

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

+15
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,21 @@ def test_encode_no_body(self):
6969

7070
self.assertEqual(encode_logs(sdk_logs), expected_encoding)
7171

72+
def test_encode_stringifiable(self):
73+
class Stringifiable:
74+
def __init__(self, data):
75+
self.data = data
76+
def __str__(self):
77+
return self.data
78+
79+
sdk_logs, expected_encoding = self.get_test_logs()
80+
for log in sdk_logs:
81+
body = log.log_record.body
82+
if isinstance(body, str):
83+
log.log_record.body = Stringifiable(body)
84+
85+
self.assertEqual(encode_logs(sdk_logs), expected_encoding)
86+
7287
def test_dropped_attributes_count(self):
7388
sdk_logs = self._get_test_logs_dropped_attributes()
7489
encoded_logs = encode_logs(sdk_logs)

0 commit comments

Comments
 (0)