Skip to content

Commit 9024ea4

Browse files
do not json dump primitive types
1 parent 63237d2 commit 9024ea4

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

logfire/_internal/json_encoder.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
from typing import Any, Callable
1717
from uuid import UUID
1818

19+
from opentelemetry.util import types as otel_types
20+
1921
from .utils import JsonValue, safe_repr
2022

2123
NUMPY_DIMENSION_MAX_SIZE = 10
@@ -292,13 +294,17 @@ def to_json_value(o: Any, seen: set[int]) -> JsonValue:
292294
return safe_repr(o)
293295

294296

295-
def logfire_json_dumps(obj: Any) -> str:
297+
def logfire_json_dumps(obj: Any) -> otel_types.AttributeValue:
296298
try:
297-
return json.dumps(obj, default=lambda o: to_json_value(o, set()), separators=(',', ':'))
299+
return json.dumps(obj, separators=(',', ':'))
298300
except Exception:
299-
# fallback to eagerly calling to_json_value to take care of object keys which are not strings
300-
# see https://github.com/pydantic/platform/pull/2045
301-
return json.dumps(to_json_value(obj, set()), separators=(',', ':'))
301+
# fallback: eagerly transform the object via to_json_value to handle
302+
# non-serializable types and non-string dictionary keys.
303+
json_value = to_json_value(obj, set())
304+
if isinstance(json_value, (int, float, str, bool)):
305+
return json_value
306+
307+
return json.dumps(json_value, separators=(',', ':'))
302308

303309

304310
def is_sqlalchemy(obj: Any) -> bool:

tests/test_console_exporter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -973,10 +973,10 @@ class MyIntEnum(int, enum.Enum):
973973
[
974974
'hi',
975975
IsStr(),
976-
'│ d=datetime.datetime(2020, 12, 31, 12, 34, 56)',
976+
"│ d='2020-12-31T12:34:56'",
977977
'│ x=None',
978-
"│ v=Decimal('1.0')",
979-
"│ e=MyEnum('abc')",
978+
'│ v=Decimal(1.0)',
979+
"│ e='abc'",
980980
"│ se=MyStrEnum('str_val')",
981981
'│ ie=MyIntEnum(1)',
982982
]

tests/test_json_args.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,7 +1345,7 @@ def __repr__(self):
13451345
'code.filepath': 'test_json_args.py',
13461346
'code.function': 'test_bad_getattr',
13471347
'code.lineno': 123,
1348-
'a': '"A()"',
1348+
'a': 'A()',
13491349
'logfire.json_schema': '{"type":"object","properties":{"a":{"type":"object","x-python-datatype":"unknown"}}}',
13501350
},
13511351
}
@@ -1413,8 +1413,8 @@ class Bar(Mixin, MagicMock):
14131413
'code.filepath': 'test_json_args.py',
14141414
'code.function': 'test_mock',
14151415
'code.lineno': 123,
1416-
'foo': '"Foo()"',
1417-
'bar': '"Bar()"',
1416+
'foo': 'Foo()',
1417+
'bar': 'Bar()',
14181418
'mock': IsStr(regex=r'^"<Mock id=\'\d+\'>"'),
14191419
'magic_mock': IsStr(regex=r'^"<MagicMock id=\'\d+\'>"'),
14201420
'logfire.json_schema': '{"type":"object","properties":{"foo":{"type":"object","x-python-datatype":"unknown"},"bar":{"type":"object","x-python-datatype":"unknown"},"mock":{"type":"object","x-python-datatype":"unknown"},"magic_mock":{"type":"object","x-python-datatype":"unknown"}}}',

0 commit comments

Comments
 (0)