Skip to content

Commit a27ea3f

Browse files
alexmojakisbhrwlr
andauthored
Handle wrong JSON schema in console exporter (#1294)
Co-authored-by: sbhrwlr <[email protected]> Co-authored-by: Rahul Sabharwal <[email protected]>
1 parent 9b04e2c commit a27ea3f

File tree

2 files changed

+40
-2
lines changed

2 files changed

+40
-2
lines changed

logfire/_internal/json_formatter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,9 @@ def _format(
6868
if schema is not None:
6969
if 'type' in schema:
7070
if (data_type := schema.get('x-python-datatype')) is None:
71-
if schema['type'] == 'object':
71+
if schema['type'] == 'object' and isinstance(value, dict):
7272
self._format_items('{', ': ', '}', True, indent_current, value, None)
73-
elif schema['type'] == 'array':
73+
elif schema['type'] == 'array' and isinstance(value, list):
7474
self._format_list_like('[', ']', indent_current, value, schema)
7575
else:
7676
# e.g. {'type': 'string', 'format': 'date-time'}

tests/test_console_exporter.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import decimal
55
import enum
66
import io
7+
import json
8+
import re
79
import sys
810
from datetime import datetime
911
from typing import Any
@@ -17,6 +19,7 @@
1719
from opentelemetry._logs import SeverityNumber, get_logger
1820
from opentelemetry.sdk._logs import LogRecord
1921
from opentelemetry.sdk.trace import ReadableSpan
22+
from opentelemetry.trace import get_tracer
2023
from opentelemetry.version import __version__ as otel_version
2124

2225
import logfire
@@ -993,6 +996,7 @@ class MyIntEnum(int, enum.Enum):
993996
e=MyEnum.ABC,
994997
se=MyStrEnum.STR,
995998
ie=MyIntEnum.INT,
999+
rp=re.compile(r'^[\w\.-]+@[\w\.-]+\.\w+$'),
9961000
)
9971001

9981002
assert capsys.readouterr().out.splitlines() == snapshot(
@@ -1005,5 +1009,39 @@ class MyIntEnum(int, enum.Enum):
10051009
"│ e=MyEnum('abc')",
10061010
"│ se=MyStrEnum('str_val')",
10071011
'│ ie=MyIntEnum(1)',
1012+
'│ rp=^[\\w\\.-]+@[\\w\\.-]+\\.\\w+$',
1013+
]
1014+
)
1015+
1016+
1017+
def test_console_exporter_list_data_with_object_schema_mismatch(capsys: pytest.CaptureFixture[str]) -> None:
1018+
logfire.configure(
1019+
send_to_logfire=False,
1020+
console=ConsoleOptions(verbose=True, colors='never', include_timestamps=False),
1021+
)
1022+
1023+
get_tracer(__name__).start_span(
1024+
'test_span',
1025+
attributes={
1026+
'foo': json.dumps(['item1', 'item2', 'item3']),
1027+
'bar': json.dumps({'name': 'Alice', 'age': 30}),
1028+
'logfire.json_schema': json.dumps(
1029+
{
1030+
'type': 'object',
1031+
'properties': {
1032+
# These are wrong
1033+
'foo': {'type': 'object'},
1034+
'bar': {'type': 'array'},
1035+
},
1036+
}
1037+
),
1038+
},
1039+
).end()
1040+
1041+
assert capsys.readouterr().out.splitlines() == snapshot(
1042+
[
1043+
'test_span',
1044+
"│ foo=['item1', 'item2', 'item3']",
1045+
"│ bar={'name': 'Alice', 'age': 30}",
10081046
]
10091047
)

0 commit comments

Comments
 (0)