Skip to content

Commit eb9fe9c

Browse files
committed
Add changelog, log exception
1 parent 66f9933 commit eb9fe9c

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

opentelemetry-exporter-gcp-logging/CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
## Unreleased
44

5-
- Added support for when a `Mapping[str, bytes]` or `Mapping[str, List[bytes]]` is in `LogRecord.body`.
6-
- Added support for when a `Mapping[str, List[Mapping]]` is in `LogRecord.body`.
5+
- Added support for when a `Mapping[str, bytes]` or `Mapping[str, List[bytes]]` is in `LogRecord.body` or `LogRecord.attributes`.
6+
- Added support for when a `Mapping[str, List[Mapping]]` is in `LogRecord.body` or `LogRecord.attributes`.
77
- Do not call `logging.warning` when `LogRecord.body` is of None type, instead leave `LogEntry.payload` empty.
88
- Update opentelemetry-api/sdk dependencies to 1.3.
99

opentelemetry-exporter-gcp-logging/src/opentelemetry/exporter/cloud_logging/__init__.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def default(self, o: Any) -> Any:
113113
return super().default(o)
114114

115115

116-
def _convert_any_value_to_string(value: Any, debug_location: str) -> str:
116+
def _convert_any_value_to_string(value: Any) -> str:
117117
if isinstance(value, bool):
118118
return "true" if value else "false"
119119
if isinstance(value, bytes):
@@ -122,12 +122,14 @@ def _convert_any_value_to_string(value: Any, debug_location: str) -> str:
122122
return str(value)
123123
if isinstance(value, (list, tuple, Mapping)):
124124
return json.dumps(value, separators=(",", ":"), cls=_GenAiJsonEncoder)
125-
logging.warning(
126-
"Unexpected type %s found in %s, this field will not be added to the LogEntry.",
127-
type(value),
128-
debug_location,
129-
)
130-
return ""
125+
try:
126+
return str(value)
127+
except Exception as exc: # pylint: disable=broad-except
128+
logging.exception(
129+
"Error mapping AnyValue to string, this field will not be added to the LogEntry: %s",
130+
exc,
131+
)
132+
return ""
131133

132134

133135
# Be careful not to mutate original body. Make copies of anything that needs to change.
@@ -189,7 +191,7 @@ def _set_payload_in_log_entry(log_entry: LogEntry, body: AnyValue):
189191
log_entry.text_payload = base64.b64encode(body).decode()
190192
elif body is not None:
191193
log_entry.text_payload = _convert_any_value_to_string(
192-
body, "LogRecord.body"
194+
body
193195
)
194196

195197

@@ -279,7 +281,7 @@ def export(self, batch: Sequence[LogData]):
279281
]
280282
log_entry.labels = {
281283
k: _convert_any_value_to_string(
282-
v, "LogRecord.Attribute {}".format(k)
284+
v
283285
)
284286
for k, v in attributes.items()
285287
}

0 commit comments

Comments
 (0)